Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 LIVE_REFRESH_IDLE_AFTER_MS,
5 LIVE_REFRESH_INTERVAL_MS,
6 LIVE_REFRESH_MIN_INTERVAL_MS,
7 shouldLiveRefresh,
8 shouldRefreshOnArrival,
9 shouldRefreshOnInterval,
10} from "./useLiveRefresh";
11
12describe("shouldLiveRefresh", () => {
13 const at = (now: number, lastRefreshedAt: number, visible = true) =>
14 shouldLiveRefresh({ visible, now, lastRefreshedAt });
15
16 it("reads a view again when it is navigated to", () => {
17 expect(at(LIVE_REFRESH_MIN_INTERVAL_MS, 0)).toBe(true);
18 });
19
20 it("does not read a view again that was left and returned to seconds later", () => {
21 expect(at(3_000, 0)).toBe(false);
22 });
23
24 it("reads again when the interval comes round on a view left open", () => {
25 expect(at(LIVE_REFRESH_INTERVAL_MS, 0)).toBe(true);
26 });
27
28 it("does not read again for every window tabbed through", () => {
29 expect(at(1_000, 0)).toBe(false);
30 });
31
32 it("stays quiet while the window is not showing", () => {
33 expect(at(LIVE_REFRESH_MIN_INTERVAL_MS * 5, 0, false)).toBe(false);
34 });
35
36 it("reads once for a window hidden an hour, not once per interval it missed", () => {
37 const hour = 60 * 60_000;
38 let lastRefreshedAt = 0;
39 let reads = 0;
40 const tick = (now: number, visible: boolean) => {
41 if (!at(now, lastRefreshedAt, visible)) return;
42 lastRefreshedAt = now;
43 reads += 1;
44 };
45
46 for (let now = LIVE_REFRESH_INTERVAL_MS; now < hour; now += LIVE_REFRESH_INTERVAL_MS) {
47 tick(now, false);
48 }
49 tick(hour, true);
50 tick(hour, true);
51
52 expect(reads).toBe(1);
53 });
54});
55
56describe("shouldRefreshOnArrival", () => {
57 it("leaves a view alone the first time it is opened, because it is already reading", () => {
58 expect(shouldRefreshOnArrival({ visible: true, now: 5_000, lastRefreshedAt: undefined })).toBe(
59 false,
60 );
61 });
62
63 it("reads a view that was read earlier in the session and returned to", () => {
64 expect(shouldRefreshOnArrival({ visible: true, now: 90_000, lastRefreshedAt: 0 })).toBe(true);
65 });
66
67 it("keeps the minimum interval on a view returned to straight away", () => {
68 expect(shouldRefreshOnArrival({ visible: true, now: 2_000, lastRefreshedAt: 0 })).toBe(false);
69 });
70});
71
72describe("shouldRefreshOnInterval", () => {
73 const tick = (now: number, lastInteractedAt: number) =>
74 shouldRefreshOnInterval({ visible: true, now, lastRefreshedAt: 0, lastInteractedAt });
75
76 it("reads for a reader who is here", () => {
77 expect(tick(LIVE_REFRESH_INTERVAL_MS, LIVE_REFRESH_INTERVAL_MS - 1_000)).toBe(true);
78 });
79
80 it("reads on the first interval after an untouched mount", () => {
81 expect(tick(LIVE_REFRESH_INTERVAL_MS + 1_000, 0)).toBe(true);
82 });
83
84 it("stops reading for a window left showing on a desk nobody is at", () => {
85 expect(tick(LIVE_REFRESH_IDLE_AFTER_MS + 60_000, 0)).toBe(false);
86 });
87
88 it("starts reading again once the reader touches the window", () => {
89 const away = LIVE_REFRESH_IDLE_AFTER_MS + 60_000;
90 expect(tick(away + LIVE_REFRESH_MIN_INTERVAL_MS, away)).toBe(true);
91 });
92});