|
| 1 | +import { createLoadingStore } from "../../../src/ts/signals/util/loadingStore"; |
| 2 | +import { vi, describe, it, expect, beforeEach } from "vitest"; |
| 3 | + |
| 4 | +const mockFetcher = vi.fn(); |
| 5 | +const initialValue = vi.fn(() => ({ data: null })); |
| 6 | + |
| 7 | +describe("createLoadingStore", () => { |
| 8 | + beforeEach(() => { |
| 9 | + mockFetcher.mockClear(); |
| 10 | + initialValue.mockClear(); |
| 11 | + }); |
| 12 | + |
| 13 | + it("should initialize with the correct state", () => { |
| 14 | + const store = createLoadingStore(mockFetcher, initialValue); |
| 15 | + |
| 16 | + expect(store.state().state).toBe("unresolved"); |
| 17 | + expect(store.state().loading).toBe(false); |
| 18 | + expect(store.state().ready).toBe(false); |
| 19 | + expect(store.state().refreshing).toBe(false); |
| 20 | + expect(store.state().error).toBeUndefined(); |
| 21 | + expect(store.store).toEqual({ data: null }); |
| 22 | + }); |
| 23 | + |
| 24 | + it("should transition to loading when load is called", async () => { |
| 25 | + const store = createLoadingStore(mockFetcher, initialValue); |
| 26 | + store.load(); |
| 27 | + |
| 28 | + expect(store.state().state).toBe("pending"); |
| 29 | + expect(store.state().loading).toBe(true); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should call the fetcher when load is called", async () => { |
| 33 | + const store = createLoadingStore(mockFetcher, initialValue); |
| 34 | + mockFetcher.mockResolvedValueOnce({ data: "test" }); |
| 35 | + store.load(); |
| 36 | + |
| 37 | + await store.ready(); |
| 38 | + |
| 39 | + expect(mockFetcher).toHaveBeenCalledTimes(1); |
| 40 | + expect(store.state().state).toBe("ready"); |
| 41 | + expect(store.store).toEqual({ data: "test" }); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should handle error when fetcher fails", async () => { |
| 45 | + mockFetcher.mockRejectedValueOnce(new Error("Failed to load")); |
| 46 | + const store = createLoadingStore(mockFetcher, initialValue); |
| 47 | + |
| 48 | + store.load(); |
| 49 | + |
| 50 | + await expect(store.ready()).rejects.toThrow("Failed to load"); |
| 51 | + |
| 52 | + expect(store.state().state).toBe("errored"); |
| 53 | + expect(store.state().error).toEqual(new Error("Failed to load")); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should transition to refreshing state on refresh", async () => { |
| 57 | + const store = createLoadingStore(mockFetcher, initialValue); |
| 58 | + mockFetcher.mockResolvedValueOnce({ data: "test" }); |
| 59 | + store.load(); |
| 60 | + |
| 61 | + store.refresh(); // trigger refresh |
| 62 | + expect(store.state().state).toBe("refreshing"); |
| 63 | + expect(store.state().refreshing).toBe(true); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should trigger load when refresh is called and shouldLoad is false", async () => { |
| 67 | + const store = createLoadingStore(mockFetcher, initialValue); |
| 68 | + mockFetcher.mockResolvedValueOnce({ data: "test" }); |
| 69 | + expect(store.state().state).toBe("unresolved"); |
| 70 | + |
| 71 | + store.refresh(); |
| 72 | + expect(store.state().state).toBe("refreshing"); |
| 73 | + expect(store.state().refreshing).toBe(true); |
| 74 | + |
| 75 | + // Wait for the store to be ready after fetching |
| 76 | + await store.ready(); |
| 77 | + |
| 78 | + // Ensure the store's state is 'ready' after the refresh |
| 79 | + expect(store.state().state).toBe("ready"); |
| 80 | + expect(store.store).toEqual({ data: "test" }); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should reset the store to its initial value on reset", async () => { |
| 84 | + const store = createLoadingStore(mockFetcher, initialValue); |
| 85 | + mockFetcher.mockResolvedValueOnce({ data: "test" }); |
| 86 | + store.load(); |
| 87 | + |
| 88 | + await store.ready(); |
| 89 | + |
| 90 | + expect(store.store).toEqual({ data: "test" }); |
| 91 | + |
| 92 | + store.reset(); |
| 93 | + expect(store.state().state).toBe("unresolved"); |
| 94 | + expect(store.state().loading).toBe(false); |
| 95 | + expect(store.store).toEqual({ data: null }); |
| 96 | + }); |
| 97 | + |
| 98 | + it("should handle a promise rejection during reset", async () => { |
| 99 | + const store = createLoadingStore(mockFetcher, initialValue); |
| 100 | + |
| 101 | + // Mock the fetcher to resolve with data |
| 102 | + mockFetcher.mockResolvedValueOnce({ data: "test" }); |
| 103 | + |
| 104 | + // Trigger loading the store |
| 105 | + store.load(); |
| 106 | + |
| 107 | + // Wait for the store to be ready |
| 108 | + await store.ready(); |
| 109 | + |
| 110 | + // Ensure the store state after loading |
| 111 | + expect(store.state().state).toBe("ready"); |
| 112 | + expect(store.store).toEqual({ data: "test" }); |
| 113 | + |
| 114 | + // Now call reset, which should reject the ready promise |
| 115 | + const readyPromise = store.ready(); // Grab the current ready promise |
| 116 | + |
| 117 | + store.reset(); // Call reset, which should reject the promise |
| 118 | + |
| 119 | + // Ensure the promise rejects as expected |
| 120 | + await expect(readyPromise).rejects.toThrow("Reset"); |
| 121 | + |
| 122 | + // Ensure the state is reset |
| 123 | + expect(store.state().state).toBe("unresolved"); |
| 124 | + expect(store.state().loading).toBe(false); |
| 125 | + expect(store.store).toEqual({ data: null }); |
| 126 | + }); |
| 127 | +}); |
0 commit comments