Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for data-test-* properties #70

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions lib/__tests__/__snapshots__/transform.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,57 @@ foo
=========="
`;

exports[`classic components handles \`data-test-*\` boolean correctly 1`] = `
"==========

export default Component.extend({
'data-test-one': true,
'data-test-two': true,
'data-test-three': false,
});

~~~~~~~~~~
foo
~~~~~~~~~~
=> tagName: div
~~~~~~~~~~

export default Component.extend({
tagName: \\"\\"
});

~~~~~~~~~~
<div data-test-one data-test-two ...attributes>
foo
</div>
=========="
`;

exports[`classic components handles \`data-test-*\` literals correctly 1`] = `
"==========

export default Component.extend({
'data-test-item': 1,
'data-test-name': 'Zoey',
});

~~~~~~~~~~
foo
~~~~~~~~~~
=> tagName: div
~~~~~~~~~~

export default Component.extend({
tagName: \\"\\"
});

~~~~~~~~~~
<div data-test-item=\\"1\\" data-test-name=\\"Zoey\\" ...attributes>
foo
</div>
=========="
`;

exports[`classic components handles \`elementId\` correctly 1`] = `
"==========

Expand Down
41 changes: 41 additions & 0 deletions lib/__tests__/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,47 @@ describe('classic components', function() {
expect(generateSnapshot(source, template)).toMatchSnapshot();
});

test('handles `data-test-*` boolean correctly', () => {
let source = `
export default Component.extend({
'data-test-one': true,
'data-test-two': true,
'data-test-three': false,
});
`;

let template = `foo`;

expect(generateSnapshot(source, template)).toMatchSnapshot();
});

test('handles `data-test-*` literals correctly', () => {
let source = `
export default Component.extend({
'data-test-item': 1,
'data-test-name': 'Zoey',
});
`;

let template = `foo`;

expect(generateSnapshot(source, template)).toMatchSnapshot();
});

test('throws if data-test-* is a computed value', () => {
let source = `
export default Component.extend({
'data-test-computed': computed(function() {
return true;
}),
});
`;

expect(() => transform(source, '')).toThrowErrorMatchingInlineSnapshot(
`"Unsupported value for 'data-test-computed'"`
);
});

test('throws if `Component.extend({ ... })` argument is not found', () => {
let source = `
export default Component.extend();
Expand Down
7 changes: 7 additions & 0 deletions lib/transform/classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const {
findAriaRole,
isMethod,
isProperty,
isStartsWithProperty,
findDataTestAttributes,
} = require('../utils/classic');

const EVENT_HANDLER_METHODS = [
Expand Down Expand Up @@ -125,6 +127,9 @@ module.exports = function transformClassicComponent(root, options) {
let classNameBindings = findClassNameBindings(properties);
debug('classNameBindings: %o', classNameBindings);

let dataTestAttributes = findDataTestAttributes(properties);
debug('dataTestAttributes: %o', dataTestAttributes);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, instead of creating a dedicated thing here, I'm wondering if it would be easier to just add all the data-test-* properties to the attribute bindings? that's what ember-test-selectors does internally and it would also support computed properties that way. I'm just not sure if data-test-foo={{this.data-test-foo}} is valid code due to the - in the property name 🤔


let ariaRole;
try {
ariaRole = findAriaRole(properties);
Expand Down Expand Up @@ -155,6 +160,7 @@ module.exports = function transformClassicComponent(root, options) {
isProperty(path, 'attributeBindings') ||
isProperty(path, 'classNames') ||
isProperty(path, 'classNameBindings') ||
isStartsWithProperty(path, 'data-test-') ||
isProperty(path, 'ariaRole')
)
.remove();
Expand All @@ -168,6 +174,7 @@ module.exports = function transformClassicComponent(root, options) {
classNames,
classNameBindings,
attributeBindings,
dataTestAttributes,
ariaRole,
};
};
13 changes: 11 additions & 2 deletions lib/transform/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ module.exports = function transformNativeComponent(root, options) {
.forEach(path => removeDecorator(root, path, 'className', '@ember-decorators/component'));

let newSource = root.toSource();

return { newSource, tagName, elementId, classNames, classNameBindings, attributeBindings };
let dataTestAttributes = new Map();

return {
newSource,
tagName,
elementId,
classNames,
classNameBindings,
attributeBindings,
dataTestAttributes,
};
};
13 changes: 12 additions & 1 deletion lib/transform/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ const PLACEHOLDER = '@@@PLACEHOLDER@@@';

module.exports = function transformTemplate(
template,
{ tagName, elementId, classNames, classNameBindings, attributeBindings, ariaRole },
{
tagName,
elementId,
classNames,
classNameBindings,
attributeBindings,
ariaRole,
dataTestAttributes,
},
options
) {
// wrap existing template with root element
Expand Down Expand Up @@ -50,6 +58,9 @@ module.exports = function transformTemplate(

attrs.push(b.attr('class', b.concat(parts)));
}
dataTestAttributes.forEach((value, key) => {
attrs.push(b.attr(key, b.text(value)));
});
attrs.push(b.attr('...attributes', b.text('')));

let templateAST = templateRecast.parse(template);
Expand Down
43 changes: 43 additions & 0 deletions lib/utils/classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ function isProperty(path, name) {
return node.type === 'ObjectProperty' && node.key.type === 'Identifier' && node.key.name === name;
}

function isStartsWithProperty(path, name) {
let node = path.value;
if (node.type === 'ObjectProperty') {
if (node.key.type === 'Identifier') {
return node.key.name.startsWith(name);
}
if (node.key.type === 'StringLiteral') {
return node.key.value.startsWith(name);
}
}
}

function isMethod(path, name) {
let node = path.value;
return node.type === 'ObjectMethod' && node.key.type === 'Identifier' && node.key.name === name;
Expand Down Expand Up @@ -91,15 +103,46 @@ function findClassNameBindings(properties) {
return classNameBindings;
}

function findDataTestAttributes(properties) {
let nodes = properties.filter(path => isStartsWithProperty(path, 'data-test-'));
if (!nodes) {
return [];
}
const mappedProperties = nodes.map(node => {
return {
name: node.value.key.value,
value: node.value.value.value,
valueType: node.value.value.type,
};
});

let dataTestAttributes = new Map();
for (let { name, value, valueType } of mappedProperties) {
if (valueType === 'BooleanLiteral') {
if (value) {
dataTestAttributes.set(name, null);
}
} else if (valueType === 'NumericLiteral' || valueType === 'StringLiteral') {
dataTestAttributes.set(name, String(value));
} else {
throw new SilentError(`Unsupported value for '${name}'`);
}
}

return dataTestAttributes;
}

module.exports = {
isProperty,
isStartsWithProperty,
isMethod,
findStringProperty,
findStringArrayProperties,
findAriaRole,
findAttributeBindings,
findClassNames,
findClassNameBindings,
findDataTestAttributes,
findElementId,
findTagName,
};