-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathDevToolsTest.ts
More file actions
148 lines (131 loc) · 6.59 KB
/
Copy pathDevToolsTest.ts
File metadata and controls
148 lines (131 loc) · 6.59 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* eslint-disable dot-notation */
import Onyx from '../../lib';
import {getDevToolsInstance} from '../../lib/DevTools';
import type {DevtoolsConnection, RealDevTools as RealDevToolsType} from '../../lib/DevTools';
import RealDevTools from '../../lib/DevTools/RealDevTools';
import utils from '../../lib/utils';
import type GenericCollection from '../utils/GenericCollection';
const ONYX_KEYS = {
NUM_KEY: 'numKey',
OBJECT_KEY: 'objectKey',
SOME_KEY: 'someKey',
COLLECTION: {
NUM_KEY: 'test_',
TEST_CONNECT_COLLECTION: 'testConnectCollection_',
TEST_POLICY: 'testPolicy_',
TEST_UPDATE: 'testUpdate_',
},
};
const initialKeyStates = {
[ONYX_KEYS.NUM_KEY]: 1,
[ONYX_KEYS.OBJECT_KEY]: {id: 42},
};
const exampleCollection = {
[`${ONYX_KEYS.COLLECTION.NUM_KEY}1`]: 1,
[`${ONYX_KEYS.COLLECTION.NUM_KEY}2`]: 2,
} as GenericCollection;
const exampleObject = {name: 'Pedro'};
const mergedCollection = {...initialKeyStates, ...exampleCollection};
const mergedObject = {...initialKeyStates, [ONYX_KEYS.OBJECT_KEY]: {...exampleObject, id: 42}};
describe('DevTools', () => {
let initMock: jest.Mock<void>;
let sendMock: jest.Mock<void>;
beforeEach(() => {
// Mock DevTools - need to mock the connectViaExtension method BEFORE Onyx.init() is called
initMock = jest.fn();
sendMock = jest.fn();
// Mock the connectViaExtension method to return our mock connection
// This needs to happen before RealDevTools is instantiated
const mockConnection = {init: initMock, send: sendMock} as unknown as DevtoolsConnection;
jest.spyOn(RealDevTools.prototype, 'connectViaExtension').mockReturnValue(mockConnection);
Onyx.init({
keys: ONYX_KEYS,
initialKeyStates,
enableDevTools: true, // Enable DevTools for testing
});
});
afterEach(() => {
Onyx.clear();
jest.restoreAllMocks();
});
describe('Init', () => {
it('Sends the initial state correctly to the extension', () => {
expect(initMock).toHaveBeenCalledWith(initialKeyStates);
});
it('Sets the default state correctly', () => {
const devToolsInstance = getDevToolsInstance() as RealDevToolsType;
expect(devToolsInstance['defaultState']).toEqual(initialKeyStates);
});
it('Sets the internal state correctly', () => {
const devToolsInstance = getDevToolsInstance() as RealDevToolsType;
expect(devToolsInstance['state']).toEqual(initialKeyStates);
});
});
describe('Set', () => {
it('Sends the set state correctly to the extension', async () => {
await Onyx.set(ONYX_KEYS.SOME_KEY, 3);
expect(sendMock).toHaveBeenCalledWith({payload: 3, type: utils.formatActionName(Onyx.METHOD.SET, ONYX_KEYS.SOME_KEY)}, {...initialKeyStates, [ONYX_KEYS.SOME_KEY]: 3});
});
it('Sets the internal state correctly', async () => {
await Onyx.set(ONYX_KEYS.SOME_KEY, 3);
const devToolsInstance = getDevToolsInstance() as RealDevToolsType;
expect(devToolsInstance['state']).toEqual({...initialKeyStates, [ONYX_KEYS.SOME_KEY]: 3});
});
});
describe('Merge', () => {
it('Sends the merged state correctly to the extension', async () => {
await Onyx.merge(ONYX_KEYS.OBJECT_KEY, exampleObject);
expect(sendMock).toHaveBeenCalledWith({payload: exampleObject, type: utils.formatActionName(Onyx.METHOD.MERGE, ONYX_KEYS.OBJECT_KEY)}, mergedObject);
});
it('Sets the internal state correctly', async () => {
await Onyx.merge(ONYX_KEYS.OBJECT_KEY, exampleObject);
const devToolsInstance = getDevToolsInstance() as RealDevToolsType;
expect(devToolsInstance['state']).toEqual(mergedObject);
});
});
describe('MergeCollection', () => {
it('Sends the mergecollection state correctly to the extension', async () => {
await Onyx.mergeCollection(ONYX_KEYS.COLLECTION.NUM_KEY, exampleCollection);
expect(sendMock).toHaveBeenCalledWith({payload: exampleCollection, type: utils.formatActionName(Onyx.METHOD.MERGE_COLLECTION)}, mergedCollection);
});
it('Sets the internal state correctly', async () => {
await Onyx.mergeCollection(ONYX_KEYS.COLLECTION.NUM_KEY, exampleCollection);
const devToolsInstance = getDevToolsInstance() as RealDevToolsType;
expect(devToolsInstance['state']).toEqual(mergedCollection);
});
});
describe('MultiSet', () => {
it('Sends the multiset state correctly to the extension', async () => {
await Onyx.multiSet(exampleCollection);
expect(sendMock).toHaveBeenCalledWith({payload: exampleCollection, type: utils.formatActionName(Onyx.METHOD.MULTI_SET)}, mergedCollection);
});
it('Sets the internal state correctly', async () => {
await Onyx.multiSet(exampleCollection);
const devToolsInstance = getDevToolsInstance() as RealDevToolsType;
expect(devToolsInstance['state']).toEqual(mergedCollection);
});
});
describe('Clear', () => {
it('Sends the clear state correctly to the extension when no keys should be preserved', async () => {
await Onyx.clear();
expect(sendMock).toHaveBeenCalledWith({payload: undefined, type: 'CLEAR'}, initialKeyStates);
});
it('Sends the clear state correctly to the extension when there are keys that should be preserved', async () => {
await Onyx.merge(ONYX_KEYS.NUM_KEY, 2);
await Onyx.clear([ONYX_KEYS.NUM_KEY]);
expect(sendMock).toHaveBeenCalledWith({payload: undefined, type: 'CLEAR'}, {...initialKeyStates, [ONYX_KEYS.NUM_KEY]: 2});
});
it('Clears internal state correctly', async () => {
await Onyx.merge(ONYX_KEYS.NUM_KEY, 2);
await Onyx.clear([ONYX_KEYS.NUM_KEY]);
const devToolsInstance = getDevToolsInstance() as RealDevToolsType;
expect(devToolsInstance['state']).toEqual({...initialKeyStates, [ONYX_KEYS.NUM_KEY]: 2});
});
it('Preserves collection member keys when a collection key is passed to keysToPreserve', async () => {
await Onyx.mergeCollection(ONYX_KEYS.COLLECTION.NUM_KEY, exampleCollection);
await Onyx.clear([ONYX_KEYS.COLLECTION.NUM_KEY]);
const devToolsInstance = getDevToolsInstance() as RealDevToolsType;
expect(devToolsInstance['state']).toEqual({...initialKeyStates, ...exampleCollection});
});
});
});