test: Add test to ImageZoom component#548
Conversation
52bd2d7 to
757baa8
Compare
alejandronanez
left a comment
There was a problem hiding this comment.
Hello @jouderianjr!
I think this are not "real" unit tests, for example, you're checking if ImageZoom props are applied to some child components, testing this is not worth it because it would be testing how React works. What you're doing here looks more like a manual snapshot.
The goal of unit tests is to verify interactions/conditional renders/class' methods.
I think you can do some valuable tests here like:
1. should render Modal if state.imgZoom is truthy
2. should not render Modal if state.imgZoom is truthy
3. should call the closeModal when onRequestClose Modal is called
4. should call closeModal method when the user press TouchableOpacity (line 72)
5. should call openModal when the user presses touchableHighlight
- If you need to find an specific
TouchableHightlightcomponent, you can add anativeIdprop to it and use Enzyme'swrapper.find({ nativeId: 'the-id' }) - If you need to simulate the
onPressinteraction, you can do it by doing something liketheElement.simulate('press')
You can find some examples here:
Let me know if you have any question about this.
andrewda
left a comment
There was a problem hiding this comment.
Ok just a couple things. Also, I think we need to have a discussion on how we're gunna organize these tests. Should we have nested describes or only use its. Usually I don't do any describe nesting as it can get a little confusing, and using only it makes everything very clear (which is the point of tests). Open to all suggestions on this matter (maybe we should open a discussion issue).
|
|
||
| beforeEach(() => { | ||
| wrapper = shallow(<ImageZoom uri={uri} style={style} />); | ||
| }); |
There was a problem hiding this comment.
Using a lot of beforeEach's can get a little confusing (usually it's used for environment configuration, such as initiating a database or something). I think a new wrapper should be defined inside of each test.
| beforeEach(() => { | ||
| touchableHighlight = wrapper.first(); | ||
| image = touchableHighlight.children().first(); | ||
| }); |
There was a problem hiding this comment.
Same thing as I said above – this stuff should go in each test.
| describe('when zoom is applied', () => { | ||
| beforeEach(() => { | ||
| wrapper.first().simulate('press'); | ||
| }); |
|
Awesome everyone, I'll refactor. Thanks for the suggestions 😄 |
757baa8 to
6b044c1
Compare
|
Hey @alejandronanez @andrewda , I recreated the tests, What do you think guys? 😄 |
| expect(clickableImg.length).toBe(1); | ||
| }); | ||
|
|
||
| it('should render Modal when the user presses Touchable', () => { |
There was a problem hiding this comment.
Modal is lowercase everywhere else, keep it like that?
andrewda
left a comment
There was a problem hiding this comment.
^ meant for my last review to be request changes
|
Nice catch! |
| uri: { uri: 'dummy.png' }, | ||
| }; | ||
|
|
||
| it('should render clickable image', () => { |
There was a problem hiding this comment.
it should render a clickable image when.... (describe some scenario here)
| expect(clickableImg.length).toBe(1); | ||
| }); | ||
|
|
||
| it('should render modal when the user presses Touchable', () => { |
There was a problem hiding this comment.
I'd find the element you want to simulate the press event on, and the do the check.
| const wrapper = shallow(<ImageZoom {...defaultProps} />); | ||
| wrapper.setState({ imgZoom: true }); | ||
|
|
||
| wrapper.props().onRequestClose(); |
There was a problem hiding this comment.
I get this test and it's ok, but it will be better if you check that the modal close after simulating a press (for example) on X or Y component. By doing this, you'll ensure that a real event did close the modal.
| <CloseButton onPress={() => this.closeModal()}> | ||
| <CloseButton | ||
| nativeId="image-zoom-close-button" | ||
| onPress={() => this.closeModal()} |
There was a problem hiding this comment.
Now that you're here, can you change this for onPress={this.closeModal} this is better for perf.
| <Touchable onPress={() => this.openModal()} underlayColor="transparent"> | ||
| <Touchable | ||
| nativeId="image-zoom-clickable-img" | ||
| onPress={() => this.openModal()} |
There was a problem hiding this comment.
Same for this one {onPress={this.openModal}}
| <StyledPhotoView onTap={() => this.closeModal()} source={uri} /> | ||
| <StyledPhotoView | ||
| nativeId="image-zoom-photo-view" | ||
| onTap={() => this.closeModal()} |
bc42919 to
6757a3c
Compare
|
@alejandronanez I think all the changes are done. Thanks for all suggestions 💃 |
|
I checked suggestions by @alejandronanez had been applied. So this PR should be able to merged. |

So, this is my test proposal for ImageZoom 😄
I'm accepting any type of suggestions 💃