-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuse-mapped-with-fallback.spec.ts
More file actions
55 lines (44 loc) · 2.14 KB
/
use-mapped-with-fallback.spec.ts
File metadata and controls
55 lines (44 loc) · 2.14 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
import { describe, expect, it } from "vitest";
import fc from "fast-check";
import { mockUseQueryResponse } from "@/graphql/tests";
import { mockUseSubscriptionResponse } from "@/graphql/tests/mock-use-subscription-response";
import { useMappedWithFallback } from "../use-mapped-with-fallback";
type QueryData = { ctrl: { state: string } };
type SubscriptionData = { ctrlState: string };
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("useMappedWithFallback()", () => {
it("Property test", async () => {
await fc.assert(
fc.asyncProperty(fcQueryArg, fc.array(fcSubArg), async (queryArg, subArg) => {
const { response: response1, issue } =
mockUseSubscriptionResponse<SubscriptionData>(subArg);
const response2 = await mockUseQueryResponse<QueryData>(queryArg);
const map1 = (d: typeof response1.data) => d.value?.ctrlState;
const map2 = (d: typeof response2.data) => d.value?.ctrl.state;
const options = { response1, response2, map1, map2 };
const { data, error } = useMappedWithFallback(options);
// Assert initial values are from query.
expect(error.value).toBe(queryArg.error);
expect(data.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(data.value).toBe(expectedState);
}
}),
);
});
});