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

Commit 83a61a0

Browse files
committed
Move rn-fetch-blob mock to it's own file
Use mockResolved* helpers where appropriate Import rather than require('rn-fetch-blob') Hacky wait for async component lifecycle methods to complete before tests finish ()
1 parent 44786b0 commit 83a61a0

File tree

6 files changed

+155
-165
lines changed

6 files changed

+155
-165
lines changed

tests/CacheableImage.test.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import imageCacheHoc from '../src/imageCacheHoc';
88
import { Image } from 'react-native';
99
import sinon from 'sinon';
1010
import 'should-sinon';
11+
import RNFetchBlob from 'rn-fetch-blob';
1112

1213
describe('CacheableImage', function () {
1314
it('HOC options validation should work as expected.', () => {
@@ -95,14 +96,11 @@ describe('CacheableImage', function () {
9596
});
9697

9798
it('#cacheFile static method should work as expected for cache dir files.', () => {
98-
// RNFetchBlob Mocks
99-
const RNFetchBlob = require('rn-fetch-blob');
100-
10199
// Mock that file does not exist on local fs.
102-
RNFetchBlob.fs.exists.mockReturnValue(false);
100+
RNFetchBlob.fs.exists.mockResolvedValue(false);
103101

104102
// Mock fetch result
105-
RNFetchBlob.fetch.mockReturnValue({
103+
RNFetchBlob.fetch.mockResolvedValue({
106104
path: () => {
107105
return '/this/is/path/to/file.jpg';
108106
},
@@ -122,14 +120,11 @@ describe('CacheableImage', function () {
122120
});
123121

124122
it('#cacheFile static method should work as expected for permanent dir files.', () => {
125-
// RNFetchBlob Mocks
126-
const RNFetchBlob = require('rn-fetch-blob');
127-
128123
// Mock that file does not exist on local fs.
129-
RNFetchBlob.fs.exists.mockReturnValue(false);
124+
RNFetchBlob.fs.exists.mockResolvedValue(false);
130125

131126
// Mock fetch result
132-
RNFetchBlob.fetch.mockReturnValue({
127+
RNFetchBlob.fetch.mockResolvedValue({
133128
path: () => {
134129
return '/this/is/path/to/file.jpg';
135130
},
@@ -150,11 +145,8 @@ describe('CacheableImage', function () {
150145
});
151146

152147
it('#flush static method should work as expected.', () => {
153-
// RNFetchBlob Mocks
154-
const RNFetchBlob = require('rn-fetch-blob');
155-
156148
// Mock unlink to always be true.
157-
RNFetchBlob.fs.unlink.mockReturnValue(true);
149+
RNFetchBlob.fs.unlink.mockResolvedValue(true);
158150

159151
const CacheableImage = imageCacheHoc(Image);
160152

@@ -354,7 +346,7 @@ describe('CacheableImage', function () {
354346
});
355347
});
356348

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

360352
const cacheableImage = new CacheableImage(mockData.mockCacheableImageProps);
@@ -364,5 +356,7 @@ describe('CacheableImage', function () {
364356
cacheableImage.state.localFilePath = './test.jpg';
365357

366358
cacheableImage.render();
359+
360+
await new Promise((r) => setTimeout(r, 100)); // Hacky wait for lifecycle functions to complete
367361
});
368362
});

tests/FileSystem.test.js

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import should from 'should'; // eslint-disable-line no-unused-vars
66
import FileSystemFactory, { FileSystem } from '../src/FileSystem';
77
import pathLib from 'path';
88
import { mockData } from './mockData';
9+
import RNFetchBlob from 'rn-fetch-blob';
910

1011
describe('FileSystem', function () {
1112
// Test static class properties and methods
@@ -69,7 +70,6 @@ describe('FileSystem', function () {
6970
});
7071

7172
it('#exists mocked as true.', () => {
72-
const RNFetchBlob = require('rn-fetch-blob');
7373
RNFetchBlob.fs.exists.mockReturnValue(true);
7474

7575
const fileSystem = FileSystemFactory();
@@ -128,71 +128,63 @@ describe('FileSystem', function () {
128128
fetch = jest.fn(); // eslint-disable-line no-global-assign
129129

130130
// Test PNG
131-
fetch.mockReturnValueOnce(
132-
Promise.resolve({
133-
headers: {
134-
get: (headerName) => {
135-
headerName.should.equals('content-type');
136-
137-
return 'image/png';
138-
},
131+
fetch.mockResolvedValueOnce({
132+
headers: {
133+
get: (headerName) => {
134+
headerName.should.equals('content-type');
135+
136+
return 'image/png';
139137
},
140-
})
141-
);
138+
},
139+
});
142140

143141
const pngFilename = await fileSystem.getFileNameFromUrl(
144142
'https://cdn2.hubspot.net/hub/42284/file-14233687-jpg/images/test_in_red'
145143
);
146144
pngFilename.should.equal('831eb245a3d9032cdce450f8760d2b8ddb442a3d.png');
147145

148146
// Test JPG
149-
fetch.mockReturnValueOnce(
150-
Promise.resolve({
151-
headers: {
152-
get: (headerName) => {
153-
headerName.should.equals('content-type');
154-
155-
return 'image/jpeg';
156-
},
147+
fetch.mockResolvedValueOnce({
148+
headers: {
149+
get: (headerName) => {
150+
headerName.should.equals('content-type');
151+
152+
return 'image/jpeg';
157153
},
158-
})
159-
);
154+
},
155+
});
160156

161157
const jpgFilename = await fileSystem.getFileNameFromUrl(
162158
'https://cdn2.hubspot.net/hub/42284/file-14233687-jpg/images/test_in_red'
163159
);
164160
jpgFilename.should.equal('831eb245a3d9032cdce450f8760d2b8ddb442a3d.jpg');
165161

166162
// Test GIF
167-
fetch.mockReturnValueOnce(
168-
Promise.resolve({
169-
headers: {
170-
get: (headerName) => {
171-
headerName.should.equals('content-type');
172-
173-
return 'image/gif';
174-
},
163+
fetch.mockResolvedValueOnce({
164+
headers: {
165+
get: (headerName) => {
166+
headerName.should.equals('content-type');
167+
168+
return 'image/gif';
175169
},
176-
})
177-
);
170+
},
171+
});
178172

179173
const gifFilename = await fileSystem.getFileNameFromUrl(
180174
'https://cdn2.hubspot.net/hub/42284/file-14233687-jpg/images/test_in_red'
181175
);
182176
gifFilename.should.equal('831eb245a3d9032cdce450f8760d2b8ddb442a3d.gif');
183177

184178
// Test BMP
185-
fetch.mockReturnValueOnce(
186-
Promise.resolve({
187-
headers: {
188-
get: (headerName) => {
189-
headerName.should.equals('content-type');
190-
191-
return 'image/bmp';
192-
},
179+
fetch.mockResolvedValueOnce({
180+
headers: {
181+
get: (headerName) => {
182+
headerName.should.equals('content-type');
183+
184+
return 'image/bmp';
193185
},
194-
})
195-
);
186+
},
187+
});
196188

197189
const bmpFilename = await fileSystem.getFileNameFromUrl(
198190
'https://cdn2.hubspot.net/hub/42284/file-14233687-jpg/images/test_in_red'
@@ -201,7 +193,6 @@ describe('FileSystem', function () {
201193
});
202194

203195
it('#getLocalFilePathFromUrl should return local filepath if it exists on local fs in permanent dir.', () => {
204-
const RNFetchBlob = require('rn-fetch-blob');
205196
RNFetchBlob.fs.exists
206197
.mockReturnValueOnce(true) // mock exist in local permanent dir
207198
.mockReturnValue(true);
@@ -221,7 +212,6 @@ describe('FileSystem', function () {
221212
});
222213

223214
it('#getLocalFilePathFromUrl should return local filepath if it exists on local fs in cache dir.', () => {
224-
const RNFetchBlob = require('rn-fetch-blob');
225215
RNFetchBlob.fs.exists
226216
.mockReturnValueOnce(false) // mock not exist in local permanent dir
227217
.mockReturnValueOnce(true) // mock exist in local cache dir
@@ -242,7 +232,6 @@ describe('FileSystem', function () {
242232
});
243233

244234
it('#getLocalFilePathFromUrl should download file and write to disk (default to cache dir) if it does not exist on local fs.', () => {
245-
const RNFetchBlob = require('rn-fetch-blob');
246235
RNFetchBlob.fs.exists
247236
.mockReturnValueOnce(false) // mock not exist in local permanent dir
248237
.mockReturnValueOnce(false) // mock not exist in local cache dir
@@ -291,7 +280,6 @@ describe('FileSystem', function () {
291280
it('#fetchFile clobber safeguard should work.', () => {
292281
const fileSystem = FileSystemFactory();
293282

294-
const RNFetchBlob = require('rn-fetch-blob');
295283
RNFetchBlob.fetch.mockReturnValue({
296284
path: () => {
297285
return '/this/is/path/to/file.jpg';
@@ -320,7 +308,6 @@ describe('FileSystem', function () {
320308
it('#fetchFile prune logic should not be called on permanent writes.', () => {
321309
const fileSystem = FileSystemFactory();
322310

323-
const RNFetchBlob = require('rn-fetch-blob');
324311
RNFetchBlob.fetch.mockReturnValue({
325312
path: () => {
326313
return '/this/is/path/to/file.jpg';
@@ -350,7 +337,6 @@ describe('FileSystem', function () {
350337
it('#fetchFile prune logic should be called on cache writes.', () => {
351338
const fileSystem = FileSystemFactory();
352339

353-
const RNFetchBlob = require('rn-fetch-blob');
354340
RNFetchBlob.fetch.mockReturnValue({
355341
path: () => {
356342
return '/this/is/path/to/file.jpg';
@@ -380,7 +366,6 @@ describe('FileSystem', function () {
380366
it('#fetchFile should work as expected.', () => {
381367
const fileSystem = FileSystemFactory();
382368

383-
const RNFetchBlob = require('rn-fetch-blob');
384369
RNFetchBlob.fetch.mockReturnValue({
385370
path: () => {
386371
return '/this/is/path/to/file.jpg';
@@ -433,7 +418,6 @@ describe('FileSystem', function () {
433418

434419
it('#unlink should work as expected for valid paths.', () => {
435420
// RNFetchBlob Mocks
436-
const RNFetchBlob = require('rn-fetch-blob');
437421

438422
// Mock unlink to be true.
439423
RNFetchBlob.fs.unlink.mockReturnValueOnce(true);

tests/__mocks__/rn-fetch-blob.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const { mockData } = require('../mockData');
2+
3+
module.exports = {
4+
fs: {
5+
dirs: {
6+
CacheDir: mockData.basePath,
7+
DocumentDir: mockData.basePath,
8+
},
9+
exists: jest.fn(),
10+
lstat: () => {
11+
let lstat = [
12+
{
13+
size: '43663',
14+
path:
15+
mockData.basePath +
16+
'/cache/0fbbfec764c73ee5b4e3a0cb8861469bc9fc6c8c.jpg',
17+
filename: '0fbbfec764c73ee5b4e3a0cb8861469bc9fc6c8c.jpg',
18+
lastModified: 1508878829000,
19+
type: 'file',
20+
},
21+
{
22+
size: '9000000',
23+
path:
24+
mockData.basePath +
25+
'/cache/6865fd0a65771b0044319f562873cc7b145abb4a.jpg',
26+
filename: '6865fd0a65771b0044319f562873cc7b145abb4a.jpg',
27+
lastModified: 1508877930000,
28+
type: 'file',
29+
},
30+
{
31+
size: '14133330',
32+
path:
33+
mockData.basePath +
34+
'/cache/b003269c377af6a2f53f59bc127a06c86f54312b.jpg',
35+
filename: 'b003269c377af6a2f53f59bc127a06c86f54312b.jpg',
36+
lastModified: 1508877698000,
37+
type: 'file',
38+
},
39+
{
40+
size: '1684',
41+
path:
42+
mockData.basePath +
43+
'/cache/d1052b9f22c1f00f4d658224f4295307b97db69f.jpg',
44+
filename: 'd1052b9f22c1f00f4d658224f4295307b97db69f.jpg',
45+
lastModified: 1508877954000,
46+
type: 'file',
47+
},
48+
{
49+
size: '65769',
50+
path:
51+
mockData.basePath +
52+
'/cache/faf4e58257d988ea6eab23aee5e5733bff9b2a9e.jpg',
53+
filename: 'faf4e58257d988ea6eab23aee5e5733bff9b2a9e.jpg',
54+
lastModified: 1509634852000,
55+
type: 'file',
56+
},
57+
];
58+
59+
return lstat;
60+
},
61+
unlink: jest.fn(),
62+
},
63+
config: jest.fn().mockReturnThis(),
64+
fetch: jest.fn(),
65+
};

tests/__snapshots__/imageCacheHoc.test.js.snap

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ exports[`CacheableImage renders correctly 1`] = `
2222
Test CacheableImage Component
2323
</Text>
2424
<Image
25+
source={
26+
Object {
27+
"uri": "/this/is/path/to/file.jpg",
28+
}
29+
}
2530
style={
2631
Object {
2732
"height": 204,
@@ -86,6 +91,26 @@ exports[`CacheableImage renders correctly with placeholder prop set 1`] = `
8691
>
8792
Test CacheableImage Component
8893
</Text>
89-
<Image />
94+
<Image
95+
placeholder={
96+
Object {
97+
"component": [Function],
98+
"props": Object {
99+
"style": undefined,
100+
},
101+
}
102+
}
103+
source={
104+
Object {
105+
"uri": "/this/is/path/to/file.jpg",
106+
}
107+
}
108+
style={
109+
Object {
110+
"height": 204,
111+
"width": 150,
112+
}
113+
}
114+
/>
90115
</View>
91116
`;

0 commit comments

Comments
 (0)