Skip to content

Commit

Permalink
Throw for malformed compound selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
nfcampos committed Apr 1, 2017
1 parent a4c63a1 commit 3ab5eae
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ export function splitSelector(selector) {
.filter(Boolean)
.reduce((obj, match) => assign({}, obj, { [match]: uuid.v4() }), {});

return selector
const splits = selector
// step 2: replace all quoted strings with the uuid, so we don't have to properly parse them
.replace(/[^" ]+|("[^"]*")|.*/g, x => quotedSegments[x] || x)
// step 3: split as best we can without a proper parser
Expand All @@ -247,6 +247,13 @@ export function splitSelector(selector) {
});
return restoredSegment;
});

if (splits.length === 1 && splits[0] === selector) {
// splitSelector expects selector to be "splittable"
throw new TypeError('Enzyme::Selector received what appears to be a malformed string selector');
}

return splits;
}


Expand Down
4 changes: 4 additions & 0 deletions test/ShallowTraversal-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ describe('ShallowTraversal', () => {
fn('div[title="title"][data-value="foo"]'),
).to.eql(['div', '[title="title"]', '[data-value="foo"]']);
});

it('throws for malformed selectors', () => {
expect(() => fn('div[data-name="xyz"')).to.throw(/Enzyme::Selector received what appears to be a malformed string selector/);
});
});

describe('hasClassName', () => {
Expand Down

0 comments on commit 3ab5eae

Please sign in to comment.