@servicetitan/react-ioc 40.2.2 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@servicetitan/react-ioc",
3
- "version": "40.2.2",
3
+ "version": "40.3.0",
4
4
  "description": "Implementation of InversifyJS for React applications using Context API",
5
5
  "homepage": "https://docs.st.dev/docs/frontend/uikit/react-ioc",
6
6
  "repository": {
@@ -13,7 +13,11 @@
13
13
  "typings": "./dist/index.d.ts",
14
14
  "files": [
15
15
  "dist",
16
- "src"
16
+ "src",
17
+ "!**/__tests__",
18
+ "!**/__mocks__",
19
+ "!**/__snapshots__",
20
+ "!**/*.test.*"
17
21
  ],
18
22
  "dependencies": {
19
23
  "inversify": "~6.2.2",
@@ -1,26 +0,0 @@
1
- import '@testing-library/jest-dom';
2
- import { render, waitFor } from '@testing-library/react';
3
- import { Await } from '../await';
4
-
5
- describe('[react-ioc] Await', () => {
6
- jest.retryTimes(3, { logErrorsBeforeRetry: true });
7
-
8
- const testContent = 'hello';
9
-
10
- const TestContent = () => <div role="content">{testContent}</div>;
11
-
12
- const subject = () =>
13
- render(
14
- <Await promises={[]}>
15
- <TestContent />
16
- </Await>
17
- );
18
-
19
- test('renders content', async () => {
20
- const { container } = subject();
21
-
22
- await waitFor(() => {
23
- expect(container).toHaveTextContent(testContent);
24
- });
25
- });
26
- });
@@ -1,159 +0,0 @@
1
- import { Container, inject, injectable } from 'inversify';
2
- import { bindEntries, initializeStores } from '../container-config';
3
- import { Store } from '../store';
4
-
5
- @injectable()
6
- class Leaf extends Store {}
7
-
8
- @injectable()
9
- class Mid extends Store {
10
- constructor(@inject(Leaf) public leaf: Leaf) {
11
- super();
12
- }
13
- }
14
-
15
- @injectable()
16
- class Top extends Store {
17
- constructor(@inject(Mid) public mid: Mid) {
18
- super();
19
- }
20
- }
21
-
22
- @injectable()
23
- class BranchOne extends Store {
24
- constructor(@inject(Leaf) public leaf: Leaf) {
25
- super();
26
- }
27
- }
28
-
29
- @injectable()
30
- class BranchTwo extends Store {
31
- constructor(@inject(Leaf) public leaf: Leaf) {
32
- super();
33
- }
34
- }
35
-
36
- @injectable()
37
- class DiamondTop extends Store {
38
- constructor(
39
- @inject(BranchOne) public one: BranchOne,
40
- @inject(BranchTwo) public two: BranchTwo
41
- ) {
42
- super();
43
- }
44
- }
45
-
46
- describe('[react-ioc] container-config', () => {
47
- describe('bindEntries', () => {
48
- let container: Container;
49
-
50
- beforeEach(() => (container = new Container()));
51
-
52
- describe('with a three-level dependency chain', () => {
53
- beforeEach(() => bindEntries(container, { singletons: [Top, Mid, Leaf] }));
54
-
55
- const subject = () => container.get(Top);
56
-
57
- test('shares the Mid instance with the one injected into Top', () => {
58
- const top = subject();
59
-
60
- expect(container.get(Mid)).toBe(top.mid);
61
- });
62
-
63
- test('shares the Leaf instance with the one injected into Mid', () => {
64
- const top = subject();
65
-
66
- expect(container.get(Leaf)).toBe(top.mid.leaf);
67
- });
68
- });
69
-
70
- describe('with a diamond dependency graph', () => {
71
- beforeEach(() =>
72
- bindEntries(container, { singletons: [DiamondTop, BranchOne, BranchTwo, Leaf] })
73
- );
74
-
75
- const subject = () => container.get(DiamondTop);
76
-
77
- test('shares the Leaf instance across both branches', () => {
78
- const top = subject();
79
-
80
- expect(top.one.leaf).toBe(top.two.leaf);
81
- });
82
- });
83
-
84
- describe('with the dependent listed before its dependency', () => {
85
- beforeEach(() => bindEntries(container, { singletons: [Mid, Leaf] }));
86
-
87
- const subject = () => container.get(Mid);
88
-
89
- test('resolves the dependency regardless of listing order', () => {
90
- const mid = subject();
91
-
92
- expect(container.get(Leaf)).toBe(mid.leaf);
93
- });
94
- });
95
- });
96
-
97
- describe('initializeStores', () => {
98
- let onError: jest.Mock;
99
-
100
- beforeEach(() => (onError = jest.fn()));
101
-
102
- describe('with a store whose initialize throws synchronously', () => {
103
- class Thrower extends Store {
104
- initialize() {
105
- throw new Error('init failed');
106
- }
107
- }
108
-
109
- class Later extends Store {
110
- initialize = jest.fn();
111
- }
112
-
113
- let later: Later;
114
-
115
- beforeEach(() => (later = new Later()));
116
-
117
- const subject = () => initializeStores([new Thrower(), later], onError);
118
-
119
- test('reports the error to onError', () => {
120
- subject();
121
-
122
- expect(onError).toHaveBeenCalledWith(
123
- expect.objectContaining({ message: 'init failed' })
124
- );
125
- });
126
-
127
- test('still initializes the later store', () => {
128
- subject();
129
-
130
- expect(later.initialize).toHaveBeenCalled();
131
- });
132
- });
133
-
134
- describe('with a store whose initialize rejects', () => {
135
- class Rejecter extends Store {
136
- initialize() {
137
- return Promise.reject(new Error('async init failed'));
138
- }
139
- }
140
-
141
- const subject = () => initializeStores([new Rejecter()], onError);
142
-
143
- test('settles the returned promise instead of rejecting', async () => {
144
- const [promise] = subject();
145
-
146
- await expect(promise).resolves.toBeUndefined();
147
- });
148
-
149
- test('reports the error to onError', async () => {
150
- const [promise] = subject();
151
- await promise;
152
-
153
- expect(onError).toHaveBeenCalledWith(
154
- expect.objectContaining({ message: 'async init failed' })
155
- );
156
- });
157
- });
158
- });
159
- });
@@ -1,143 +0,0 @@
1
- import { symbolToken } from '../common';
2
- import { dedupeEntries } from '../dedupe-entries';
3
-
4
- class StoreA {}
5
- class StoreB {}
6
- class StoreC {}
7
-
8
- describe('[react-ioc] dedupeEntries', () => {
9
- let singletons: any;
10
- let instances: any;
11
-
12
- beforeEach(() => {
13
- singletons = undefined;
14
- instances = undefined;
15
- });
16
-
17
- const subject = () => dedupeEntries({ singletons, instances });
18
-
19
- describe('with empty input', () => {
20
- test('reports no duplicates', () => {
21
- expect(subject().duplicates).toEqual([]);
22
- });
23
-
24
- test('returns empty singletons', () => {
25
- expect(subject().singletons).toEqual([]);
26
- });
27
-
28
- test('returns empty instances', () => {
29
- expect(subject().instances).toEqual([]);
30
- });
31
- });
32
-
33
- describe('with all distinct tokens', () => {
34
- beforeEach(() => {
35
- singletons = [StoreA, { provide: StoreB }];
36
- instances = [StoreC];
37
- });
38
-
39
- test('reports no duplicates', () => {
40
- expect(subject().duplicates).toEqual([]);
41
- });
42
-
43
- test('returns singletons unchanged', () => {
44
- expect(subject().singletons).toEqual(singletons);
45
- });
46
-
47
- test('returns instances unchanged', () => {
48
- expect(subject().instances).toEqual(instances);
49
- });
50
- });
51
-
52
- function itReturnsDuplicatedToken() {
53
- test('returns the duplicate token', () => {
54
- const token = singletons[0].provide ?? singletons[0];
55
- expect(subject().duplicates).toEqual([token]);
56
- });
57
- }
58
-
59
- function itReturnsDedupedSingletons() {
60
- test('returns deduped singletons', () => {
61
- expect(subject().singletons).toEqual(singletons?.slice(0, 1));
62
- });
63
- }
64
-
65
- describe('with duplicate class', () => {
66
- beforeEach(() => (singletons = [StoreA, StoreA]));
67
-
68
- itReturnsDuplicatedToken();
69
- itReturnsDedupedSingletons();
70
- });
71
-
72
- describe('with duplicate class and provide entry', () => {
73
- beforeEach(() => {
74
- singletons = [StoreA, { provide: StoreA }];
75
- });
76
-
77
- itReturnsDuplicatedToken();
78
- itReturnsDedupedSingletons();
79
- });
80
-
81
- describe('with duplicate provide entries', () => {
82
- beforeEach(() => {
83
- singletons = [
84
- { provide: StoreA, useClass: StoreB },
85
- { provide: StoreA, useClass: StoreC },
86
- ];
87
- });
88
-
89
- itReturnsDuplicatedToken();
90
- itReturnsDedupedSingletons();
91
- });
92
-
93
- describe('with duplicate useValue entries', () => {
94
- const valueToken = symbolToken<{ kind: string }>('test/value');
95
-
96
- beforeEach(() => {
97
- singletons = [
98
- { provide: valueToken, useValue: { kind: 'first' } },
99
- { provide: valueToken, useValue: { kind: 'second' } },
100
- ];
101
- });
102
-
103
- itReturnsDuplicatedToken();
104
- itReturnsDedupedSingletons();
105
- });
106
-
107
- describe('with the same token in singletons and instances', () => {
108
- beforeEach(() => {
109
- singletons = [StoreA];
110
- instances = [StoreA];
111
- });
112
-
113
- itReturnsDuplicatedToken();
114
-
115
- test('keeps singletons entry', () => {
116
- expect(subject().singletons).toEqual([singletons[0]]);
117
- });
118
-
119
- test('drops instances the entry', () => {
120
- expect(subject().instances).toEqual([]);
121
- });
122
- });
123
-
124
- describe('with multiple duplicate tokens', () => {
125
- beforeEach(() => (singletons = [StoreA, StoreA, { provide: StoreB }, StoreB]));
126
-
127
- test('returns duplicated tokens', () => {
128
- expect(subject().duplicates).toEqual([singletons[1], singletons[3]]);
129
- });
130
-
131
- test('returns deduped singletons', () => {
132
- expect(subject().singletons).toEqual([singletons[0], singletons[2]]);
133
- });
134
- });
135
-
136
- describe('when a token appears >2 times', () => {
137
- beforeEach(() => (singletons = [StoreA, StoreA, StoreA]));
138
-
139
- test('returns duplicated token once', () => {
140
- expect(subject().duplicates).toEqual([singletons[0]]);
141
- });
142
- });
143
- });
@@ -1,247 +0,0 @@
1
- import { symbolToken } from '../common';
2
- import {
3
- formatToken,
4
- getProvideToken,
5
- getProvideTokenWithClass,
6
- isClassProviderConfigEntryToken,
7
- isClassProviderConfigEntryUseClass,
8
- isProviderConfigEntryInheritable,
9
- isValueProviderConfigEntry,
10
- } from '../entry-helpers';
11
-
12
- class StoreA {}
13
- class StoreB {}
14
-
15
- function itReturns(subject: () => boolean, value: boolean) {
16
- test(`returns ${value}`, () => {
17
- expect(subject()).toBe(value);
18
- });
19
- }
20
-
21
- describe('[react-ioc] formatToken', () => {
22
- let token: any;
23
-
24
- const subject = () => formatToken(token);
25
-
26
- describe('with a named class', () => {
27
- beforeEach(() => (token = StoreA));
28
-
29
- test('returns class name', () => {
30
- expect(subject()).toBe(token.name);
31
- });
32
- });
33
-
34
- describe('with unnamed class', () => {
35
- beforeEach(() => (token = (() => class {})()));
36
-
37
- test('returns (anonymous)', () => {
38
- expect(subject()).toBe('(anonymous)');
39
- });
40
- });
41
-
42
- describe('with a symbol token', () => {
43
- beforeEach(() => (token = symbolToken('test/symbol-token')));
44
-
45
- test('returns symbol description', () => {
46
- expect(subject()).toEqual(token.description);
47
- });
48
-
49
- describe('when symbol has no description', () => {
50
- beforeEach(() => (token = Symbol()));
51
-
52
- test('returns token.toString()', () => {
53
- expect(subject()).toEqual(token.toString());
54
- });
55
- });
56
- });
57
- });
58
-
59
- describe('[react-ioc] getProvideToken', () => {
60
- let entry: any;
61
-
62
- const subject = () => getProvideToken(entry);
63
-
64
- describe('with a bare class entry', () => {
65
- beforeEach(() => (entry = StoreA));
66
-
67
- test('returns the class', () => {
68
- expect(subject()).toBe(entry);
69
- });
70
- });
71
-
72
- describe('with a provide entry', () => {
73
- beforeEach(() => (entry = { provide: StoreA }));
74
-
75
- function itReturnsEntryProvide() {
76
- test('returns entry.provide', () => {
77
- expect(subject()).toBe(entry.provide);
78
- });
79
- }
80
-
81
- itReturnsEntryProvide();
82
-
83
- describe('with useClass', () => {
84
- beforeEach(() => (entry.useClass = StoreB));
85
-
86
- itReturnsEntryProvide();
87
- });
88
-
89
- describe('with useValue', () => {
90
- beforeEach(() => (entry.useValue = { kind: 'first' }));
91
-
92
- itReturnsEntryProvide();
93
- });
94
-
95
- describe('with inherit: true', () => {
96
- beforeEach(() => (entry.inherit = true));
97
-
98
- itReturnsEntryProvide();
99
- });
100
- });
101
- });
102
-
103
- describe('[react-ioc] getProvideTokenWithClass', () => {
104
- let entry: any;
105
-
106
- const subject = () => getProvideTokenWithClass(entry);
107
-
108
- describe('with a bare class entry', () => {
109
- beforeEach(() => (entry = StoreA));
110
-
111
- test('returns [class, class]', () => {
112
- expect(subject()).toEqual([entry, entry]);
113
- });
114
- });
115
-
116
- describe('with a provide entry', () => {
117
- beforeEach(() => (entry = { provide: StoreA }));
118
-
119
- test('returns [entry.provide, entry.provide]', () => {
120
- expect(subject()).toEqual([entry.provide, entry.provide]);
121
- });
122
-
123
- describe('with useClass', () => {
124
- beforeEach(() => (entry.useClass = StoreB));
125
-
126
- test('returns [entry.provide, entry.useClass]', () => {
127
- expect(subject()).toEqual([entry.provide, entry.useClass]);
128
- });
129
- });
130
-
131
- describe('with useValue', () => {
132
- beforeEach(() => (entry.useValue = { kind: 'first' }));
133
-
134
- test('returns [undefined, undefined]', () => {
135
- expect(subject()).toEqual([undefined, undefined]);
136
- });
137
- });
138
- });
139
- });
140
-
141
- describe('[react-ioc] isClassProviderConfigEntryToken', () => {
142
- let entry: any;
143
-
144
- const subject = () => isClassProviderConfigEntryToken(entry);
145
-
146
- describe('with a bare class entry', () => {
147
- beforeEach(() => (entry = StoreA));
148
-
149
- itReturns(subject, false);
150
- });
151
-
152
- describe('with a provide entry', () => {
153
- beforeEach(() => (entry = { provide: StoreA }));
154
-
155
- itReturns(subject, true);
156
-
157
- describe('with useClass', () => {
158
- beforeEach(() => (entry.useClass = StoreB));
159
-
160
- itReturns(subject, false);
161
- });
162
-
163
- describe('with useValue', () => {
164
- beforeEach(() => (entry.useValue = { kind: 'first' }));
165
-
166
- itReturns(subject, false);
167
- });
168
- });
169
- });
170
-
171
- describe('[react-ioc] isClassProviderConfigEntryUseClass', () => {
172
- let entry: any;
173
-
174
- const subject = () => isClassProviderConfigEntryUseClass(entry);
175
-
176
- describe('with a bare class entry', () => {
177
- beforeEach(() => (entry = StoreA));
178
-
179
- itReturns(subject, false);
180
- });
181
-
182
- describe('with a provide-only entry', () => {
183
- beforeEach(() => (entry = { provide: StoreA }));
184
-
185
- itReturns(subject, false);
186
-
187
- describe('with a useClass entry', () => {
188
- beforeEach(() => (entry.useClass = StoreB));
189
-
190
- itReturns(subject, true);
191
- });
192
- });
193
- });
194
-
195
- describe('[react-ioc] isProviderConfigEntryInheritable', () => {
196
- let entry: any;
197
-
198
- const subject = () => isProviderConfigEntryInheritable(entry);
199
-
200
- describe('with a bare class entry', () => {
201
- beforeEach(() => (entry = StoreA));
202
-
203
- itReturns(subject, false);
204
- });
205
-
206
- describe('with a provide entry', () => {
207
- beforeEach(() => (entry = { provide: StoreA }));
208
-
209
- itReturns(subject, true);
210
-
211
- describe('with useClass', () => {
212
- beforeEach(() => (entry.useClass = StoreB));
213
-
214
- itReturns(subject, true);
215
- });
216
-
217
- describe('with useValue', () => {
218
- beforeEach(() => (entry.useValue = { kind: 'first' }));
219
-
220
- itReturns(subject, true);
221
- });
222
- });
223
- });
224
-
225
- describe('[react-ioc] isValueProviderConfigEntry', () => {
226
- let entry: any;
227
-
228
- const subject = () => isValueProviderConfigEntry(entry);
229
-
230
- describe('with a bare class entry', () => {
231
- beforeEach(() => (entry = StoreA));
232
-
233
- itReturns(subject, false);
234
- });
235
-
236
- describe('with a provide entry', () => {
237
- beforeEach(() => (entry = { provide: StoreA }));
238
-
239
- itReturns(subject, false);
240
-
241
- describe('with a useValue entry', () => {
242
- beforeEach(() => (entry.useValue = { kind: 'first' }));
243
-
244
- itReturns(subject, true);
245
- });
246
- });
247
- });
@@ -1,108 +0,0 @@
1
- import '@testing-library/jest-dom';
2
- import { render, screen } from '@testing-library/react';
3
- import { FC, Fragment, PropsWithChildren } from 'react';
4
- import { provide } from '../provide';
5
-
6
- jest.mock('../provider', () => ({
7
- Provider: ({ children, ...props }: PropsWithChildren<any>) => {
8
- return (
9
- <Fragment>
10
- {`<Provider ${JSON.stringify(props)} />`}
11
- {children}
12
- </Fragment>
13
- );
14
- },
15
- }));
16
-
17
- describe('[react-ioc] provide', () => {
18
- let stores: Parameters<typeof provide>[0];
19
- let options: Parameters<typeof provide>[1];
20
- let props: Record<string, any> | undefined;
21
-
22
- const subject = () => {
23
- const Component = provide(
24
- stores,
25
- options
26
- )(renderProps => <div>{`<Component ${JSON.stringify(renderProps)} />`}</div>);
27
-
28
- return render(<Component {...props} />);
29
- };
30
-
31
- beforeEach(() => {
32
- stores = { singletons: [], instances: [] };
33
- options = undefined;
34
- props = { foo: 'bar' };
35
- });
36
-
37
- test('renders component with props', () => {
38
- subject();
39
-
40
- expect(screen.getByText(`<Component ${JSON.stringify(props)} />`)).toBeInTheDocument();
41
- });
42
-
43
- test('wraps component within <Provider>', () => {
44
- subject();
45
-
46
- expect(screen.getByText(`<Provider ${JSON.stringify(stores)} />`)).toBeInTheDocument();
47
- });
48
-
49
- describe('with fallback options', () => {
50
- const LoadingFallback: FC = () => null;
51
- const ErrorFallback: FC = () => null;
52
-
53
- beforeEach(() => {
54
- options = { loadingFallback: <LoadingFallback />, errorFallback: <ErrorFallback /> };
55
- });
56
-
57
- test('passes fallback options to <Provider>', () => {
58
- subject();
59
-
60
- const props = { ...stores, ...(options as any) };
61
- expect(screen.getByText(`<Provider ${JSON.stringify(props)} />`)).toBeInTheDocument();
62
- });
63
-
64
- describe('with empty fallback options', () => {
65
- beforeEach(() => (options = {}));
66
-
67
- test('does not pass options to <Provider>', () => {
68
- subject();
69
-
70
- expect(
71
- screen.getByText(`<Provider ${JSON.stringify({ ...stores })} />`)
72
- ).toBeInTheDocument();
73
- });
74
- });
75
- });
76
-
77
- describe('with legacy fallback element', () => {
78
- const LoadingFallback: FC = () => null;
79
-
80
- beforeEach(() => (options = <LoadingFallback />));
81
-
82
- test('passes {loadingFallback} option', () => {
83
- subject();
84
-
85
- expect(
86
- screen.getByText(
87
- `<Provider ${JSON.stringify({
88
- ...stores,
89
- loadingFallback: <LoadingFallback />,
90
- })} />`
91
- )
92
- ).toBeInTheDocument();
93
- });
94
-
95
- describe('when fallback element is "null"', () => {
96
- beforeEach(() => (options = null));
97
-
98
- test('passes null {loadingFallback} option', () => {
99
- subject();
100
-
101
- const props = { ...stores, loadingFallback: null };
102
- expect(
103
- screen.getByText(`<Provider ${JSON.stringify(props)} />`)
104
- ).toBeInTheDocument();
105
- });
106
- });
107
- });
108
- });