-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.test.ts
167 lines (139 loc) · 4.79 KB
/
index.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const devInit = jest.fn();
const devSend = jest.fn();
const devDisconnect = jest.fn();
const jumpToStateEvent = {
payload: {
type: 'JUMP_TO_STATE',
},
state: '{ "root": [] }'
};
const jumpToActionEvent = {
payload: {
type: 'JUMP_TO_ACTION',
},
state: '{ "root": [] }'
};
const importEvent = {
type: 'DISPATCH',
payload: {
nextLiftedState: require('./mocks.json'),
},
};
const devToolsMock = {
connect: jest.fn(() => ({
init: devInit,
send: devSend,
disconnect: devDisconnect,
subscribe: (callback) => callback(jumpToStateEvent),
})),
}
const devToolsMock1 = {
connect: jest.fn(() => ({
init: devInit,
send: devSend,
disconnect: devDisconnect,
subscribe: (callback) => callback(jumpToActionEvent),
})),
};
const devToolsMock2 = {
connect: jest.fn(() => ({
init: devInit,
send: devSend,
disconnect: devDisconnect,
subscribe: (callback) => callback(importEvent),
})),
};
describe('Dev Tools Extension (Without Extension)', () => {
let dispatchFn = {};
let dispatchContext: any = {};
beforeAll(() => {
const useContextDevTools = require('./index').default;
dispatchFn = jest.fn();
dispatchContext = useContextDevTools();
});
test('Should initialize devTools', () => {
dispatchContext.sendDispatch({});
expect(dispatchFn).not.toHaveBeenCalledWith({});
});
test('Should be able to send updated state', () => {
dispatchContext.sendUpdatedState({});
expect(devInit).not.toHaveBeenCalled();
dispatchContext.sendUpdatedState({});
expect(devSend).not.toHaveBeenCalled();
});
test('Should be able to subscribe to new events (Jump To State)', () => {
devToolsMock.connect().subscribe(() => jumpToStateEvent);
expect(devSend).not.toHaveBeenCalled();
});
test('Should be able to unsubscribe on tab close', () => {
window.dispatchEvent(new Event('beforeunload'));
dispatchContext.disconnectDevTools();
expect(devDisconnect).not.toHaveBeenCalled();
});
})
describe('Dev Tools Extension (JumpToState Event)', () => {
let dispatchFn = {};
let dispatchContext: any = {};
beforeAll(() => {
(window as any).__REDUX_DEVTOOLS_EXTENSION__ = devToolsMock;
const useContextDevTools = require('./index').default;
dispatchFn = jest.fn();
dispatchContext = useContextDevTools(dispatchFn);
});
test('Should initialize devTools', () => {
dispatchContext.sendDispatch({});
expect(dispatchFn).toHaveBeenCalledWith({});
});
test('Should be able to send updated state', () => {
dispatchContext.sendUpdatedState({});
expect(devInit).toHaveBeenCalled();
dispatchContext.sendUpdatedState({});
expect(devSend).toHaveBeenCalled();
});
test('Should be able to subscribe to new events (Jump To State)', () => {
devToolsMock.connect().subscribe(() => jumpToStateEvent);
expect(devSend).toHaveBeenCalled();
});
test('Should be able to unsubscribe on tab close', () => {
window.dispatchEvent(new Event('beforeunload'));
expect(devDisconnect).toHaveBeenCalled();
});
})
describe('Dev Tools Extension (Jump to Action Event)', () => {
let dispatchFn = {};
let dispatchContext: any = {};
beforeAll(() => {
(window as any).__REDUX_DEVTOOLS_EXTENSION__ = devToolsMock1;
const useContextDevTools = require('./index').default;
dispatchFn = jest.fn();
dispatchContext = useContextDevTools(dispatchFn);
});
test('Should initialize devTools', () => {
dispatchContext.sendDispatch({});
expect(dispatchFn).toHaveBeenCalledWith({});
});
test('Should be able to subscribe to new events (Jump To Action)', () => {
devToolsMock1.connect().subscribe(() => jumpToActionEvent);
window.dispatchEvent(new Event('beforeunload'));
expect(devSend).toHaveBeenCalled();
});
})
describe('Dev Tools Extension (Import Event)', () => {
let dispatchFn = {};
let dispatchContext: any = {};
beforeAll(() => {
(window as any).__REDUX_DEVTOOLS_EXTENSION__ = devToolsMock2;
const useContextDevTools = require('./index').default;
dispatchFn = jest.fn();
dispatchContext = useContextDevTools(dispatchFn);
});
test('Should initialize devTools', () => {
dispatchContext.sendDispatch({});
expect(dispatchFn).toHaveBeenCalledWith({});
});
test('Should be able to subscribe to new events (Import)', () => {
devToolsMock2.connect().subscribe(() => importEvent);
window.dispatchEvent(new Event('beforeunload'));
expect(devSend).toHaveBeenCalled();
});
})