Skip to content

Commit

Permalink
Filter MustacheStatement and BlockStatement params for data-test-* wi…
Browse files Browse the repository at this point in the history
…th no value
  • Loading branch information
raido authored and Turbo87 committed Apr 7, 2017
1 parent dfe9731 commit 6b7a645
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
12 changes: 10 additions & 2 deletions strip-test-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

let TEST_SELECTOR_PREFIX = /data-test-.*/;

function isNotTestSelector(attribute) {
return !TEST_SELECTOR_PREFIX.test(attribute);
}

function StripTestSelectorsTransform() {
this.syntax = null;
}
Expand All @@ -14,11 +18,15 @@ StripTestSelectorsTransform.prototype.transform = function(ast) {
walker.visit(ast, function(node) {
if (node.type === 'ElementNode') {
node.attributes = node.attributes.filter(function(attribute) {
return !TEST_SELECTOR_PREFIX.test(attribute.name);
return isNotTestSelector(attribute.name);
});
} else if (node.type === 'MustacheStatement' || node.type === 'BlockStatement') {
node.params = node.params.filter(function(param) {
return isNotTestSelector(param.original);
});

node.hash.pairs = node.hash.pairs.filter(function(pair) {
return !TEST_SELECTOR_PREFIX.test(pair.key);
return isNotTestSelector(pair.key);
});
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,26 @@ moduleForComponent('print-test-attributes', 'StripTestSelectorsTransform plugin'

if (config.stripTestSelectors) {

test('it strips data-test-* attributes from components with positional params', function(assert) {
this.render(hbs`{{print-test-attributes "param1" data-test-first}}`);
test('it strips data-test-* attributes from components with single positional params', function(assert) {
this.render(hbs`{{print-test-attributes data-test-should-not-be}}`);

assert.equal(this.$('.data-test-positional-params').text(), 0, 'there should be no params');
});

test('it strips data-test-* attributes from components with positional params data-test-* as first param', function(assert) {
this.render(hbs`{{print-test-attributes data-test-should-not-be "param1"}}`);

assert.equal(this.$('.data-test-positional-params').text(), 1, 'there should be no params');
});

test('it strips data-test-* attributes from components with multiple positional params', function(assert) {
this.render(hbs`{{print-test-attributes "param1" data-test-should-not-be}}`);

assert.equal(this.$('.data-test-positional-params').text(), 1, 'there should be only one param');
});

test('it strips data-test-* attributes from components with block and multiple positional params', function(assert) {
this.render(hbs`{{#print-test-attributes "param1" data-test-should-not-be}}{{/print-test-attributes}}`);

assert.equal(this.$('.data-test-positional-params').text(), 1, 'there should be only one param');
});
Expand Down

0 comments on commit 6b7a645

Please sign in to comment.