Skip to content

Commit 0ceb857

Browse files
committed
Clean code
1 parent bc0dab0 commit 0ceb857

File tree

1 file changed

+27
-65
lines changed

1 file changed

+27
-65
lines changed

src/api/__tests__/use-state-subscription.spec.ts

Lines changed: 27 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2-
import type { UseQueryResponse } from "@urql/vue";
32
import fc from "fast-check";
43

54
import type {
65
CtrlStateQuery,
7-
CtrlStateQueryVariables,
86
CtrlStateSSubscription,
97
} from "@/graphql/codegen/generated";
108
import {
@@ -23,63 +21,17 @@ vi.mock("@/graphql/codegen/generated", () => ({
2321
}));
2422

2523
const fcState = fc.string({ minLength: 1 });
24+
const fcQueryData = fc.oneof(
25+
fc.constant(undefined),
26+
fc.record({ ctrl: fc.record({ state: fcState }) }),
27+
);
28+
const fcSubData = fc.oneof(fc.constant(undefined), fc.record({ ctrlState: fcState }));
2629

2730
const fcErrorInstance = fc.string().map((msg) => new Error(msg));
2831
const fcError = fc.oneof(fc.constant(undefined), fcErrorInstance);
2932

30-
interface QRes {
31-
data: { ctrl: { state: string } } | undefined;
32-
error: Error | undefined;
33-
}
34-
35-
const fcQRes: fc.Arbitrary<QRes> = fc.record({
36-
data: fc.oneof(
37-
fc.constant(undefined),
38-
fc.record({
39-
ctrl: fc.record({
40-
state: fcState,
41-
}),
42-
}),
43-
),
44-
error: fcError,
45-
});
46-
47-
interface SRes {
48-
data: { ctrlState: string } | undefined;
49-
error: Error | undefined;
50-
}
51-
52-
const fcSRes: fc.Arbitrary<SRes> = fc.record({
53-
data: fc.oneof(
54-
fc.constant(undefined),
55-
fc.record({
56-
ctrlState: fcState,
57-
}),
58-
),
59-
error: fcError,
60-
});
61-
62-
// type Query = ReturnType<typeof useCtrlStateQuery>;
63-
type Query = UseQueryResponse<CtrlStateQuery, CtrlStateQueryVariables>;
64-
65-
type Sub = ReturnType<typeof useCtrlStateSSubscription>;
66-
67-
function mockUseCtrlStateQueryResponse(res: QRes): Query {
68-
return mockUseQueryResponse<CtrlStateQuery>(res) as Query;
69-
}
70-
71-
interface MockSubscription {
72-
response: Sub;
73-
issue: Iterable<SRes>;
74-
}
75-
76-
function mockUseCtrlStateSSubscriptionResponse(
77-
resArray: Iterable<SRes>,
78-
): MockSubscription {
79-
return mockUseSubscriptionResponse<CtrlStateSSubscription>(
80-
resArray,
81-
) as MockSubscription;
82-
}
33+
const fcQueryArg = fc.record({ data: fcQueryData, error: fcError });
34+
const fcSubArg = fc.record({ data: fcSubData, error: fcError });
8335

8436
describe("useSubscribeState()", () => {
8537
beforeEach(() => {
@@ -92,22 +44,32 @@ describe("useSubscribeState()", () => {
9244

9345
it("Property test", async () => {
9446
await fc.assert(
95-
fc.asyncProperty(fcQRes, fc.array(fcSRes), async (queryRes, subResArray) => {
96-
const query = mockUseCtrlStateQueryResponse(queryRes);
97-
const { response: sub, issue } =
98-
mockUseCtrlStateSSubscriptionResponse(subResArray);
99-
vi.mocked(useCtrlStateQuery).mockReturnValue(query);
100-
vi.mocked(useCtrlStateSSubscription).mockReturnValue(sub);
47+
fc.asyncProperty(fcQueryArg, fc.array(fcSubArg), async (queryArg, subArg) => {
48+
// Mock useCtrlStateQuery()
49+
const queryRes = mockUseQueryResponse<CtrlStateQuery>(queryArg);
50+
type Query = ReturnType<typeof useCtrlStateQuery>;
51+
vi.mocked(useCtrlStateQuery).mockReturnValue(queryRes as Query);
52+
53+
// Mock useCtrlStateSSubscription()
54+
const { response: subRes, issue } =
55+
mockUseSubscriptionResponse<CtrlStateSSubscription>(subArg);
56+
type SubRes = ReturnType<typeof useCtrlStateSSubscription>;
57+
vi.mocked(useCtrlStateSSubscription).mockReturnValue(subRes as SubRes);
58+
10159
const { state, error } = await useSubscribeState();
102-
expect(error.value).toBe(queryRes.error);
60+
61+
// Assert initial values are from query.
62+
expect(error.value).toBe(queryArg.error);
10363
expect(state.value).toBe(
104-
queryRes.error ? undefined : queryRes.data?.ctrl.state,
64+
queryArg.error ? undefined : queryArg.data?.ctrl.state,
10565
);
66+
67+
// Assert the subsequent values are issued from subscription backed up by query.
10668
for (const issued of issue) {
107-
const expectedError = issued.error || queryRes.error;
69+
const expectedError = issued.error || queryArg.error;
10870
const expectedState = expectedError
10971
? undefined
110-
: issued.data?.ctrlState || queryRes.data?.ctrl.state;
72+
: issued.data?.ctrlState || queryArg.data?.ctrl.state;
11173
expect(error.value).toBe(expectedError);
11274
expect(state.value).toBe(expectedState);
11375
}

0 commit comments

Comments
 (0)