|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { DashboardDetailRequestOwner } from './dashboardDetailRequest'; |
| 3 | + |
| 4 | +interface Deferred<T> { |
| 5 | + promise: Promise<T>; |
| 6 | + resolve: (value: T) => void; |
| 7 | +} |
| 8 | + |
| 9 | +function deferred<T>(): Deferred<T> { |
| 10 | + let resolvePromise: ((value: T) => void) | undefined; |
| 11 | + const promise = new Promise<T>((resolve) => { |
| 12 | + resolvePromise = resolve; |
| 13 | + }); |
| 14 | + return { |
| 15 | + promise, |
| 16 | + resolve: (value) => resolvePromise?.(value), |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +async function testLatestDashboardDetailOwnsCurrentState() { |
| 21 | + const owner = new DashboardDetailRequestOwner(); |
| 22 | + const firstRequest = deferred<{ id: number }>(); |
| 23 | + const latestRequest = deferred<{ id: number }>(); |
| 24 | + let currentDashboard: { id: number } | null = null; |
| 25 | + const commit = (dashboard: { id: number }) => { |
| 26 | + currentDashboard = dashboard; |
| 27 | + }; |
| 28 | + |
| 29 | + const firstLoad = owner.run(() => firstRequest.promise, commit); |
| 30 | + const latestLoad = owner.run(() => latestRequest.promise, commit); |
| 31 | + latestRequest.resolve({ id: 2 }); |
| 32 | + await latestLoad; |
| 33 | + assert.equal(currentDashboard?.id, 2); |
| 34 | + |
| 35 | + firstRequest.resolve({ id: 1 }); |
| 36 | + await firstLoad; |
| 37 | + assert.equal(currentDashboard?.id, 2, 'an older response must not replace the latest dashboard'); |
| 38 | +} |
| 39 | + |
| 40 | +async function testDirectSelectionInvalidatesPendingDetail() { |
| 41 | + const owner = new DashboardDetailRequestOwner(); |
| 42 | + const request = deferred<{ id: number }>(); |
| 43 | + let currentDashboard: { id: number } | null = null; |
| 44 | + const load = owner.run( |
| 45 | + () => request.promise, |
| 46 | + (dashboard) => { |
| 47 | + currentDashboard = dashboard; |
| 48 | + }, |
| 49 | + ); |
| 50 | + |
| 51 | + owner.invalidate(); |
| 52 | + currentDashboard = { id: 2 }; |
| 53 | + request.resolve({ id: 1 }); |
| 54 | + await load; |
| 55 | + |
| 56 | + assert.equal(currentDashboard.id, 2, 'a direct selection must not be overwritten by an older request'); |
| 57 | +} |
| 58 | + |
| 59 | +async function testLatestFailureStillInvalidatesOlderSuccess() { |
| 60 | + const owner = new DashboardDetailRequestOwner(); |
| 61 | + const olderRequest = deferred<{ id: number }>(); |
| 62 | + let currentDashboard: { id: number } | null = null; |
| 63 | + const olderLoad = owner.run( |
| 64 | + () => olderRequest.promise, |
| 65 | + (dashboard) => { |
| 66 | + currentDashboard = dashboard; |
| 67 | + }, |
| 68 | + ); |
| 69 | + const latestError = new Error('latest request failed'); |
| 70 | + await assert.rejects( |
| 71 | + owner.run( |
| 72 | + () => Promise.reject(latestError), |
| 73 | + () => undefined, |
| 74 | + ), |
| 75 | + latestError, |
| 76 | + ); |
| 77 | + |
| 78 | + olderRequest.resolve({ id: 1 }); |
| 79 | + await olderLoad; |
| 80 | + assert.equal(currentDashboard, null, 'an older success must stay stale after the latest request fails'); |
| 81 | +} |
| 82 | + |
| 83 | +void Promise.all([ |
| 84 | + testLatestDashboardDetailOwnsCurrentState(), |
| 85 | + testDirectSelectionInvalidatesPendingDetail(), |
| 86 | + testLatestFailureStillInvalidatesOlderSuccess(), |
| 87 | +]) |
| 88 | + .then(() => console.log('Dashboard detail request ownership tests passed')) |
| 89 | + .catch((error) => { |
| 90 | + console.error(error); |
| 91 | + process.exitCode = 1; |
| 92 | + }); |
0 commit comments