Skip to content

Commit

Permalink
utils/bind-data-test-attributes: Handle failing with assert instead o…
Browse files Browse the repository at this point in the history
…f warning
  • Loading branch information
mathieupoteriepeopledoc committed Sep 3, 2018
1 parent 6ea5ec9 commit 7c0bd14
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
15 changes: 6 additions & 9 deletions addon/utils/bind-data-test-attributes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { warn } from '@ember/debug';
import { assert, warn } from '@ember/debug';
import { isArray } from '@ember/array';

const TEST_SELECTOR_PREFIX = /data-test-.*/;
Expand All @@ -16,16 +16,13 @@ export default function bindDataTestAttributes(component) {
}

let tagName = component.get('tagName');
if (tagName === '') {
let message = `ember-test-selectors could not bind data-test-* properties on ${component} ` +
`automatically because tagName is empty.`;

warn(message, false, {
id: 'ember-test-selectors.empty-tag-name',
});
let message = `ember-test-selectors could not bind data-test-* properties on ${component} ` +
`automatically because tagName is empty.`;

return;
}
assert(message, tagName!== '', {
id: 'ember-test-selectors.empty-tag-name',
});

let attributeBindings = component.getWithDefault('attributeBindings', []);
if (!isArray(attributeBindings)) {
Expand Down
34 changes: 34 additions & 0 deletions tests/integration/bind-data-test-attributes-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import config from 'dummy/config/environment';

moduleForComponent(
'print-test-attributes',
'StripTestSelectorsTransform plugin',
{
integration: true
}
);

if (!config.stripTestSelectors) {
test('It throws an error when a data-test-attribute is added on a tagless component', function(assert) {
assert.expect(1);
try {
this.render(
hbs`{{data-test-component tagName='' data-test-attribute=true}}`
);
assert.ok(false, 'this should not be called');
} catch (e) {
assert.ok(true, 'this is called');
}
});
} else {
test('it doest not throw an error when stripTestSelectors is set to true in the config', function(assert) {
this.render(
hbs`{{data-test-component tagName='' data-test-attribute=true}}`
);
assert
.dom('[data-test-attribute]')
.doesNotExist('data-test-attribute was stripped, so no error is thrown');
});
}
16 changes: 0 additions & 16 deletions tests/unit/utils/bind-data-test-attributes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,3 @@ test('it skips if attributeBindings is a computed property', function(assert) {

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

test('it skips 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.deepEqual(instance.get('attributeBindings'), undefined);

bindDataTestAttributes(instance);

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

0 comments on commit 7c0bd14

Please sign in to comment.