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

Commit 9954b93

Browse files
committed
Add tests for cacheLocalFile
1 parent 5c330b4 commit 9954b93

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

src/FileSystem.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,13 @@ export class FileSystem {
234234
const path = this.baseFilePath + fileName
235235
this._validatePath(path, true)
236236

237+
if (!(await RNFS.exists(local))) {
238+
return {
239+
url,
240+
path: null,
241+
}
242+
}
243+
237244
// Logic here prunes cache directory on "cache" writes to ensure cache doesn't get too large.
238245
await this.pruneCache()
239246

@@ -252,14 +259,14 @@ export class FileSystem {
252259
} catch (error) {
253260
await RNFSUnlinkIfExists(path)
254261
return {
255-
url: null,
262+
url,
256263
path: null,
257264
}
258265
}
259266

260267
return {
261-
url: url,
262-
path: path,
268+
url,
269+
path,
263270
}
264271
}
265272

tests/CacheableImage.test.tsx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,61 @@ describe('CacheableImage', function () {
9292
})
9393
})
9494

95+
describe('cacheLocalFile', () => {
96+
it('When local file exists, it should be copied to the cache', async () => {
97+
const CacheableImage = imageCacheHoc(Image)
98+
99+
const local = '/exists.png'
100+
const url = 'https://example.com/exists.png'
101+
102+
const result = await CacheableImage.cacheLocalFile(local, url)
103+
104+
expect(result).toStrictEqual({
105+
url: 'https://example.com/exists.png',
106+
path:
107+
'/base/file/path/react-native-image-cache-hoc/90c1be491d18ff2a7280039e9b65749461a65403.png',
108+
})
109+
110+
expect(MockedRNFS.copyFile).toHaveBeenCalled()
111+
})
112+
113+
it('When local file exists, it should be moved to the cache', async () => {
114+
const CacheableImage = imageCacheHoc(Image)
115+
116+
const local = '/exists.png'
117+
const url = 'https://example.com/exists.png'
118+
119+
const result = await CacheableImage.cacheLocalFile(local, url, true)
120+
121+
expect(result).toStrictEqual({
122+
url: 'https://example.com/exists.png',
123+
path:
124+
'/base/file/path/react-native-image-cache-hoc/90c1be491d18ff2a7280039e9b65749461a65403.png',
125+
})
126+
127+
expect(MockedRNFS.moveFile).toHaveBeenCalled()
128+
})
129+
130+
it('When local file does not exist, no fs operation should be performed', async () => {
131+
MockedRNFS.exists.mockResolvedValueOnce(false)
132+
133+
const CacheableImage = imageCacheHoc(Image)
134+
135+
const local = '/missing.png'
136+
const url = 'https://example.com/missing.png'
137+
138+
const result = await CacheableImage.cacheLocalFile(local, url)
139+
140+
expect(result).toStrictEqual({
141+
url: 'https://example.com/missing.png',
142+
path: null,
143+
})
144+
145+
expect(MockedRNFS.copyFile).not.toHaveBeenCalled()
146+
expect(MockedRNFS.moveFile).not.toHaveBeenCalled()
147+
})
148+
})
149+
95150
it('#flush static method should work as expected.', () => {
96151
// Mock unlink to always be true.
97152
MockedRNFS.unlink.mockResolvedValueOnce()

0 commit comments

Comments
 (0)