Skip to content

Commit

Permalink
Merge pull request #57 from Turbo87/add-support-for-component-test-se…
Browse files Browse the repository at this point in the history
…lectors

Add support for component data-test-* attributes without values
  • Loading branch information
Turbo87 authored Jan 23, 2017
2 parents a2b84eb + d1cae3c commit 817c645
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ module.exports = {
plugin: StripTestSelectorsTransform,
baseDir: function() { return __dirname; }
});
} else {
var TransformTestSelectorParamsToHashPairs = require('./transform-test-selector-params-to-hash-pairs');

registry.add('htmlbars-ast-plugin', {
name: 'transform-test-selector-params-to-hash-pairs',
plugin: TransformTestSelectorParamsToHashPairs,
baseDir: function() { return __dirname; }
});
}
}
},
Expand Down
19 changes: 17 additions & 2 deletions tests/acceptance/bind-data-test-attributes-in-components-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,28 @@ if (!config.stripTestSelectors) {
assert.equal(find('.test4').find('div[data-non-test]').length, 0, 'data-non-test does not exists');
});

test('it leaves data-test attributes untouched on components', function (assert) {
test('it leaves data-test attribute untouched on components', function (assert) {
assert.equal(find('.test5').find('div[data-test]').length, 0, 'data-test does not exists');
});

test('it leaves other data attributes untouched on components', function (assert) {
assert.equal(find('.test6').find('div[data-non-test]').length, 0, 'data-non-test does not exists');
});

}
test('it binds data-test-* attributes with boolean values on components', function (assert) {
assert.equal(find('.test7').find('div[data-test-with-boolean-value]').length, 1, 'data-test-with-boolean-value exists');
});

test('it binds data-test-* attributes without values on components', function (assert) {
assert.equal(find('.test8').find('div[data-test-without-value]').length, 1, 'data-test-without-value exists');
});

test('it binds data-test-* attributes without values on block components', function (assert) {
assert.equal(find('.test9').find('div[data-test-without-value]').length, 1, 'data-test-without-value exists');
});

test('it leaves data-test attribute without value untouched on components', function (assert) {
assert.equal(find('.test10').find('div[data-test]').length, 0, 'data-test does not exists');
});

}
8 changes: 8 additions & 0 deletions tests/dummy/app/templates/bind-test.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@
<div class="test5">{{data-test-component data-test="foo"}}</div>

<div class="test6">{{data-test-component data-non-test="foo"}}</div>

<div class="test7">{{data-test-component data-test-with-boolean-value=true}}</div>

<div class="test8">{{data-test-component data-test-without-value}}</div>

<div class="test9">{{#data-test-component data-test-without-value}}foo{{/data-test-component}}</div>

<div class="test10">{{data-test-component data-test}}</div>
42 changes: 42 additions & 0 deletions transform-test-selector-params-to-hash-pairs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* eslint-env node */
var TEST_SELECTOR_PREFIX = /data-test-.*/;

function TransformTestSelectorParamsToHashPairs() {
this.syntax = null;
}

function isTestSelectorParam(param) {
return param.type === 'PathExpression'
&& TEST_SELECTOR_PREFIX.test(param.original);
}

TransformTestSelectorParamsToHashPairs.prototype.transform = function(ast) {
var b = this.syntax.builders;
var walker = new this.syntax.Walker();

walker.visit(ast, function(node) {
if (node.type === 'MustacheStatement' || node.type === 'BlockStatement') {
var testSelectorParams = [];
var otherParams = [];

node.params.forEach(function(param) {
if (isTestSelectorParam(param)) {
testSelectorParams.push(param);
} else {
otherParams.push(param);
}
});

node.params = otherParams;

testSelectorParams.forEach(function(param) {
var pair = b.pair(param.original, b.boolean(true));
node.hash.pairs.push(pair);
});
}
});

return ast;
};

module.exports = TransformTestSelectorParamsToHashPairs;

0 comments on commit 817c645

Please sign in to comment.