import { invoke } from 'ember-awesome-macros/array';
import EmberObject from 'ember-object';
import { A as emberA } from 'ember-array/utils';
import get from 'ember-metal/get';
import { module, test } from 'qunit';
import compute from 'ember-macro-test-helpers/compute';

let array;

module('Integration | Macro | array | invoke', {
  beforeEach() {
    array = [{
      foo(arg='bar') {
        return arg + '-eval';
      }
    }];
  }
});

test('it invokes the given method name on each item in array without args', function(assert) {
  compute({
    assert,
    computed: invoke('array', 'methodName'),
    properties: {
      array,
      methodName: 'foo'
    },
    deepEqual: ['bar-eval']
  });
});

test('it invokes the given method name on each item in array with args', function(assert) {
  compute({
    assert,
    computed: invoke('array', 'methodName', 'args'),
    properties: {
      array,
      methodName: 'foo',
      args: 'baz'
    },
    deepEqual: ['baz-eval']
  });
});

test('it responds to array property value changes', function(assert) {
  let ObjClass = EmberObject.extend({
    foo(arg) {
      let prop = get(this, 'prop');
      return `${arg}-${prop}`;
    }
  });
  let array = emberA([
    ObjClass.create({ prop: 'val1' }),
    ObjClass.create({ prop: 'val2' })
  ]);

  let { subject } = compute({
    computed: invoke('array.@each.prop', 'methodName', 'args'),
    properties: {
      array,
      methodName: 'foo',
      args: ['baz']
    }
  });

  assert.deepEqual(subject.get('computed'), [
    'baz-val1',
    'baz-val2'
  ]);

  array.set('1.prop', 'val1');

  assert.deepEqual(subject.get('computed'), [
    'baz-val1',
    'baz-val1'
  ]);

  array.pushObject(ObjClass.create({ prop: 'val2' }));

  assert.deepEqual(subject.get('computed'), [
    'baz-val1',
    'baz-val1',
    'baz-val2'
  ]);
});

@@ -1103,20 +1103,26 @@
 d(%7B%0A    
+_
 foo(
+foo, 
 arg) %7B%0A 
@@ -1172,16 +1172,23 @@
 turn %60$%7B
+foo%7D-$%7B
 arg%7D-$%7Bp
@@ -1194,16 +1194,136 @@
 prop%7D%60;%0A
+    %7D,%0A    foo1(arg) %7B%0A      return this._foo('foo1', arg);%0A    %7D,%0A    foo2(arg) %7B%0A      return this._foo('foo2', arg);%0A
     %7D%0A  
@@ -1572,32 +1572,33 @@
 methodName: 'foo
+1
 ',%0A      args: %5B
@@ -1660,32 +1660,37 @@
 puted'), %5B%0A    '
+foo1-
 baz-val1',%0A    '
@@ -1681,32 +1681,37 @@
 baz-val1',%0A    '
+foo1-
 baz-val2'%0A  %5D);%0A
@@ -1786,32 +1786,37 @@
 puted'), %5B%0A    '
+foo1-
 baz-val1',%0A    '
@@ -1807,32 +1807,37 @@
 baz-val1',%0A    '
+foo1-
 baz-val1'%0A  %5D);%0A
@@ -1940,24 +1940,29 @@
 d'), %5B%0A    '
+foo1-
 baz-val1',%0A 
@@ -1961,24 +1961,29 @@
 val1',%0A    '
+foo1-
 baz-val1',%0A 
@@ -1986,16 +1986,174 @@
 ',%0A    '
+foo1-baz-val2'%0A  %5D);%0A%0A  subject.set('methodName', 'foo2');%0A%0A  assert.deepEqual(subject.get('computed'), %5B%0A    'foo2-baz-val1',%0A    'foo2-baz-val1',%0A    'foo2-
 baz-val2

clean up invoke test