Skip to content

Commit cb0617f

Browse files
committed
fix: tests covered to 100% now
1 parent a6d1a84 commit cb0617f

File tree

3 files changed

+767
-39
lines changed

3 files changed

+767
-39
lines changed

index.test.ts

Lines changed: 137 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,98 @@
1-
import useContextDevTools from './index';
2-
31
const devInit = jest.fn();
42
const devSend = jest.fn();
53
const devDisconnect = jest.fn();
64

5+
const jumpToStateEvent = {
6+
payload: {
7+
type: 'JUMP_TO_STATE',
8+
},
9+
state: '{ "root": [] }'
10+
};
11+
12+
const jumpToActionEvent = {
13+
payload: {
14+
type: 'JUMP_TO_ACTION',
15+
},
16+
state: '{ "root": [] }'
17+
};
18+
19+
const importEvent = {
20+
type: 'DISPATCH',
21+
payload: {
22+
nextLiftedState: require('./mocks.json'),
23+
},
24+
};
25+
726
const devToolsMock = {
827
connect: jest.fn(() => ({
928
init: devInit,
1029
send: devSend,
1130
disconnect: devDisconnect,
31+
subscribe: (callback) => callback(jumpToStateEvent),
32+
})),
33+
}
34+
35+
const devToolsMock1 = {
36+
connect: jest.fn(() => ({
37+
init: devInit,
38+
send: devSend,
39+
disconnect: devDisconnect,
40+
subscribe: (callback) => callback(jumpToActionEvent),
41+
})),
42+
};
43+
44+
const devToolsMock2 = {
45+
connect: jest.fn(() => ({
46+
init: devInit,
47+
send: devSend,
48+
disconnect: devDisconnect,
49+
subscribe: (callback) => callback(importEvent),
1250
})),
1351
};
1452

15-
describe('Dev Tools Extension', () => {
16-
(window as any).__REDUX_DEVTOOLS_EXTENSION__ = devToolsMock;
17-
const dispatchFn = jest.fn();
18-
const dispatchContext = useContextDevTools(dispatchFn);
53+
describe('Dev Tools Extension (Without Extension)', () => {
54+
let dispatchFn = {};
55+
let dispatchContext: any = {};
56+
57+
beforeAll(() => {
58+
const useContextDevTools = require('./index').default;
59+
dispatchFn = jest.fn();
60+
dispatchContext = useContextDevTools(dispatchFn);
61+
});
62+
63+
test('Should initialize devTools', () => {
64+
dispatchContext.sendDispatch({});
65+
expect(dispatchFn).toHaveBeenCalledWith({});
66+
});
67+
68+
test('Should be able to send updated state', () => {
69+
dispatchContext.sendUpdatedState({});
70+
expect(devInit).not.toHaveBeenCalled();
71+
dispatchContext.sendUpdatedState({});
72+
expect(devSend).not.toHaveBeenCalled();
73+
});
74+
75+
test('Should be able to subscribe to new events (Jump To State)', () => {
76+
devToolsMock.connect().subscribe(() => jumpToStateEvent);
77+
expect(devSend).not.toHaveBeenCalled();
78+
});
79+
80+
test('Should be able to unsubscribe on tab close', () => {
81+
window.dispatchEvent(new Event('beforeunload'));
82+
expect(devDisconnect).not.toHaveBeenCalled();
83+
});
84+
})
85+
86+
describe('Dev Tools Extension (JumpToState Event)', () => {
87+
let dispatchFn = {};
88+
let dispatchContext: any = {};
89+
90+
beforeAll(() => {
91+
(window as any).__REDUX_DEVTOOLS_EXTENSION__ = devToolsMock;
92+
const useContextDevTools = require('./index').default;
93+
dispatchFn = jest.fn();
94+
dispatchContext = useContextDevTools(dispatchFn);
95+
});
1996

2097
test('Should initialize devTools', () => {
2198
dispatchContext.sendDispatch({});
@@ -29,9 +106,61 @@ describe('Dev Tools Extension', () => {
29106
expect(devSend).toHaveBeenCalled();
30107
});
31108

32-
test('Should be able to disconnect', () => {
33-
dispatchContext.disconnectDevTools();
109+
test('Should be able to subscribe to new events (Jump To State)', () => {
110+
devToolsMock.connect().subscribe(() => jumpToStateEvent);
111+
expect(devSend).toHaveBeenCalled();
112+
});
113+
114+
test('Should be able to unsubscribe on tab close', () => {
115+
window.dispatchEvent(new Event('beforeunload'));
34116
expect(devDisconnect).toHaveBeenCalled();
35117
});
36118
})
37119

120+
121+
describe('Dev Tools Extension (Jump to Action Event)', () => {
122+
let dispatchFn = {};
123+
let dispatchContext: any = {};
124+
125+
beforeAll(() => {
126+
(window as any).__REDUX_DEVTOOLS_EXTENSION__ = devToolsMock1;
127+
const useContextDevTools = require('./index').default;
128+
dispatchFn = jest.fn();
129+
dispatchContext = useContextDevTools(dispatchFn);
130+
});
131+
132+
test('Should initialize devTools', () => {
133+
dispatchContext.sendDispatch({});
134+
expect(dispatchFn).toHaveBeenCalledWith({});
135+
});
136+
137+
test('Should be able to subscribe to new events (Jump To Action)', () => {
138+
devToolsMock1.connect().subscribe(() => jumpToActionEvent);
139+
window.dispatchEvent(new Event('beforeunload'));
140+
expect(devSend).toHaveBeenCalled();
141+
});
142+
})
143+
144+
describe('Dev Tools Extension (Import Event)', () => {
145+
let dispatchFn = {};
146+
let dispatchContext: any = {};
147+
148+
beforeAll(() => {
149+
(window as any).__REDUX_DEVTOOLS_EXTENSION__ = devToolsMock2;
150+
const useContextDevTools = require('./index').default;
151+
dispatchFn = jest.fn();
152+
dispatchContext = useContextDevTools(dispatchFn);
153+
});
154+
155+
test('Should initialize devTools', () => {
156+
dispatchContext.sendDispatch({});
157+
expect(dispatchFn).toHaveBeenCalledWith({});
158+
});
159+
160+
test('Should be able to subscribe to new events (Import)', () => {
161+
devToolsMock2.connect().subscribe(() => importEvent);
162+
window.dispatchEvent(new Event('beforeunload'));
163+
expect(devSend).toHaveBeenCalled();
164+
});
165+
166+
})

index.ts

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
1-
// Context API Dev Tools
2-
const devTools: any = { send: () => {} };
3-
let isInitialized = false;
1+
// Context API Dev Toolsx
2+
const devTools: any = { isInitialized: false };
43
let firstRender = false;
54
let oldArgs: any = {};
65

76
const useContextDevTools = (dispatch: Function) => {
8-
if (!isInitialized && (window as any).__REDUX_DEVTOOLS_EXTENSION__) {
7+
8+
const sendDispatch = (args: any) => {
9+
if(dispatch) {
10+
dispatch(args);
11+
}
12+
oldArgs = args;
13+
};
14+
15+
const sendUpdatedState = (updatedState: any) => {
16+
if ((window as any).__REDUX_DEVTOOLS_EXTENSION__) {
17+
if (!firstRender) {
18+
devTools.current.init(updatedState);
19+
firstRender = true;
20+
} else {
21+
if (oldArgs.type !== 'IMPORT_STATE') {
22+
devTools.current.send(oldArgs, updatedState);
23+
}
24+
}
25+
}
26+
};
27+
28+
const disconnectDevTools = () => {
29+
if ((window as any).__REDUX_DEVTOOLS_EXTENSION__) {
30+
devTools.isInitialized = false;
31+
return typeof devTools.current.disconnect === 'function' && devTools.current.disconnect();
32+
}
33+
}
34+
35+
if (!devTools.isInitialized && (window as any).__REDUX_DEVTOOLS_EXTENSION__) {
936
devTools.current = (window as any).__REDUX_DEVTOOLS_EXTENSION__.connect({
1037
features: {
1138
pause: true, // start/pause recording of dispatched actions
@@ -21,7 +48,7 @@ const useContextDevTools = (dispatch: Function) => {
2148
},
2249
// other options like actionSanitizer, stateSanitizer
2350
});
24-
isInitialized = true;
51+
devTools.isInitialized = true;
2552
if (typeof devTools.current.subscribe === 'function') {
2653
devTools.current.subscribe((message: any) => {
2754
if (message.payload && (message.payload.type === 'JUMP_TO_STATE' || message.payload.type === 'JUMP_TO_ACTION') && message.state) {
@@ -46,32 +73,6 @@ const useContextDevTools = (dispatch: Function) => {
4673
}
4774
}
4875

49-
const sendDispatch = (args: any) => {
50-
if(dispatch) {
51-
dispatch(args);
52-
}
53-
oldArgs = args;
54-
};
55-
56-
const sendUpdatedState = (updatedState: any) => {
57-
if ((window as any).__REDUX_DEVTOOLS_EXTENSION__){
58-
if (!firstRender) {
59-
devTools.current.init(updatedState);
60-
firstRender = true;
61-
} else {
62-
if (oldArgs.type !== 'IMPORT_STATE') {
63-
devTools.current.send(oldArgs, updatedState);
64-
}
65-
}
66-
}
67-
};
68-
69-
const disconnectDevTools = () => {
70-
if ((window as any).__REDUX_DEVTOOLS_EXTENSION__){
71-
return typeof devTools?.current?.disconnect === 'function' && devTools?.current?.disconnect();
72-
}
73-
}
74-
7576
return {
7677
sendDispatch,
7778
sendUpdatedState,

0 commit comments

Comments
 (0)