@nuxt/test-utils 3.16.0 → 3.17.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.
|
@@ -7,24 +7,10 @@ import { RenderOptions as RenderOptions$1 } from '@testing-library/vue';
|
|
|
7
7
|
|
|
8
8
|
type Awaitable<T> = T | Promise<T>;
|
|
9
9
|
type OptionalFunction<T> = T | (() => Awaitable<T>);
|
|
10
|
-
/**
|
|
11
|
-
* `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.
|
|
12
|
-
* @param url - endpoint name (e.g. `/test/`).
|
|
13
|
-
* @param options - factory function that returns the mocked data or an object containing both the `handler` and the `method` properties.
|
|
14
|
-
* @example
|
|
15
|
-
* ```ts
|
|
16
|
-
* import { registerEndpoint } from '@nuxt/test-utils/runtime'
|
|
17
|
-
*
|
|
18
|
-
* registerEndpoint("/test/", () => {
|
|
19
|
-
* test: "test-field"
|
|
20
|
-
* })
|
|
21
|
-
* ```
|
|
22
|
-
* @see https://nuxt.com/docs/getting-started/testing#registerendpoint
|
|
23
|
-
*/
|
|
24
10
|
declare function registerEndpoint(url: string, options: EventHandler | {
|
|
25
11
|
handler: EventHandler;
|
|
26
12
|
method: HTTPMethod;
|
|
27
|
-
}): void;
|
|
13
|
+
}): () => void;
|
|
28
14
|
/**
|
|
29
15
|
* `mockNuxtImport` allows you to mock Nuxt's auto import functionality.
|
|
30
16
|
* @param _name - name of an import to mock.
|
|
@@ -5,19 +5,34 @@ import { defu } from 'defu';
|
|
|
5
5
|
import { defineComponent, useRouter, h, tryUseNuxtApp } from '#imports';
|
|
6
6
|
import NuxtRoot from '#build/root-component.mjs';
|
|
7
7
|
|
|
8
|
+
const endpointRegistry = {};
|
|
8
9
|
function registerEndpoint(url, options) {
|
|
9
10
|
const app = window.__app;
|
|
10
|
-
if (!app)
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
if (!app) {
|
|
12
|
+
throw new Error("registerEndpoint() can only be used in a `@nuxt/test-utils` runtime environment");
|
|
13
|
+
}
|
|
14
|
+
const config = typeof options === "function" ? { handler: options, method: void 0 } : options;
|
|
15
|
+
config.handler = defineEventHandler(config.handler);
|
|
16
|
+
const hasBeenRegistered = window.__registry.has(url);
|
|
17
|
+
endpointRegistry[url] ||= [];
|
|
18
|
+
endpointRegistry[url].push(config);
|
|
19
|
+
if (!hasBeenRegistered) {
|
|
20
|
+
window.__registry.add(url);
|
|
21
|
+
app.use("/_" + url, defineEventHandler((event) => {
|
|
22
|
+
const latestHandler = [...endpointRegistry[url]].reverse().find((config2) => config2.method ? event.method === config2.method : true);
|
|
23
|
+
return latestHandler?.handler(event);
|
|
24
|
+
}), {
|
|
25
|
+
match(_, event) {
|
|
26
|
+
return endpointRegistry[url]?.some((config2) => config2.method ? event?.method === config2.method : true);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
return () => {
|
|
31
|
+
endpointRegistry[url].splice(endpointRegistry[url].indexOf(config), 1);
|
|
32
|
+
if (endpointRegistry[url].length === 0) {
|
|
33
|
+
window.__registry.delete(url);
|
|
18
34
|
}
|
|
19
|
-
}
|
|
20
|
-
window.__registry.add(url);
|
|
35
|
+
};
|
|
21
36
|
}
|
|
22
37
|
function mockNuxtImport(_name, _factory) {
|
|
23
38
|
throw new Error(
|