-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfc.ts
More file actions
53 lines (46 loc) · 1.54 KB
/
fc.ts
File metadata and controls
53 lines (46 loc) · 1.54 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
import { expect, it } from "vitest";
import fc from "fast-check";
export const fcUndefinedOr = <T>(arb: fc.Arbitrary<T>) =>
fc.oneof(fc.constant(undefined), arb);
const fcError = fc.string().map((msg) => new Error(msg));
export const fcMockUseQueryResponseArg = <T>(arbData: fc.Arbitrary<T>) =>
fc.record({
data: fcUndefinedOr(arbData),
error: fcUndefinedOr(fcError),
});
export const fcMockUseSubscriptionResponseArg = <T>(arbData: fc.Arbitrary<T>) =>
fc.array(fcMockUseQueryResponseArg(arbData));
if (import.meta.vitest) {
it("fcUndefinedOr()", () => {
fc.assert(
fc.property(fcUndefinedOr(fc.integer()), (v) => {
expect(v === undefined || typeof v === "number").toBe(true);
}),
);
});
it("fcMockUseQueryResponse()", () => {
const fcData = fc.record({ a: fc.integer() });
const fcArg = fcMockUseQueryResponseArg(fcData);
fc.assert(
fc.property(fcArg, (v) => {
expect(v.data === undefined || typeof v.data.a === "number").toBe(true);
expect(v.error === undefined || v.error instanceof Error).toBe(true);
}),
);
});
it("fcMockUseSubscriptionResponse()", () => {
const fcData = fc.record({ a: fc.integer() });
const fcArg = fcMockUseSubscriptionResponseArg(fcData);
fc.assert(
fc.property(fcArg, (v) => {
expect(
v.every(
(e) =>
(e.data === undefined || typeof e.data.a === "number") &&
(e.error === undefined || e.error instanceof Error),
),
).toBe(true);
}),
);
});
}