-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuse-state-subscription.spec.ts
More file actions
79 lines (65 loc) · 2.64 KB
/
use-state-subscription.spec.ts
File metadata and controls
79 lines (65 loc) · 2.64 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
75
76
77
78
79
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import fc from "fast-check";
import type {
CtrlStateQuery,
CtrlStateSSubscription,
} from "@/graphql/codegen/generated";
import {
useCtrlStateQuery,
useCtrlStateSSubscription,
} from "@/graphql/codegen/generated";
import { useSubscribeState } from "../use-state-subscription";
import { mockUseQueryResponse } from "./mock-use-query-response";
import { mockUseSubscriptionResponse } from "./mock-use-subscription-response";
vi.mock("@/graphql/codegen/generated", () => ({
useCtrlStateQuery: vi.fn(),
useCtrlStateSSubscription: vi.fn(),
}));
const fcState = fc.string();
const fcQueryData = fc.oneof(
fc.constant(undefined),
fc.record({ ctrl: fc.record({ state: fcState }) }),
);
const fcSubData = fc.oneof(fc.constant(undefined), fc.record({ ctrlState: fcState }));
const fcErrorInstance = fc.string().map((msg) => new Error(msg));
const fcError = fc.oneof(fc.constant(undefined), fcErrorInstance);
const fcQueryArg = fc.record({ data: fcQueryData, error: fcError });
const fcSubArg = fc.record({ data: fcSubData, error: fcError });
describe("useSubscribeState()", () => {
beforeEach(() => {
vi.clearAllMocks();
});
afterEach(() => {
vi.resetAllMocks();
});
it("Property test", async () => {
await fc.assert(
fc.asyncProperty(fcQueryArg, fc.array(fcSubArg), async (queryArg, subArg) => {
// Mock useCtrlStateQuery()
const queryRes = mockUseQueryResponse<CtrlStateQuery>(queryArg);
type Query = ReturnType<typeof useCtrlStateQuery>;
vi.mocked(useCtrlStateQuery).mockReturnValue(queryRes as Query);
// Mock useCtrlStateSSubscription()
const { response: subRes, issue } =
mockUseSubscriptionResponse<CtrlStateSSubscription>(subArg);
type SubRes = ReturnType<typeof useCtrlStateSSubscription>;
vi.mocked(useCtrlStateSSubscription).mockReturnValue(subRes as SubRes);
const { state, error } = await useSubscribeState();
// Assert initial values are from query.
expect(error.value).toBe(queryArg.error);
expect(state.value).toBe(
queryArg.error ? undefined : queryArg.data?.ctrl.state,
);
// Assert the subsequent values are issued from subscription backed up by query.
for (const issued of issue) {
const expectedError = issued.error || queryArg.error;
const expectedState = expectedError
? undefined
: (issued.data?.ctrlState ?? queryArg.data?.ctrl.state);
expect(error.value).toBe(expectedError);
expect(state.value).toBe(expectedState);
}
}),
);
});
});