|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { ref } from "vue"; |
| 3 | +import fc from "fast-check"; |
| 4 | + |
| 5 | +import { useSubscribeScheduleAutoModeState } from "@/api"; |
| 6 | +import { onReady } from "@/utils/on-ready"; |
| 7 | + |
| 8 | +import { usePulling } from "../use-pulling"; |
| 9 | + |
| 10 | +vi.mock("@/api", () => ({ |
| 11 | + useSubscribeScheduleAutoModeState: vi.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +const AUTO_MODE_STATES = [ |
| 15 | + "created", |
| 16 | + "off", |
| 17 | + "auto_waiting", |
| 18 | + "auto_pulling", |
| 19 | + "auto_running", |
| 20 | +]; |
| 21 | + |
| 22 | +const fcAutoModeState = () => fc.constantFrom(...AUTO_MODE_STATES); |
| 23 | + |
| 24 | +describe("scratch", () => { |
| 25 | + beforeEach(() => { |
| 26 | + vi.clearAllMocks(); |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(() => { |
| 30 | + vi.resetAllMocks(); |
| 31 | + }); |
| 32 | + |
| 33 | + it("Property test", async () => { |
| 34 | + await fc.assert( |
| 35 | + fc.asyncProperty(fcAutoModeState(), async (auto_mode_state) => { |
| 36 | + // Initially `undefined` |
| 37 | + const autoModeState = ref(undefined as string | undefined); |
| 38 | + |
| 39 | + // A value set when ready |
| 40 | + const ready = (async () => { |
| 41 | + await Promise.resolve(); |
| 42 | + autoModeState.value = auto_mode_state; |
| 43 | + })(); |
| 44 | + |
| 45 | + type Sub = ReturnType<typeof useSubscribeScheduleAutoModeState>; |
| 46 | + |
| 47 | + // Thenable |
| 48 | + const sub = onReady({ autoModeState }, ready) as unknown as Sub; |
| 49 | + |
| 50 | + vi.mocked(useSubscribeScheduleAutoModeState).mockReturnValue(sub); |
| 51 | + |
| 52 | + const { pulling } = await usePulling(); |
| 53 | + const expected = auto_mode_state === "auto_pulling"; |
| 54 | + expect(pulling.value).toBe(expected); |
| 55 | + }), |
| 56 | + ); |
| 57 | + }); |
| 58 | +}); |
0 commit comments