UNPKG

@virtualstate/app-history

Version:

Native JavaScript [app-history](https://github.com/WICG/app-history) implementation

64 lines 2.49 kB
import { AppHistorySync } from "../../history.js"; import { ok } from "../util.js"; export async function syncLocationExample(appHistory) { const sync = new AppHistorySync({ appHistory }), location = sync; const expectedHash = `#hash${Math.random()}`; location.hash = expectedHash; ok(location.hash === expectedHash); await finished(appHistory); const expectedPathname = `/pathname/1/${Math.random()}`; location.pathname = expectedPathname; ok(location.pathname === expectedPathname); await finished(appHistory); const searchParams = new URLSearchParams(location.search); searchParams.append("test", "test"); location.search = searchParams.toString(); ok(new URLSearchParams(location.search).get("test") === "test"); await finished(appHistory); } export async function syncHistoryExample(appHistory) { const sync = new AppHistorySync({ appHistory }), history = sync; const expected = `expected${Math.random()}`; const expectedUrl = new URL(`https://example.com/${expected}/1`); history.pushState({ [expected]: expected }, "", expectedUrl); ok(history.state[expected] === expected); await finished(appHistory); ok(appHistory.current.url === expectedUrl.toString()); await appHistory.navigate("/1").finished; ok(history.state?.[expected] !== expected); await appHistory.navigate("/2").finished; await appHistory.navigate("/3").finished; history.back(); await finished(appHistory); ok(history.state?.[expected] !== expected); history.back(); await finished(appHistory); ok(history.state?.[expected] !== expected); history.back(); await finished(appHistory); ok(history.state[expected] === expected); ok(appHistory.current.url === expectedUrl.toString()); history.forward(); await finished(appHistory); ok(history.state?.[expected] !== expected); history.go(-1); await finished(appHistory); ok(history.state[expected] === expected); history.go(1); await finished(appHistory); ok(history.state?.[expected] !== expected); history.go(-1); await finished(appHistory); ok(history.state[expected] === expected); history.go(0); await finished(appHistory); ok(history.state[expected] === expected); } async function finished(appHistory) { ok(appHistory.transition); ok(appHistory.transition.finished); await appHistory.transition.finished; } //# sourceMappingURL=sync-legacy.js.map