@ohos-ports/nuxt-test-utils 0.0.0-stage → 4.4.0-beta.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/LICENSE +21 -0
- package/README.md +20 -2
- package/dirs.js +4 -0
- package/dist/browser/index.d.mts +7 -0
- package/dist/browser/index.mjs +12 -0
- package/dist/browser/pure.d.mts +2 -0
- package/dist/browser/pure.mjs +1 -0
- package/dist/config.d.mts +82 -0
- package/dist/config.mjs +336 -0
- package/dist/e2e/dev-server.d.mts +1 -0
- package/dist/e2e/dev-server.mjs +10 -0
- package/dist/e2e-BO5mj9tk.d.mts +244 -0
- package/dist/e2e-DzsWJ8cS.mjs +373 -0
- package/dist/e2e.d.mts +2 -0
- package/dist/e2e.mjs +3 -0
- package/dist/experimental.d.mts +5 -0
- package/dist/experimental.mjs +20 -0
- package/dist/interface-Dhh_nx8F.mjs +34 -0
- package/dist/module.d.mts +32 -0
- package/dist/module.mjs +987 -0
- package/dist/playwright.d.mts +33 -0
- package/dist/playwright.mjs +68 -0
- package/dist/pure-Ba9-jZYN.d.mts +56 -0
- package/dist/pure-C_CSGRkt.mjs +114 -0
- package/dist/runtime/browser-entry.mjs +15 -0
- package/dist/runtime/entry.mjs +19 -0
- package/dist/runtime/global-setup.mjs +15 -0
- package/dist/runtime/mocks/vue-devtools.mjs +11 -0
- package/dist/runtime/nuxt-root.mjs +17 -0
- package/dist/runtime/shared/environment.mjs +67 -0
- package/dist/runtime/shared/h3-v1.mjs +32 -0
- package/dist/runtime/shared/h3-v2.mjs +34 -0
- package/dist/runtime/shared/h3.mjs +5 -0
- package/dist/runtime/shared/nuxt.mjs +17 -0
- package/dist/runtime/shared/vue-wrapper-plugin.mjs +42 -0
- package/dist/runtime-utils/index.d.mts +208 -0
- package/dist/runtime-utils/index.mjs +276 -0
- package/dist/server-Vl1UKNYB.mjs +301 -0
- package/dist/suspended-BbyJTvUP.d.mts +45 -0
- package/dist/suspended-Bjq6Zao6.mjs +1 -0
- package/dist/suspended-UrdAzOmb.mjs +255 -0
- package/dist/utils-CCklE0VI.mjs +196 -0
- package/dist/vitest-environment.d.mts +42 -0
- package/dist/vitest-environment.mjs +318 -0
- package/dist/vitest-wrapper/cli.d.mts +1 -0
- package/dist/vitest-wrapper/cli.mjs +81 -0
- package/package.json +145 -4
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { i as WrapperSuspendedResult, r as WrapperSuspendedOptions } from "../suspended-BbyJTvUP.mjs";
|
|
2
|
+
import { ComponentInjectOptions, ComponentOptionsMixin, ComponentOptionsWithArrayProps, ComponentOptionsWithObjectProps, ComponentOptionsWithoutProps, ComponentPropsOptions, ComputedOptions, EmitsOptions, MethodOptions, RenderFunction, SetupContext } from "vue";
|
|
3
|
+
import { mount } from "@ohos-ports/vue-test-utils";
|
|
4
|
+
import { EventHandler, HTTPMethod } from "h3";
|
|
5
|
+
import { render } from "@testing-library/vue";
|
|
6
|
+
type Awaitable<T> = T | Promise<T>;
|
|
7
|
+
type OptionalFunction<T> = T | (() => Awaitable<T>);
|
|
8
|
+
/** h3 types for `registerEndpoint`, augmented by the `@nuxt/test-utils` module. */
|
|
9
|
+
interface RegisterEndpointH3Types {}
|
|
10
|
+
type EventHandler$1 = RegisterEndpointH3Types extends {
|
|
11
|
+
EventHandler: infer T;
|
|
12
|
+
} ? T : EventHandler;
|
|
13
|
+
type HTTPMethod$1 = RegisterEndpointH3Types extends {
|
|
14
|
+
HTTPMethod: infer T;
|
|
15
|
+
} ? T : HTTPMethod;
|
|
16
|
+
/**
|
|
17
|
+
* `registerEndpoint` allows you create Nitro endpoint that returns mocked data. It can come in handy if you want to test a component that makes requests to API to display some data.
|
|
18
|
+
* @param url - endpoint name (e.g. `/test/`).
|
|
19
|
+
* @param options - factory function that returns the mocked data or an object containing the `handler`, `method`, and `once` properties.
|
|
20
|
+
* - `handler`: the event handler function
|
|
21
|
+
* - `method`: (optional) HTTP method to match (e.g., 'GET', 'POST')
|
|
22
|
+
* - `once`: (optional) if true, the handler will only be used for the first matching request and then automatically removed
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* import { registerEndpoint } from '@nuxt/test-utils/runtime'
|
|
26
|
+
*
|
|
27
|
+
* registerEndpoint("/test/", () => ({
|
|
28
|
+
* test: "test-field"
|
|
29
|
+
* }))
|
|
30
|
+
*
|
|
31
|
+
* // With once option
|
|
32
|
+
* registerEndpoint("/api/user", {
|
|
33
|
+
* handler: () => ({ name: "Alice" }),
|
|
34
|
+
* once: true
|
|
35
|
+
* })
|
|
36
|
+
* ```
|
|
37
|
+
* @see https://nuxt.com/docs/getting-started/testing#registerendpoint
|
|
38
|
+
*/
|
|
39
|
+
export declare function registerEndpoint(url: string, options: EventHandler$1 | {
|
|
40
|
+
handler: EventHandler$1;
|
|
41
|
+
method?: HTTPMethod$1;
|
|
42
|
+
once?: boolean;
|
|
43
|
+
}): () => void;
|
|
44
|
+
type MockNuxtImportTarget<T> = T extends string ? any : T;
|
|
45
|
+
/**
|
|
46
|
+
* `mockNuxtImport` allows you to mock Nuxt's auto import functionality.
|
|
47
|
+
* @param _target - name of an import to mock or mocked target.
|
|
48
|
+
* @param _factory - factory function that returns mocked import.
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { mockNuxtImport } from '@nuxt/test-utils/runtime'
|
|
52
|
+
*
|
|
53
|
+
* mockNuxtImport('useStorage', () => {
|
|
54
|
+
* return () => {
|
|
55
|
+
* return { value: 'mocked storage' }
|
|
56
|
+
* }
|
|
57
|
+
* })
|
|
58
|
+
*
|
|
59
|
+
* // With mocked target
|
|
60
|
+
* mockNuxtImport(useStorage, () => {
|
|
61
|
+
* return () => {
|
|
62
|
+
* return { value: 'mocked storage' }
|
|
63
|
+
* }
|
|
64
|
+
* })
|
|
65
|
+
* ```
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* // Making partial mock with original implementation
|
|
69
|
+
* mockNuxtImport(useRoute, original => vi.fn(original))
|
|
70
|
+
* // or (with name based)
|
|
71
|
+
* mockNuxtImport('useRoute', original => vi.fn(original))
|
|
72
|
+
* // or (with name based, type parameter)
|
|
73
|
+
* mockNuxtImport<typeof useRoute>('useRoute', original => vi.fn(original))
|
|
74
|
+
*
|
|
75
|
+
* // Override in test
|
|
76
|
+
* vi.mocked(useRoute).mockImplementation(
|
|
77
|
+
* (...args) => ({ ...vi.mocked(useRoute).getMockImplementation()!(...args), path: '/mocked' }),
|
|
78
|
+
* )
|
|
79
|
+
* ```
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* // Using the returned mock
|
|
83
|
+
* const useStorageMock = mockNuxtImport('useStorage', () => vi.fn())
|
|
84
|
+
*
|
|
85
|
+
* // Override in test
|
|
86
|
+
* useStorageMock.mockReturnValue({ value: 'mocked storage' })
|
|
87
|
+
*
|
|
88
|
+
* // With a type parameter, pass the mock type as well to keep it in the return type
|
|
89
|
+
* const useRouteMock = mockNuxtImport<typeof useRoute, Mock<typeof useRoute>>('useRoute', () => vi.fn())
|
|
90
|
+
* ```
|
|
91
|
+
* @see https://nuxt.com/docs/getting-started/testing#mocknuxtimport
|
|
92
|
+
*/
|
|
93
|
+
export declare function mockNuxtImport<T = unknown, R extends MockNuxtImportTarget<T> = MockNuxtImportTarget<T>>(_target: string | T, _factory: (original: MockNuxtImportTarget<T>) => R | Promise<R>): R;
|
|
94
|
+
/**
|
|
95
|
+
* `unmockNuxtImport` allows you to unmock Nuxt's auto import functionality.
|
|
96
|
+
* @param _target - name of an import to unmock or unmocked target.
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* import { unmockNuxtImport } from '@nuxt/test-utils/runtime'
|
|
100
|
+
*
|
|
101
|
+
* unmockNuxtImport('useStorage')
|
|
102
|
+
*
|
|
103
|
+
* // With mocked target
|
|
104
|
+
* unmockNuxtImport(useStorage)
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export declare function unmockNuxtImport<T = unknown>(_target: string | T): void;
|
|
108
|
+
/**
|
|
109
|
+
* `mockComponent` allows you to mock Nuxt's component.
|
|
110
|
+
* @param path - component name in PascalCase, or the relative path of the component.
|
|
111
|
+
* @param setup - factory function that returns the mocked component.
|
|
112
|
+
* @example
|
|
113
|
+
* ```ts
|
|
114
|
+
* import { mockComponent } from '@nuxt/test-utils/runtime'
|
|
115
|
+
*
|
|
116
|
+
* mockComponent('MyComponent', {
|
|
117
|
+
* props: {
|
|
118
|
+
* value: String
|
|
119
|
+
* },
|
|
120
|
+
* setup(props) {
|
|
121
|
+
* // ...
|
|
122
|
+
* }
|
|
123
|
+
* })
|
|
124
|
+
*
|
|
125
|
+
* // relative path or alias also works
|
|
126
|
+
* mockComponent('~/components/my-component.vue', async () => {
|
|
127
|
+
* // or a factory function
|
|
128
|
+
* return {
|
|
129
|
+
* setup(props) {
|
|
130
|
+
* // ...
|
|
131
|
+
* }
|
|
132
|
+
* }
|
|
133
|
+
* })
|
|
134
|
+
*
|
|
135
|
+
* // or you can use SFC for redirecting to a mock component
|
|
136
|
+
* mockComponent('MyComponent', () => import('./MockComponent.vue'))
|
|
137
|
+
* ```
|
|
138
|
+
* @see https://nuxt.com/docs/getting-started/testing#mockcomponent
|
|
139
|
+
*/
|
|
140
|
+
export declare function mockComponent<Props, RawBindings = object>(path: string, setup: OptionalFunction<(props: Readonly<Props>, ctx: SetupContext) => RawBindings | RenderFunction>): void;
|
|
141
|
+
export declare function mockComponent<Props = {}, RawBindings = {}, D = {}, C extends ComputedOptions = {}, M extends MethodOptions = {}, Mixin extends ComponentOptionsMixin = ComponentOptionsMixin, Extends extends ComponentOptionsMixin = ComponentOptionsMixin, E extends EmitsOptions = {}, EE extends string = string, I extends ComponentInjectOptions = {}, II extends string = string>(path: string, options: OptionalFunction<ComponentOptionsWithoutProps<Props, RawBindings, D, C, M, Mixin, Extends, E, EE, I, II>>): void;
|
|
142
|
+
export declare function mockComponent<PropNames extends string, RawBindings, D, C extends ComputedOptions = {}, M extends MethodOptions = {}, Mixin extends ComponentOptionsMixin = ComponentOptionsMixin, Extends extends ComponentOptionsMixin = ComponentOptionsMixin, E extends EmitsOptions = {}, EE extends string = string, I extends ComponentInjectOptions = {}, II extends string = string>(path: string, options: OptionalFunction<ComponentOptionsWithArrayProps<PropNames, RawBindings, D, C, M, Mixin, Extends, E, EE, I, II>>): void;
|
|
143
|
+
export declare function mockComponent<PropsOptions extends Readonly<ComponentPropsOptions>, RawBindings, D, C extends ComputedOptions = {}, M extends MethodOptions = {}, Mixin extends ComponentOptionsMixin = ComponentOptionsMixin, Extends extends ComponentOptionsMixin = ComponentOptionsMixin, E extends EmitsOptions = {}, EE extends string = string, I extends ComponentInjectOptions = {}, II extends string = string>(path: string, options: OptionalFunction<ComponentOptionsWithObjectProps<PropsOptions, RawBindings, D, C, M, Mixin, Extends, E, EE, I, II>>): void;
|
|
144
|
+
type WrapperFn$1<C> = typeof mount<C>;
|
|
145
|
+
type WrapperOptions$1<C> = WrapperSuspendedOptions<WrapperFn$1<C>>;
|
|
146
|
+
type WrapperResult$1<C> = WrapperSuspendedResult<WrapperFn$1<C>, C>;
|
|
147
|
+
/**
|
|
148
|
+
* `mountSuspended` allows you to mount any vue component within the Nuxt environment, allowing async setup and access to injections from your Nuxt plugins. For example:
|
|
149
|
+
*
|
|
150
|
+
* ```ts
|
|
151
|
+
* // tests/components/SomeComponents.nuxt.spec.ts
|
|
152
|
+
* it('can mount some component', async () => {
|
|
153
|
+
* const component = await mountSuspended(SomeComponent)
|
|
154
|
+
* expect(component.text()).toMatchInlineSnapshot(
|
|
155
|
+
* 'This is an auto-imported component'
|
|
156
|
+
* )
|
|
157
|
+
* })
|
|
158
|
+
*
|
|
159
|
+
* // tests/App.nuxt.spec.ts
|
|
160
|
+
* it('can also mount an app', async () => {
|
|
161
|
+
* const component = await mountSuspended(App, { route: '/test' })
|
|
162
|
+
* expect(component.html()).toMatchInlineSnapshot(`
|
|
163
|
+
* "<div>This is an auto-imported component</div>
|
|
164
|
+
* <div> I am a global component </div>
|
|
165
|
+
* <div>/</div>
|
|
166
|
+
* <a href=\\"/test\\"> Test link </a>"
|
|
167
|
+
* `)
|
|
168
|
+
* })
|
|
169
|
+
* ```
|
|
170
|
+
* @param component the component to be tested
|
|
171
|
+
* @param options optional options to set up your component
|
|
172
|
+
*/
|
|
173
|
+
export declare function mountSuspended<T>(component: T, options?: WrapperOptions$1<T>): Promise<WrapperResult$1<T>>;
|
|
174
|
+
type WrapperFn<C> = typeof render<C>;
|
|
175
|
+
type WrapperOptions<C> = WrapperSuspendedOptions<WrapperFn<C>>;
|
|
176
|
+
type WrapperResult<C> = WrapperSuspendedResult<WrapperFn<C>, C>;
|
|
177
|
+
/**
|
|
178
|
+
* `renderSuspended` allows you to mount any vue component within the Nuxt environment, allowing async setup and access to injections from your Nuxt plugins.
|
|
179
|
+
*
|
|
180
|
+
* This is a wrapper around the `render` function from @testing-libary/vue, and should be used together with
|
|
181
|
+
* utilities from that package.
|
|
182
|
+
*
|
|
183
|
+
* ```ts
|
|
184
|
+
* // tests/components/SomeComponents.nuxt.spec.ts
|
|
185
|
+
* import { renderSuspended } from '@nuxt/test-utils/runtime'
|
|
186
|
+
*
|
|
187
|
+
* it('can render some component', async () => {
|
|
188
|
+
* const { html } = await renderSuspended(SomeComponent)
|
|
189
|
+
* expect(html()).toMatchInlineSnapshot(
|
|
190
|
+
* 'This is an auto-imported component'
|
|
191
|
+
* )
|
|
192
|
+
*
|
|
193
|
+
* })
|
|
194
|
+
*
|
|
195
|
+
* // tests/App.nuxt.spec.ts
|
|
196
|
+
* import { renderSuspended } from '@nuxt/test-utils/runtime'
|
|
197
|
+
* import { screen } from '@testing-library/vue'
|
|
198
|
+
*
|
|
199
|
+
* it('can also mount an app', async () => {
|
|
200
|
+
* const { html } = await renderSuspended(App, { route: '/test' })
|
|
201
|
+
* expect(screen.getByRole('link', { name: 'Test Link' })).toBeVisible()
|
|
202
|
+
* })
|
|
203
|
+
* ```
|
|
204
|
+
* @param component the component to be tested
|
|
205
|
+
* @param options optional options to set up your component
|
|
206
|
+
*/
|
|
207
|
+
export declare function renderSuspended<T>(component: T, options?: WrapperOptions<T>): Promise<WrapperResult<T>>;
|
|
208
|
+
export type { RegisterEndpointH3Types };
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { h, nextTick } from "vue";
|
|
2
|
+
import { mount } from "@ohos-ports/vue-test-utils";
|
|
3
|
+
// src/runtime-utils/mock.ts
|
|
4
|
+
function getEndpointRegistry() {
|
|
5
|
+
const app = window.__app ?? {};
|
|
6
|
+
return app._registeredEndpointRegistry ||= {};
|
|
7
|
+
}
|
|
8
|
+
function findEndpointRegistryHandlers(url) {
|
|
9
|
+
const endpointRegistry = getEndpointRegistry();
|
|
10
|
+
const pathname = url.replace(/[?#].*$/, "");
|
|
11
|
+
for (const [key, handlers] of Object.entries(endpointRegistry)) if (key === url || key === pathname) {
|
|
12
|
+
if (handlers?.length) return handlers;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* `registerEndpoint` allows you create Nitro endpoint that returns mocked data. It can come in handy if you want to test a component that makes requests to API to display some data.
|
|
17
|
+
* @param url - endpoint name (e.g. `/test/`).
|
|
18
|
+
* @param options - factory function that returns the mocked data or an object containing the `handler`, `method`, and `once` properties.
|
|
19
|
+
* - `handler`: the event handler function
|
|
20
|
+
* - `method`: (optional) HTTP method to match (e.g., 'GET', 'POST')
|
|
21
|
+
* - `once`: (optional) if true, the handler will only be used for the first matching request and then automatically removed
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { registerEndpoint } from '@nuxt/test-utils/runtime'
|
|
25
|
+
*
|
|
26
|
+
* registerEndpoint("/test/", () => ({
|
|
27
|
+
* test: "test-field"
|
|
28
|
+
* }))
|
|
29
|
+
*
|
|
30
|
+
* // With once option
|
|
31
|
+
* registerEndpoint("/api/user", {
|
|
32
|
+
* handler: () => ({ name: "Alice" }),
|
|
33
|
+
* once: true
|
|
34
|
+
* })
|
|
35
|
+
* ```
|
|
36
|
+
* @see https://nuxt.com/docs/getting-started/testing#registerendpoint
|
|
37
|
+
*/
|
|
38
|
+
function registerEndpoint(url, options) {
|
|
39
|
+
const app = window.__app;
|
|
40
|
+
if (!app) throw new Error("registerEndpoint() can only be used in a `@nuxt/test-utils` runtime environment");
|
|
41
|
+
const config = typeof options === "function" ? {
|
|
42
|
+
url,
|
|
43
|
+
handler: options,
|
|
44
|
+
method: void 0,
|
|
45
|
+
once: false
|
|
46
|
+
} : {
|
|
47
|
+
...options,
|
|
48
|
+
url
|
|
49
|
+
};
|
|
50
|
+
config.handler = Object.assign(config.handler, { __is_handler__: true });
|
|
51
|
+
const endpointRegistry = getEndpointRegistry();
|
|
52
|
+
endpointRegistry[url] ||= [];
|
|
53
|
+
endpointRegistry[url].push(config);
|
|
54
|
+
window.__registry.add(url);
|
|
55
|
+
app._registered ||= registerGlobalHandler(app);
|
|
56
|
+
return () => {
|
|
57
|
+
endpointRegistry[url]?.splice(endpointRegistry[url].indexOf(config), 1);
|
|
58
|
+
if (endpointRegistry[url]?.length === 0) window.__registry.delete(url);
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* `mockNuxtImport` allows you to mock Nuxt's auto import functionality.
|
|
63
|
+
* @param _target - name of an import to mock or mocked target.
|
|
64
|
+
* @param _factory - factory function that returns mocked import.
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* import { mockNuxtImport } from '@nuxt/test-utils/runtime'
|
|
68
|
+
*
|
|
69
|
+
* mockNuxtImport('useStorage', () => {
|
|
70
|
+
* return () => {
|
|
71
|
+
* return { value: 'mocked storage' }
|
|
72
|
+
* }
|
|
73
|
+
* })
|
|
74
|
+
*
|
|
75
|
+
* // With mocked target
|
|
76
|
+
* mockNuxtImport(useStorage, () => {
|
|
77
|
+
* return () => {
|
|
78
|
+
* return { value: 'mocked storage' }
|
|
79
|
+
* }
|
|
80
|
+
* })
|
|
81
|
+
* ```
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* // Making partial mock with original implementation
|
|
85
|
+
* mockNuxtImport(useRoute, original => vi.fn(original))
|
|
86
|
+
* // or (with name based)
|
|
87
|
+
* mockNuxtImport('useRoute', original => vi.fn(original))
|
|
88
|
+
* // or (with name based, type parameter)
|
|
89
|
+
* mockNuxtImport<typeof useRoute>('useRoute', original => vi.fn(original))
|
|
90
|
+
*
|
|
91
|
+
* // Override in test
|
|
92
|
+
* vi.mocked(useRoute).mockImplementation(
|
|
93
|
+
* (...args) => ({ ...vi.mocked(useRoute).getMockImplementation()!(...args), path: '/mocked' }),
|
|
94
|
+
* )
|
|
95
|
+
* ```
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* // Using the returned mock
|
|
99
|
+
* const useStorageMock = mockNuxtImport('useStorage', () => vi.fn())
|
|
100
|
+
*
|
|
101
|
+
* // Override in test
|
|
102
|
+
* useStorageMock.mockReturnValue({ value: 'mocked storage' })
|
|
103
|
+
*
|
|
104
|
+
* // With a type parameter, pass the mock type as well to keep it in the return type
|
|
105
|
+
* const useRouteMock = mockNuxtImport<typeof useRoute, Mock<typeof useRoute>>('useRoute', () => vi.fn())
|
|
106
|
+
* ```
|
|
107
|
+
* @see https://nuxt.com/docs/getting-started/testing#mocknuxtimport
|
|
108
|
+
*/
|
|
109
|
+
function mockNuxtImport(_target, _factory) {
|
|
110
|
+
throw new Error("mockNuxtImport() is a macro and it did not get transpiled. This may be an internal bug of @nuxt/test-utils.");
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* `unmockNuxtImport` allows you to unmock Nuxt's auto import functionality.
|
|
114
|
+
* @param _target - name of an import to unmock or unmocked target.
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* import { unmockNuxtImport } from '@nuxt/test-utils/runtime'
|
|
118
|
+
*
|
|
119
|
+
* unmockNuxtImport('useStorage')
|
|
120
|
+
*
|
|
121
|
+
* // With mocked target
|
|
122
|
+
* unmockNuxtImport(useStorage)
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
function unmockNuxtImport(_target) {
|
|
126
|
+
throw new Error("unmockNuxtImport() is a macro and it did not get transpiled. This may be an internal bug of @nuxt/test-utils.");
|
|
127
|
+
}
|
|
128
|
+
function mockComponent(_path, _component) {
|
|
129
|
+
throw new Error("mockComponent() is a macro and it did not get transpiled. This may be an internal bug of @nuxt/test-utils.");
|
|
130
|
+
}
|
|
131
|
+
const handler = Object.assign(async (event) => {
|
|
132
|
+
const registeredHandlers = findEndpointRegistryHandlers("url" in event && event.url ? (event.url.pathname + event.url.search).replace(/^\/_/, "") : event.path.replace(/^\/_/, ""));
|
|
133
|
+
const latestHandler = [...registeredHandlers || []].reverse().find((config) => config.method ? event.method === config.method : true);
|
|
134
|
+
if (!latestHandler) return;
|
|
135
|
+
const result = await latestHandler.handler(event);
|
|
136
|
+
if (!latestHandler.once) return result;
|
|
137
|
+
const index = registeredHandlers?.indexOf(latestHandler);
|
|
138
|
+
if (index === void 0 || index === -1) return result;
|
|
139
|
+
registeredHandlers?.splice(index, 1);
|
|
140
|
+
if (registeredHandlers?.length === 0) window.__registry.delete(latestHandler.url);
|
|
141
|
+
return result;
|
|
142
|
+
}, { __is_handler__: true });
|
|
143
|
+
function registerGlobalHandler(app) {
|
|
144
|
+
app.use(handler, { match: (...args) => {
|
|
145
|
+
const [eventOrPath, _event = eventOrPath] = args;
|
|
146
|
+
const url = typeof eventOrPath === "string" ? eventOrPath.replace(/^\/_/, "") : (eventOrPath.url.pathname + eventOrPath.url.search).replace(/^\/_/, "");
|
|
147
|
+
const event = _event;
|
|
148
|
+
return findEndpointRegistryHandlers(url)?.some((config) => config.method ? event?.method === config.method : true) ?? false;
|
|
149
|
+
} });
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
// src/runtime-utils/mount.ts
|
|
153
|
+
/**
|
|
154
|
+
* `mountSuspended` allows you to mount any vue component within the Nuxt environment, allowing async setup and access to injections from your Nuxt plugins. For example:
|
|
155
|
+
*
|
|
156
|
+
* ```ts
|
|
157
|
+
* // tests/components/SomeComponents.nuxt.spec.ts
|
|
158
|
+
* it('can mount some component', async () => {
|
|
159
|
+
* const component = await mountSuspended(SomeComponent)
|
|
160
|
+
* expect(component.text()).toMatchInlineSnapshot(
|
|
161
|
+
* 'This is an auto-imported component'
|
|
162
|
+
* )
|
|
163
|
+
* })
|
|
164
|
+
*
|
|
165
|
+
* // tests/App.nuxt.spec.ts
|
|
166
|
+
* it('can also mount an app', async () => {
|
|
167
|
+
* const component = await mountSuspended(App, { route: '/test' })
|
|
168
|
+
* expect(component.html()).toMatchInlineSnapshot(`
|
|
169
|
+
* "<div>This is an auto-imported component</div>
|
|
170
|
+
* <div> I am a global component </div>
|
|
171
|
+
* <div>/</div>
|
|
172
|
+
* <a href=\\"/test\\"> Test link </a>"
|
|
173
|
+
* `)
|
|
174
|
+
* })
|
|
175
|
+
* ```
|
|
176
|
+
* @param component the component to be tested
|
|
177
|
+
* @param options optional options to set up your component
|
|
178
|
+
*/
|
|
179
|
+
async function mountSuspended(component, options = {}) {
|
|
180
|
+
const { cleanupAll, patchWrapperSetProps, wrapperSuspended } = await import("../suspended-Bjq6Zao6.mjs");
|
|
181
|
+
const suspendedHelperName = "MountSuspendedHelper";
|
|
182
|
+
const clonedComponentName = "MountSuspendedComponent";
|
|
183
|
+
cleanupAll();
|
|
184
|
+
const { wrapper, setProps } = await wrapperSuspended(component, options, {
|
|
185
|
+
wrapperFn: mount,
|
|
186
|
+
suspendedHelperName,
|
|
187
|
+
clonedComponentName
|
|
188
|
+
});
|
|
189
|
+
patchWrapperSetProps(wrapper, setProps);
|
|
190
|
+
return wrappedMountedWrapper(wrapper, wrapper.findComponent({ name: clonedComponentName }));
|
|
191
|
+
}
|
|
192
|
+
function wrappedMountedWrapper(wrapper, component) {
|
|
193
|
+
const wrapperProps = [
|
|
194
|
+
"setProps",
|
|
195
|
+
"emitted",
|
|
196
|
+
"setupState",
|
|
197
|
+
"unmount"
|
|
198
|
+
];
|
|
199
|
+
return new Proxy(wrapper, { get: (_, prop, receiver) => {
|
|
200
|
+
if (prop === "getCurrentComponent") return getCurrentComponentPatchedProxy;
|
|
201
|
+
const target = wrapperProps.includes(prop) ? wrapper : Reflect.has(component, prop) ? component : wrapper;
|
|
202
|
+
const value = Reflect.get(target, prop, receiver);
|
|
203
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
204
|
+
} });
|
|
205
|
+
function getCurrentComponentPatchedProxy() {
|
|
206
|
+
const currentComponent = component.getCurrentComponent();
|
|
207
|
+
return new Proxy(currentComponent, { get: (target, prop, receiver) => {
|
|
208
|
+
const value = Reflect.get(target, prop, receiver);
|
|
209
|
+
if (prop === "proxy" && value) return new Proxy(value, { get(o, p, r) {
|
|
210
|
+
if (!Reflect.has(currentComponent.props, p)) {
|
|
211
|
+
const setupState = wrapper.setupState;
|
|
212
|
+
if (setupState && typeof setupState === "object") {
|
|
213
|
+
if (Reflect.has(setupState, p)) return Reflect.get(setupState, p, r);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return Reflect.get(o, p, r);
|
|
217
|
+
} });
|
|
218
|
+
return value;
|
|
219
|
+
} });
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
// src/runtime-utils/render.ts
|
|
223
|
+
/**
|
|
224
|
+
* `renderSuspended` allows you to mount any vue component within the Nuxt environment, allowing async setup and access to injections from your Nuxt plugins.
|
|
225
|
+
*
|
|
226
|
+
* This is a wrapper around the `render` function from @testing-libary/vue, and should be used together with
|
|
227
|
+
* utilities from that package.
|
|
228
|
+
*
|
|
229
|
+
* ```ts
|
|
230
|
+
* // tests/components/SomeComponents.nuxt.spec.ts
|
|
231
|
+
* import { renderSuspended } from '@nuxt/test-utils/runtime'
|
|
232
|
+
*
|
|
233
|
+
* it('can render some component', async () => {
|
|
234
|
+
* const { html } = await renderSuspended(SomeComponent)
|
|
235
|
+
* expect(html()).toMatchInlineSnapshot(
|
|
236
|
+
* 'This is an auto-imported component'
|
|
237
|
+
* )
|
|
238
|
+
*
|
|
239
|
+
* })
|
|
240
|
+
*
|
|
241
|
+
* // tests/App.nuxt.spec.ts
|
|
242
|
+
* import { renderSuspended } from '@nuxt/test-utils/runtime'
|
|
243
|
+
* import { screen } from '@testing-library/vue'
|
|
244
|
+
*
|
|
245
|
+
* it('can also mount an app', async () => {
|
|
246
|
+
* const { html } = await renderSuspended(App, { route: '/test' })
|
|
247
|
+
* expect(screen.getByRole('link', { name: 'Test Link' })).toBeVisible()
|
|
248
|
+
* })
|
|
249
|
+
* ```
|
|
250
|
+
* @param component the component to be tested
|
|
251
|
+
* @param options optional options to set up your component
|
|
252
|
+
*/
|
|
253
|
+
async function renderSuspended(component, options = {}) {
|
|
254
|
+
const { cleanupAll, wrapperSuspended } = await import("../suspended-Bjq6Zao6.mjs");
|
|
255
|
+
const wrapperId = "test-wrapper";
|
|
256
|
+
const suspendedHelperName = "RenderHelper";
|
|
257
|
+
const clonedComponentName = "RenderSuspendedComponent";
|
|
258
|
+
const { render: wrapperFn } = await import("@testing-library/vue");
|
|
259
|
+
cleanupAll();
|
|
260
|
+
document.getElementById(wrapperId)?.remove();
|
|
261
|
+
const { wrapper, setProps } = await wrapperSuspended(component, options, {
|
|
262
|
+
wrapperFn,
|
|
263
|
+
wrappedRender: (render) => () => h({
|
|
264
|
+
inheritAttrs: false,
|
|
265
|
+
render: () => h("div", { id: wrapperId }, render())
|
|
266
|
+
}),
|
|
267
|
+
suspendedHelperName,
|
|
268
|
+
clonedComponentName
|
|
269
|
+
});
|
|
270
|
+
wrapper.rerender = async (props) => {
|
|
271
|
+
setProps(props);
|
|
272
|
+
await nextTick();
|
|
273
|
+
};
|
|
274
|
+
return wrapper;
|
|
275
|
+
}
|
|
276
|
+
export { mockComponent, mockNuxtImport, mountSuspended, registerEndpoint, renderSuspended, unmockNuxtImport };
|