Skip to content

Commit 6e6a1f7

Browse files
committed
Update use-state-subscription.spec.ts
1 parent 68d5edb commit 6e6a1f7

File tree

1 file changed

+65
-87
lines changed

1 file changed

+65
-87
lines changed
Lines changed: 65 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2+
import { ref } from "vue";
3+
import fc from "fast-check";
24

35
import {
46
useCtrlStateQuery,
57
useCtrlStateSSubscription,
68
} from "@/graphql/codegen/generated";
9+
import type { CtrlStateSSubscription } from "@/graphql/codegen/generated";
10+
import { onReady } from "@/utils/on-ready";
711

812
import { useSubscribeState } from "../use-state-subscription";
913

@@ -12,104 +16,78 @@ vi.mock("@/graphql/codegen/generated", () => ({
1216
useCtrlStateSSubscription: vi.fn(),
1317
}));
1418

15-
describe("useSubscribeState", () => {
16-
beforeEach(() => {
17-
vi.clearAllMocks();
18-
});
19+
const fcState = fc.oneof(fc.constant(undefined), fc.string({ minLength: 1 }));
1920

20-
afterEach(() => {
21-
vi.resetAllMocks();
22-
});
21+
const fcErrorInstance = fc.string().map((msg) => new Error(msg));
22+
const fcError = fc.oneof(fc.constant(undefined), fcErrorInstance);
2323

24-
it("fetches initial state successfully from the query", () => {
25-
vi.mocked(useCtrlStateQuery).mockReturnValue({
26-
data: { value: { ctrl: { state: "initialized" } } },
27-
error: { value: undefined },
28-
} as any);
29-
vi.mocked(useCtrlStateSSubscription).mockReturnValue({
30-
data: { value: undefined },
31-
error: { value: undefined },
32-
} as any);
33-
34-
const { state } = useSubscribeState();
35-
expect(state.value).toBe("initialized");
36-
});
24+
type Query = ReturnType<typeof useCtrlStateQuery>;
25+
type Sub = ReturnType<typeof useCtrlStateSSubscription>;
3726

38-
it("updates state from subscription", () => {
39-
const queryMock = vi.mocked(useCtrlStateQuery);
40-
const subscriptionMock = vi.mocked(useCtrlStateSSubscription);
27+
function createMockQuery(
28+
state_value: string | undefined,
29+
error_value: Error | undefined,
30+
): Query {
31+
type Data = NonNullable<Query["data"]["value"]>;
32+
const data = ref<Data | undefined>(undefined);
33+
const error = ref<Error | undefined>(undefined);
4134

42-
queryMock.mockReturnValue({
43-
data: { value: { ctrl: { state: "initial" } } },
44-
error: { value: undefined },
45-
} as any);
35+
const ready = (async () => {
36+
await Promise.resolve();
37+
data.value = { ctrl: { state: state_value } } as Data;
38+
error.value = error_value;
39+
})();
4640

47-
subscriptionMock.mockReturnValue({
48-
data: { value: { ctrlState: "updated" } },
49-
error: { value: undefined },
50-
} as any);
41+
return onReady({ data, error }, ready) as Query;
42+
}
5143

52-
const { state } = useSubscribeState();
53-
expect(state.value).toBe("updated");
54-
});
44+
function createMockSubscription(
45+
state_value: string | undefined,
46+
error_value: Error | undefined,
47+
): Sub {
48+
const data = ref<CtrlStateSSubscription | undefined>(undefined);
49+
const error = ref<Error | undefined>(undefined);
5550

56-
it("handles query error", () => {
57-
vi.mocked(useCtrlStateQuery).mockReturnValue({
58-
data: { value: undefined },
59-
error: { value: new Error("Query error") },
60-
} as any);
61-
vi.mocked(useCtrlStateSSubscription).mockReturnValue({
62-
data: { value: undefined },
63-
error: { value: undefined },
64-
} as any);
65-
66-
const { state, error } = useSubscribeState();
67-
expect(state.value).toBeUndefined();
68-
expect(error.value).toEqual(new Error("Query error"));
69-
});
51+
const ready = (async () => {
52+
await Promise.resolve();
53+
data.value = { ctrlState: state_value } as CtrlStateSSubscription;
54+
error.value = error_value;
55+
})();
7056

71-
it("handles subscription error", () => {
72-
vi.mocked(useCtrlStateQuery).mockReturnValue({
73-
data: { value: { ctrl: { state: "initial" } } },
74-
error: { value: undefined },
75-
} as any);
76-
vi.mocked(useCtrlStateSSubscription).mockReturnValue({
77-
data: { value: undefined },
78-
error: { value: new Error("Subscription error") },
79-
} as any);
80-
81-
const { state, error } = useSubscribeState();
82-
expect(state.value).toBeUndefined();
83-
expect(error.value).toEqual(new Error("Subscription error"));
57+
return onReady({ data, error }, ready) as unknown as Sub;
58+
}
59+
60+
describe("useSubscribeState()", () => {
61+
beforeEach(() => {
62+
vi.clearAllMocks();
8463
});
8564

86-
it("can be used as a promise", async () => {
87-
vi.mocked(useCtrlStateQuery).mockReturnValue({
88-
data: { value: { ctrl: { state: "promised" } } },
89-
error: { value: undefined },
90-
then: (resolve: (value: any) => void) =>
91-
resolve({ data: { value: { ctrl: { state: "promised" } } } }),
92-
} as any);
93-
vi.mocked(useCtrlStateSSubscription).mockReturnValue({
94-
data: { value: undefined },
95-
error: { value: undefined },
96-
} as any);
97-
98-
const result = await useSubscribeState();
99-
expect(result.state.value).toBe("promised");
65+
afterEach(() => {
66+
vi.resetAllMocks();
10067
});
10168

102-
it("handles empty responses", () => {
103-
vi.mocked(useCtrlStateQuery).mockReturnValue({
104-
data: { value: null },
105-
error: { value: undefined },
106-
} as any);
107-
vi.mocked(useCtrlStateSSubscription).mockReturnValue({
108-
data: { value: null },
109-
error: { value: undefined },
110-
} as any);
111-
112-
const { state } = useSubscribeState();
113-
expect(state.value).toBeUndefined();
69+
it("Property test", async () => {
70+
await fc.assert(
71+
fc.asyncProperty(
72+
fcState,
73+
fcError,
74+
fcState,
75+
fcError,
76+
async (queryState, queryError, subState, subError) => {
77+
const query = createMockQuery(queryState, queryError);
78+
const sub = createMockSubscription(subState, subError);
79+
vi.mocked(useCtrlStateQuery).mockReturnValue(query);
80+
vi.mocked(useCtrlStateSSubscription).mockReturnValue(sub);
81+
const { state, error } = await useSubscribeState();
82+
const expectedError = subError || queryError;
83+
const expectedState = expectedError
84+
? undefined
85+
: subState || queryState || undefined;
86+
87+
expect(error.value).toBe(expectedError);
88+
expect(state.value).toBe(expectedState);
89+
},
90+
),
91+
);
11492
});
11593
});

0 commit comments

Comments
 (0)