-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuse-state-subscription.spec.ts
More file actions
93 lines (77 loc) · 2.72 KB
/
use-state-subscription.spec.ts
File metadata and controls
93 lines (77 loc) · 2.72 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { ref } from "vue";
import fc from "fast-check";
import {
useCtrlStateQuery,
useCtrlStateSSubscription,
} from "@/graphql/codegen/generated";
import type { CtrlStateSSubscription } from "@/graphql/codegen/generated";
import { onReady } from "@/utils/on-ready";
import { useSubscribeState } from "../use-state-subscription";
vi.mock("@/graphql/codegen/generated", () => ({
useCtrlStateQuery: vi.fn(),
useCtrlStateSSubscription: vi.fn(),
}));
const fcState = fc.oneof(fc.constant(undefined), fc.string({ minLength: 1 }));
const fcErrorInstance = fc.string().map((msg) => new Error(msg));
const fcError = fc.oneof(fc.constant(undefined), fcErrorInstance);
type Query = ReturnType<typeof useCtrlStateQuery>;
type Sub = ReturnType<typeof useCtrlStateSSubscription>;
function createMockQuery(
state_value: string | undefined,
error_value: Error | undefined,
): Query {
type Data = NonNullable<Query["data"]["value"]>;
const data = ref<Data | undefined>(undefined);
const error = ref<Error | undefined>(undefined);
const ready = (async () => {
await Promise.resolve();
data.value = { ctrl: { state: state_value } } as Data;
error.value = error_value;
})();
return onReady({ data, error }, ready) as Query;
}
function createMockSubscription(
state_value: string | undefined,
error_value: Error | undefined,
): Sub {
const data = ref<CtrlStateSSubscription | undefined>(undefined);
const error = ref<Error | undefined>(undefined);
const ready = (async () => {
await Promise.resolve();
data.value = { ctrlState: state_value } as CtrlStateSSubscription;
error.value = error_value;
})();
return onReady({ data, error }, ready) as unknown as Sub;
}
describe("useSubscribeState()", () => {
beforeEach(() => {
vi.clearAllMocks();
});
afterEach(() => {
vi.resetAllMocks();
});
it("Property test", async () => {
await fc.assert(
fc.asyncProperty(
fcState,
fcError,
fcState,
fcError,
async (queryState, queryError, subState, subError) => {
const query = createMockQuery(queryState, queryError);
const sub = createMockSubscription(subState, subError);
vi.mocked(useCtrlStateQuery).mockReturnValue(query);
vi.mocked(useCtrlStateSSubscription).mockReturnValue(sub);
const { state, error } = await useSubscribeState();
const expectedError = subError || queryError;
const expectedState = expectedError
? undefined
: subState || queryState || undefined;
expect(error.value).toBe(expectedError);
expect(state.value).toBe(expectedState);
},
),
);
});
});