import { module, test } from 'qunit';
import EmberObject, { computed } from '@ember/object';

import bindDataTestAttributes from 'ember-test-selectors/utils/bind-data-test-attributes';

module('Unit | Utility | bind data test attributes');

test('it adds missing attributeBindings array', function(assert) {
  let Fixture = EmberObject.extend({
    'data-test-from-factory': 'foo',
  });
  let instance = Fixture.create({
    'data-test-from-invocation': 'bar',
  });

  assert.deepEqual(instance.get('attributeBindings'), undefined);

  bindDataTestAttributes(instance);

  assert.deepEqual(instance.get('attributeBindings'),
    ['data-test-from-invocation', 'data-test-from-factory']);
});

test('it adds to existing attributeBindings array', function(assert) {
  let Fixture = EmberObject.extend({
    attributeBindings: ['foo', 'bar'],

    foo: 1,
    bar: 2,

    'data-test-from-factory': 'foo',
  });
  let instance = Fixture.create({
    'data-test-from-invocation': 'bar',
  });

  assert.deepEqual(instance.get('attributeBindings'), ['foo', 'bar']);

  bindDataTestAttributes(instance);

  assert.deepEqual(instance.get('attributeBindings'),
    ['foo', 'bar', 'data-test-from-invocation', 'data-test-from-factory']);
});

test('it converts existing attributeBindings string to array', function(assert) {
  let Fixture = EmberObject.extend({
    attributeBindings: 'foo',

    foo: 1,

    'data-test-from-factory': 'foo',
  });
  let instance = Fixture.create({
    'data-test-from-invocation': 'bar',
  });

  assert.deepEqual(instance.get('attributeBindings'), 'foo');

  bindDataTestAttributes(instance);

  assert.deepEqual(instance.get('attributeBindings'),
    ['foo', 'data-test-from-invocation', 'data-test-from-factory']);
});

test('it only adds data-test-* properties', function(assert) {
  let Fixture = EmberObject.extend({
    foo: 1,
    bar: 2,

    'data-test-from-factory': 'foo',
  });
  let instance = Fixture.create({
    baz: 3,

    'data-test-from-invocation': 'bar',
  });

  assert.deepEqual(instance.get('attributeBindings'), undefined);

  bindDataTestAttributes(instance);

  assert.deepEqual(instance.get('attributeBindings'),
    ['data-test-from-invocation', 'data-test-from-factory']);
});

test('it does not add a data-test property', function(assert) {
  let Fixture = EmberObject.extend({
    'data-test': 'foo',
  });
  let instance = Fixture.create();

  assert.deepEqual(instance.get('attributeBindings'), undefined);

  bindDataTestAttributes(instance);

  assert.deepEqual(instance.get('attributeBindings'), undefined);
});

test('it breaks if attributeBindings is a computed property', function(assert) {
  let Fixture = EmberObject.extend({
    attributeBindings: computed('prop', function() {
      return [this.get('prop')];
    }).readOnly(),

    foo: 5,

    'data-test-from-factory': 'foo',
  });
  let instance = Fixture.create({
    prop: 'foo',

    'data-test-from-invocation': 'bar',
  });

  assert.throws(() => bindDataTestAttributes(instance));
});

test('it breaks if tagName is empty', function(assert) {
  let Fixture = EmberObject.extend({
    tagName: '',
    'data-test-from-factory': 'foo',
  });
  let instance = Fixture.create({
    'data-test-from-invocation': 'bar',
  });

  assert.throws(() => bindDataTestAttributes(instance));
});

@@ -85,16 +85,105 @@
 object';
+%0Aimport EmberMixin from '@ember/object/mixin';%0Aimport %7B dasherize %7D from '@ember/string';
 %0A%0Aimport
@@ -3378,28 +3378,1162 @@
 tAttributes(instance));%0A%7D);%0A
+%0Atest('issue #106', function(assert) %7B%0A  let Component = EmberObject.extend(%7B%7D);%0A%0A  Component.reopen(%7B%0A    init() %7B%0A      this._super(...arguments);%0A      bindDataTestAttributes(this);%0A    %7D,%0A  %7D);%0A%0A  let Mixin = EmberMixin.create(%7B%0A    init() %7B%0A      this._super(...arguments);%0A%0A      if (this.tagName !== '') %7B%0A        let componentName = dasherize(this._XXXdebugContainerKey.replace(/%5C//g, '-').split(':')%5B1%5D);%0A        this.set('data-test-component', componentName);%0A%0A        let dataTestAttr = %5B'data-test-component'%5D;%0A        this.attributeBindings = this.attributeBindings ? this.attributeBindings.concat(dataTestAttr) : dataTestAttr;%0A      %7D%0A    %7D,%0A  %7D);%0A%0A  Component.reopen(Mixin);%0A%0A  let Fixture1 = Component.extend(%7B%0A    _XXXdebugContainerKey: 'component:fixture1',%0A    tagName: 'span',%0A  %7D);%0A%0A  let fixture1 = Fixture1.create();%0A%0A  assert.strictEqual(fixture1.get('data-test-component'), 'fixture1');%0A%0A  let Fixture2 = Component.extend(%7B%0A    _XXXdebugContainerKey: 'component:fixture2',%0A    tagName: '',%0A  %7D);%0A%0A  let fixture2 = Fixture2.create();%0A%0A  assert.strictEqual(fixture2.get('data-test-component'), undefined);%0A%7D);%0A%0A

Add test for #106