-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuse-pulling.spec.ts
More file actions
60 lines (48 loc) · 1.51 KB
/
use-pulling.spec.ts
File metadata and controls
60 lines (48 loc) · 1.51 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
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { ref } from "vue";
import fc from "fast-check";
import { useSubscribeScheduleAutoModeState } from "@/api";
import { onReady } from "@/utils/on-ready";
import { usePulling } from "../use-pulling";
vi.mock("@/api", () => ({
useSubscribeScheduleAutoModeState: vi.fn(),
}));
const AUTO_MODE_STATES = [
"created",
"off",
"auto_waiting",
"auto_pulling",
"auto_running",
];
const fcAutoModeState = () => fc.constantFrom(...AUTO_MODE_STATES);
type Sub = ReturnType<typeof useSubscribeScheduleAutoModeState>;
function createMockSubscription(auto_mode_state: string): Sub {
// Initially `undefined`
const autoModeState = ref(undefined as string | undefined);
// A value set when ready
const ready = (async () => {
await Promise.resolve();
autoModeState.value = auto_mode_state;
})();
// Thenable
return onReady({ autoModeState }, ready) as Sub;
}
describe("usePulling()", () => {
beforeEach(() => {
vi.clearAllMocks();
});
afterEach(() => {
vi.resetAllMocks();
});
it("Property test", async () => {
await fc.assert(
fc.asyncProperty(fcAutoModeState(), async (auto_mode_state) => {
const sub = createMockSubscription(auto_mode_state);
vi.mocked(useSubscribeScheduleAutoModeState).mockReturnValue(sub);
const { pulling } = await usePulling();
const expected = auto_mode_state === "auto_pulling";
expect(pulling.value).toBe(expected);
}),
);
});
});