Skip to content

Commit

Permalink
[fix] bug enzymejs#693 allow numbers in attributes
Browse files Browse the repository at this point in the history
Throw when attribute selector is invalid

include the selector in the error message
  • Loading branch information
jsmey authored and John Smey committed Nov 29, 2016
1 parent 01f415c commit aa403ef
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/ShallowTraversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,13 @@ export function buildPredicate(selector) {
return node => nodeHasId(node, selector.slice(1));

case SELECTOR.PROP_TYPE: {
const propKey = selector.split(/\[([a-zA-Z-]*?)(=|])/)[1];
const propKey = selector.split(/\[([a-z]+[a-z-0-9-]*?)(=|])/i);
if (propKey.length === 1) {
throw new TypeError(`Enzyme::Invalid attribute selector ('${selector}')`);
}
const propValue = selector.split(/=(.*?)]/)[1];

return node => nodeHasProperty(node, propKey, propValue);
return node => nodeHasProperty(node, propKey[1], propValue);
}
default:
// selector is a string. match to DOM tag or constructor displayName
Expand Down
30 changes: 30 additions & 0 deletions test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,36 @@ describe('shallow', () => {
expect(wrapper.find('a[value=true]')).to.have.length(0);
});

it('should support attributes with numbers', () => {
const wrapper = shallow(
<div>
<span value-1={1} />
<a value-1={false} />
</div>,
);

expect(wrapper.find('span[value-1=1]')).to.have.length(1);
expect(wrapper.find('a[value-1=false]')).to.have.length(1);
});

it('throws if when selector is invalid', () => {
const wrapper = shallow(
<div />,
);
expect(() => wrapper.find('span[1-foo=1]')).to.throw(
TypeError,
'Enzyme::Invalid attribute selector',
);
expect(() => wrapper.find('span[1foo=1]')).to.throw(
TypeError,
'Enzyme::Invalid attribute selector',
);
expect(() => wrapper.find('span[^^^=1]')).to.throw(
TypeError,
'Enzyme::Invalid attribute selector',
);
});

it('should not find key or ref via property selector', () => {
const arrayOfComponents = [<div key="1" />, <div key="2" />];

Expand Down

0 comments on commit aa403ef

Please sign in to comment.