-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Make ReactWrapper and ShallowWrapper iterable #594
Changes from all commits
2e08d9e
1e78d6a
6cfb0d7
3a81f46
5b9022a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ import { | |
REACT15, | ||
} from './version'; | ||
|
||
export const ITERATOR_SYMBOL = typeof Symbol === 'function' && Symbol.iterator; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't that exclude environments where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's true, because Symbols can't be polyfilled (because of I suppose I don't need to make it a sticking point here, but I'd prefer a more robust test than truthiness - for now this is fine, but I'll extract https://github.com/ljharb/object.assign/blob/master/hasSymbols.js out to a module or two and we can use that instead :-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like a great module to put out there! As far as the tests go, I did consciously chose to test the actual public API that this provides, rather then testing the internal implementation. Though more robust tests are rarely a bad thing! I can add some more tests now that verify the iterator method is there, and only there when Otherwise, I can make sure to add tests using |
||
|
||
function internalInstanceKey(node) { | ||
return Object.keys(Object(node)).filter(key => key.match(/^__reactInternalInstance\$/))[0]; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ import { | |
render, | ||
ReactWrapper, | ||
} from '../src'; | ||
import { ITERATOR_SYMBOL } from '../src/Utils'; | ||
import { REACT013, REACT014, REACT15 } from '../src/version'; | ||
|
||
describeWithDOM('mount', () => { | ||
|
@@ -3024,4 +3025,31 @@ describeWithDOM('mount', () => { | |
}); | ||
}); | ||
}); | ||
|
||
describeIf(ITERATOR_SYMBOL, '@@iterator', () => { | ||
it('should be iterable', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these tests should be skipped when the environment lacks Symbols. |
||
class Foo extends React.Component { | ||
render() { | ||
return ( | ||
<div> | ||
<a href="#1">Hello</a> | ||
<a href="#2">Hello</a> | ||
<a href="#3">Hello</a> | ||
<a href="#4">Hello</a> | ||
</div> | ||
); | ||
} | ||
} | ||
const wrapper = mount(<Foo />); | ||
const [a, b, c, d] = wrapper.find('a'); | ||
const a1 = wrapper.find('a').get(0); | ||
const b1 = wrapper.find('a').get(1); | ||
const c1 = wrapper.find('a').get(2); | ||
const d1 = wrapper.find('a').get(3); | ||
expect(a1).to.equal(a); | ||
expect(b1).to.equal(b); | ||
expect(c1).to.equal(c); | ||
expect(d1).to.equal(d); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import sinon from 'sinon'; | |
|
||
import { shallow, render, ShallowWrapper } from '../src/'; | ||
import { describeIf, itIf, itWithData, generateEmptyRenderData } from './_helpers'; | ||
import { ITERATOR_SYMBOL } from '../src/Utils'; | ||
import { REACT013, REACT15 } from '../src/version'; | ||
|
||
describe('shallow', () => { | ||
|
@@ -3626,4 +3627,31 @@ describe('shallow', () => { | |
expect(underwater.is(RendersDOM)).to.equal(true); | ||
}); | ||
}); | ||
|
||
describeIf(ITERATOR_SYMBOL, '@@iterator', () => { | ||
it('should be iterable', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these tests should be skipped when the environment lacks Symbols. |
||
class Foo extends React.Component { | ||
render() { | ||
return ( | ||
<div> | ||
<a href="#1">Hello</a> | ||
<a href="#2">Hello</a> | ||
<a href="#3">Hello</a> | ||
<a href="#4">Hello</a> | ||
</div> | ||
); | ||
} | ||
} | ||
const wrapper = shallow(<Foo />); | ||
const [a, b, c, d] = wrapper.find('a'); | ||
const a1 = wrapper.find('a').get(0); | ||
const b1 = wrapper.find('a').get(1); | ||
const c1 = wrapper.find('a').get(2); | ||
const d1 = wrapper.find('a').get(3); | ||
expect(a1).to.equal(a); | ||
expect(b1).to.equal(b); | ||
expect(c1).to.equal(c); | ||
expect(d1).to.equal(d); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
configurable
defaults to false, so we need to explicitly make ittrue
here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious why would we want it to be
configurable
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's just no reason to lock it down - someone might want to override it for their project, or something.