|
9 | 9 | * @oncall react_native
|
10 | 10 | */
|
11 | 11 |
|
12 |
| -import type {BuildParameters} from '../../flow-types'; |
| 12 | +import type {BuildParameters, CacheData} from '../../flow-types'; |
13 | 13 |
|
14 | 14 | import {DiskCacheManager} from '../DiskCacheManager';
|
15 | 15 | import * as path from 'path';
|
| 16 | +import {serialize} from 'v8'; |
| 17 | + |
| 18 | +const mockReadFile = jest.fn(); |
| 19 | +const mockWriteFile = jest.fn(); |
| 20 | + |
| 21 | +jest.mock('fs', () => ({ |
| 22 | + promises: { |
| 23 | + readFile: (...args) => mockReadFile(...args), |
| 24 | + writeFile: (...args) => mockWriteFile(...args), |
| 25 | + }, |
| 26 | +})); |
16 | 27 |
|
17 | 28 | const buildParameters: BuildParameters = {
|
18 | 29 | cacheBreaker: '',
|
@@ -42,6 +53,10 @@ const defaultConfig = {
|
42 | 53 | };
|
43 | 54 |
|
44 | 55 | describe('cacheManager', () => {
|
| 56 | + beforeEach(() => { |
| 57 | + jest.clearAllMocks(); |
| 58 | + }); |
| 59 | + |
45 | 60 | test('creates valid cache file paths', () => {
|
46 | 61 | expect(
|
47 | 62 | DiskCacheManager.getCacheFilePath(buildParameters, 'file-prefix', '/'),
|
@@ -158,4 +173,43 @@ describe('cacheManager', () => {
|
158 | 173 | cacheManager2.getCacheFilePath(),
|
159 | 174 | );
|
160 | 175 | });
|
| 176 | + |
| 177 | + test('reads a cache file and deserialises its contents', async () => { |
| 178 | + const cacheManager = new DiskCacheManager({buildParameters}, defaultConfig); |
| 179 | + mockReadFile.mockResolvedValueOnce(serialize({foo: 'bar'})); |
| 180 | + const cache = await cacheManager.read(); |
| 181 | + expect(mockReadFile).toHaveBeenCalledWith(cacheManager.getCacheFilePath()); |
| 182 | + expect(cache).toEqual({foo: 'bar'}); |
| 183 | + }); |
| 184 | + |
| 185 | + test('serialises and writes a cache file', async () => { |
| 186 | + const cacheManager = new DiskCacheManager({buildParameters}, defaultConfig); |
| 187 | + const data: CacheData = { |
| 188 | + clocks: new Map([['foo', 'bar']]), |
| 189 | + fileSystemData: new Map(), |
| 190 | + plugins: new Map(), |
| 191 | + }; |
| 192 | + await cacheManager.write(data, { |
| 193 | + changed: new Map(), |
| 194 | + removed: new Set(['foo']), |
| 195 | + }); |
| 196 | + expect(mockWriteFile).toHaveBeenCalledWith( |
| 197 | + cacheManager.getCacheFilePath(), |
| 198 | + serialize(data), |
| 199 | + ); |
| 200 | + }); |
| 201 | + |
| 202 | + test('does not write when there have been no changes', async () => { |
| 203 | + const cacheManager = new DiskCacheManager({buildParameters}, defaultConfig); |
| 204 | + await cacheManager.write( |
| 205 | + { |
| 206 | + clocks: new Map([['foo', 'bar']]), |
| 207 | + fileSystemData: new Map(), |
| 208 | + plugins: new Map(), |
| 209 | + }, |
| 210 | + // Empty delta |
| 211 | + {changed: new Map(), removed: new Set()}, |
| 212 | + ); |
| 213 | + expect(mockWriteFile).not.toHaveBeenCalled(); |
| 214 | + }); |
161 | 215 | });
|
0 commit comments