Countries
Upstream timeout
// @vitest-environment happy-dom
import { describe, expect, it } from 'vitest';
import { createCatalog, diffScenario } from '../../../packages/executable-stories-showcase/src/catalog.ts';
import {
checkReadySelector,
closestReadySelector,
selectorForElement,
suggestReadySelectors,
} from '../../../packages/executable-stories-showcase/src/ready.ts';
import { parseScenario } from '../../../packages/executable-stories-showcase/src/schema.ts';
describe('diffScenario', () => {
const catalog = createCatalog([
{
name: 'base',
description: 'base only',
tags: ['t'],
ready: 'text=base',
path: '/',
url: { a: '1', b: '1' },
network: [
{ path: '/x', response: { body: 'base' } },
{ path: '/y', response: { body: 'y' } },
{ path: '/y', query: { p: '2' }, response: { body: 'y2' } },
],
state: { s: { keep: 1, over: 1, drop: 1 } },
},
{
name: 'leaf',
extends: 'base',
description: 'leaf only',
ready: 'text=leaf',
url: { a: null, b: '2' },
network: [
{ path: '/x', response: { body: 'leaf' } },
{ path: '/y', query: { p: '2' }, remove: true },
{ path: '/z', response: { body: 'z' } },
],
state: { s: { over: 2, drop: null, extra: 3 } },
},
]);
it('round-trips through resolve', () => {
const flatBase = catalog.resolve(catalog.get('base')!);
const captured = catalog.resolve(catalog.get('leaf')!);
const diff = diffScenario(captured, flatBase);
expect(diff.extends).toBe('base');
expect(diff.ready).toBe('text=leaf');
expect(diff.description).toBe('leaf only');
const again = createCatalog([catalog.get('base')!, diff]).resolve(
parseScenario(diff),
);
expect(again.url).toEqual(captured.url);
expect(again.state).toEqual(captured.state);
expect(again.network).toEqual(captured.network);
expect(again.path).toEqual(captured.path);
expect(again.ready).toBe(captured.ready);
});
it('round-trips a null slice over a base that sets it', () => {
const nulls = createCatalog([
{ name: 'signed-in', state: { user: { id: 1 }, cart: { n: 1 } } },
{ name: 'signed-out', extends: 'signed-in', state: { user: null } },
]);
const flatBase = nulls.resolve(nulls.get('signed-in')!);
const captured = nulls.resolve(nulls.get('signed-out')!);
const diff = diffScenario(captured, flatBase);
expect(captured.state).toEqual({ user: null, cart: { n: 1 } });
expect(createCatalog([flatBase, diff]).resolve(parseScenario(diff)).state).toEqual(captured.state);
});
it('inherits a slice the capture has no adapter for', () => {
const flatBase = catalog.resolve(catalog.get('base')!);
const diff = diffScenario(catalog.resolve(parseScenario({ name: 'bare' })), flatBase);
expect(diff.state).toBeUndefined();
});
it('omits fields equal to the base', () => {
const base = catalog.get('base');
expect(base).toBeDefined();
const flatBase = catalog.resolve(base!);
const same = parseScenario({
name: 'twin',
ready: 'text=twin',
path: flatBase.path,
url: flatBase.url,
network: flatBase.network,
state: flatBase.state,
});
const diff = diffScenario(catalog.resolve(same), flatBase);
expect(diff).toEqual({
name: 'twin',
extends: 'base',
ready: 'text=twin',
});
});
it('keeps the first of duplicate captured routes, the one MSW serves', () => {
const flatBase = catalog.resolve(catalog.get('base')!);
const captured = catalog.resolve(parseScenario({
...flatBase,
name: 'dup',
network: [
{ path: '/x', response: { body: 'served' } },
{ path: '/x', response: { body: 'shadowed' } },
...flatBase.network!.filter((e) => e.path !== '/x'),
],
}));
expect(diffScenario(captured, flatBase).network?.map((e) => e.response?.body)).toEqual(['served']);
});
it('returns a full snapshot with no base', () => {
const leaf = catalog.get('leaf');
expect(leaf).toBeDefined();
const captured = catalog.resolve(leaf!);
const full = diffScenario(captured);
expect(full.extends).toBeUndefined();
expect(full.network?.length).toBeGreaterThan(0);
expect(full.ready).toBe('text=leaf');
});
});
describe('ready selectors', () => {
it('prefers data-testid, then named role, then unique text', () => {
document.body.innerHTML = `
Upstream timeoutCountries
No orders
No orders yet
`; expect(selectorForElement(document.querySelector('p')!)).toBe('text="No orders"'); }); it('suggests ranked candidates and skips the panel host', () => { document.body.innerHTML = `No countries match
No countries match.
0 results
Plain mixed text
spaced out