Testing
You will be able to test parseUrl and href under Node, test components that use useAppState in a browser, and clear stores and stored values between tests.
On this page
Pure functions
src/state.test.tsimport { expect, test } from 'vitest';
import { density, listState } from './state';
test('a page below 1 reads as the first page', () => {
expect(listState.parseUrl({ page: '0' }).page).toBe(1);
});
test('a link leaves the defaults out', () => {
expect(listState.href('/products', { page: 1 })).toBe('/products');
});
test('a compact cookie reads back as compact', () => {
const cookies = new Map([
[density.cookieName, density.cookieValue({ density: 'compact' })],
]);
expect(density.parseCookies(cookies).density).toBe('compact');
});parseUrl, href, search, and a cookie state’s parseCookies and cookieValue use no browser API. An ordinary test under Node checks them.
parseUrl takes a URLSearchParams or a plain object shaped like what a framework hands a page. parseCookies takes cookies built as a Map.
Tests in a browser
A component that uses useAppState runs on the real Navigation API, Web Storage and Cookie Store API, so test it in a browser. This package tests itself with Vitest’s browser mode.
Serve tests that touch a cookie state over HTTPS. The Secure attribute the Cookie Store API sets is covered in Cookie.
Cleanup between tests
import { resetStateRegistry } from '@k8ordo/state';
import { afterEach } from 'vitest';
import { density, prefs } from './state';
afterEach(async () => {
resetStateRegistry();
localStorage.removeItem(prefs.storageKey);
await cookieStore.delete(density.cookieName);
});Stores live in a registry inside the module, not under a Provider. Unless resetStateRegistry() empties it, one test’s state carries into the next.
Emptying the registry leaves the values stored in the browser. Remove them by the definition’s storageKey in its storage area, and a cookie by its cookieName.
Pitfall
Unmount components before calling resetStateRegistry(). A hook that is still mounted keeps its old store through closures.
A router stand-in
import { mountPageUrl } from '@k8ordo/state/testing';
beforeEach(() => mountPageUrl());An update() that changes the URL calls navigation.navigate(). Without an intercept(), that is a cross-document load, and the page running the tests navigates away. mountPageUrl() from @k8ordo/state/testing intercepts the navigate event in a router’s place.
mountPageUrl() returns the cleanup. It calls resetStateRegistry() and puts back the URL and the history entry’s state as they were. Vitest and Storybook call what beforeEach returns after the test, so returning it is enough.
beforeEach(() => mountPageUrl({ search: 'q=css', hash: '3' }));The arguments set the URL a test starts from. search is added after the params the page already has, in the spelling given. hash becomes the fragment.
Another tab’s write
localStorage.setItem(
prefs.storageKey,
JSON.stringify({ view: 'table', pageSize: 20 }),
);
window.dispatchEvent(
new StorageEvent('storage', {
key: prefs.storageKey,
storageArea: localStorage,
}),
);The storage event does not fire in the document that wrote. Write the value with setItem and dispatch the StorageEvent yourself, and it stands in for another tab’s write.
await cookieStore.set(
density.cookieName,
encodeURIComponent(density.cookieValue({ density: 'compact' })),
);The Cookie Store API’s change event fires in the same tab too, so a test’s own cookieStore.set() stands in for another tab’s write. Percent-encode the value, as update() does when it writes.