Skip to content

Commit

Permalink
Id selector not working when combined with a tag selector (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate Piche committed May 12, 2016
1 parent 597edb4 commit 99218f9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export function withSetStateAllowed(fn) {
}

export function splitSelector(selector) {
return selector.split(/(?=\.|\[.*\])/);
return selector.split(/(?=\.|\[.*\])|(?=#|\[#.*\])/);
}

export function isSimpleSelector(selector) {
Expand All @@ -176,7 +176,7 @@ export function selectorError(selector) {
);
}

export const isCompoundSelector = /([a-z]\.[a-z]|[a-z]\[.*\])/i;
export const isCompoundSelector = /([a-z]\.[a-z]|[a-z]\[.*\]|[a-z]#[a-z])/i;

const isPropSelector = /^\[.*\]$/;

Expand Down
27 changes: 27 additions & 0 deletions test/ReactWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,33 @@ describeWithDOM('mount', () => {
expect(wrapper.find('input').props().className).to.equal('foo');
});

it('should find an element based on a tag name and class name', () => {
const wrapper = mount(
<div>
<input className="foo" />
</div>
);
expect(wrapper.find('input.foo').length).to.equal(1);
});

it('should find an element based on a tag name and id', () => {
const wrapper = mount(
<div>
<input id="foo" />
</div>
);
expect(wrapper.find('input#foo').length).to.equal(1);
});

it('should find an element based on a tag name, id, and class name', () => {
const wrapper = mount(
<div>
<input id="foo" className="bar" />
</div>
);
expect(wrapper.find('input#foo.bar').length).to.equal(1);
});

it('should find a component based on a constructor', () => {
class Foo extends React.Component {
render() { return <div />; }
Expand Down
27 changes: 27 additions & 0 deletions test/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,33 @@ describe('shallow', () => {
expect(wrapper.find('.foo').type()).to.equal('input');
});

it('should find an element based on a tag name and class name', () => {
const wrapper = shallow(
<div>
<input className="foo" />
</div>
);
expect(wrapper.find('input.foo').length).to.equal(1);
});

it('should find an element based on a tag name and id', () => {
const wrapper = shallow(
<div>
<input id="foo" />
</div>
);
expect(wrapper.find('input#foo').length).to.equal(1);
});

it('should find an element based on a tag name, id, and class name', () => {
const wrapper = shallow(
<div>
<input id="foo" className="bar" />
</div>
);
expect(wrapper.find('input#foo.bar').length).to.equal(1);
});

it('should find an element based on a tag name', () => {
const wrapper = shallow(
<div>
Expand Down

2 comments on commit 99218f9

@rixth
Copy link

@rixth rixth commented on 99218f9 Jul 11, 2016

Choose a reason for hiding this comment

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

This commit broke attribute selectors with values containing #, ex a[href='/page#anchor']

@ljharb
Copy link
Member

@ljharb ljharb commented on 99218f9 Jul 11, 2016

Choose a reason for hiding this comment

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

ref #495

Please sign in to comment.