import {HistWindow, UrlState} from 'app/client/lib/UrlState'; import {assert} from 'chai'; import {dom} from 'grainjs'; import {popGlobals, pushGlobals} from 'grainjs/dist/cjs/lib/browserGlobals'; import {JSDOM} from 'jsdom'; import fromPairs = require('lodash/fromPairs'); describe('UrlState', function() { let mockWindow: HistWindow; function pushState(state: any, title: any, href: string) { mockWindow.location = new URL(href) as unknown as Location; } beforeEach(function() { mockWindow = { location: new URL('http://localhost:8080') as unknown as Location, history: {pushState} as History, addEventListener: () => undefined, removeEventListener: () => undefined, dispatchEvent: () => true, }; // These grainjs browserGlobals are needed for using dom() in tests. const jsdomDoc = new JSDOM("
"); pushGlobals(jsdomDoc.window); }); afterEach(function() { popGlobals(); }); interface State { [key: string]: string; } function encodeUrl(state: State, baseLocation: Location | URL): string { const url = new URL(baseLocation.href); for (const key of Object.keys(state)) { url.searchParams.set(key, state[key]); } return url.href; } function decodeUrl(location: Location | URL): State { const url = new URL(location.href); return fromPairs(Array.from(url.searchParams.entries())); } function updateState(prevState: State, newState: State): State { return {...prevState, ...newState}; } function needPageLoad(prevState: State, newState: State): boolean { return false; } async function delayPushUrl(prevState: State, newState: State): Promise