Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 339b76a

Browse files
committed
Use enzyme instead of snapshot tests and remove hacky workaround
1 parent 83a61a0 commit 339b76a

File tree

6 files changed

+444
-227
lines changed

6 files changed

+444
-227
lines changed

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,17 @@
4343
"@babel/core": "^7.11.6",
4444
"babel-eslint": "^10.1.0",
4545
"coveralls": "^3.0.0",
46+
"enzyme": "^3.11.0",
47+
"enzyme-adapter-react-16": "^1.15.4",
4648
"eslint": "^7.8.1",
4749
"eslint-plugin-react": "^7.20.6",
4850
"eslint-plugin-react-native": "^3.9.1",
4951
"jest": "^26.4.2",
5052
"metro-react-native-babel-preset": "^0.63.0",
5153
"prettier": "^2.1.1",
52-
"react": "^16.13.1",
54+
"react": "^16.0.0",
55+
"react-dom": "16",
5356
"react-native": "^0.63.2",
54-
"react-test-renderer": "^16.13.1",
5557
"should": "^13.1.2",
5658
"should-sinon": "^0.0.6",
5759
"sinon": "^4.1.3"

tests/CacheableImage.test.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { Image } from 'react-native';
99
import sinon from 'sinon';
1010
import 'should-sinon';
1111
import RNFetchBlob from 'rn-fetch-blob';
12+
import { shallow } from 'enzyme';
13+
import React from 'react';
1214

1315
describe('CacheableImage', function () {
1416
it('HOC options validation should work as expected.', () => {
@@ -346,17 +348,25 @@ describe('CacheableImage', function () {
346348
});
347349
});
348350

349-
it('#render with valid props does not throw an error.', async () => {
351+
it('#render with valid props does not throw an error.', (done) => {
350352
const CacheableImage = imageCacheHoc(Image);
351353

352-
const cacheableImage = new CacheableImage(mockData.mockCacheableImageProps);
354+
const wrapper = shallow(
355+
<CacheableImage {...mockData.mockCacheableImageProps} />
356+
);
353357

354-
cacheableImage.render();
358+
setImmediate(() => {
359+
expect(wrapper.prop('source')).toStrictEqual({
360+
uri: '/this/is/path/to/file.jpg',
361+
});
355362

356-
cacheableImage.state.localFilePath = './test.jpg';
363+
wrapper.setState({ localFilePath: './test.jpg' });
357364

358-
cacheableImage.render();
365+
expect(wrapper.prop('source')).toStrictEqual({
366+
uri: './test.jpg',
367+
});
359368

360-
await new Promise((r) => setTimeout(r, 100)); // Hacky wait for lifecycle functions to complete
369+
done();
370+
});
361371
});
362372
});

tests/__snapshots__/imageCacheHoc.test.js.snap

Lines changed: 0 additions & 116 deletions
This file was deleted.

tests/config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
/* global jest */
88
import * as ReactNative from 'react-native';
9+
import Adapter from 'enzyme-adapter-react-16';
10+
import Enzyme from 'enzyme';
11+
12+
Enzyme.configure({ adapter: new Adapter() });
913

1014
/**
1115
* Override native modules with mocks where necessary.

tests/imageCacheHoc.test.js

Lines changed: 35 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,16 @@
22
/* global expect describe it */
33

44
// Load dependencies
5-
import should from 'should'; // eslint-disable-line no-unused-vars
65
import React from 'react';
7-
import 'react-native';
86
import imageCacheHoc from '../src/imageCacheHoc';
9-
import { StyleSheet, View, Text, Image } from 'react-native';
7+
import { StyleSheet, Image } from 'react-native';
108
import { mockData } from './mockData';
119
import RNFetchBlob from 'rn-fetch-blob';
12-
13-
// Note: test renderer must be required after react-native.
14-
import renderer from 'react-test-renderer';
10+
import { shallow } from 'enzyme';
1511

1612
// Ensure component can mount successfully.
1713
describe('CacheableImage', function () {
18-
it('renders correctly', async () => {
14+
it('renders correctly 1', (done) => {
1915
//Mock values for local/remote file request logic.
2016
RNFetchBlob.fs.exists
2117
.mockResolvedValueOnce(false) // mock not exist in local permanent dir
@@ -32,43 +28,29 @@ describe('CacheableImage', function () {
3228
const CacheableImage = imageCacheHoc(Image);
3329

3430
const styles = StyleSheet.create({
35-
container: {
36-
alignItems: 'center',
37-
flex: 1,
38-
justifyContent: 'center',
39-
},
4031
image: {
4132
height: 204,
4233
width: 150,
4334
},
44-
welcome: {
45-
fontSize: 20,
46-
margin: 10,
47-
textAlign: 'center',
48-
},
4935
});
5036

51-
const tree = renderer.create(
52-
<View style={styles.container}>
53-
<Text style={styles.welcome}>Test CacheableImage Component</Text>
54-
<CacheableImage
55-
style={styles.image}
56-
source={mockData.mockCacheableImageProps.source}
57-
permanent={mockData.mockCacheableImageProps.permanent}
58-
/>
59-
</View>
37+
const wrapper = shallow(
38+
<CacheableImage
39+
style={styles.image}
40+
source={mockData.mockCacheableImageProps.source}
41+
permanent={mockData.mockCacheableImageProps.permanent}
42+
/>
6043
);
61-
await new Promise((r) => setTimeout(r, 100)); // Hacky wait for lifecycle functions to complete
62-
expect(tree).toMatchSnapshot(); //If UI changes, this snapshot must be updated. See comment below.
63-
64-
/**
65-
The next time you run the tests, the rendered output will be compared to the previously created snapshot.
66-
The snapshot should be committed along code changes. When a snapshot test fails, you need to inspect whether it is an intended or unintended change.
67-
If the change is expected you can invoke Jest with npm test -- -u to overwrite the existing snapshot.
68-
*/
44+
45+
setImmediate(() => {
46+
expect(wrapper.prop('source')).toStrictEqual({
47+
uri: '/this/is/path/to/file.jpg',
48+
});
49+
done();
50+
});
6951
});
7052

71-
it('renders correctly with placeholder prop set', async () => {
53+
it('renders correctly with placeholder prop set', () => {
7254
//Mock values for local/remote file request logic.
7355
RNFetchBlob.fs.exists
7456
.mockResolvedValueOnce(false) // mock not exist in local permanent dir
@@ -85,11 +67,6 @@ describe('CacheableImage', function () {
8567
const CacheableImage = imageCacheHoc(Image);
8668

8769
const styles = StyleSheet.create({
88-
container: {
89-
alignItems: 'center',
90-
flex: 1,
91-
justifyContent: 'center',
92-
},
9370
image: {
9471
height: 204,
9572
width: 150,
@@ -100,11 +77,6 @@ describe('CacheableImage', function () {
10077
height: 204,
10178
width: 150,
10279
},
103-
welcome: {
104-
fontSize: 20,
105-
margin: 10,
106-
textAlign: 'center',
107-
},
10880
});
10981

11082
const propPlaceholder = {
@@ -114,28 +86,19 @@ describe('CacheableImage', function () {
11486
},
11587
};
11688

117-
const tree = renderer.create(
118-
<View style={styles.container}>
119-
<Text style={styles.welcome}>Test CacheableImage Component</Text>
120-
<CacheableImage
121-
style={styles.image}
122-
source={mockData.mockCacheableImageProps.source}
123-
permanent={mockData.mockCacheableImageProps.permanent}
124-
placeholder={propPlaceholder}
125-
/>
126-
</View>
89+
const wrapper = shallow(
90+
<CacheableImage
91+
style={styles.image}
92+
source={mockData.mockCacheableImageProps.source}
93+
permanent={mockData.mockCacheableImageProps.permanent}
94+
placeholder={propPlaceholder}
95+
/>,
96+
{ disableLifecycleMethods: true }
12797
);
128-
await new Promise((r) => setTimeout(r, 100)); // Hacky wait for lifecycle functions to complete
129-
expect(tree).toMatchSnapshot(); //If UI changes, this snapshot must be updated. See comment below.
130-
131-
/**
132-
The next time you run the tests, the rendered output will be compared to the previously created snapshot.
133-
The snapshot should be committed along code changes. When a snapshot test fails, you need to inspect whether it is an intended or unintended change.
134-
If the change is expected you can invoke Jest with npm test -- -u to overwrite the existing snapshot.
135-
*/
98+
expect(wrapper.prop('style')).toBe(propPlaceholder.props.style);
13699
});
137100

138-
it('renders correctly with placeholder option set', async () => {
101+
it('renders correctly with placeholder option set', () => {
139102
//Mock values for local/remote file request logic.
140103
RNFetchBlob.fs.exists
141104
.mockResolvedValueOnce(false) // mock not exist in local permanent dir
@@ -150,11 +113,6 @@ describe('CacheableImage', function () {
150113
});
151114

152115
const styles = StyleSheet.create({
153-
container: {
154-
alignItems: 'center',
155-
flex: 1,
156-
justifyContent: 'center',
157-
},
158116
image: {
159117
height: 204,
160118
width: 150,
@@ -165,11 +123,6 @@ describe('CacheableImage', function () {
165123
height: 204,
166124
width: 150,
167125
},
168-
welcome: {
169-
fontSize: 20,
170-
margin: 10,
171-
textAlign: 'center',
172-
},
173126
});
174127

175128
const optionPlaceholder = {
@@ -183,23 +136,14 @@ describe('CacheableImage', function () {
183136
defaultPlaceholder: optionPlaceholder,
184137
});
185138

186-
const tree = renderer.create(
187-
<View style={styles.container}>
188-
<Text style={styles.welcome}>Test CacheableImage Component</Text>
189-
<CacheableImage
190-
style={styles.image}
191-
source={mockData.mockCacheableImageProps.source}
192-
permanent={mockData.mockCacheableImageProps.permanent}
193-
/>
194-
</View>
139+
const wrapper = shallow(
140+
<CacheableImage
141+
style={styles.image}
142+
source={mockData.mockCacheableImageProps.source}
143+
permanent={mockData.mockCacheableImageProps.permanent}
144+
/>,
145+
{ disableLifecycleMethods: true }
195146
);
196-
expect(tree).toMatchSnapshot(); //If UI changes, this snapshot must be updated. See comment below.
197-
await new Promise((r) => setTimeout(r, 100)); // Hacky wait for lifecycle functions to complete, this is after the snapshot check because we want to see the first render
198-
199-
/**
200-
The next time you run the tests, the rendered output will be compared to the previously created snapshot.
201-
The snapshot should be committed along code changes. When a snapshot test fails, you need to inspect whether it is an intended or unintended change.
202-
If the change is expected you can invoke Jest with npm test -- -u to overwrite the existing snapshot.
203-
*/
147+
expect(wrapper.prop('style')).toBe(optionPlaceholder.props.style);
204148
});
205149
});

0 commit comments

Comments
 (0)