-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmock-use-query-response.spec.ts
More file actions
74 lines (58 loc) · 2.06 KB
/
mock-use-query-response.spec.ts
File metadata and controls
74 lines (58 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { useQuery } from "@urql/vue";
import type { UseQueryResponse } from "@urql/vue";
import gql from "graphql-tag";
import fc from "fast-check";
import { mockUseQueryResponse } from "./mock-use-query-response";
vi.mock("@urql/vue", () => ({
useQuery: vi.fn(),
}));
const query = gql`
query CtrlState {
ctrl {
state
}
}
`;
type Data = { ctrl: { state: string } };
const fcState = fc.string({ minLength: 1 });
const fcData: fc.Arbitrary<Data | undefined> = fc.oneof(
fc.constant(undefined),
fc.record({ ctrl: fc.record({ state: fcState }) }),
);
const fcErrorInstance = fc.string().map((msg) => new Error(msg));
const fcError = fc.oneof(fc.constant(undefined), fcErrorInstance);
const fcArg = fc.record({ data: fcData, error: fcError });
describe("mockUseQueryResponse()", () => {
beforeEach(() => {
vi.clearAllMocks();
});
afterEach(() => {
vi.resetAllMocks();
});
it("Property test", async () => {
fc.assert(
fc.asyncProperty(fcArg, async (arg) => {
const response = mockUseQueryResponse<Data>(arg);
type Response = UseQueryResponse<Data>;
vi.mocked(useQuery).mockReturnValue(response as Response);
// Assert the mock response is returned.
const returned = useQuery<Data>({ query });
expect(response).toBe(returned);
// Assert initially undefined.
expect(response.fetching.value).toBe(true);
expect(response.error.value).toBeUndefined();
expect(response.data.value).toBeUndefined();
// Await until the values are assigned.
const state = await response;
expect(response.fetching.value).toBe(false);
// Confirm the object returned by await contains the same objects.
expect(state.data).toBe(response.data);
expect(state.error).toBe(response.error);
// Assert the mocked values are assigned.
expect(response.error.value).toBe(arg.error);
expect(response.data.value).toStrictEqual(arg.data);
}),
);
});
});