@servicetitan/react-ioc 40.2.1 → 40.3.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.
@@ -1,306 +0,0 @@
1
- import { render } from '@testing-library/react';
2
- import { Component, createContext, FC, PropsWithChildren, useRef } from 'react';
3
- import {
4
- Container,
5
- injectable,
6
- injectDependency,
7
- provide,
8
- Provider,
9
- symbolToken,
10
- useDependencies,
11
- } from '..';
12
- import { ContainerContext } from '../common';
13
-
14
- @injectable()
15
- class SampleStore {
16
- singletonVariable = 2;
17
- }
18
-
19
- interface IAnotherStore {
20
- value: string;
21
- }
22
-
23
- const ANOTHER_STORE_TOKEN = symbolToken<IAnotherStore>('ANOTHER_STORE_TOKEN');
24
-
25
- @injectable()
26
- class AnotherStore implements IAnotherStore {
27
- value = '3';
28
- }
29
-
30
- const PLAIN_OBJECT_TOKEN = symbolToken<{ success: boolean }>('PLAIN_OBJECT_TOKEN');
31
-
32
- abstract class SampleConfig {
33
- title!: string;
34
- }
35
-
36
- const SampleConsumer: FC<
37
- PropsWithChildren<{ setContainer(container: Container): void }>
38
- > = props => (
39
- <ContainerContext.Consumer>
40
- {container => {
41
- props.setContainer(container);
42
- return props.children;
43
- }}
44
- </ContainerContext.Consumer>
45
- );
46
-
47
- describe('[react-ioc]', () => {
48
- test('Context container should bind the provided store', () => {
49
- let contextContainer!: Container;
50
-
51
- const setContainer = (container: Container) => {
52
- contextContainer = container;
53
- };
54
- render(
55
- <Provider
56
- singletons={[SampleStore, { provide: ANOTHER_STORE_TOKEN, useClass: AnotherStore }]}
57
- >
58
- <SampleConsumer setContainer={setContainer} />
59
- </Provider>
60
- );
61
-
62
- expect(contextContainer.get(SampleStore)).toBeDefined();
63
- expect(contextContainer.get(SampleStore).singletonVariable).toBe(2);
64
- expect(contextContainer.get(ANOTHER_STORE_TOKEN)).toBeDefined();
65
- expect(contextContainer.get<IAnotherStore>(ANOTHER_STORE_TOKEN).value).toBe('3');
66
- });
67
-
68
- test('Provide decorator must inject stores to the container', () => {
69
- @provide({
70
- singletons: [SampleStore],
71
- })
72
- class SampleProvider extends Component {
73
- setContainer(container: Container) {
74
- expect(container.get(SampleStore).singletonVariable).toBe(2);
75
- }
76
-
77
- render() {
78
- return <SampleConsumer setContainer={this.setContainer} />;
79
- }
80
- }
81
-
82
- render(<SampleProvider />);
83
- });
84
-
85
- test('Unmounted component should unbind the depended store', () => {
86
- let contextContainer!: Container;
87
-
88
- const setContainer = (container: Container) => {
89
- contextContainer = container;
90
- };
91
-
92
- const renderer = render(
93
- <Provider
94
- singletons={[
95
- SampleStore,
96
- { provide: ANOTHER_STORE_TOKEN, useClass: AnotherStore },
97
- { provide: PLAIN_OBJECT_TOKEN, useValue: { success: true } },
98
- ]}
99
- >
100
- <SampleConsumer setContainer={setContainer} />
101
- </Provider>
102
- );
103
-
104
- renderer.unmount();
105
-
106
- expect(() => contextContainer.get(SampleStore)).toThrow();
107
- expect(() => contextContainer.get(ANOTHER_STORE_TOKEN)).toThrow();
108
- expect(() => contextContainer.get(PLAIN_OBJECT_TOKEN)).toThrow();
109
- });
110
-
111
- test('Injected store in transient scope should not retain change', () => {
112
- let contextContainer!: Container;
113
-
114
- const setContainer = (container: Container) => {
115
- contextContainer = container;
116
- };
117
-
118
- render(
119
- <Provider instances={[SampleStore]}>
120
- <SampleConsumer setContainer={setContainer} />
121
- </Provider>
122
- );
123
-
124
- expect(contextContainer.get(SampleStore)).toBeDefined();
125
- expect(contextContainer.get(SampleStore).singletonVariable).toBe(2);
126
-
127
- contextContainer.get(SampleStore).singletonVariable = 42;
128
- expect(contextContainer.get(SampleStore).singletonVariable).toBe(2);
129
- });
130
-
131
- test('Child with same singleton store should have different version of store', () => {
132
- let parentContainer!: Container;
133
- let childContainer!: Container;
134
-
135
- const setParentContainer = (container: Container) => {
136
- parentContainer = container;
137
- };
138
-
139
- const setChildContainer = (container: Container) => {
140
- childContainer = container;
141
- };
142
-
143
- render(
144
- <Provider singletons={[SampleStore]}>
145
- <SampleConsumer setContainer={setParentContainer}>
146
- <Provider singletons={[SampleStore]}>
147
- <SampleConsumer setContainer={setChildContainer} />
148
- </Provider>
149
- </SampleConsumer>
150
- </Provider>
151
- );
152
-
153
- const parentStore = parentContainer.get(SampleStore);
154
- const childStore = childContainer.get(SampleStore);
155
- expect(childStore === parentStore).toBe(false);
156
- });
157
-
158
- test('Child should be able to access stores in parent scope', () => {
159
- let parentContainer!: Container;
160
- let childContainer!: Container;
161
-
162
- const setParentContainer = (container: Container) => {
163
- parentContainer = container;
164
- parentContainer.get(SampleStore).singletonVariable = 4;
165
- };
166
-
167
- const setChildContainer = (container: Container) => {
168
- childContainer = container;
169
- };
170
-
171
- render(
172
- <Provider singletons={[SampleStore]}>
173
- <SampleConsumer setContainer={setParentContainer}>
174
- <Provider>
175
- <SampleConsumer setContainer={setChildContainer} />
176
- </Provider>
177
- </SampleConsumer>
178
- </Provider>
179
- );
180
-
181
- const childStore = childContainer.get(SampleStore);
182
- expect(childStore.singletonVariable).toBe(4);
183
- });
184
-
185
- test('injected store must be initialized properly', () => {
186
- class SampleComponent extends Component {
187
- @injectDependency(SampleStore)
188
- declare sampleStore: SampleStore;
189
-
190
- componentDidMount() {
191
- expect(this.sampleStore.singletonVariable).toBe(2);
192
- }
193
-
194
- render() {
195
- return <h1>Hakunamatata</h1>;
196
- }
197
- }
198
-
199
- render(
200
- <Provider singletons={[SampleStore]}>
201
- <SampleComponent />
202
- </Provider>
203
- );
204
- });
205
-
206
- test('store injection must throw an error if custom contextType is present', () => {
207
- expect(() => {
208
- class SampleComponent extends Component {
209
- static contextType = createContext({});
210
-
211
- @injectDependency(SampleStore)
212
- declare sampleStore: SampleStore;
213
-
214
- render() {
215
- return <h1>Hakunamatata</h1>;
216
- }
217
- }
218
-
219
- render(
220
- <Provider singletons={[SampleStore]}>
221
- <SampleComponent />
222
- </Provider>
223
- );
224
- }).toThrow();
225
- });
226
-
227
- test('store injection must throw an error if React context is not passed to constructor', () => {
228
- jest.spyOn(console, 'error').mockImplementation(jest.fn()); // suppress "context doesn't exist" error
229
-
230
- expect(() => {
231
- class SampleComponent extends Component {
232
- @injectDependency(SampleStore)
233
- declare sampleStore: SampleStore;
234
-
235
- constructor(props: {}) {
236
- super(props);
237
- // eslint-disable-next-line @typescript-eslint/no-unused-expressions
238
- this.sampleStore.singletonVariable;
239
- }
240
-
241
- render() {
242
- return <h1>Hakunamatata</h1>;
243
- }
244
- }
245
-
246
- render(
247
- <Provider singletons={[SampleStore]}>
248
- <SampleComponent />
249
- </Provider>
250
- );
251
- }).toThrow(
252
- "Context doesn't exist on a target class. Please make sure to pass context object to super method if you access injectable stores in constructor."
253
- );
254
- });
255
-
256
- test('store in singleton scope usage with hooks must be initialized properly', () => {
257
- const SampleComponent = () => {
258
- const [sampleStore, anotherStore, plainObject, sampleConfig] = useDependencies(
259
- SampleStore,
260
- ANOTHER_STORE_TOKEN,
261
- PLAIN_OBJECT_TOKEN,
262
- SampleConfig
263
- );
264
- expect(sampleStore.singletonVariable).toBe(2);
265
- expect(anotherStore.value).toBe('3');
266
- expect(plainObject.success).toBe(true);
267
- expect(sampleConfig.title).toBe('Title 1');
268
-
269
- const storeRef = useRef(sampleStore);
270
- expect(storeRef.current === sampleStore).toBe(true);
271
- return <h1>It works</h1>;
272
- };
273
- const Component = () => (
274
- <Provider
275
- singletons={[
276
- SampleStore,
277
- { provide: ANOTHER_STORE_TOKEN, useClass: AnotherStore },
278
- { provide: PLAIN_OBJECT_TOKEN, useValue: { success: true } },
279
- { provide: SampleConfig, useValue: { title: 'Title 1' } },
280
- ]}
281
- >
282
- <SampleComponent />
283
- </Provider>
284
- );
285
- const renderer = render(<Component />);
286
- renderer.rerender(<Component />);
287
- });
288
-
289
- test('store in transient scope usage with hooks must be initialized properly', () => {
290
- const SampleComponent = () => {
291
- const [sampleStore] = useDependencies(SampleStore);
292
- expect(sampleStore.singletonVariable).toBe(2);
293
-
294
- const storeRef = useRef(sampleStore);
295
- expect(storeRef.current === sampleStore).toBe(true);
296
- return <h1>It works</h1>;
297
- };
298
- const Component = () => (
299
- <Provider instances={[SampleStore]}>
300
- <SampleComponent />
301
- </Provider>
302
- );
303
- const renderer = render(<Component />);
304
- renderer.rerender(<Component />);
305
- });
306
- });