@@ -74,6 +74,10 @@ describe('lib/FileSystem', function() {
74
74
75
75
it ( '#exists mocked as true.' , ( ) => {
76
76
77
+ const RNFetchBlob = require ( 'react-native-fetch-blob' ) ;
78
+ RNFetchBlob . fs . exists
79
+ . mockReturnValue ( true ) ;
80
+
77
81
const fileSystem = FileSystemFactory ( ) ;
78
82
79
83
fileSystem . exists ( 'abitrary-file.jpg' ) . should . be . true ( ) ;
@@ -98,6 +102,64 @@ describe('lib/FileSystem', function() {
98
102
99
103
} ) ;
100
104
105
+ it ( '#getLocalFilePathFromUrl should return local filepath if it exists on local fs in permanent dir.' , ( ) => {
106
+
107
+ const RNFetchBlob = require ( 'react-native-fetch-blob' ) ;
108
+ RNFetchBlob . fs . exists
109
+ . mockReturnValueOnce ( true ) // mock exist in local permanent dir
110
+ . mockReturnValue ( true ) ;
111
+
112
+ const fileSystem = FileSystemFactory ( ) ;
113
+
114
+ return fileSystem . getLocalFilePathFromUrl ( 'https://img.wennermedia.com/5333a62d-07db-432a-92e2-198cafa38a14-326adb1a-d8ed-4a5d-b37e-5c88883e1989.png' )
115
+ . then ( localFilePath => {
116
+ localFilePath . should . equal ( mockData . basePath + '/react-native-image-cache-hoc/permanent/cd7d2199cd8e088cdfd9c99fc6359666adc36289.png' ) ;
117
+ } ) ;
118
+
119
+ } ) ;
120
+
121
+ it ( '#getLocalFilePathFromUrl should return local filepath if it exists on local fs in cache dir.' , ( ) => {
122
+
123
+ const RNFetchBlob = require ( 'react-native-fetch-blob' ) ;
124
+ RNFetchBlob . fs . exists
125
+ . mockReturnValueOnce ( false ) // mock not exist in local permanent dir
126
+ . mockReturnValueOnce ( true ) // mock exist in local cache dir
127
+ . mockReturnValue ( true ) ;
128
+
129
+ const fileSystem = FileSystemFactory ( ) ;
130
+
131
+ return fileSystem . getLocalFilePathFromUrl ( 'https://img.wennermedia.com/5333a62d-07db-432a-92e2-198cafa38a14-326adb1a-d8ed-4a5d-b37e-5c88883e1989.png' )
132
+ . then ( localFilePath => {
133
+ localFilePath . should . equal ( mockData . basePath + '/react-native-image-cache-hoc/cache/cd7d2199cd8e088cdfd9c99fc6359666adc36289.png' ) ;
134
+ } ) ;
135
+
136
+ } ) ;
137
+
138
+ it ( '#getLocalFilePathFromUrl should download file and write to disk (default to cache dir) if it does not exist on local fs.' , ( ) => {
139
+
140
+ const RNFetchBlob = require ( 'react-native-fetch-blob' ) ;
141
+ RNFetchBlob . fs . exists
142
+ . mockReturnValueOnce ( false ) // mock not exist in local permanent dir
143
+ . mockReturnValueOnce ( false ) // mock not exist in local cache dir
144
+ . mockReturnValueOnce ( false ) // mock does not exist to get past clobber
145
+ . mockReturnValue ( true ) ;
146
+
147
+ RNFetchBlob . fetch
148
+ . mockReturnValue ( {
149
+ path : ( ) => {
150
+ return '/this/is/path/to/file.jpg' ;
151
+ }
152
+ } ) ;
153
+
154
+ const fileSystem = FileSystemFactory ( ) ;
155
+
156
+ return fileSystem . getLocalFilePathFromUrl ( 'https://img.wennermedia.com/5333a62d-07db-432a-92e2-198cafa38a14-326adb1a-d8ed-4a5d-b37e-5c88883e1989.png' )
157
+ . then ( localFilePath => {
158
+ localFilePath . should . equal ( '/this/is/path/to/file.jpg' ) ;
159
+ } ) ;
160
+
161
+ } ) ;
162
+
101
163
it ( '#fetchFile should validate path.' , ( ) => {
102
164
103
165
const fileSystem = FileSystemFactory ( ) ;
@@ -119,6 +181,14 @@ describe('lib/FileSystem', function() {
119
181
120
182
const fileSystem = FileSystemFactory ( ) ;
121
183
184
+ const RNFetchBlob = require ( 'react-native-fetch-blob' ) ;
185
+ RNFetchBlob . fetch
186
+ . mockReturnValue ( {
187
+ path : ( ) => {
188
+ return '/this/is/path/to/file.jpg' ;
189
+ }
190
+ } ) ;
191
+
122
192
// fileSystem.exists() is mocked to always return true, so error should always be thrown unless clobber is set to true.
123
193
return fileSystem . fetchFile ( 'https://img.wennermedia.com/5333a62d-07db-432a-92e2-198cafa38a14-326adb1a-d8ed-4a5d-b37e-5c88883e1989.png' )
124
194
. then ( ( ) => {
@@ -134,6 +204,14 @@ describe('lib/FileSystem', function() {
134
204
135
205
const fileSystem = FileSystemFactory ( ) ;
136
206
207
+ const RNFetchBlob = require ( 'react-native-fetch-blob' ) ;
208
+ RNFetchBlob . fetch
209
+ . mockReturnValue ( {
210
+ path : ( ) => {
211
+ return '/this/is/path/to/file.jpg' ;
212
+ }
213
+ } ) ;
214
+
137
215
let pruneCacheHit = false ;
138
216
139
217
// Mock fileSystem.pruneCache() to determine if it is called correctly.
@@ -153,6 +231,14 @@ describe('lib/FileSystem', function() {
153
231
154
232
const fileSystem = FileSystemFactory ( ) ;
155
233
234
+ const RNFetchBlob = require ( 'react-native-fetch-blob' ) ;
235
+ RNFetchBlob . fetch
236
+ . mockReturnValue ( {
237
+ path : ( ) => {
238
+ return '/this/is/path/to/file.jpg' ;
239
+ }
240
+ } ) ;
241
+
156
242
let pruneCacheHit = false ;
157
243
158
244
// Mock fileSystem.pruneCache() to determine if it is called correctly.
@@ -172,6 +258,14 @@ describe('lib/FileSystem', function() {
172
258
173
259
const fileSystem = FileSystemFactory ( ) ;
174
260
261
+ const RNFetchBlob = require ( 'react-native-fetch-blob' ) ;
262
+ RNFetchBlob . fetch
263
+ . mockReturnValue ( {
264
+ path : ( ) => {
265
+ return '/this/is/path/to/file.jpg' ;
266
+ }
267
+ } ) ;
268
+
175
269
// Mock fileSystem.pruneCache().
176
270
fileSystem . pruneCache = ( ) => { } ;
177
271
0 commit comments