-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathbootstrap.test.jsx
More file actions
217 lines (211 loc) · 6.38 KB
/
bootstrap.test.jsx
File metadata and controls
217 lines (211 loc) · 6.38 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import ReactDOM from 'react-dom/client';
import createSagaMiddleware from 'redux-saga';
import bootstrap, * as internals from '../src/bootstrap';
import actionCreator from '../src/actionCreator';
import component from '../src/component';
import expression from '../src/expression';
import onError from '../src/onError';
import { registerInternals } from '../src/register';
import registry from '../src/registry';
import sagas from '../src/sagas';
import storeAPI from '../src/store';
jest.mock('react-dom/client', () => ({
createRoot: jest.fn().mockImplementation(() => ({
render: jest.fn(),
})),
}));
jest.mock('redux-saga', () => ({
__esModule: true, // this property makes it work
default: (() => {
const run = jest.fn();
const middleware = jest.fn(() => ({ reduxSagaMocked: true, run }));
middleware.run = run;
middleware.clearRun = () => run.mockClear();
return middleware;
})(),
effects: {
spawn: jest.fn(),
},
}));
jest.mock('../src/onError', () => ({
report: jest.fn(),
bootstrap: jest.fn(),
}));
jest.mock('../src/registry', () => ({
registerMany: jest.fn(),
getRegistry: jest.fn(),
}));
jest.mock('../src/actionCreator', () => ({
registerMany: jest.fn(),
}));
jest.mock('../src/component', () => ({
registerMany: jest.fn(),
}));
jest.mock('../src/expression', () => ({
registerMany: jest.fn(),
}));
jest.mock('../src/sagas', () => ({
registerMany: jest.fn(),
}));
jest.mock('../src/register', () => ({
registerInternals: jest.fn(),
}));
jest.mock('../src/store', () => ({
addPreReducer: jest.fn(),
setHttpMiddleware: jest.fn(),
initialize: jest.fn(() => ({ dispatch: jest.fn(), applyMiddleware: jest.fn() })),
}));
describe('bootstrap', () => {
beforeEach(() => {
onError.bootstrap.mockClear();
jest.clearAllMocks();
});
describe('error management', () => {
it('should bootstrap onError', async () => {
const options = {
onError: {
reportURL: '/api/v1/report',
sensibleKeys: [],
},
};
await bootstrap(options);
expect(onError.bootstrap).toHaveBeenCalled();
const call = onError.bootstrap.mock.calls[0];
expect(call[0]).toMatchObject(options);
});
});
describe('registry', () => {
it('should check options', async () => {
try {
await bootstrap({ appId: {} });
fail();
} catch (e) {
expect(e.message).toBe('appId must be a string but got object');
}
});
it('should call registerInternals', async () => {
await bootstrap({});
expect(registerInternals).toHaveBeenCalled();
});
it('should register options.registry using registry.registerMany', async () => {
registry.registerMany.mockClear();
const options = {
registry: {
foo: jest.fn(),
},
};
await bootstrap(options);
expect(registry.registerMany).toHaveBeenCalledWith(options.registry);
});
it('should register options.components using component.registerMany', async () => {
component.registerMany.mockClear();
const options = {
components: {
foo: jest.fn(),
},
};
await bootstrap(options);
expect(component.registerMany).toHaveBeenCalledWith(options.components);
});
it('should register options.expressions using expression.registerMany', async () => {
expression.registerMany.mockClear();
const options = {
expressions: {
foo: jest.fn(),
},
};
await bootstrap(options);
expect(expression.registerMany).toHaveBeenCalledWith(options.expressions);
});
it('should register options.actionCreators using actionCreator.registerMany', async () => {
actionCreator.registerMany.mockClear();
const options = {
actionCreators: {
foo: jest.fn(),
},
};
await bootstrap(options);
expect(actionCreator.registerMany).toHaveBeenCalledWith(options.actionCreators);
});
it('should register options.sagas using sagas.registerMany', async () => {
sagas.registerMany.mockClear();
const options = {
sagas: {
foo: jest.fn(),
},
};
await bootstrap(options);
expect(sagas.registerMany).toHaveBeenCalledWith(options.sagas);
});
});
describe('saga', () => {
it('should call createSagaMiddleware and run the middleware', async () => {
createSagaMiddleware.mockClear();
createSagaMiddleware.clearRun();
await bootstrap({});
expect(createSagaMiddleware).toHaveBeenCalled();
expect(createSagaMiddleware.run).toHaveBeenCalled();
});
});
describe('redux', () => {
it('should call storeAPI.addPreReducer if options.preReducer', async () => {
const options = {
preReducer: jest.fn(),
};
await bootstrap(options);
expect(storeAPI.addPreReducer).toHaveBeenCalledWith(options.preReducer);
});
it('should call storeAPI.setHttpMiddleware if options.preReducer', async () => {
const options = {
httpMiddleware: jest.fn(),
};
await bootstrap(options);
expect(storeAPI.setHttpMiddleware).toHaveBeenCalledWith(options.httpMiddleware);
});
it('should call storeAPI.initialize with all options', async () => {
storeAPI.initialize.mockClear();
createSagaMiddleware.mockClear();
const options = {
reducer: { app: jest.fn() },
preloadedState: {},
middlewares: [],
};
await bootstrap(options);
expect(storeAPI.initialize).toHaveBeenCalled();
expect(storeAPI.initialize.mock.calls[0][0]).toBe(options.reducer);
expect(storeAPI.initialize.mock.calls[0][1]).toBe(options.preloadedState);
// only the default enhancer
expect(storeAPI.initialize.mock.calls[0][2]).toBe(internals.bactchedSubscribe);
expect(Array.isArray(storeAPI.initialize.mock.calls[0][3])).toBe(true);
// the only middleware is redux-saga one (mocked)
expect(storeAPI.initialize.mock.calls[0][3][0].reduxSagaMocked).toBe(true);
});
it('should support options.settingsURL to fetch them', async () => {
const options = {
settingsURL: '/foo/settings.json',
storeCallback: store => {
expect(store.dispatch).toHaveBeenCalled();
expect(store.dispatch.mock.calls[0][0].url).toBe('/foo/settings.json');
},
};
await bootstrap(options);
});
it('should work without settings', async () => {
const options = {
storeCallback: store => {
expect(store.dispatch).not.toHaveBeenCalled();
},
};
await bootstrap(options);
});
it('should bootstrap in element', async () => {
const div = document.createElement('div');
const options = {
root: div,
};
expect(ReactDOM.createRoot).not.toHaveBeenCalled();
await bootstrap(options);
expect(ReactDOM.createRoot).toHaveBeenCalled();
});
});
});