From 6b7a645d95b3497ed9dac560211b4708bfa7462f Mon Sep 17 00:00:00 2001 From: Raido Kuli Date: Fri, 31 Mar 2017 22:43:50 +0300 Subject: [PATCH] Filter MustacheStatement and BlockStatement params for data-test-* with no value --- strip-test-selectors.js | 12 ++++++++-- ...ta-test-attributes-from-components-test.js | 22 +++++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/strip-test-selectors.js b/strip-test-selectors.js index cb00248d..aeacc961 100644 --- a/strip-test-selectors.js +++ b/strip-test-selectors.js @@ -4,6 +4,10 @@ let TEST_SELECTOR_PREFIX = /data-test-.*/; +function isNotTestSelector(attribute) { + return !TEST_SELECTOR_PREFIX.test(attribute); +} + function StripTestSelectorsTransform() { this.syntax = null; } @@ -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); }); } }); diff --git a/tests/integration/strip-data-test-attributes-from-components-test.js b/tests/integration/strip-data-test-attributes-from-components-test.js index 24ef2b21..85ff1cc6 100644 --- a/tests/integration/strip-data-test-attributes-from-components-test.js +++ b/tests/integration/strip-data-test-attributes-from-components-test.js @@ -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'); });