@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.
@@ -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
- const mockQrl = (_callbackQrl) => {
6
- return qwik.$(vi_2VT5v0um.v.fn());
7
- };
8
- const mock$ = qwik.implicit$FirstArg(mockQrl);
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
  }
@@ -1,9 +1,27 @@
1
- import { implicit$FirstArg, $ } from "@builder.io/qwik";
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
- const mockQrl = (_callbackQrl) => {
4
- return $(vi.fn());
5
- };
6
- const mock$ = implicit$FirstArg(mockQrl);
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
  }
@@ -1 +1,2 @@
1
- export * from "./lib/qwik-mock";
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 QRL mock that can be used in tests to verify interactions.
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
- * As Qwik is an async framework, you need to `resolve()` the mock before making your verifications.
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
- * describe('<MyButton />', () => {
14
- * beforeEach(() => {
15
- * clearAllMocks();
16
- * });
22
+ * const onClickMock = mock$();
23
+ * await render(<MyButton onClick$={onClickMock} />);
17
24
  *
18
- * it('should call onClick$', async () => {
19
- * const onClickMock = mock$();
20
- * await render(<MyButton onClick$={onClickMock} />);
25
+ * await userEvent.click(screen.getByRole('button'));
21
26
  *
22
- * await userEvent.click(screen.getByRole('button'));
23
- *
24
- * await waitFor(() => expect(onClickMock.resolve()).resolves.toHaveBeenCalled());
25
- * });
26
- * });
27
+ * await waitFor(() => expect(onClickMock).toHaveBeenCalled());
27
28
  * ```
28
29
  */
29
- export declare const mockQrl: (_callbackQrl: QRL<(...args: any[]) => any>) => QRL<Mock>;
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
- * Will call `.mockClear()` on all spies. This will clear mock history, but not reset its implementation to the
33
- * default one.
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noma.to/qwik-mock",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "Small utility to mock Qwik QRLs",
5
5
  "repository": "https://github.com/ianlet/qwik-testing-library",
6
6
  "homepage": "https://github.com/ianlet/qwik-testing-library",