|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +import { selectTimelineLoadingState } from "./timelineLoadingState.ts"; |
| 5 | + |
| 6 | +const settled = { |
| 7 | + isPending: false, |
| 8 | + isFetching: false, |
| 9 | + isPlaceholderData: false, |
| 10 | + dataLength: null, |
| 11 | +}; |
| 12 | + |
| 13 | +test("pending first fetch with no cache is loading", () => { |
| 14 | + assert.equal( |
| 15 | + selectTimelineLoadingState({ ...settled, isPending: true }), |
| 16 | + true, |
| 17 | + ); |
| 18 | +}); |
| 19 | + |
| 20 | +test("stale placeholder while refetching is loading", () => { |
| 21 | + // Revisited within gcTime: placeholderData hands back a cached array while the |
| 22 | + // authoritative fetch runs. Must keep the skeleton up, not flash the intro. |
| 23 | + assert.equal( |
| 24 | + selectTimelineLoadingState({ |
| 25 | + ...settled, |
| 26 | + isFetching: true, |
| 27 | + isPlaceholderData: true, |
| 28 | + dataLength: 0, |
| 29 | + }), |
| 30 | + true, |
| 31 | + ); |
| 32 | +}); |
| 33 | + |
| 34 | +test("subscription-seeded empty cache while fetching is loading", () => { |
| 35 | + // The live subscription's setQueryData seeds [] before history settles, so |
| 36 | + // data is defined but empty and a fetch is still in flight. |
| 37 | + assert.equal( |
| 38 | + selectTimelineLoadingState({ |
| 39 | + ...settled, |
| 40 | + isFetching: true, |
| 41 | + isPlaceholderData: false, |
| 42 | + dataLength: 0, |
| 43 | + }), |
| 44 | + true, |
| 45 | + ); |
| 46 | +}); |
| 47 | + |
| 48 | +test("settled with rows is not loading", () => { |
| 49 | + assert.equal( |
| 50 | + selectTimelineLoadingState({ ...settled, dataLength: 5 }), |
| 51 | + false, |
| 52 | + ); |
| 53 | +}); |
| 54 | + |
| 55 | +test("settled and genuinely empty is not loading (real empty channel)", () => { |
| 56 | + assert.equal( |
| 57 | + selectTimelineLoadingState({ ...settled, dataLength: 0 }), |
| 58 | + false, |
| 59 | + ); |
| 60 | +}); |
| 61 | + |
| 62 | +test("background refetch of a populated channel is not loading", () => { |
| 63 | + // staleTime expiry can trigger a background refetch; with rows already present |
| 64 | + // we are loaded and must not re-show the skeleton. |
| 65 | + assert.equal( |
| 66 | + selectTimelineLoadingState({ |
| 67 | + ...settled, |
| 68 | + isFetching: true, |
| 69 | + dataLength: 12, |
| 70 | + }), |
| 71 | + false, |
| 72 | + ); |
| 73 | +}); |
| 74 | + |
| 75 | +import { resolveTimelineLoadingLatch } from "./timelineLoadingState.ts"; |
| 76 | + |
| 77 | +test("latch: loading on first entry to a channel", () => { |
| 78 | + const r = resolveTimelineLoadingLatch(null, "chan-a", true); |
| 79 | + assert.equal(r.isLoading, true); |
| 80 | + assert.equal(r.settledChannelId, null); |
| 81 | +}); |
| 82 | + |
| 83 | +test("latch: settles when loadingNow turns false, recording the channel", () => { |
| 84 | + const r = resolveTimelineLoadingLatch(null, "chan-a", false); |
| 85 | + assert.equal(r.isLoading, false); |
| 86 | + assert.equal(r.settledChannelId, "chan-a"); |
| 87 | +}); |
| 88 | + |
| 89 | +test("latch: background refetch blip stays loaded once settled", () => { |
| 90 | + // settled for chan-a, then loadingNow blips true (isFetching) — must NOT |
| 91 | + // re-show the skeleton (the bounce Wes reported). |
| 92 | + const r = resolveTimelineLoadingLatch("chan-a", "chan-a", true); |
| 93 | + assert.equal(r.isLoading, false); |
| 94 | + assert.equal(r.settledChannelId, "chan-a"); |
| 95 | +}); |
| 96 | + |
| 97 | +test("latch: switching channels resets and loads the new one", () => { |
| 98 | + const r = resolveTimelineLoadingLatch("chan-a", "chan-b", true); |
| 99 | + assert.equal(r.isLoading, true); |
| 100 | + assert.equal(r.settledChannelId, "chan-a"); // not yet settled for b |
| 101 | +}); |
| 102 | + |
| 103 | +test("latch: no active channel passes loadingNow through untouched", () => { |
| 104 | + assert.equal( |
| 105 | + resolveTimelineLoadingLatch("chan-a", null, true).isLoading, |
| 106 | + true, |
| 107 | + ); |
| 108 | + assert.equal( |
| 109 | + resolveTimelineLoadingLatch("chan-a", null, false).isLoading, |
| 110 | + false, |
| 111 | + ); |
| 112 | +}); |
0 commit comments