Skip to content

Commit

Permalink
fix: strip data-test-* attributes without explicit value from product…
Browse files Browse the repository at this point in the history
…ion build (#88)

* Components with positional params failure if not full attribute definition

* Filter MustacheStatement and BlockStatement params for data-test-* with no value

* tests: Fix assertion message

* strip-test-selectors: Replace isNotTestSelector() with isTestSelector()
  • Loading branch information
raido authored and Turbo87 committed Apr 7, 2017
1 parent 3d68a4a commit cfb712c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 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 isTestSelector(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 !isTestSelector(attribute.name);
});
} else if (node.type === 'MustacheStatement' || node.type === 'BlockStatement') {
node.params = node.params.filter(function(param) {
return !isTestSelector(param.original);
});

node.hash.pairs = node.hash.pairs.filter(function(pair) {
return !TEST_SELECTOR_PREFIX.test(pair.key);
return !isTestSelector(pair.key);
});
}
});
Expand Down
11 changes: 11 additions & 0 deletions tests/dummy/app/components/print-test-attributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Ember from 'ember';

const { Component } = Ember;

const component = Component.extend();

component.reopenClass({
positionalParams: 'params'
});

export default component;
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
<div class="data-test-second">{{data-test-second}}</div>
<div class="data-non-test">{{data-non-test}}</div>
<div class="data-test">{{data-test}}</div>
<div class="data-test-positional-params">{{params.length}}</div>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,30 @@ moduleForComponent('print-test-attributes', 'StripTestSelectorsTransform plugin'

if (config.stripTestSelectors) {

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 only one param');
});

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');
});

test('it strips data-test-* attributes from components', function(assert) {
this.render(hbs`{{print-test-attributes data-test-first="foobar"}}`);

Expand Down

0 comments on commit cfb712c

Please sign in to comment.