-
Notifications
You must be signed in to change notification settings - Fork 784
test: Add test to ImageZoom component #548
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
Conversation
52bd2d7
to
757baa8
Compare
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.
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
TouchableHightlight
component, you can add anativeId
prop to it and use Enzyme'swrapper.find({ nativeId: 'the-id' })
- If you need to simulate the
onPress
interaction, 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.
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.
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 describe
s or only use it
s. 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thing as I said above – this stuff should go in each test.
describe('when zoom is applied', () => { | ||
beforeEach(() => { | ||
wrapper.first().simulate('press'); | ||
}); |
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.
Also should be in each test.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modal
is lowercase everywhere else, keep it like that?
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.
^ 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for this one {onPress={this.openModal}}
<StyledPhotoView onTap={() => this.closeModal()} source={uri} /> | ||
<StyledPhotoView | ||
nativeId="image-zoom-photo-view" | ||
onTap={() => this.closeModal()} |
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.
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 💃