| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { register, activateUpdate } from "../js/client.mjs"; |
| 4 | |
| 5 | test("unsupported browsers retain the ordinary web app", async () => { |
| 6 | assert.equal(await register(), null); |
| 7 | }); |
| 8 | |
| 9 | test("waiting updates are announced without activating or reloading", async () => { |
| 10 | const previous = Object.getOwnPropertyDescriptor(globalThis, "navigator"); |
| 11 | const events = {}; |
| 12 | const messages = []; |
| 13 | const waiting = { postMessage: (message) => messages.push(message) }; |
| 14 | const registration = { waiting, addEventListener: (name, fn) => { events[name] = fn; } }; |
| 15 | Object.defineProperty(globalThis, "navigator", { configurable: true, value: { |
| 16 | serviceWorker: { register: async (url, options) => { |
| 17 | assert.equal(url, "/app/sw.js"); |
| 18 | assert.equal(options.updateViaCache, "none"); |
| 19 | return registration; |
| 20 | } }, |
| 21 | } }); |
| 22 | try { |
| 23 | let updates = 0; |
| 24 | assert.equal(await register({ url: "/app/sw.js", onUpdate: () => { updates++; } }), registration); |
| 25 | assert.equal(updates, 1); |
| 26 | events.updatefound(); |
| 27 | assert.equal(updates, 1, "do not announce the same waiting worker twice"); |
| 28 | assert.equal(messages.length, 0); |
| 29 | assert.equal(activateUpdate(registration), true); |
| 30 | assert.deepEqual(messages, [{ type: "rastrillo:pwa:activate" }]); |
| 31 | registration.waiting = null; |
| 32 | assert.equal(activateUpdate(registration), false); |
| 33 | } finally { |
| 34 | Object.defineProperty(globalThis, "navigator", previous); |
| 35 | } |
| 36 | }); |
| 37 | |