diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-10-18 21:26:24 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-10-18 21:26:24 +0200 |
| commit | 031f99e626f52ea7baac9155b0cbe6e4fcfbfb31 (patch) | |
| tree | 5c6851213527ce7c8f5945c56a11448c7e8fdeac /src/lib/shared/refresh-policy.test.ts | |
| parent | 66939bab93cdcb9b7732c45d7bc3b99b6951fe15 (diff) | |
feat: poll server data so shared pages stay near-live
Refetch the current page every 15s while the tab is visible, gated by a
pure refresh policy: edit pages, hidden or offline tabs and in-flight
navigations are left alone. Replaces refresh-on-refocus with auto-refresh.
Diffstat (limited to 'src/lib/shared/refresh-policy.test.ts')
| -rw-r--r-- | src/lib/shared/refresh-policy.test.ts | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/lib/shared/refresh-policy.test.ts b/src/lib/shared/refresh-policy.test.ts new file mode 100644 index 0000000..5cab234 --- /dev/null +++ b/src/lib/shared/refresh-policy.test.ts @@ -0,0 +1,43 @@ +import { describe, it, expect } from 'vitest'; +import { shouldAutoRefresh } from './refresh-policy'; + +const idle = { visible: true, online: true, navigating: false }; + +describe('shouldAutoRefresh', () => { + it('refreshes a list page when the tab is visible and idle', () => { + expect(shouldAutoRefresh({ routeId: '/groceries', ...idle })).toBe(true); + expect(shouldAutoRefresh({ routeId: '/groceries/catalog', ...idle })).toBe( + true, + ); + expect(shouldAutoRefresh({ routeId: '/projects/[id]', ...idle })).toBe( + true, + ); + }); + + it('never refreshes a page holding an in-progress edit', () => { + expect(shouldAutoRefresh({ routeId: '/grocery/[id]', ...idle })).toBe( + false, + ); + expect( + shouldAutoRefresh({ + routeId: '/projects/[projectId]/routine/[routineId]', + ...idle, + }), + ).toBe(false); + expect(shouldAutoRefresh({ routeId: '/login', ...idle })).toBe(false); + }); + + it('holds off while hidden, offline or mid-navigation', () => { + const routeId = '/groceries'; + expect(shouldAutoRefresh({ routeId, ...idle, visible: false })).toBe(false); + expect(shouldAutoRefresh({ routeId, ...idle, online: false })).toBe(false); + expect(shouldAutoRefresh({ routeId, ...idle, navigating: true })).toBe( + false, + ); + }); + + it('holds off when the route is not resolved yet', () => { + expect(shouldAutoRefresh({ routeId: null, ...idle })).toBe(false); + expect(shouldAutoRefresh({ routeId: undefined, ...idle })).toBe(false); + }); +}); |
