@navios/di-react 1.0.0-alpha.3 → 2.0.0-alpha.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.
Files changed (58) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +45 -665
  3. package/lib/{index.js → index.cjs} +249 -255
  4. package/lib/index.cjs.map +1 -0
  5. package/lib/{index.d.ts → index.d.cts} +94 -123
  6. package/lib/index.d.cts.map +1 -0
  7. package/lib/index.d.mts +93 -122
  8. package/lib/index.d.mts.map +1 -1
  9. package/lib/index.mjs +248 -255
  10. package/lib/index.mjs.map +1 -1
  11. package/package.json +32 -18
  12. package/coverage/base.css +0 -224
  13. package/coverage/block-navigation.js +0 -87
  14. package/coverage/clover.xml +0 -270
  15. package/coverage/coverage-final.json +0 -10
  16. package/coverage/favicon.png +0 -0
  17. package/coverage/hooks/index.html +0 -191
  18. package/coverage/hooks/use-container.mts.html +0 -268
  19. package/coverage/hooks/use-invalidate.mts.html +0 -208
  20. package/coverage/hooks/use-optional-service.mts.html +0 -910
  21. package/coverage/hooks/use-scope.mts.html +0 -346
  22. package/coverage/hooks/use-service.mts.html +0 -760
  23. package/coverage/hooks/use-suspense-service.mts.html +0 -784
  24. package/coverage/index.html +0 -131
  25. package/coverage/prettify.css +0 -1
  26. package/coverage/prettify.js +0 -2
  27. package/coverage/providers/container-provider.mts.html +0 -139
  28. package/coverage/providers/context.mts.html +0 -130
  29. package/coverage/providers/index.html +0 -146
  30. package/coverage/providers/scope-provider.mts.html +0 -355
  31. package/coverage/sort-arrow-sprite.png +0 -0
  32. package/coverage/sorter.js +0 -210
  33. package/lib/index.d.ts.map +0 -1
  34. package/lib/index.js.map +0 -1
  35. package/project.json +0 -61
  36. package/src/hooks/__tests__/use-container.spec.mts +0 -143
  37. package/src/hooks/__tests__/use-invalidate.spec.mts +0 -80
  38. package/src/hooks/__tests__/use-optional-service.spec.mts +0 -233
  39. package/src/hooks/__tests__/use-scope.spec.mts +0 -293
  40. package/src/hooks/__tests__/use-service.spec.mts +0 -212
  41. package/src/hooks/__tests__/use-suspense-service.spec.mts +0 -286
  42. package/src/hooks/index.mts +0 -14
  43. package/src/hooks/use-container.mts +0 -61
  44. package/src/hooks/use-invalidate.mts +0 -41
  45. package/src/hooks/use-optional-service.mts +0 -275
  46. package/src/hooks/use-scope.mts +0 -87
  47. package/src/hooks/use-service.mts +0 -225
  48. package/src/hooks/use-suspense-service.mts +0 -233
  49. package/src/index.mts +0 -8
  50. package/src/providers/__tests__/scope-provider.spec.mts +0 -366
  51. package/src/providers/container-provider.mts +0 -18
  52. package/src/providers/context.mts +0 -15
  53. package/src/providers/index.mts +0 -5
  54. package/src/providers/scope-provider.mts +0 -90
  55. package/src/types.mts +0 -21
  56. package/tsconfig.json +0 -13
  57. package/tsdown.config.mts +0 -14
  58. package/vitest.config.mts +0 -12
@@ -1,293 +0,0 @@
1
- import { Container, globalRegistry, Registry } from '@navios/di'
2
-
3
- import { renderHook } from '@testing-library/react'
4
- import { createElement } from 'react'
5
- import { afterEach, beforeEach, describe, expect, it } from 'vitest'
6
-
7
- import { ContainerProvider } from '../../providers/container-provider.mjs'
8
- import { ScopeProvider } from '../../providers/scope-provider.mjs'
9
- import {
10
- useScope,
11
- useScopedContainer,
12
- useScopedContainerOrThrow,
13
- useScopeMetadata,
14
- useScopeOrThrow,
15
- } from '../use-scope.mjs'
16
-
17
- describe('useScope', () => {
18
- let container: Container
19
- let registry: Registry
20
-
21
- beforeEach(() => {
22
- registry = new Registry(globalRegistry)
23
- container = new Container(registry)
24
- })
25
-
26
- afterEach(async () => {
27
- await container.dispose()
28
- })
29
-
30
- it('should return null when not inside a ScopeProvider', () => {
31
- const wrapper = ({ children }: { children: React.ReactNode }) =>
32
- // @ts-expect-error - container is required
33
- createElement(ContainerProvider, { container }, children)
34
-
35
- const { result } = renderHook(() => useScope(), { wrapper })
36
-
37
- expect(result.current).toBeNull()
38
- })
39
-
40
- it('should return the scope ID when inside a ScopeProvider', () => {
41
- const wrapper = ({ children }: { children: React.ReactNode }) =>
42
- createElement(
43
- ContainerProvider,
44
- // @ts-expect-error - props are not typed
45
- { container },
46
- // @ts-expect-error - props are not typed
47
- createElement(ScopeProvider, { scopeId: 'test-scope-123' }, children),
48
- )
49
-
50
- const { result } = renderHook(() => useScope(), { wrapper })
51
-
52
- expect(result.current).toBe('test-scope-123')
53
- })
54
-
55
- it('should return the innermost scope ID when nested', () => {
56
- const wrapper = ({ children }: { children: React.ReactNode }) =>
57
- createElement(
58
- ContainerProvider,
59
- // @ts-expect-error - props are not typed
60
- { container },
61
- createElement(
62
- ScopeProvider,
63
- // @ts-expect-error - props are not typed
64
- { scopeId: 'outer-scope' },
65
- // @ts-expect-error - props are not typed
66
- createElement(ScopeProvider, { scopeId: 'inner-scope' }, children),
67
- ),
68
- )
69
-
70
- const { result } = renderHook(() => useScope(), { wrapper })
71
-
72
- expect(result.current).toBe('inner-scope')
73
- })
74
- })
75
-
76
- describe('useScopeOrThrow', () => {
77
- let container: Container
78
- let registry: Registry
79
-
80
- beforeEach(() => {
81
- registry = new Registry(globalRegistry)
82
- container = new Container(registry)
83
- })
84
-
85
- afterEach(async () => {
86
- await container.dispose()
87
- })
88
-
89
- it('should throw when not inside a ScopeProvider', () => {
90
- const wrapper = ({ children }: { children: React.ReactNode }) =>
91
- // @ts-expect-error - container is required
92
- createElement(ContainerProvider, { container }, children)
93
-
94
- expect(() => {
95
- renderHook(() => useScopeOrThrow(), { wrapper })
96
- }).toThrow('useScopeOrThrow must be used within a ScopeProvider')
97
- })
98
-
99
- it('should return the scope ID when inside a ScopeProvider', () => {
100
- const wrapper = ({ children }: { children: React.ReactNode }) =>
101
- createElement(
102
- ContainerProvider,
103
- // @ts-expect-error - props are not typed
104
- { container },
105
- // @ts-expect-error - props are not typed
106
- createElement(ScopeProvider, { scopeId: 'test-scope' }, children),
107
- )
108
-
109
- const { result } = renderHook(() => useScopeOrThrow(), { wrapper })
110
-
111
- expect(result.current).toBe('test-scope')
112
- })
113
- })
114
-
115
- describe('useScopedContainer', () => {
116
- let container: Container
117
- let registry: Registry
118
-
119
- beforeEach(() => {
120
- registry = new Registry(globalRegistry)
121
- container = new Container(registry)
122
- })
123
-
124
- afterEach(async () => {
125
- await container.dispose()
126
- })
127
-
128
- it('should return null when not inside a ScopeProvider', () => {
129
- const wrapper = ({ children }: { children: React.ReactNode }) =>
130
- // @ts-expect-error - container is required
131
- createElement(ContainerProvider, { container }, children)
132
-
133
- const { result } = renderHook(() => useScopedContainer(), { wrapper })
134
-
135
- expect(result.current).toBeNull()
136
- })
137
-
138
- it('should return the ScopedContainer when inside a ScopeProvider', () => {
139
- const wrapper = ({ children }: { children: React.ReactNode }) =>
140
- createElement(
141
- ContainerProvider,
142
- // @ts-expect-error - props are not typed
143
- { container },
144
- // @ts-expect-error - props are not typed
145
- createElement(ScopeProvider, { scopeId: 'test-scope' }, children),
146
- )
147
-
148
- const { result } = renderHook(() => useScopedContainer(), { wrapper })
149
-
150
- expect(result.current).not.toBeNull()
151
- expect(result.current?.getRequestId()).toBe('test-scope')
152
- })
153
- })
154
-
155
- describe('useScopedContainerOrThrow', () => {
156
- let container: Container
157
- let registry: Registry
158
-
159
- beforeEach(() => {
160
- registry = new Registry(globalRegistry)
161
- container = new Container(registry)
162
- })
163
-
164
- afterEach(async () => {
165
- await container.dispose()
166
- })
167
-
168
- it('should throw when not inside a ScopeProvider', () => {
169
- const wrapper = ({ children }: { children: React.ReactNode }) =>
170
- // @ts-expect-error - container is required
171
- createElement(ContainerProvider, { container }, children)
172
-
173
- expect(() => {
174
- renderHook(() => useScopedContainerOrThrow(), { wrapper })
175
- }).toThrow('useScopedContainerOrThrow must be used within a ScopeProvider')
176
- })
177
-
178
- it('should return the ScopedContainer when inside a ScopeProvider', () => {
179
- const wrapper = ({ children }: { children: React.ReactNode }) =>
180
- createElement(
181
- ContainerProvider,
182
- // @ts-expect-error - props are not typed
183
- { container },
184
- // @ts-expect-error - props are not typed
185
- createElement(ScopeProvider, { scopeId: 'test-scope' }, children),
186
- )
187
-
188
- const { result } = renderHook(() => useScopedContainerOrThrow(), { wrapper })
189
-
190
- expect(result.current.getRequestId()).toBe('test-scope')
191
- })
192
- })
193
-
194
- describe('useScopeMetadata', () => {
195
- let container: Container
196
- let registry: Registry
197
-
198
- beforeEach(() => {
199
- registry = new Registry(globalRegistry)
200
- container = new Container(registry)
201
- })
202
-
203
- afterEach(async () => {
204
- await container.dispose()
205
- })
206
-
207
- it('should return undefined when not inside a ScopeProvider', () => {
208
- const wrapper = ({ children }: { children: React.ReactNode }) =>
209
- // @ts-expect-error - container is required
210
- createElement(ContainerProvider, { container }, children)
211
-
212
- const { result } = renderHook(() => useScopeMetadata('someKey'), { wrapper })
213
-
214
- expect(result.current).toBeUndefined()
215
- })
216
-
217
- it('should return undefined when key does not exist', () => {
218
- const wrapper = ({ children }: { children: React.ReactNode }) =>
219
- createElement(
220
- ContainerProvider,
221
- // @ts-expect-error - props are not typed
222
- { container },
223
- createElement(
224
- ScopeProvider,
225
- // @ts-expect-error - props are not typed
226
- { scopeId: 'test-scope', metadata: { existingKey: 'value' } },
227
- children,
228
- ),
229
- )
230
-
231
- const { result } = renderHook(() => useScopeMetadata('nonExistentKey'), {
232
- wrapper,
233
- })
234
-
235
- expect(result.current).toBeUndefined()
236
- })
237
-
238
- it('should return the metadata value when key exists', () => {
239
- const wrapper = ({ children }: { children: React.ReactNode }) =>
240
- createElement(
241
- ContainerProvider,
242
- // @ts-expect-error - props are not typed
243
- { container },
244
- createElement(
245
- ScopeProvider,
246
- // @ts-expect-error - props are not typed
247
- {
248
- scopeId: 'test-scope',
249
- metadata: { userId: '123', theme: 'dark' },
250
- },
251
- children,
252
- ),
253
- )
254
-
255
- // Test both values in a single hook to ensure they're from the same scope
256
- const { result } = renderHook(
257
- () => ({
258
- userId: useScopeMetadata<string>('userId'),
259
- theme: useScopeMetadata<string>('theme'),
260
- }),
261
- { wrapper },
262
- )
263
-
264
- expect(result.current.userId).toBe('123')
265
- expect(result.current.theme).toBe('dark')
266
- })
267
-
268
- it('should return metadata from the innermost scope', () => {
269
- const wrapper = ({ children }: { children: React.ReactNode }) =>
270
- createElement(
271
- ContainerProvider,
272
- // @ts-expect-error - props are not typed
273
- { container },
274
- createElement(
275
- ScopeProvider,
276
- // @ts-expect-error - props are not typed
277
- { scopeId: 'outer-scope', metadata: { value: 'outer' } },
278
- createElement(
279
- ScopeProvider,
280
- // @ts-expect-error - props are not typed
281
- { scopeId: 'inner-scope', metadata: { value: 'inner' } },
282
- children,
283
- ),
284
- ),
285
- )
286
-
287
- const { result } = renderHook(() => useScopeMetadata<string>('value'), {
288
- wrapper,
289
- })
290
-
291
- expect(result.current).toBe('inner')
292
- })
293
- })
@@ -1,212 +0,0 @@
1
- import { Container, Injectable, InjectionToken, Registry } from '@navios/di'
2
-
3
- import { act, renderHook, waitFor } from '@testing-library/react'
4
- import { createElement, useMemo } from 'react'
5
- import { afterEach, beforeEach, describe, expect, it } from 'vitest'
6
- import { z } from 'zod/v4'
7
-
8
- import { ContainerProvider } from '../../providers/container-provider.mjs'
9
- import { useService } from '../use-service.mjs'
10
-
11
- describe('useService', () => {
12
- let container: Container
13
- let registry: Registry
14
-
15
- beforeEach(() => {
16
- registry = new Registry()
17
- container = new Container(registry)
18
- })
19
-
20
- afterEach(async () => {
21
- await container.dispose()
22
- })
23
-
24
- const createWrapper = () => {
25
- return ({ children }: { children: React.ReactNode }) =>
26
- // @ts-expect-error - container is required
27
- createElement(ContainerProvider, { container }, children)
28
- }
29
-
30
- describe('with class tokens', () => {
31
- it('should load a service and return success state', async () => {
32
- @Injectable({ registry })
33
- class TestService {
34
- getValue() {
35
- return 'test-value'
36
- }
37
- }
38
-
39
- const { result } = renderHook(() => useService(TestService), {
40
- wrapper: createWrapper(),
41
- })
42
-
43
- // Initially loading
44
- expect(result.current.isLoading).toBe(true)
45
- expect(result.current.data).toBeUndefined()
46
-
47
- // Wait for success
48
- await waitFor(() => {
49
- expect(result.current.isSuccess).toBe(true)
50
- })
51
-
52
- expect(result.current.data).toBeInstanceOf(TestService)
53
- expect(result.current.data?.getValue()).toBe('test-value')
54
- expect(result.current.isLoading).toBe(false)
55
- expect(result.current.isError).toBe(false)
56
- })
57
-
58
- it('should return error state when service fails to load', async () => {
59
- @Injectable({ registry })
60
- class FailingService {
61
- constructor() {
62
- throw new Error('Service initialization failed')
63
- }
64
- }
65
-
66
- const { result } = renderHook(() => useService(FailingService), {
67
- wrapper: createWrapper(),
68
- })
69
-
70
- await waitFor(() => {
71
- expect(result.current.isError).toBe(true)
72
- })
73
-
74
- expect(result.current.error?.message).toContain(
75
- 'Service initialization failed',
76
- )
77
- expect(result.current.data).toBeUndefined()
78
- expect(result.current.isLoading).toBe(false)
79
- expect(result.current.isSuccess).toBe(false)
80
- })
81
-
82
- it('should refetch service when refetch is called', async () => {
83
- let callCount = 0
84
-
85
- @Injectable({ registry })
86
- class CountingService {
87
- public count: number
88
-
89
- constructor() {
90
- callCount++
91
- this.count = callCount
92
- }
93
- }
94
-
95
- const { result } = renderHook(() => useService(CountingService), {
96
- wrapper: createWrapper(),
97
- })
98
-
99
- await waitFor(() => {
100
- expect(result.current.isSuccess).toBe(true)
101
- })
102
-
103
- expect(result.current.data?.count).toBe(1)
104
-
105
- // Invalidate the service first so refetch creates a new instance
106
- await container.invalidate(result.current.data)
107
-
108
- // Trigger refetch
109
- await act(async () => {
110
- result.current.refetch()
111
- })
112
-
113
- await waitFor(() => {
114
- expect(result.current.data?.count).toBe(2)
115
- })
116
- })
117
- })
118
-
119
- describe('with injection tokens', () => {
120
- it('should load a service with injection token', async () => {
121
- const TestToken = InjectionToken.create<{ value: string }>('TestToken')
122
-
123
- @Injectable({ registry, token: TestToken })
124
- class TestService {
125
- value = 'token-value'
126
- }
127
-
128
- const { result } = renderHook(() => useService(TestToken), {
129
- wrapper: createWrapper(),
130
- })
131
-
132
- await waitFor(() => {
133
- expect(result.current.isSuccess).toBe(true)
134
- })
135
-
136
- expect(result.current.data?.value).toBe('token-value')
137
- })
138
-
139
- it('should load a service with injection token and args', async () => {
140
- const UserSchema = z.object({ userId: z.string() })
141
- const UserToken = InjectionToken.create<
142
- { userId: string; name: string },
143
- typeof UserSchema
144
- >('User', UserSchema)
145
-
146
- @Injectable({ registry, token: UserToken })
147
- class UserService {
148
- public userId: string
149
- public name: string
150
-
151
- constructor(args: z.infer<typeof UserSchema>) {
152
- this.userId = args.userId
153
- this.name = `User-${args.userId}`
154
- }
155
- }
156
-
157
- const { result } = renderHook(
158
- () => {
159
- const args = useMemo(() => ({ userId: '123' }), [])
160
- return useService(UserToken, args)
161
- },
162
- { wrapper: createWrapper() },
163
- )
164
-
165
- await waitFor(() => {
166
- expect(result.current.isSuccess).toBe(true)
167
- })
168
-
169
- expect(result.current.data?.userId).toBe('123')
170
- expect(result.current.data?.name).toBe('User-123')
171
- })
172
- })
173
-
174
- describe('service invalidation', () => {
175
- it('should re-fetch when service is invalidated', async () => {
176
- let instanceCount = 0
177
-
178
- @Injectable({ registry })
179
- class InvalidatableService {
180
- public instanceId: number
181
-
182
- constructor() {
183
- instanceCount++
184
- this.instanceId = instanceCount
185
- }
186
- }
187
-
188
- const { result } = renderHook(() => useService(InvalidatableService), {
189
- wrapper: createWrapper(),
190
- })
191
-
192
- await waitFor(() => {
193
- expect(result.current.isSuccess).toBe(true)
194
- })
195
-
196
- expect(result.current.data?.instanceId).toBe(1)
197
-
198
- // Invalidate the service
199
- await act(async () => {
200
- await container.invalidate(result.current.data)
201
- })
202
-
203
- // Wait for re-fetch with increased timeout for event propagation
204
- await waitFor(
205
- () => {
206
- expect(result.current.data?.instanceId).toBe(2)
207
- },
208
- { timeout: 2000 },
209
- )
210
- })
211
- })
212
- })