|
| 1 | +import { |
| 2 | + hasRecentPWAUpdateReload, |
| 3 | + markPWAUpdateReload, |
| 4 | + requestPWAUpdate, |
| 5 | + watchForPWAUpdate, |
| 6 | +} from './pwaUpdate'; |
| 7 | + |
| 8 | +class MockServiceWorker extends EventTarget { |
| 9 | + state: ServiceWorkerState = 'installing'; |
| 10 | + postMessage = jest.fn(); |
| 11 | +} |
| 12 | + |
| 13 | +class MockRegistration extends EventTarget { |
| 14 | + installing: MockServiceWorker | null = null; |
| 15 | + waiting: MockServiceWorker | null = null; |
| 16 | +} |
| 17 | + |
| 18 | +describe('watchForPWAUpdate', () => { |
| 19 | + it('reports an existing waiting update when a worker already controls the page', () => { |
| 20 | + const registration = new MockRegistration(); |
| 21 | + registration.waiting = new MockServiceWorker(); |
| 22 | + const onUpdateReady = jest.fn(); |
| 23 | + |
| 24 | + watchForPWAUpdate(registration as unknown as ServiceWorkerRegistration, { |
| 25 | + getController: () => new MockServiceWorker() as unknown as ServiceWorker, |
| 26 | + onUpdateReady, |
| 27 | + }); |
| 28 | + |
| 29 | + expect(onUpdateReady).toHaveBeenCalledTimes(1); |
| 30 | + }); |
| 31 | + |
| 32 | + it('reports a newly installed update', () => { |
| 33 | + const registration = new MockRegistration(); |
| 34 | + const installingWorker = new MockServiceWorker(); |
| 35 | + registration.installing = installingWorker; |
| 36 | + const onUpdateReady = jest.fn(); |
| 37 | + |
| 38 | + watchForPWAUpdate(registration as unknown as ServiceWorkerRegistration, { |
| 39 | + getController: () => new MockServiceWorker() as unknown as ServiceWorker, |
| 40 | + onUpdateReady, |
| 41 | + }); |
| 42 | + |
| 43 | + registration.dispatchEvent(new Event('updatefound')); |
| 44 | + registration.waiting = installingWorker; |
| 45 | + installingWorker.state = 'installed'; |
| 46 | + installingWorker.dispatchEvent(new Event('statechange')); |
| 47 | + |
| 48 | + expect(onUpdateReady).toHaveBeenCalledTimes(1); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe('requestPWAUpdate', () => { |
| 53 | + it('asks a waiting worker to activate', () => { |
| 54 | + const worker = new MockServiceWorker(); |
| 55 | + const updated = requestPWAUpdate({ waiting: worker }); |
| 56 | + |
| 57 | + expect(updated).toBe(true); |
| 58 | + expect(worker.postMessage).toHaveBeenCalledWith({ type: 'SKIP_WAITING' }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('does nothing when no update is waiting', () => { |
| 62 | + const updated = requestPWAUpdate({ waiting: null }); |
| 63 | + |
| 64 | + expect(updated).toBe(false); |
| 65 | + }); |
| 66 | +}); |
| 67 | + |
| 68 | +describe('the PWA reload guard', () => { |
| 69 | + it('blocks only a second reload during the guard window', () => { |
| 70 | + const storage = new Map<string, string>(); |
| 71 | + const sessionStorage = { |
| 72 | + getItem: (key: string) => storage.get(key) ?? null, |
| 73 | + setItem: (key: string, value: string) => storage.set(key, value), |
| 74 | + }; |
| 75 | + |
| 76 | + markPWAUpdateReload(sessionStorage, 1_000); |
| 77 | + |
| 78 | + expect(hasRecentPWAUpdateReload(sessionStorage, 20_000)).toBe(true); |
| 79 | + expect(hasRecentPWAUpdateReload(sessionStorage, 40_000)).toBe(false); |
| 80 | + }); |
| 81 | +}); |
0 commit comments