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("live refresh cadence", () => {
13 it("waits five minutes between automatic host reads", () => {
14 expect(LIVE_REFRESH_INTERVAL_MS).toBe(5 * 60_000);
15 });
16});
17
18describe("shouldLiveRefresh", () => {
19 const at = (now: number, lastRefreshedAt: number, visible = true) =>
20 shouldLiveRefresh({ visible, now, lastRefreshedAt });
21
22 it("reads a view again when it is navigated to", () => {
23 expect(at(LIVE_REFRESH_MIN_INTERVAL_MS, 0)).toBe(true);
24 });
25
26 it("does not read a view again that was left and returned to seconds later", () => {
27 expect(at(3_000, 0)).toBe(false);
28 });
29
30 it("reads again when the interval comes round on a view left open", () => {
31 expect(at(LIVE_REFRESH_INTERVAL_MS, 0)).toBe(true);
32 });
33
34 it("does not read again for every window tabbed through", () => {
35 expect(at(1_000, 0)).toBe(false);
36 });
37
38 it("stays quiet while the window is not showing", () => {
39 // A focus event can arrive for a window that is still hidden behind another one.
40 expect(at(LIVE_REFRESH_MIN_INTERVAL_MS * 5, 0, false)).toBe(false);
41 });
42
43 it("reads once for a window hidden an hour, not once per interval it missed", () => {
44 const hour = 60 * 60_000;
45 let lastRefreshedAt = 0;
46 let reads = 0;
47 const tick = (now: number, visible: boolean) => {
48 if (!at(now, lastRefreshedAt, visible)) return;
49 lastRefreshedAt = now;
50 reads += 1;
51 };
52
53 for (let now = LIVE_REFRESH_INTERVAL_MS; now < hour; now += LIVE_REFRESH_INTERVAL_MS) {
54 tick(now, false);
55 }
56 // Coming back raises a visibility change and a focus event, one straight after the other.
57 tick(hour, true);
58 tick(hour, true);
59
60 expect(reads).toBe(1);
61 });
62});
63
64describe("shouldRefreshOnArrival", () => {
65 it("leaves a view alone the first time it is opened, because it is already reading", () => {
66 expect(shouldRefreshOnArrival({ visible: true, now: 5_000, lastRefreshedAt: undefined })).toBe(
67 false,
68 );
69 });
70
71 it("reads a view that was read earlier in the session and returned to", () => {
72 expect(shouldRefreshOnArrival({ visible: true, now: 90_000, lastRefreshedAt: 0 })).toBe(true);
73 });
74
75 it("keeps the minimum interval on a view returned to straight away", () => {
76 expect(shouldRefreshOnArrival({ visible: true, now: 2_000, lastRefreshedAt: 0 })).toBe(false);
77 });
78});
79
80describe("shouldRefreshOnInterval", () => {
81 const tick = (now: number, lastInteractedAt: number) =>
82 shouldRefreshOnInterval({ visible: true, now, lastRefreshedAt: 0, lastInteractedAt });
83
84 it("reads for a reader who is here", () => {
85 expect(tick(LIVE_REFRESH_INTERVAL_MS, LIVE_REFRESH_INTERVAL_MS - 1_000)).toBe(true);
86 });
87
88 it("reads on the first interval after an untouched mount", () => {
89 expect(tick(LIVE_REFRESH_INTERVAL_MS + 1_000, 0)).toBe(true);
90 });
91
92 it("stops reading for a window left showing on a desk nobody is at", () => {
93 expect(tick(LIVE_REFRESH_IDLE_AFTER_MS + 60_000, 0)).toBe(false);
94 });
95
96 it("starts reading again once the reader touches the window", () => {
97 const away = LIVE_REFRESH_IDLE_AFTER_MS + 60_000;
98 expect(tick(away + LIVE_REFRESH_MIN_INTERVAL_MS, away)).toBe(true);
99 });
100});