-
Notifications
You must be signed in to change notification settings - Fork 28k
/
Copy pathload-manifest.test.ts
82 lines (64 loc) · 2.69 KB
/
load-manifest.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { loadManifest } from './load-manifest'
import { readFileSync } from 'fs'
jest.mock('fs')
describe('loadManifest', () => {
const cache = new Map<string, unknown>()
afterEach(() => {
jest.resetAllMocks()
cache.clear()
})
it('should load the manifest from the file system when not cached', () => {
const mockManifest = { key: 'value' }
;(readFileSync as jest.Mock).mockReturnValue(JSON.stringify(mockManifest))
let result = loadManifest('path/to/manifest', false)
expect(result).toEqual(mockManifest)
expect(readFileSync).toHaveBeenCalledTimes(1)
expect(readFileSync).toHaveBeenCalledWith('path/to/manifest', 'utf8')
expect(cache.has('path/to/manifest')).toBe(false)
result = loadManifest('path/to/manifest', false)
expect(result).toEqual(mockManifest)
expect(readFileSync).toHaveBeenCalledTimes(2)
expect(readFileSync).toHaveBeenCalledWith('path/to/manifest', 'utf8')
expect(cache.has('path/to/manifest')).toBe(false)
})
it('should return the cached manifest when available', () => {
const mockManifest = { key: 'value' }
cache.set('path/to/manifest', mockManifest)
let result = loadManifest('path/to/manifest', true, cache)
expect(result).toBe(mockManifest)
expect(readFileSync).not.toHaveBeenCalled()
result = loadManifest('path/to/manifest', true, cache)
expect(result).toBe(mockManifest)
expect(readFileSync).not.toHaveBeenCalled()
})
it('should cache the manifest when not already cached', () => {
const mockManifest = { key: 'value' }
;(readFileSync as jest.Mock).mockReturnValue(JSON.stringify(mockManifest))
const result = loadManifest('path/to/manifest', true, cache)
expect(result).toEqual(mockManifest)
expect(cache.get('path/to/manifest')).toEqual(mockManifest)
expect(readFileSync).toHaveBeenCalledWith('path/to/manifest', 'utf8')
})
it('should throw an error when the manifest file cannot be read', () => {
;(readFileSync as jest.Mock).mockImplementation(() => {
throw new Error('File not found')
})
expect(() => loadManifest('path/to/manifest', false)).toThrow(
'File not found'
)
})
it('should freeze the manifest when caching', () => {
const mockManifest = { key: 'value', nested: { key: 'value' } }
;(readFileSync as jest.Mock).mockReturnValue(JSON.stringify(mockManifest))
const result = loadManifest(
'path/to/manifest',
true,
cache
) as typeof mockManifest
expect(Object.isFrozen(result)).toBe(true)
expect(Object.isFrozen(result.nested)).toBe(true)
const result2 = loadManifest('path/to/manifest', true, cache)
expect(Object.isFrozen(result2)).toBe(true)
expect(result).toBe(result2)
})
})