-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.syncCache.spec.ts
More file actions
80 lines (60 loc) · 3.07 KB
/
index.syncCache.spec.ts
File metadata and controls
80 lines (60 loc) · 3.07 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
import splitObject from './mocks/input.json';
import splitView from './mocks/output.json';
import { sdkManagerFactory } from '../index';
import { SplitsCacheInMemory } from '../../storages/inMemory/SplitsCacheInMemory';
import { ISdkReadinessManager } from '../../readiness/types';
import { loggerMock } from '../../logger/__tests__/sdkLogger.mock';
// @ts-expect-error
const sdkReadinessManagerMock = {
readinessManager: {
isReady: jest.fn(() => true),
isDestroyed: jest.fn(() => false)
},
sdkStatus: jest.fn()
} as ISdkReadinessManager;
describe('Manager with sync cache (In Memory)', () => {
/** Setup: create manager */
const cache = new SplitsCacheInMemory();
const manager = sdkManagerFactory({ mode: 'standalone', log: loggerMock }, cache, sdkReadinessManagerMock);
cache.addSplit(splitObject as any);
test('List all splits', () => {
const views = manager.splits();
expect(views[0]).toEqual(splitView);
});
test('Read only one split by name', () => {
const split = manager.split(splitObject.name);
expect(split).toEqual(splitView);
});
test('List all the split names', () => {
const names = manager.names();
expect(names.indexOf(splitObject.name) !== -1).toBe(true);
});
test('Input Validation', () => {
// control assertions to verify that the manager is connected with that cache.
expect(manager.split(splitObject.name) != null).toBe(true); // control assertion for split.
expect(manager.splits().length > 0).toBe(true); // control assertion for splits.
expect(manager.names().length > 0).toBe(true); // control assertion for names.
// @ts-expect-error
expect(manager.split(undefined)).toBe(null); // If the split name is invalid, `manager.split(invalidName)` returns null.
// This is kind of tied to the implementation of the isOperational validator.
(sdkReadinessManagerMock.readinessManager.isDestroyed as jest.Mock).mockImplementation(() => true);
expect(manager.split(splitObject.name)).toBe(null); // If the factory/client is destroyed, `manager.split(validName)` will return null either way since the storage is not valid.
expect(manager.splits()).toEqual([]); // If the factory/client is destroyed, `manager.splits()` will return empty array either way since the storage is not valid.
expect(manager.names()).toEqual([]); // If the factory/client is destroyed, `manager.names()` will return empty array either way since the storage is not valid.
});
test('returns empty results when not operational', async () => {
// SDK is flagged as destroyed
sdkReadinessManagerMock.readinessManager.isDestroyed = () => true;
function validateManager() {
expect(manager.split('some_spplit')).toBe(null);
expect(manager.splits()).toEqual([]);
expect(manager.names()).toEqual([]);
}
validateManager();
// SDK is not ready
sdkReadinessManagerMock.readinessManager.isReady = () => false;
sdkReadinessManagerMock.readinessManager.isReadyFromCache = () => false;
sdkReadinessManagerMock.readinessManager.isDestroyed = () => false;
validateManager();
});
});