@servicetitan/react-ioc 40.2.2 → 40.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -2
- package/src/__tests__/await.test.tsx +0 -26
- package/src/__tests__/container-config.test.ts +0 -159
- package/src/__tests__/dedupe-entries.test.ts +0 -143
- package/src/__tests__/entry-helpers.test.ts +0 -247
- package/src/__tests__/provide.test.tsx +0 -108
- package/src/__tests__/provider.test.tsx +0 -462
- package/src/__tests__/react-ioc.test.tsx +0 -306
- package/src/__tests__/use-local-stores.test.tsx +0 -849
|
@@ -1,462 +0,0 @@
|
|
|
1
|
-
import '@testing-library/jest-dom';
|
|
2
|
-
import { act, render, screen, waitFor } from '@testing-library/react';
|
|
3
|
-
import { FC, PropsWithChildren, ReactNode, useEffect, useState } from 'react';
|
|
4
|
-
import {
|
|
5
|
-
inject,
|
|
6
|
-
injectable,
|
|
7
|
-
Provider,
|
|
8
|
-
ProviderProps,
|
|
9
|
-
Store,
|
|
10
|
-
symbolToken,
|
|
11
|
-
useDependencies,
|
|
12
|
-
} from '..';
|
|
13
|
-
|
|
14
|
-
@injectable()
|
|
15
|
-
class StatStore {
|
|
16
|
-
readonly id = 'stat';
|
|
17
|
-
counters: Record<string, number> = {};
|
|
18
|
-
queueCreate: string[] = [];
|
|
19
|
-
queueRender: string[] = [];
|
|
20
|
-
|
|
21
|
-
testInitialize: (id: string) => void = jest.fn();
|
|
22
|
-
testDispose: (id: string) => void = jest.fn();
|
|
23
|
-
testRender = (id: string) => {
|
|
24
|
-
this.queueRender.push(id);
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
register(name: string) {
|
|
28
|
-
if (!this.counters[name]) {
|
|
29
|
-
this.counters[name] = 0;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
this.counters[name]++;
|
|
33
|
-
|
|
34
|
-
const id = `${name}-${this.counters[name]}`;
|
|
35
|
-
this.queueCreate.push(id);
|
|
36
|
-
|
|
37
|
-
return id;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
@injectable()
|
|
42
|
-
class StoreSimple {
|
|
43
|
-
readonly id;
|
|
44
|
-
|
|
45
|
-
constructor(@inject(StatStore) stat: StatStore) {
|
|
46
|
-
this.id = stat.register('StoreSimple');
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
@injectable()
|
|
51
|
-
class StoreWithInitialize extends Store {
|
|
52
|
-
readonly id;
|
|
53
|
-
|
|
54
|
-
constructor(@inject(StatStore) private stat: StatStore) {
|
|
55
|
-
super();
|
|
56
|
-
this.id = stat.register('StoreWithInitialize');
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
initialize() {
|
|
60
|
-
this.stat.testInitialize(this.id);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
dispose() {
|
|
64
|
-
this.stat.testDispose(this.id);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
type TestStore = typeof StoreSimple | typeof StoreWithInitialize | ReturnType<typeof symbolToken>;
|
|
69
|
-
|
|
70
|
-
const getTestComponent = (inst: TestStore) => {
|
|
71
|
-
return ({ children }: PropsWithChildren) => {
|
|
72
|
-
const [{ id }, { testRender }] = useDependencies<[any, StatStore]>(inst, StatStore);
|
|
73
|
-
useEffect(() => {
|
|
74
|
-
testRender(id);
|
|
75
|
-
}, [testRender, id]);
|
|
76
|
-
return <div>{children}</div>;
|
|
77
|
-
};
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
function TestProvider(props: PropsWithChildren<ProviderProps & { store: TestStore }>) {
|
|
81
|
-
const { singletons, instances, children, store = StoreSimple } = props;
|
|
82
|
-
const Component = getTestComponent(store);
|
|
83
|
-
return (
|
|
84
|
-
<Provider singletons={singletons} instances={instances}>
|
|
85
|
-
<Component>{children}</Component>
|
|
86
|
-
</Provider>
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
describe('[react-ioc] Provider', () => {
|
|
91
|
-
let stat: StatStore;
|
|
92
|
-
|
|
93
|
-
beforeEach(() => (stat = new StatStore()));
|
|
94
|
-
|
|
95
|
-
const subject = (children: ReactNode) =>
|
|
96
|
-
render(
|
|
97
|
-
<Provider singletons={[{ provide: StatStore, useValue: stat }]}>{children}</Provider>
|
|
98
|
-
);
|
|
99
|
-
|
|
100
|
-
const expectQueueRenderToBe = (queueRender: string[]) =>
|
|
101
|
-
waitFor(() => {
|
|
102
|
-
expect(stat.queueRender).toEqual(queueRender);
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
describe('with singletons', () => {
|
|
106
|
-
test('reuses singletons for each container.get', async () => {
|
|
107
|
-
subject(
|
|
108
|
-
<TestProvider store={StoreSimple} singletons={[StoreSimple]}>
|
|
109
|
-
<TestProvider store={StoreSimple} />
|
|
110
|
-
</TestProvider>
|
|
111
|
-
);
|
|
112
|
-
|
|
113
|
-
expect(stat.queueCreate).toEqual(['StoreSimple-1']);
|
|
114
|
-
|
|
115
|
-
await expectQueueRenderToBe(['StoreSimple-1', 'StoreSimple-1']);
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
test('creates separate singletons for nested tokens', async () => {
|
|
119
|
-
subject(
|
|
120
|
-
<TestProvider store={StoreSimple} singletons={[StoreSimple]}>
|
|
121
|
-
<TestProvider store={StoreSimple} singletons={[StoreSimple]} />
|
|
122
|
-
</TestProvider>
|
|
123
|
-
);
|
|
124
|
-
|
|
125
|
-
expect(stat.queueCreate).toEqual(['StoreSimple-1', 'StoreSimple-2']);
|
|
126
|
-
|
|
127
|
-
await expectQueueRenderToBe(['StoreSimple-1', 'StoreSimple-2']);
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
test('reuses inheritable singleton', async () => {
|
|
131
|
-
subject(
|
|
132
|
-
<TestProvider store={StoreSimple} singletons={[StoreSimple]}>
|
|
133
|
-
<TestProvider
|
|
134
|
-
store={StoreSimple}
|
|
135
|
-
singletons={[{ provide: StoreSimple, inherit: true }]}
|
|
136
|
-
/>
|
|
137
|
-
</TestProvider>
|
|
138
|
-
);
|
|
139
|
-
|
|
140
|
-
expect(stat.queueCreate).toEqual(['StoreSimple-1']);
|
|
141
|
-
|
|
142
|
-
await expectQueueRenderToBe(['StoreSimple-1', 'StoreSimple-1']);
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
test('creates singletons when inheritable singleton does not exist', () => {
|
|
146
|
-
subject(
|
|
147
|
-
<TestProvider
|
|
148
|
-
store={StoreSimple}
|
|
149
|
-
singletons={[{ provide: StoreSimple, inherit: true }]}
|
|
150
|
-
>
|
|
151
|
-
<TestProvider store={StoreSimple} singletons={[StoreSimple]} />
|
|
152
|
-
</TestProvider>
|
|
153
|
-
);
|
|
154
|
-
|
|
155
|
-
expect(stat.queueCreate).toEqual(['StoreSimple-1', 'StoreSimple-2']);
|
|
156
|
-
});
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
describe('with instances', () => {
|
|
160
|
-
test('creates separate instances for each container.get', async () => {
|
|
161
|
-
subject(
|
|
162
|
-
<TestProvider store={StoreSimple} instances={[StoreSimple]}>
|
|
163
|
-
<TestProvider store={StoreSimple} />
|
|
164
|
-
</TestProvider>
|
|
165
|
-
);
|
|
166
|
-
|
|
167
|
-
expect(stat.queueCreate).toEqual(['StoreSimple-1', 'StoreSimple-2']);
|
|
168
|
-
|
|
169
|
-
await expectQueueRenderToBe(['StoreSimple-1', 'StoreSimple-2']);
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
test('creates separate instances for nested tokens', async () => {
|
|
173
|
-
subject(
|
|
174
|
-
<TestProvider store={StoreSimple} instances={[StoreSimple]}>
|
|
175
|
-
<TestProvider store={StoreSimple} instances={[StoreSimple]} />
|
|
176
|
-
</TestProvider>
|
|
177
|
-
);
|
|
178
|
-
|
|
179
|
-
expect(stat.queueCreate).toEqual(['StoreSimple-1', 'StoreSimple-2']);
|
|
180
|
-
|
|
181
|
-
await expectQueueRenderToBe(['StoreSimple-1', 'StoreSimple-2']);
|
|
182
|
-
});
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
describe('useValue', () => {
|
|
186
|
-
const valueToken = symbolToken('valueToken');
|
|
187
|
-
|
|
188
|
-
test('creates new values', async () => {
|
|
189
|
-
subject(
|
|
190
|
-
<TestProvider
|
|
191
|
-
store={valueToken}
|
|
192
|
-
singletons={[{ provide: valueToken, useValue: { id: 'value1' } }]}
|
|
193
|
-
>
|
|
194
|
-
<TestProvider
|
|
195
|
-
store={valueToken}
|
|
196
|
-
singletons={[{ provide: valueToken, useValue: { id: 'value2' } }]}
|
|
197
|
-
/>
|
|
198
|
-
</TestProvider>
|
|
199
|
-
);
|
|
200
|
-
|
|
201
|
-
await expectQueueRenderToBe(['value1', 'value2']);
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
test('reuses nested values', async () => {
|
|
205
|
-
subject(
|
|
206
|
-
<TestProvider
|
|
207
|
-
store={valueToken}
|
|
208
|
-
singletons={[{ provide: valueToken, useValue: { id: 'value1' } }]}
|
|
209
|
-
>
|
|
210
|
-
<TestProvider
|
|
211
|
-
store={valueToken}
|
|
212
|
-
singletons={[
|
|
213
|
-
{ provide: valueToken, useValue: { id: 'value2' }, inherit: true },
|
|
214
|
-
]}
|
|
215
|
-
/>
|
|
216
|
-
</TestProvider>
|
|
217
|
-
);
|
|
218
|
-
|
|
219
|
-
await expectQueueRenderToBe(['value1', 'value1']);
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
test('does not reuse nested values for instances', async () => {
|
|
223
|
-
subject(
|
|
224
|
-
<TestProvider
|
|
225
|
-
store={valueToken}
|
|
226
|
-
singletons={[{ provide: valueToken, useValue: { id: 'value1' } }]}
|
|
227
|
-
>
|
|
228
|
-
<TestProvider
|
|
229
|
-
store={valueToken}
|
|
230
|
-
instances={[
|
|
231
|
-
{
|
|
232
|
-
provide: valueToken,
|
|
233
|
-
useValue: { id: 'value2' },
|
|
234
|
-
inherit: true,
|
|
235
|
-
} as any,
|
|
236
|
-
]}
|
|
237
|
-
/>
|
|
238
|
-
</TestProvider>
|
|
239
|
-
);
|
|
240
|
-
|
|
241
|
-
await expectQueueRenderToBe(['value1', 'value2']);
|
|
242
|
-
});
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
describe('check store initialization', () => {
|
|
246
|
-
test('waits for store initialization', async () => {
|
|
247
|
-
subject(
|
|
248
|
-
<TestProvider store={StoreWithInitialize} singletons={[StoreWithInitialize]} />
|
|
249
|
-
);
|
|
250
|
-
|
|
251
|
-
expect(stat.testInitialize).toHaveBeenCalledWith('StoreWithInitialize-1');
|
|
252
|
-
|
|
253
|
-
await expectQueueRenderToBe(['StoreWithInitialize-1']);
|
|
254
|
-
});
|
|
255
|
-
|
|
256
|
-
test('waits for nested initialization', async () => {
|
|
257
|
-
subject(
|
|
258
|
-
<TestProvider store={StoreWithInitialize} singletons={[StoreWithInitialize]}>
|
|
259
|
-
<TestProvider store={StoreWithInitialize} singletons={[StoreWithInitialize]} />
|
|
260
|
-
</TestProvider>
|
|
261
|
-
);
|
|
262
|
-
|
|
263
|
-
await waitFor(() => {
|
|
264
|
-
expect(stat.testInitialize).toHaveBeenCalledWith('StoreWithInitialize-1');
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
await waitFor(() => {
|
|
268
|
-
expect(stat.testInitialize).toHaveBeenCalledWith('StoreWithInitialize-2');
|
|
269
|
-
});
|
|
270
|
-
|
|
271
|
-
expect(stat.queueCreate).toEqual(['StoreWithInitialize-1', 'StoreWithInitialize-2']);
|
|
272
|
-
|
|
273
|
-
await expectQueueRenderToBe(['StoreWithInitialize-1', 'StoreWithInitialize-2']);
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
test('initializes only once when store is reused', async () => {
|
|
277
|
-
subject(
|
|
278
|
-
<TestProvider store={StoreWithInitialize} singletons={[StoreWithInitialize]}>
|
|
279
|
-
<TestProvider
|
|
280
|
-
store={StoreWithInitialize}
|
|
281
|
-
singletons={[{ provide: StoreWithInitialize, inherit: true }]}
|
|
282
|
-
/>
|
|
283
|
-
</TestProvider>
|
|
284
|
-
);
|
|
285
|
-
|
|
286
|
-
await waitFor(() => {
|
|
287
|
-
expect(stat.testInitialize).toHaveBeenCalledWith('StoreWithInitialize-1');
|
|
288
|
-
});
|
|
289
|
-
|
|
290
|
-
expect(stat.queueCreate).toEqual(['StoreWithInitialize-1']);
|
|
291
|
-
await expectQueueRenderToBe(['StoreWithInitialize-1', 'StoreWithInitialize-1']);
|
|
292
|
-
expect(stat.testInitialize).toHaveBeenCalledTimes(1);
|
|
293
|
-
});
|
|
294
|
-
|
|
295
|
-
describe('error initialization', () => {
|
|
296
|
-
beforeAll(() => jest.useFakeTimers());
|
|
297
|
-
afterAll(() => jest.useRealTimers());
|
|
298
|
-
|
|
299
|
-
@injectable()
|
|
300
|
-
class StoreWithErrorInitialize extends Store {
|
|
301
|
-
initialize() {
|
|
302
|
-
throw new Error('error');
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
@injectable()
|
|
306
|
-
class StoreWithAsyncErrorInitialize extends Store {
|
|
307
|
-
async initialize() {
|
|
308
|
-
await new Promise<void>(resolve => resolve());
|
|
309
|
-
throw new Error('async error');
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
test('async error initialization rethrows and shows error', async () => {
|
|
314
|
-
render(<Provider singletons={[StoreWithAsyncErrorInitialize]} />);
|
|
315
|
-
|
|
316
|
-
await act(async () => {
|
|
317
|
-
await expect(() => jest.runAllTimersAsync()).rejects.toThrow('async error');
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
await waitFor(() => {
|
|
321
|
-
expect(screen.getByText('Error')).toBeInTheDocument();
|
|
322
|
-
});
|
|
323
|
-
});
|
|
324
|
-
|
|
325
|
-
test('sync error initialization rethrows and shows error', async () => {
|
|
326
|
-
render(<Provider singletons={[StoreWithErrorInitialize]} />);
|
|
327
|
-
|
|
328
|
-
await expect(() => jest.runAllTimersAsync()).rejects.toThrow('error');
|
|
329
|
-
|
|
330
|
-
expect(screen.getByText('Error')).toBeInTheDocument();
|
|
331
|
-
});
|
|
332
|
-
});
|
|
333
|
-
});
|
|
334
|
-
|
|
335
|
-
describe('check store dispose', () => {
|
|
336
|
-
test('runs store dispose', () => {
|
|
337
|
-
const { unmount } = subject(
|
|
338
|
-
<TestProvider store={StoreWithInitialize} singletons={[StoreWithInitialize]} />
|
|
339
|
-
);
|
|
340
|
-
|
|
341
|
-
unmount();
|
|
342
|
-
|
|
343
|
-
expect(stat.testDispose).toHaveBeenCalledWith('StoreWithInitialize-1');
|
|
344
|
-
});
|
|
345
|
-
|
|
346
|
-
test('runs dispose only once when store is reused', async () => {
|
|
347
|
-
const { unmount } = subject(
|
|
348
|
-
<TestProvider store={StoreWithInitialize} singletons={[StoreWithInitialize]}>
|
|
349
|
-
<TestProvider
|
|
350
|
-
store={StoreWithInitialize}
|
|
351
|
-
singletons={[{ provide: StoreWithInitialize, inherit: true }]}
|
|
352
|
-
/>
|
|
353
|
-
</TestProvider>
|
|
354
|
-
);
|
|
355
|
-
|
|
356
|
-
expect(stat.queueCreate).toEqual(['StoreWithInitialize-1']);
|
|
357
|
-
await expectQueueRenderToBe(['StoreWithInitialize-1', 'StoreWithInitialize-1']);
|
|
358
|
-
|
|
359
|
-
unmount();
|
|
360
|
-
|
|
361
|
-
expect(stat.testDispose).toHaveBeenCalledTimes(1);
|
|
362
|
-
});
|
|
363
|
-
|
|
364
|
-
test('does not dispose inheritable singleton from parent container when child container is unmounted', () => {
|
|
365
|
-
const UnmountOnMount: FC<PropsWithChildren> = ({ children }) => {
|
|
366
|
-
const [isMounted, setIsMounted] = useState(false);
|
|
367
|
-
|
|
368
|
-
useEffect(() => {
|
|
369
|
-
setIsMounted(true);
|
|
370
|
-
}, []);
|
|
371
|
-
|
|
372
|
-
return isMounted ? null : children;
|
|
373
|
-
};
|
|
374
|
-
|
|
375
|
-
subject(
|
|
376
|
-
<TestProvider store={StoreWithInitialize} singletons={[StoreWithInitialize]}>
|
|
377
|
-
<UnmountOnMount>
|
|
378
|
-
<TestProvider
|
|
379
|
-
store={StoreWithInitialize}
|
|
380
|
-
singletons={[{ provide: StoreWithInitialize, inherit: true }]}
|
|
381
|
-
/>
|
|
382
|
-
</UnmountOnMount>
|
|
383
|
-
</TestProvider>
|
|
384
|
-
);
|
|
385
|
-
|
|
386
|
-
expect(stat.testDispose).not.toHaveBeenCalled();
|
|
387
|
-
});
|
|
388
|
-
});
|
|
389
|
-
|
|
390
|
-
describe('duplicates handling', () => {
|
|
391
|
-
const originalNodeEnv = process.env.NODE_ENV;
|
|
392
|
-
const childContent = 'foo';
|
|
393
|
-
const errorFallback = 'Oops!';
|
|
394
|
-
let singletons: ProviderProps['singletons'];
|
|
395
|
-
let instances: ProviderProps['instances'];
|
|
396
|
-
let warnSpy: jest.SpyInstance;
|
|
397
|
-
|
|
398
|
-
beforeAll(() => jest.useFakeTimers());
|
|
399
|
-
afterAll(() => jest.useRealTimers());
|
|
400
|
-
|
|
401
|
-
beforeEach(() => {
|
|
402
|
-
jest.clearAllMocks();
|
|
403
|
-
warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
404
|
-
singletons = undefined;
|
|
405
|
-
instances = undefined;
|
|
406
|
-
});
|
|
407
|
-
|
|
408
|
-
afterEach(() => {
|
|
409
|
-
process.env.NODE_ENV = originalNodeEnv;
|
|
410
|
-
});
|
|
411
|
-
|
|
412
|
-
const subject = () =>
|
|
413
|
-
render(
|
|
414
|
-
<Provider
|
|
415
|
-
singletons={singletons}
|
|
416
|
-
instances={instances}
|
|
417
|
-
errorFallback={<div>{errorFallback}</div>}
|
|
418
|
-
>
|
|
419
|
-
<div>{childContent}</div>
|
|
420
|
-
</Provider>
|
|
421
|
-
);
|
|
422
|
-
|
|
423
|
-
describe('with a duplicate singleton', () => {
|
|
424
|
-
beforeEach(() => {
|
|
425
|
-
singletons = [StoreSimple, StoreSimple];
|
|
426
|
-
});
|
|
427
|
-
|
|
428
|
-
test('renders error fallback', async () => {
|
|
429
|
-
subject();
|
|
430
|
-
|
|
431
|
-
await jest.runAllTimersAsync().catch(() => {});
|
|
432
|
-
expect(screen.getByText(errorFallback)).toBeInTheDocument();
|
|
433
|
-
});
|
|
434
|
-
|
|
435
|
-
test('throws error', async () => {
|
|
436
|
-
subject();
|
|
437
|
-
|
|
438
|
-
await expect(() => jest.runAllTimersAsync()).rejects.toThrow(
|
|
439
|
-
/listed more than once/
|
|
440
|
-
);
|
|
441
|
-
});
|
|
442
|
-
|
|
443
|
-
describe('when in production', () => {
|
|
444
|
-
beforeEach(() => (process.env.NODE_ENV = 'production'));
|
|
445
|
-
|
|
446
|
-
test('logs warning', () => {
|
|
447
|
-
subject();
|
|
448
|
-
|
|
449
|
-
expect(warnSpy).toHaveBeenCalledWith(
|
|
450
|
-
expect.stringMatching(/listed more than once/)
|
|
451
|
-
);
|
|
452
|
-
});
|
|
453
|
-
|
|
454
|
-
test('renders children', () => {
|
|
455
|
-
subject();
|
|
456
|
-
|
|
457
|
-
expect(screen.getByText(childContent)).toBeInTheDocument();
|
|
458
|
-
});
|
|
459
|
-
});
|
|
460
|
-
});
|
|
461
|
-
});
|
|
462
|
-
});
|