-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcaching.unit.test.ts
More file actions
119 lines (100 loc) · 3.19 KB
/
caching.unit.test.ts
File metadata and controls
119 lines (100 loc) · 3.19 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import * as hasher from 'nx/src/hasher/file-hasher';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { cacheKey, getCacheRecord, setCacheRecord } from './caching';
describe('cacheKey', () => {
let hashObjectSpy;
beforeEach(() => {
hashObjectSpy = vi
.spyOn(hasher, 'hashObject')
.mockImplementation(() => '42');
});
afterEach(() => {
hashObjectSpy.mockClear();
});
it('should start with provided prefix', () => {
expect(cacheKey('verdaccio', {} as Record<string, unknown>)).toMatch(
/^verdaccio-/,
);
});
it('should use hashObject to generate hash', () => {
expect(cacheKey('x', { prop: 42 } as Record<string, unknown>)).toMatch(
/[0-9]*$/,
);
expect(hashObjectSpy).toHaveBeenCalledTimes(1);
expect(hashObjectSpy).toHaveBeenCalledWith({ prop: 42 });
});
it('should generate correct hash for empty object', () => {
expect(cacheKey('x', { prop: 42 } as Record<string, unknown>)).toBe('x-42');
expect(hashObjectSpy).toHaveBeenCalledTimes(1);
expect(hashObjectSpy).toHaveBeenCalledWith({ prop: 42 });
});
});
describe('getCacheRecord', () => {
let hashObjectSpy;
beforeEach(() => {
hashObjectSpy = vi
.spyOn(hasher, 'hashObject')
.mockImplementation(() => '42');
});
afterEach(() => {
hashObjectSpy.mockClear();
});
it('should get cached data if given', () => {
const prefix = 'verdaccio';
const targetsCache = {
'verdaccio-42': 'cacheData',
};
const hashData = { prop: 42 };
expect(getCacheRecord(targetsCache, prefix, hashData)).toBe('cacheData');
});
it('should return undefined if no cache hit', () => {
const targetsCache = {};
const prefix = 'verdaccio';
const hashData = { prop: 43 };
expect(getCacheRecord(targetsCache, prefix, hashData)).toBe(undefined);
});
it('should call cacheKey and hashObject', () => {
const targetsCache = {
'verdaccio-42': 'cacheData',
};
const prefix = 'verdaccio';
const hashData = { prop: 42 };
const hashObjectSpy = vi.spyOn(hasher, 'hashObject');
getCacheRecord(targetsCache, prefix, hashData);
expect(hashObjectSpy).toHaveBeenCalledTimes(1);
expect(hashObjectSpy).toHaveBeenCalledWith({ prop: 42 });
});
});
describe('setCacheRecord', () => {
let hashObjectSpy;
beforeEach(() => {
hashObjectSpy = vi
.spyOn(hasher, 'hashObject')
.mockImplementation(() => '42');
});
afterEach(() => {
hashObjectSpy.mockClear();
});
it('should set cached data if given', () => {
const prefix = 'verdaccio';
const targetsCache = {};
const hashData = { prop: 42 };
expect(getCacheRecord(targetsCache, prefix, hashData)).toStrictEqual(
undefined,
);
expect(
setCacheRecord(targetsCache, prefix, hashData, { test: 41 }),
).not.toThrowError();
expect(getCacheRecord(targetsCache, prefix, hashData)).toStrictEqual({
test: 41,
});
});
it('should return cached data after setting', () => {
const prefix = 'verdaccio';
const targetsCache = {};
const hashData = { prop: 42 };
expect(
setCacheRecord(targetsCache, prefix, hashData, { test: 41 }),
).toStrictEqual({ test: 41 });
});
});