@noma.to/qwik-mock 1.0.2 → 1.1.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/lib/lib/qwik-mock.cjs +22 -4
- package/lib/lib/qwik-mock.mjs +23 -5
- package/lib-types/index.d.ts +2 -1
- package/lib-types/lib/qwik-mock.d.ts +21 -19
- package/package.json +1 -1
package/lib/lib/qwik-mock.cjs
CHANGED
|
@@ -2,10 +2,28 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const qwik = require("@builder.io/qwik");
|
|
4
4
|
const vi_2VT5v0um = require("../node_modules/.pnpm/vitest@4.0.18_@types_node@25.1.0_@vitest_ui@4.0.18_happy-dom@20.0.11_jiti@2.4.2_jsdom@26.1.0__pd2l5poczrcnqygjcqiguyknnm/node_modules/vitest/dist/chunks/vi.2VT5v0um.cjs");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
function mockQrl(implQrl) {
|
|
6
|
+
const mockFn = vi_2VT5v0um.v.fn(implQrl?.resolved);
|
|
7
|
+
if (implQrl && !mockFn.getMockImplementation()) {
|
|
8
|
+
implQrl.resolve().then((impl) => mockFn.mockImplementation(impl));
|
|
9
|
+
}
|
|
10
|
+
const qrl = qwik.$(mockFn);
|
|
11
|
+
return new Proxy(qrl, {
|
|
12
|
+
get(target, prop, receiver) {
|
|
13
|
+
if (!(prop in target) && prop in mockFn) {
|
|
14
|
+
const value = mockFn[prop];
|
|
15
|
+
return typeof value === "function" ? value.bind(mockFn) : value;
|
|
16
|
+
}
|
|
17
|
+
return Reflect.get(target, prop, receiver);
|
|
18
|
+
},
|
|
19
|
+
has(target, prop) {
|
|
20
|
+
return Reflect.has(target, prop) || Reflect.has(mockFn, prop);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
function mock$(impl) {
|
|
25
|
+
return mockQrl(impl ? qwik.$(impl) : void 0);
|
|
26
|
+
}
|
|
9
27
|
function clearAllMocks() {
|
|
10
28
|
vi_2VT5v0um.v.clearAllMocks();
|
|
11
29
|
}
|
package/lib/lib/qwik-mock.mjs
CHANGED
|
@@ -1,9 +1,27 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { $ } from "@builder.io/qwik";
|
|
2
2
|
import { v as vi } from "../node_modules/.pnpm/vitest@4.0.18_@types_node@25.1.0_@vitest_ui@4.0.18_happy-dom@20.0.11_jiti@2.4.2_jsdom@26.1.0__pd2l5poczrcnqygjcqiguyknnm/node_modules/vitest/dist/chunks/vi.2VT5v0um.mjs";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
function mockQrl(implQrl) {
|
|
4
|
+
const mockFn = vi.fn(implQrl?.resolved);
|
|
5
|
+
if (implQrl && !mockFn.getMockImplementation()) {
|
|
6
|
+
implQrl.resolve().then((impl) => mockFn.mockImplementation(impl));
|
|
7
|
+
}
|
|
8
|
+
const qrl = $(mockFn);
|
|
9
|
+
return new Proxy(qrl, {
|
|
10
|
+
get(target, prop, receiver) {
|
|
11
|
+
if (!(prop in target) && prop in mockFn) {
|
|
12
|
+
const value = mockFn[prop];
|
|
13
|
+
return typeof value === "function" ? value.bind(mockFn) : value;
|
|
14
|
+
}
|
|
15
|
+
return Reflect.get(target, prop, receiver);
|
|
16
|
+
},
|
|
17
|
+
has(target, prop) {
|
|
18
|
+
return Reflect.has(target, prop) || Reflect.has(mockFn, prop);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
function mock$(impl) {
|
|
23
|
+
return mockQrl(impl ? $(impl) : void 0);
|
|
24
|
+
}
|
|
7
25
|
function clearAllMocks() {
|
|
8
26
|
vi.clearAllMocks();
|
|
9
27
|
}
|
package/lib-types/index.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { clearAllMocks, mock$, mockQrl } from "./lib/qwik-mock";
|
|
2
|
+
export type { QrlMock } from "./lib/qwik-mock";
|
|
@@ -1,35 +1,37 @@
|
|
|
1
1
|
import { type QRL } from "@builder.io/qwik";
|
|
2
2
|
import { type Mock } from "vitest";
|
|
3
|
+
/**
|
|
4
|
+
* A mock that can be passed as a component callback prop (`onClick$`, `onChange$`, etc.)
|
|
5
|
+
* and asserted on directly with `expect()`.
|
|
6
|
+
*/
|
|
7
|
+
export type QrlMock<T extends (...args: any[]) => any = (...args: any[]) => any> = QRL<T> & Mock<T>;
|
|
8
|
+
/** @internal Called by the Qwik optimizer — use {@link mock$} instead. */
|
|
9
|
+
export declare function mockQrl<T extends (...args: any[]) => any = (...args: any[]) => any>(implQrl?: QRL<T>): QrlMock<T>;
|
|
3
10
|
/**
|
|
4
11
|
* @experimental
|
|
5
12
|
*
|
|
6
|
-
* Create a
|
|
13
|
+
* Create a mock for a component callback prop (`onClick$`, `onChange$`, etc.).
|
|
14
|
+
*
|
|
15
|
+
* Pass it to a component like any other `$` prop, then assert on it directly —
|
|
16
|
+
* no need to resolve.
|
|
7
17
|
*
|
|
8
|
-
*
|
|
9
|
-
* And remember to clear the mocks before each test to start with a clean slate!
|
|
18
|
+
* @param impl - Optional implementation function for the mock.
|
|
10
19
|
*
|
|
11
20
|
* @example
|
|
12
21
|
* ```tsx
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* clearAllMocks();
|
|
16
|
-
* });
|
|
22
|
+
* const onClickMock = mock$();
|
|
23
|
+
* await render(<MyButton onClick$={onClickMock} />);
|
|
17
24
|
*
|
|
18
|
-
*
|
|
19
|
-
* const onClickMock = mock$();
|
|
20
|
-
* await render(<MyButton onClick$={onClickMock} />);
|
|
25
|
+
* await userEvent.click(screen.getByRole('button'));
|
|
21
26
|
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* await waitFor(() => expect(onClickMock.resolve()).resolves.toHaveBeenCalled());
|
|
25
|
-
* });
|
|
26
|
-
* });
|
|
27
|
+
* await waitFor(() => expect(onClickMock).toHaveBeenCalled());
|
|
27
28
|
* ```
|
|
28
29
|
*/
|
|
29
|
-
export declare
|
|
30
|
-
export declare const mock$: (qrl: (...args: any[]) => any) => QRL<Mock>;
|
|
30
|
+
export declare function mock$<T extends (...args: any[]) => any = (...args: any[]) => any>(impl?: T): QrlMock<T>;
|
|
31
31
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
32
|
+
* Clear mock history on all mocks created with {@link mock$}.
|
|
33
|
+
* Does not reset their implementation.
|
|
34
|
+
*
|
|
35
|
+
* Typically called in `beforeEach` to start each test with a clean slate.
|
|
34
36
|
*/
|
|
35
37
|
export declare function clearAllMocks(): void;
|
package/package.json
CHANGED