2
2
/* global expect describe it */
3
3
4
4
// Load dependencies
5
- import should from 'should' ; // eslint-disable-line no-unused-vars
6
5
import React from 'react' ;
7
- import 'react-native' ;
8
6
import imageCacheHoc from '../src/imageCacheHoc' ;
9
- import { StyleSheet , View , Text , Image } from 'react-native' ;
7
+ import { StyleSheet , Image } from 'react-native' ;
10
8
import { mockData } from './mockData' ;
11
9
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' ;
15
11
16
12
// Ensure component can mount successfully.
17
13
describe ( 'CacheableImage' , function ( ) {
18
- it ( 'renders correctly' , async ( ) => {
14
+ it ( 'renders correctly 1 ' , ( done ) => {
19
15
//Mock values for local/remote file request logic.
20
16
RNFetchBlob . fs . exists
21
17
. mockResolvedValueOnce ( false ) // mock not exist in local permanent dir
@@ -32,43 +28,29 @@ describe('CacheableImage', function () {
32
28
const CacheableImage = imageCacheHoc ( Image ) ;
33
29
34
30
const styles = StyleSheet . create ( {
35
- container : {
36
- alignItems : 'center' ,
37
- flex : 1 ,
38
- justifyContent : 'center' ,
39
- } ,
40
31
image : {
41
32
height : 204 ,
42
33
width : 150 ,
43
34
} ,
44
- welcome : {
45
- fontSize : 20 ,
46
- margin : 10 ,
47
- textAlign : 'center' ,
48
- } ,
49
35
} ) ;
50
36
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
+ />
60
43
) ;
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
+ } ) ;
69
51
} ) ;
70
52
71
- it ( 'renders correctly with placeholder prop set' , async ( ) => {
53
+ it ( 'renders correctly with placeholder prop set' , ( ) => {
72
54
//Mock values for local/remote file request logic.
73
55
RNFetchBlob . fs . exists
74
56
. mockResolvedValueOnce ( false ) // mock not exist in local permanent dir
@@ -85,11 +67,6 @@ describe('CacheableImage', function () {
85
67
const CacheableImage = imageCacheHoc ( Image ) ;
86
68
87
69
const styles = StyleSheet . create ( {
88
- container : {
89
- alignItems : 'center' ,
90
- flex : 1 ,
91
- justifyContent : 'center' ,
92
- } ,
93
70
image : {
94
71
height : 204 ,
95
72
width : 150 ,
@@ -100,11 +77,6 @@ describe('CacheableImage', function () {
100
77
height : 204 ,
101
78
width : 150 ,
102
79
} ,
103
- welcome : {
104
- fontSize : 20 ,
105
- margin : 10 ,
106
- textAlign : 'center' ,
107
- } ,
108
80
} ) ;
109
81
110
82
const propPlaceholder = {
@@ -114,28 +86,19 @@ describe('CacheableImage', function () {
114
86
} ,
115
87
} ;
116
88
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 }
127
97
) ;
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 ) ;
136
99
} ) ;
137
100
138
- it ( 'renders correctly with placeholder option set' , async ( ) => {
101
+ it ( 'renders correctly with placeholder option set' , ( ) => {
139
102
//Mock values for local/remote file request logic.
140
103
RNFetchBlob . fs . exists
141
104
. mockResolvedValueOnce ( false ) // mock not exist in local permanent dir
@@ -150,11 +113,6 @@ describe('CacheableImage', function () {
150
113
} ) ;
151
114
152
115
const styles = StyleSheet . create ( {
153
- container : {
154
- alignItems : 'center' ,
155
- flex : 1 ,
156
- justifyContent : 'center' ,
157
- } ,
158
116
image : {
159
117
height : 204 ,
160
118
width : 150 ,
@@ -165,11 +123,6 @@ describe('CacheableImage', function () {
165
123
height : 204 ,
166
124
width : 150 ,
167
125
} ,
168
- welcome : {
169
- fontSize : 20 ,
170
- margin : 10 ,
171
- textAlign : 'center' ,
172
- } ,
173
126
} ) ;
174
127
175
128
const optionPlaceholder = {
@@ -183,23 +136,14 @@ describe('CacheableImage', function () {
183
136
defaultPlaceholder : optionPlaceholder ,
184
137
} ) ;
185
138
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 }
195
146
) ;
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 ) ;
204
148
} ) ;
205
149
} ) ;
0 commit comments