Skip to content

Fix: 2 issues (#1699, #1700) - #1843

Merged
Akanimoh12 merged 1 commit into
Akanimoh12:test-implement-dripsfrom
CodingBabe-1:simple/1699-1700
Nov 23, 2026
Merged

Akanimoh12 merged 1 commit into
Akanimoh12:test-implement-dripsfrom
CodingBabe-1:simple/1699-1700

Conversation

@CodingBabe-1

Copy link
Copy Markdown
Contributor

Closes #1699
Closes #1700

Issues

#1699 Add tests for the service worker

sw.ts has no tests. Cover caching strategy and update flow.

#1700 Lazy-load html2canvas

Load it only when the user generates an image to reduce the main bundle.

Summary

The service worker test must mock globals before importing sw.ts; use vi.resetModules + dynamic import per test and a helper to dispatch events with a waitUntil collector. Prefer the in-file // @vitest-environment node pragma over config globs to keep changes minimal, but update both vitest configs if globs are used. For html2canvas, keep a single cached promise so repeated PDF exports reuse the loaded module, and verify with the bundle analyzer/size-limit that html2canvas lands in a separate chunk. No behavior change to CSV export.

Changes

  • Create a Vitest test file for the service worker. Mock the ServiceWorkerGlobalScope (self with addEventListener, skipWaiting, clients.claim/matchAll/openWindow, registration.showNotification, location.origin) and the Cache Storage API (caches.open/match/keys/delete/put/addAll) before importing sw.ts. Capture the registered install/activate/fetch/push/notificationclick listeners and drive them with synthetic events (with waitUntil collecting promises). Cover: install precaches STATIC_ASSETS into the versioned static cache and calls skipWaiting; activate deletes caches not starting with CACHE_VERSION and calls clients.claim; fetch ignores cross-origin requests, uses network-first for navigate requests (cache hit on network failure, offline.html fallback, 503 text fallback), and cache-first for script/style/image/font destinations (cache hit short-circuits fetch, cache miss fetches and puts when ok); push shows a notification with default and payload-provided title/body/url; notificationclick focuses/navigates an existing client or opens a new window. Use vi.resetModules and dynamic import of './sw' per test so listeners are re-registered cleanly. (frontend-scaffold/src/sw.test.ts)
  • Ensure the new service worker test is picked up and runs in a suitable environment. The sw.ts module references webworker globals (ServiceWorkerGlobalScope, caches, clients, registration) that are not provided by jsdom, so add a per-file environment override at the top of the test file (e.g. // @vitest-environment node) or configure an environmentMatchGlobs entry mapping 'src/sw.test.ts' to 'node' in both configs. Verify the include globs already match src/**/*.test.ts (they do) and that no setup file breaks under the chosen environment; if src/test/setup.ts assumes DOM, guard it or exclude the sw test from that setup via the project config. (frontend-scaffold/vitest.config.ts)
  • Mirror the service worker test environment configuration in the unit config so vitest --config vitest.unit.config.ts also runs src/sw.test.ts correctly: add the same environmentMatchGlobs (or rely on the in-file // @vitest-environment node pragma) and confirm the include glob covers src/sw.test.ts. Keep the rest of the config unchanged. (frontend-scaffold/vitest.unit.config.ts)
  • Refactor generatePDF so html2canvas is not statically imported and is only fetched when the user generates a PDF. Introduce a module-level cached loader, e.g. let html2canvasPromise: Promise<typeof import('html2canvas').default> | null = null; function loadHtml2Canvas() { if (!html2canvasPromise) html2canvasPromise = import('html2canvas').then((m) => m.default); return html2canvasPromise; }, and inside generatePDF await loadHtml2Canvas() right before rendering the table (keep the existing dynamic import of jspdf or load it lazily too). Ensure the function remains async and callers (exportTipHistoryPDF) already await the promise. Do not change the public API or the generated PDF output. (frontend-scaffold/src/helpers/export.ts)
  • Update the html2canvas type shim to also describe the dynamic import shape used by the lazy loader, so import('html2canvas') resolves to a module whose default is the html2canvas function. Keep the existing default export declaration and add an explicit export default typing compatible with typeof import('html2canvas').default; no runtime code changes. (frontend-scaffold/src/types/html2canvas.d.ts)

Co-authored-by: Simple Agent <agent@simple.dev>
@drips-wave

drips-wave Bot commented Nov 23, 2026

Copy link
Copy Markdown

@CodingBabe-1 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Akanimoh12
Akanimoh12 merged commit d4d24e4 into Akanimoh12:test-implement-drips Nov 23, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lazy-load html2canvas Add tests for the service worker

2 participants