@navios/di-react 1.0.0-alpha.3 → 2.0.0-alpha.1
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/CHANGELOG.md +47 -1
- package/README.md +45 -665
- package/lib/index.cjs +573 -0
- package/lib/index.cjs.map +1 -0
- package/lib/index.d.cts +262 -0
- package/lib/index.d.cts.map +1 -0
- package/lib/index.d.mts +159 -140
- package/lib/index.d.mts.map +1 -1
- package/lib/index.mjs +371 -381
- package/lib/index.mjs.map +1 -1
- package/package.json +32 -18
- package/coverage/base.css +0 -224
- package/coverage/block-navigation.js +0 -87
- package/coverage/clover.xml +0 -270
- package/coverage/coverage-final.json +0 -10
- package/coverage/favicon.png +0 -0
- package/coverage/hooks/index.html +0 -191
- package/coverage/hooks/use-container.mts.html +0 -268
- package/coverage/hooks/use-invalidate.mts.html +0 -208
- package/coverage/hooks/use-optional-service.mts.html +0 -910
- package/coverage/hooks/use-scope.mts.html +0 -346
- package/coverage/hooks/use-service.mts.html +0 -760
- package/coverage/hooks/use-suspense-service.mts.html +0 -784
- package/coverage/index.html +0 -131
- package/coverage/prettify.css +0 -1
- package/coverage/prettify.js +0 -2
- package/coverage/providers/container-provider.mts.html +0 -139
- package/coverage/providers/context.mts.html +0 -130
- package/coverage/providers/index.html +0 -146
- package/coverage/providers/scope-provider.mts.html +0 -355
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +0 -210
- package/lib/index.d.ts +0 -243
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js +0 -582
- package/lib/index.js.map +0 -1
- package/project.json +0 -61
- package/src/hooks/__tests__/use-container.spec.mts +0 -143
- package/src/hooks/__tests__/use-invalidate.spec.mts +0 -80
- package/src/hooks/__tests__/use-optional-service.spec.mts +0 -233
- package/src/hooks/__tests__/use-scope.spec.mts +0 -293
- package/src/hooks/__tests__/use-service.spec.mts +0 -212
- package/src/hooks/__tests__/use-suspense-service.spec.mts +0 -286
- package/src/hooks/index.mts +0 -14
- package/src/hooks/use-container.mts +0 -61
- package/src/hooks/use-invalidate.mts +0 -41
- package/src/hooks/use-optional-service.mts +0 -275
- package/src/hooks/use-scope.mts +0 -87
- package/src/hooks/use-service.mts +0 -225
- package/src/hooks/use-suspense-service.mts +0 -233
- package/src/index.mts +0 -8
- package/src/providers/__tests__/scope-provider.spec.mts +0 -366
- package/src/providers/container-provider.mts +0 -18
- package/src/providers/context.mts +0 -15
- package/src/providers/index.mts +0 -5
- package/src/providers/scope-provider.mts +0 -90
- package/src/types.mts +0 -21
- package/tsconfig.json +0 -13
- package/tsdown.config.mts +0 -14
- package/vitest.config.mts +0 -12
package/project.json
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@navios/di-react",
|
|
3
|
-
"$schema": "../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
-
"sourceRoot": "packages/di-react/src",
|
|
5
|
-
"prefix": "di-react",
|
|
6
|
-
"tags": [],
|
|
7
|
-
"projectType": "library",
|
|
8
|
-
"targets": {
|
|
9
|
-
"check": {
|
|
10
|
-
"executor": "nx:run-commands",
|
|
11
|
-
"outputs": ["{projectRoot}/dist"],
|
|
12
|
-
"inputs": ["^projectSources", "projectSources"],
|
|
13
|
-
"options": {
|
|
14
|
-
"command": ["tsc -b"],
|
|
15
|
-
"cwd": "packages/di-react"
|
|
16
|
-
}
|
|
17
|
-
},
|
|
18
|
-
"lint": {
|
|
19
|
-
"executor": "nx:run-commands",
|
|
20
|
-
"inputs": ["^projectSources", "project"],
|
|
21
|
-
"options": {
|
|
22
|
-
"command": "oxlint --fix",
|
|
23
|
-
"cwd": "packages/di-react"
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
|
-
"test:ci": {
|
|
27
|
-
"executor": "nx:run-commands",
|
|
28
|
-
"inputs": ["^projectSources", "project"],
|
|
29
|
-
"options": {
|
|
30
|
-
"command": "vitest run",
|
|
31
|
-
"cwd": "packages/di-react"
|
|
32
|
-
}
|
|
33
|
-
},
|
|
34
|
-
"build": {
|
|
35
|
-
"executor": "nx:run-commands",
|
|
36
|
-
"inputs": ["projectSources", "{projectRoot}/tsdown.config.mts"],
|
|
37
|
-
"outputs": ["{projectRoot}/lib"],
|
|
38
|
-
"dependsOn": ["check", "test:ci", "lint"],
|
|
39
|
-
"options": {
|
|
40
|
-
"command": "tsdown",
|
|
41
|
-
"cwd": "packages/di-react"
|
|
42
|
-
}
|
|
43
|
-
},
|
|
44
|
-
"publish": {
|
|
45
|
-
"executor": "nx:run-commands",
|
|
46
|
-
"dependsOn": ["build"],
|
|
47
|
-
"options": {
|
|
48
|
-
"command": "yarn npm publish --access public",
|
|
49
|
-
"cwd": "packages/di-react"
|
|
50
|
-
}
|
|
51
|
-
},
|
|
52
|
-
"publish:next": {
|
|
53
|
-
"executor": "nx:run-commands",
|
|
54
|
-
"dependsOn": ["build"],
|
|
55
|
-
"options": {
|
|
56
|
-
"command": "yarn npm publish --access public --tag next",
|
|
57
|
-
"cwd": "packages/di-react"
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import { Container, globalRegistry, Injectable, 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 { useContainer, useRootContainer } from '../use-container.mjs'
|
|
10
|
-
|
|
11
|
-
describe('useContainer', () => {
|
|
12
|
-
let container: Container
|
|
13
|
-
let registry: Registry
|
|
14
|
-
|
|
15
|
-
beforeEach(() => {
|
|
16
|
-
registry = new Registry(globalRegistry)
|
|
17
|
-
container = new Container(registry)
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
afterEach(async () => {
|
|
21
|
-
await container.dispose()
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
it('should return the container from context', () => {
|
|
25
|
-
const wrapper = ({ children }: { children: React.ReactNode }) =>
|
|
26
|
-
// @ts-expect-error - container is required
|
|
27
|
-
createElement(ContainerProvider, { container }, children)
|
|
28
|
-
|
|
29
|
-
const { result } = renderHook(() => useContainer(), { wrapper })
|
|
30
|
-
|
|
31
|
-
expect(result.current).toBe(container)
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
it('should throw an error when used outside of ContainerProvider', () => {
|
|
35
|
-
expect(() => {
|
|
36
|
-
renderHook(() => useContainer())
|
|
37
|
-
}).toThrow('useContainer must be used within a ContainerProvider')
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
it('should provide access to container methods', async () => {
|
|
41
|
-
@Injectable({ registry })
|
|
42
|
-
class TestService {
|
|
43
|
-
getValue() {
|
|
44
|
-
return 'test-value'
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const wrapper = ({ children }: { children: React.ReactNode }) =>
|
|
49
|
-
// @ts-expect-error - props are not typed
|
|
50
|
-
createElement(ContainerProvider, { container }, children)
|
|
51
|
-
|
|
52
|
-
const { result } = renderHook(() => useContainer(), { wrapper })
|
|
53
|
-
|
|
54
|
-
// @ts-expect-error - token is valid
|
|
55
|
-
const service = await result.current.get(TestService)
|
|
56
|
-
expect(service.getValue()).toBe('test-value')
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
it('should return ScopedContainer when inside ScopeProvider', () => {
|
|
60
|
-
const wrapper = ({ children }: { children: React.ReactNode }) =>
|
|
61
|
-
createElement(
|
|
62
|
-
ContainerProvider,
|
|
63
|
-
// @ts-expect-error - props are not typed
|
|
64
|
-
{ container },
|
|
65
|
-
// @ts-expect-error - props are not typed
|
|
66
|
-
createElement(ScopeProvider, { scopeId: 'test-scope' }, children),
|
|
67
|
-
)
|
|
68
|
-
|
|
69
|
-
const { result } = renderHook(() => useContainer(), { wrapper })
|
|
70
|
-
|
|
71
|
-
// Should return the scoped container, not the root container
|
|
72
|
-
expect(result.current).not.toBe(container)
|
|
73
|
-
// @ts-expect-error - getRequestId exists on ScopedContainer
|
|
74
|
-
expect(result.current.getRequestId?.()).toBe('test-scope')
|
|
75
|
-
})
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
describe('useRootContainer', () => {
|
|
79
|
-
let container: Container
|
|
80
|
-
let registry: Registry
|
|
81
|
-
|
|
82
|
-
beforeEach(() => {
|
|
83
|
-
registry = new Registry(globalRegistry)
|
|
84
|
-
container = new Container(registry)
|
|
85
|
-
})
|
|
86
|
-
|
|
87
|
-
afterEach(async () => {
|
|
88
|
-
await container.dispose()
|
|
89
|
-
})
|
|
90
|
-
|
|
91
|
-
it('should return the root container from context', () => {
|
|
92
|
-
const wrapper = ({ children }: { children: React.ReactNode }) =>
|
|
93
|
-
// @ts-expect-error - container is required
|
|
94
|
-
createElement(ContainerProvider, { container }, children)
|
|
95
|
-
|
|
96
|
-
const { result } = renderHook(() => useRootContainer(), { wrapper })
|
|
97
|
-
|
|
98
|
-
expect(result.current).toBe(container)
|
|
99
|
-
})
|
|
100
|
-
|
|
101
|
-
it('should throw an error when used outside of ContainerProvider', () => {
|
|
102
|
-
expect(() => {
|
|
103
|
-
renderHook(() => useRootContainer())
|
|
104
|
-
}).toThrow('useRootContainer must be used within a ContainerProvider')
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
it('should return root container even when inside ScopeProvider', () => {
|
|
108
|
-
const wrapper = ({ children }: { children: React.ReactNode }) =>
|
|
109
|
-
createElement(
|
|
110
|
-
ContainerProvider,
|
|
111
|
-
// @ts-expect-error - props are not typed
|
|
112
|
-
{ container },
|
|
113
|
-
// @ts-expect-error - props are not typed
|
|
114
|
-
createElement(ScopeProvider, { scopeId: 'test-scope' }, children),
|
|
115
|
-
)
|
|
116
|
-
|
|
117
|
-
const { result } = renderHook(() => useRootContainer(), { wrapper })
|
|
118
|
-
|
|
119
|
-
// Should always return the root container, not the scoped container
|
|
120
|
-
expect(result.current).toBe(container)
|
|
121
|
-
})
|
|
122
|
-
|
|
123
|
-
it('should return root container from deeply nested scopes', () => {
|
|
124
|
-
const wrapper = ({ children }: { children: React.ReactNode }) =>
|
|
125
|
-
createElement(
|
|
126
|
-
ContainerProvider,
|
|
127
|
-
// @ts-expect-error - props are not typed
|
|
128
|
-
{ container },
|
|
129
|
-
createElement(
|
|
130
|
-
ScopeProvider,
|
|
131
|
-
// @ts-expect-error - props are not typed
|
|
132
|
-
{ scopeId: 'outer-scope' },
|
|
133
|
-
// @ts-expect-error - props are not typed
|
|
134
|
-
createElement(ScopeProvider, { scopeId: 'inner-scope' }, children),
|
|
135
|
-
),
|
|
136
|
-
)
|
|
137
|
-
|
|
138
|
-
const { result } = renderHook(() => useRootContainer(), { wrapper })
|
|
139
|
-
|
|
140
|
-
// Should always return the root container
|
|
141
|
-
expect(result.current).toBe(container)
|
|
142
|
-
})
|
|
143
|
-
})
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { Container, Injectable, Registry } from '@navios/di'
|
|
2
|
-
|
|
3
|
-
import { act, render, screen, waitFor } from '@testing-library/react'
|
|
4
|
-
import { createElement } from 'react'
|
|
5
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
6
|
-
|
|
7
|
-
import { ContainerProvider } from '../../providers/container-provider.mjs'
|
|
8
|
-
import { useInvalidateInstance } from '../use-invalidate.mjs'
|
|
9
|
-
import { useService } from '../use-service.mjs'
|
|
10
|
-
|
|
11
|
-
describe('useInvalidateInstance', () => {
|
|
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
|
-
vi.clearAllMocks()
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
const createWrapper = (children: React.ReactNode) =>
|
|
26
|
-
createElement(ContainerProvider, { container, children })
|
|
27
|
-
|
|
28
|
-
it('should invalidate a service instance directly', async () => {
|
|
29
|
-
let instanceCount = 0
|
|
30
|
-
|
|
31
|
-
@Injectable({ registry })
|
|
32
|
-
class CounterService {
|
|
33
|
-
public readonly id: number
|
|
34
|
-
|
|
35
|
-
constructor() {
|
|
36
|
-
instanceCount++
|
|
37
|
-
this.id = instanceCount
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
let invalidateInstanceFn: ((instance: unknown) => Promise<void>) | null =
|
|
42
|
-
null
|
|
43
|
-
let currentInstance: CounterService | undefined
|
|
44
|
-
|
|
45
|
-
function TestComponent() {
|
|
46
|
-
const { data, isSuccess } = useService(CounterService)
|
|
47
|
-
const invalidateInstance = useInvalidateInstance()
|
|
48
|
-
invalidateInstanceFn = invalidateInstance
|
|
49
|
-
currentInstance = data
|
|
50
|
-
|
|
51
|
-
if (!isSuccess) {
|
|
52
|
-
return createElement('div', { 'data-testid': 'loading' }, 'Loading...')
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return createElement(
|
|
56
|
-
'div',
|
|
57
|
-
{ 'data-testid': 'counter' },
|
|
58
|
-
String(data!.id),
|
|
59
|
-
)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
render(createWrapper(createElement(TestComponent)))
|
|
63
|
-
|
|
64
|
-
await waitFor(() => {
|
|
65
|
-
expect(screen.getByTestId('counter')).toBeDefined()
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
expect(screen.getByTestId('counter').textContent).toBe('1')
|
|
69
|
-
|
|
70
|
-
// Invalidate the instance directly
|
|
71
|
-
await act(async () => {
|
|
72
|
-
await invalidateInstanceFn!(currentInstance!)
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
// Wait for re-fetch
|
|
76
|
-
await waitFor(() => {
|
|
77
|
-
expect(screen.getByTestId('counter').textContent).toBe('2')
|
|
78
|
-
})
|
|
79
|
-
})
|
|
80
|
-
})
|
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
import { Container, Injectable, InjectionToken, Registry } from '@navios/di'
|
|
2
|
-
|
|
3
|
-
import { render, screen, waitFor } from '@testing-library/react'
|
|
4
|
-
import { createElement, useMemo } from 'react'
|
|
5
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
6
|
-
import { z } from 'zod/v4'
|
|
7
|
-
|
|
8
|
-
import { ContainerProvider } from '../../providers/container-provider.mjs'
|
|
9
|
-
import { useOptionalService } from '../use-optional-service.mjs'
|
|
10
|
-
|
|
11
|
-
describe('useOptionalService', () => {
|
|
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
|
-
vi.clearAllMocks()
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
const createWrapper = (children: React.ReactNode) =>
|
|
26
|
-
createElement(ContainerProvider, { container, children })
|
|
27
|
-
|
|
28
|
-
describe('when service is registered', () => {
|
|
29
|
-
it('should load the service successfully', async () => {
|
|
30
|
-
@Injectable({ registry })
|
|
31
|
-
class TestService {
|
|
32
|
-
getValue() {
|
|
33
|
-
return 'test-value'
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function TestComponent() {
|
|
38
|
-
const { data, isSuccess, isNotFound, isLoading } = useOptionalService(TestService)
|
|
39
|
-
|
|
40
|
-
if (isLoading) {
|
|
41
|
-
return createElement('div', { 'data-testid': 'loading' }, 'Loading...')
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (isNotFound) {
|
|
45
|
-
return createElement('div', { 'data-testid': 'not-found' }, 'Not Found')
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (isSuccess) {
|
|
49
|
-
return createElement('div', { 'data-testid': 'result' }, data!.getValue())
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return createElement('div', { 'data-testid': 'idle' }, 'Idle')
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
render(createWrapper(createElement(TestComponent)))
|
|
56
|
-
|
|
57
|
-
await waitFor(() => {
|
|
58
|
-
expect(screen.getByTestId('result')).toBeDefined()
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
expect(screen.getByTestId('result').textContent).toBe('test-value')
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
it('should load service with injection token and args', async () => {
|
|
65
|
-
const UserSchema = z.object({ userId: z.string() })
|
|
66
|
-
const UserToken = InjectionToken.create<
|
|
67
|
-
{ userId: string; name: string },
|
|
68
|
-
typeof UserSchema
|
|
69
|
-
>('User', UserSchema)
|
|
70
|
-
|
|
71
|
-
@Injectable({ registry, token: UserToken })
|
|
72
|
-
class _UserService {
|
|
73
|
-
public userId: string
|
|
74
|
-
public name: string
|
|
75
|
-
|
|
76
|
-
constructor(args: z.infer<typeof UserSchema>) {
|
|
77
|
-
this.userId = args.userId
|
|
78
|
-
this.name = `User ${args.userId}`
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function TestComponent() {
|
|
83
|
-
const args = useMemo(() => ({ userId: '123' }), [])
|
|
84
|
-
const { data, isSuccess, isNotFound } = useOptionalService(UserToken, args)
|
|
85
|
-
|
|
86
|
-
if (isNotFound) {
|
|
87
|
-
return createElement('div', { 'data-testid': 'not-found' }, 'Not Found')
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (isSuccess) {
|
|
91
|
-
return createElement('div', { 'data-testid': 'result' }, data!.name)
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return createElement('div', { 'data-testid': 'loading' }, 'Loading...')
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
render(createWrapper(createElement(TestComponent)))
|
|
98
|
-
|
|
99
|
-
await waitFor(() => {
|
|
100
|
-
expect(screen.getByTestId('result')).toBeDefined()
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
expect(screen.getByTestId('result').textContent).toBe('User 123')
|
|
104
|
-
})
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
describe('when service is not registered', () => {
|
|
108
|
-
it('should return isNotFound true for unregistered class token', async () => {
|
|
109
|
-
// Create a class that is NOT registered with Injectable
|
|
110
|
-
class UnregisteredService {
|
|
111
|
-
getValue() {
|
|
112
|
-
return 'value'
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function TestComponent() {
|
|
117
|
-
const { isSuccess, isNotFound, isError, isLoading } =
|
|
118
|
-
useOptionalService(UnregisteredService)
|
|
119
|
-
|
|
120
|
-
if (isLoading) {
|
|
121
|
-
return createElement('div', { 'data-testid': 'loading' }, 'Loading...')
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (isNotFound) {
|
|
125
|
-
return createElement('div', { 'data-testid': 'not-found' }, 'Service Not Found')
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (isError) {
|
|
129
|
-
return createElement('div', { 'data-testid': 'error' }, 'Error')
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if (isSuccess) {
|
|
133
|
-
return createElement('div', { 'data-testid': 'success' }, 'Success')
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
return createElement('div', { 'data-testid': 'idle' }, 'Idle')
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
render(createWrapper(createElement(TestComponent)))
|
|
140
|
-
|
|
141
|
-
await waitFor(() => {
|
|
142
|
-
const notFound = screen.queryByTestId('not-found')
|
|
143
|
-
const error = screen.queryByTestId('error')
|
|
144
|
-
// Either not-found or error is acceptable for unregistered services
|
|
145
|
-
expect(notFound || error).toBeTruthy()
|
|
146
|
-
})
|
|
147
|
-
})
|
|
148
|
-
|
|
149
|
-
it('should return isNotFound true for unregistered injection token', async () => {
|
|
150
|
-
const UnregisteredToken = InjectionToken.create<{ value: string }>('Unregistered')
|
|
151
|
-
|
|
152
|
-
function TestComponent() {
|
|
153
|
-
const { isSuccess, isNotFound, isError, isLoading } =
|
|
154
|
-
useOptionalService(UnregisteredToken)
|
|
155
|
-
|
|
156
|
-
if (isLoading) {
|
|
157
|
-
return createElement('div', { 'data-testid': 'loading' }, 'Loading...')
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
if (isNotFound) {
|
|
161
|
-
return createElement('div', { 'data-testid': 'not-found' }, 'Token Not Found')
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
if (isError) {
|
|
165
|
-
return createElement('div', { 'data-testid': 'error' }, 'Error')
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
if (isSuccess) {
|
|
169
|
-
return createElement('div', { 'data-testid': 'success' }, 'Success')
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
return createElement('div', { 'data-testid': 'idle' }, 'Idle')
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
render(createWrapper(createElement(TestComponent)))
|
|
176
|
-
|
|
177
|
-
await waitFor(() => {
|
|
178
|
-
const notFound = screen.queryByTestId('not-found')
|
|
179
|
-
const error = screen.queryByTestId('error')
|
|
180
|
-
// Either not-found or error is acceptable for unregistered tokens
|
|
181
|
-
expect(notFound || error).toBeTruthy()
|
|
182
|
-
})
|
|
183
|
-
})
|
|
184
|
-
})
|
|
185
|
-
|
|
186
|
-
describe('refetch functionality', () => {
|
|
187
|
-
it('should allow manual refetch', async () => {
|
|
188
|
-
let instanceCount = 0
|
|
189
|
-
|
|
190
|
-
@Injectable({ registry })
|
|
191
|
-
class CounterService {
|
|
192
|
-
public readonly id: number
|
|
193
|
-
|
|
194
|
-
constructor() {
|
|
195
|
-
instanceCount++
|
|
196
|
-
this.id = instanceCount
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
let refetchFn: (() => void) | null = null
|
|
201
|
-
|
|
202
|
-
function TestComponent() {
|
|
203
|
-
const { data, isSuccess, refetch } = useOptionalService(CounterService)
|
|
204
|
-
refetchFn = refetch
|
|
205
|
-
|
|
206
|
-
if (!isSuccess) {
|
|
207
|
-
return createElement('div', { 'data-testid': 'loading' }, 'Loading...')
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
return createElement('div', { 'data-testid': 'counter' }, String(data!.id))
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
render(createWrapper(createElement(TestComponent)))
|
|
214
|
-
|
|
215
|
-
await waitFor(() => {
|
|
216
|
-
expect(screen.getByTestId('counter')).toBeDefined()
|
|
217
|
-
})
|
|
218
|
-
|
|
219
|
-
expect(screen.getByTestId('counter').textContent).toBe('1')
|
|
220
|
-
|
|
221
|
-
// Note: refetch alone won't create a new instance since the service is cached
|
|
222
|
-
// It will return the same cached instance
|
|
223
|
-
refetchFn!()
|
|
224
|
-
|
|
225
|
-
await waitFor(() => {
|
|
226
|
-
expect(screen.getByTestId('counter')).toBeDefined()
|
|
227
|
-
})
|
|
228
|
-
|
|
229
|
-
// Same instance because it's cached
|
|
230
|
-
expect(screen.getByTestId('counter').textContent).toBe('1')
|
|
231
|
-
})
|
|
232
|
-
})
|
|
233
|
-
})
|