mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
test(atlas): cover the new Atlas from engine to hub views
SonarCloud measured 16.9% coverage on this PR's new code. Tests now cover the WebGL globe engine (boot, caching, input, camera, regions, prefetch) and renderer (against a fake WebGL2 context that reads the real shaders), the paint, labels, sky and pins, the central useAtlasGlobe hook and the hub hooks, the snapshot, account queue and Atlas repos, every desktop and phone hub view, the server's people, flights and mesh routes, the shared badge rules, and the new view transition, warm bags and kept planner maps.
This commit is contained in:
@@ -0,0 +1,191 @@
|
||||
// FE-ATLAS2-REST-001 to 010: the View Transitions groundwork (canAnimate, settled, transitionTo).
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { canAnimate, settled, transitionTo } from './viewTransition';
|
||||
|
||||
interface FakeTransition {
|
||||
ready: Promise<void>;
|
||||
finished: Promise<void>;
|
||||
updateCallbackDone: Promise<void>;
|
||||
skipTransition: () => void;
|
||||
}
|
||||
|
||||
/** A hand-made deferred, so a test decides when a transition finishes or is skipped. */
|
||||
function deferred() {
|
||||
let resolve: () => void = () => {};
|
||||
let reject: (e: unknown) => void = () => {};
|
||||
const promise = new Promise<void>((res, rej) => {
|
||||
resolve = res;
|
||||
reject = rej;
|
||||
});
|
||||
return { promise, resolve, reject };
|
||||
}
|
||||
|
||||
interface Started {
|
||||
update: () => Promise<void>;
|
||||
done: Promise<void>;
|
||||
finished: ReturnType<typeof deferred>;
|
||||
ready: ReturnType<typeof deferred>;
|
||||
}
|
||||
|
||||
let started: Started[] = [];
|
||||
|
||||
function installStart() {
|
||||
const start = vi.fn((update: () => Promise<void>): FakeTransition => {
|
||||
const finished = deferred();
|
||||
const ready = deferred();
|
||||
const done = update();
|
||||
started.push({ update, done, finished, ready });
|
||||
return { ready: ready.promise, finished: finished.promise, updateCallbackDone: done, skipTransition: vi.fn() };
|
||||
});
|
||||
Object.defineProperty(document, 'startViewTransition', { value: start, configurable: true, writable: true });
|
||||
return start;
|
||||
}
|
||||
|
||||
function stubReducedMotion(reduce: boolean) {
|
||||
vi.stubGlobal(
|
||||
'matchMedia',
|
||||
vi.fn((query: string) => ({ matches: reduce && query.includes('reduce'), media: query }))
|
||||
);
|
||||
}
|
||||
|
||||
const flush = () => new Promise<void>((r) => setTimeout(r, 0));
|
||||
|
||||
beforeEach(() => {
|
||||
started = [];
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
delete (document as { startViewTransition?: unknown }).startViewTransition;
|
||||
delete document.documentElement.dataset.routeTransition;
|
||||
document.querySelectorAll('[data-route-fallback]').forEach((n) => n.remove());
|
||||
vi.unstubAllGlobals();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('canAnimate', () => {
|
||||
it('FE-ATLAS2-REST-001: is false where the browser has no View Transitions', () => {
|
||||
expect(canAnimate()).toBe(false);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-REST-002: is true with View Transitions and motion allowed, false when reduced motion is asked', () => {
|
||||
installStart();
|
||||
stubReducedMotion(false);
|
||||
expect(canAnimate()).toBe(true);
|
||||
stubReducedMotion(true);
|
||||
expect(canAnimate()).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('settled', () => {
|
||||
it('FE-ATLAS2-REST-003: resolves at once while no loading placeholder is on screen', async () => {
|
||||
let done = false;
|
||||
void settled().then(() => {
|
||||
done = true;
|
||||
});
|
||||
await Promise.resolve();
|
||||
expect(done).toBe(true);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-REST-004: waits while a route fallback shows and resolves once it is gone', async () => {
|
||||
const fallback = document.createElement('div');
|
||||
fallback.setAttribute('data-route-fallback', '');
|
||||
document.body.appendChild(fallback);
|
||||
let done = false;
|
||||
const wait = settled().then(() => {
|
||||
done = true;
|
||||
});
|
||||
await new Promise((r) => setTimeout(r, 40));
|
||||
expect(done).toBe(false);
|
||||
fallback.remove();
|
||||
await wait;
|
||||
expect(done).toBe(true);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-REST-005: gives up waiting after the settle time even while the fallback stays', async () => {
|
||||
const fallback = document.createElement('div');
|
||||
fallback.setAttribute('data-route-fallback', '');
|
||||
document.body.appendChild(fallback);
|
||||
const clock = [0, 100, 1000];
|
||||
vi.spyOn(performance, 'now').mockImplementation(() => clock.shift() ?? 1000);
|
||||
let done = false;
|
||||
const wait = settled().then(() => {
|
||||
done = true;
|
||||
});
|
||||
await Promise.resolve();
|
||||
expect(done).toBe(false);
|
||||
await wait;
|
||||
expect(done).toBe(true);
|
||||
expect(document.querySelector('[data-route-fallback]')).not.toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('transitionTo', () => {
|
||||
it('FE-ATLAS2-REST-006: just runs the update without View Transitions and marks nothing', () => {
|
||||
const update = vi.fn();
|
||||
transitionTo('tab', update);
|
||||
expect(update).toHaveBeenCalledTimes(1);
|
||||
expect(document.documentElement.dataset.routeTransition).toBeUndefined();
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-REST-007: just runs the update when reduced motion is asked', () => {
|
||||
const start = installStart();
|
||||
stubReducedMotion(true);
|
||||
const update = vi.fn();
|
||||
transitionTo('tab', update);
|
||||
expect(update).toHaveBeenCalledTimes(1);
|
||||
expect(start).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-REST-008: runs the update inside a transition marked with its kind and clears the mark when it finishes', async () => {
|
||||
const start = installStart();
|
||||
stubReducedMotion(false);
|
||||
const update = vi.fn();
|
||||
transitionTo('fade', update);
|
||||
expect(start).toHaveBeenCalledTimes(1);
|
||||
expect(update).toHaveBeenCalledTimes(1);
|
||||
expect(document.documentElement.dataset.routeTransition).toBe('fade');
|
||||
await started[0].done;
|
||||
started[0].ready.resolve();
|
||||
started[0].finished.resolve();
|
||||
await flush();
|
||||
expect(document.documentElement.dataset.routeTransition).toBeUndefined();
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-REST-009: a skipped transition does not take the mark away from the newer one', async () => {
|
||||
installStart();
|
||||
stubReducedMotion(false);
|
||||
transitionTo('forward', vi.fn());
|
||||
transitionTo('back', vi.fn());
|
||||
expect(document.documentElement.dataset.routeTransition).toBe('back');
|
||||
// The first one was skipped by the second: its promises reject, which is no error.
|
||||
started[0].ready.reject(new DOMException('skipped', 'AbortError'));
|
||||
started[0].finished.reject(new DOMException('skipped', 'AbortError'));
|
||||
await flush();
|
||||
expect(document.documentElement.dataset.routeTransition).toBe('back');
|
||||
started[1].finished.resolve();
|
||||
await flush();
|
||||
expect(document.documentElement.dataset.routeTransition).toBeUndefined();
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-REST-010: the update callback waits for the loading placeholder to go', async () => {
|
||||
installStart();
|
||||
stubReducedMotion(false);
|
||||
const fallback = document.createElement('div');
|
||||
fallback.setAttribute('data-route-fallback', '');
|
||||
document.body.appendChild(fallback);
|
||||
let updated = false;
|
||||
transitionTo('tab', () => {
|
||||
updated = true;
|
||||
});
|
||||
let callbackDone = false;
|
||||
void started[0].done.then(() => {
|
||||
callbackDone = true;
|
||||
});
|
||||
await new Promise((r) => setTimeout(r, 40));
|
||||
expect(updated).toBe(true);
|
||||
expect(callbackDone).toBe(false);
|
||||
fallback.remove();
|
||||
await started[0].done;
|
||||
expect(callbackDone).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user