Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useLiveRefresh.test.ts

90 lines3.1 KB

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 // A focus event can arrive for a window that is still hidden behind another one.
34 expect(at(LIVE_REFRESH_MIN_INTERVAL_MS * 5, 0, false)).toBe(false);
35 });
36​
37 it("reads once for a window hidden an hour, not once per interval it missed", () => {
38 const hour = 60 * 60_000;
39 let lastRefreshedAt = 0;
40 let reads = 0;
41 const tick = (now: number, visible: boolean) => {
42 if (!at(now, lastRefreshedAt, visible)) return;
43 lastRefreshedAt = now;
44 reads += 1;
45 };
46​
47 for (let now = LIVE_REFRESH_INTERVAL_MS; now < hour; now += LIVE_REFRESH_INTERVAL_MS) {
48 tick(now, false);
49 }
50 // Coming back raises a visibility change and a focus event, one straight after the other.
51 tick(hour, true);
52 tick(hour, true);
53​
54 expect(reads).toBe(1);
55 });
56});
57​
58describe("shouldRefreshOnArrival", () => {
59 it("leaves a view alone the first time it is opened, because it is already reading", () => {
60 expect(shouldRefreshOnArrival({ visible: true, now: 5_000, lastRefreshedAt: undefined })).toBe(
61 false,
62 );
63 });
64​
65 it("reads a view that was read earlier in the session and returned to", () => {
66 expect(shouldRefreshOnArrival({ visible: true, now: 90_000, lastRefreshedAt: 0 })).toBe(true);
67 });
68​
69 it("keeps the minimum interval on a view returned to straight away", () => {
70 expect(shouldRefreshOnArrival({ visible: true, now: 2_000, lastRefreshedAt: 0 })).toBe(false);
71 });
72});
73​
74describe("shouldRefreshOnInterval", () => {
75 const tick = (now: number, lastInteractedAt: number) =>
76 shouldRefreshOnInterval({ visible: true, now, lastRefreshedAt: 0, lastInteractedAt });
77​
78 it("reads for a reader who is here", () => {
79 expect(tick(LIVE_REFRESH_INTERVAL_MS, LIVE_REFRESH_INTERVAL_MS - 1_000)).toBe(true);
80 });
81​
82 it("stops reading for a window left showing on a desk nobody is at", () => {
83 expect(tick(LIVE_REFRESH_IDLE_AFTER_MS + 60_000, 0)).toBe(false);
84 });
85​
86 it("starts reading again once the reader touches the window", () => {
87 const away = LIVE_REFRESH_IDLE_AFTER_MS + 60_000;
88 expect(tick(away + LIVE_REFRESH_INTERVAL_MS, away)).toBe(true);
89 });
90});