Skip to content

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

Merged
merged 4 commits into from
Jan 6, 2018

Conversation

jouderianjr
Copy link
Member

So, this is my test proposal for ImageZoom 😄

I'm accepting any type of suggestions 💃

Copy link
Member

@alejandronanez alejandronanez left a 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 a nativeId prop to it and use Enzyme's wrapper.find({ nativeId: 'the-id' })
  • If you need to simulate the onPress interaction, you can do it by doing something like theElement.simulate('press')

You can find some examples here:

Let me know if you have any question about this.

Copy link
Member

@andrewda andrewda left a 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 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} />);
});
Copy link
Member

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();
});
Copy link
Member

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');
});
Copy link
Member

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.

@andrewda andrewda mentioned this pull request Oct 22, 2017
63 tasks
@jouderianjr
Copy link
Member Author

Awesome everyone, I'll refactor. Thanks for the suggestions 😄

@coveralls
Copy link

Coverage Status

Coverage increased (+0.9%) to 38.124% when pulling 6b044c1 on jouderianjr:testes/imageZoom into 6780754 on gitpoint:master.

@jouderianjr
Copy link
Member Author

Hey @alejandronanez @andrewda , I recreated the tests, What do you think guys? 😄

screen shot 2017-10-28 at 18 28 46

expect(clickableImg.length).toBe(1);
});

it('should render Modal when the user presses Touchable', () => {
Copy link
Member

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?

Copy link
Member

@andrewda andrewda left a 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

@jouderianjr
Copy link
Member Author

Nice catch!

@coveralls
Copy link

Coverage Status

Coverage increased (+0.9%) to 38.124% when pulling bc42919 on jouderianjr:testes/imageZoom into 6780754 on gitpoint:master.

uri: { uri: 'dummy.png' },
};

it('should render clickable image', () => {
Copy link
Member

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', () => {
Copy link
Member

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();
Copy link
Member

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()}
Copy link
Member

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()}
Copy link
Member

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()}
Copy link
Member

Choose a reason for hiding this comment

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

onTap={this.closeModal}

@jouderianjr
Copy link
Member Author

@alejandronanez I think all the changes are done. Thanks for all suggestions 💃

@coveralls
Copy link

Coverage Status

Coverage increased (+0.8%) to 40.18% when pulling 6757a3c on jouderianjr:testes/imageZoom into 34b84d2 on gitpoint:master.

@chinesedfan
Copy link
Member

I checked suggestions by @alejandronanez had been applied. So this PR should be able to merged.

@chinesedfan chinesedfan merged commit 280210a into gitpoint:master Jan 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants