Files
wehub-resource-sync 070959e133
landing-page-staging / Deploy landing page to staging (push) Has been skipped
landing-page-ci / Validate landing page (push) Failing after 4s
visual-baseline / Capture visual baselines (push) Has been cancelled
bake-plugin-previews / Bake plugin previews (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:00:47 +08:00

46 lines
1.5 KiB
TypeScript

// @vitest-environment jsdom
import { afterEach, describe, expect, it, vi } from 'vitest';
import {
HOME_CHIP_INTENT_EVENT,
consumePendingHomeChip,
hasPendingHomeChip,
requestHomeChip,
} from '../../src/runtime/home-intent';
afterEach(() => {
// Drain any pending intent so tests stay independent.
consumePendingHomeChip();
vi.restoreAllMocks();
});
describe('home-intent latch', () => {
it('queues a chip id and consumes it exactly once', () => {
expect(hasPendingHomeChip()).toBe(false);
requestHomeChip('prototype');
expect(hasPendingHomeChip()).toBe(true);
expect(consumePendingHomeChip()).toBe('prototype');
// Second consume returns null — a one-shot latch, not a sticky default.
expect(consumePendingHomeChip()).toBeNull();
expect(hasPendingHomeChip()).toBe(false);
});
it('notifies mounted listeners via the intent event', () => {
const handler = vi.fn();
window.addEventListener(HOME_CHIP_INTENT_EVENT, handler);
try {
requestHomeChip('prototype');
expect(handler).toHaveBeenCalledTimes(1);
const event = handler.mock.calls[0]![0] as CustomEvent;
expect(event.detail).toMatchObject({ chipId: 'prototype' });
} finally {
window.removeEventListener(HOME_CHIP_INTENT_EVENT, handler);
}
});
it('keeps the latest requested chip when called twice before consume', () => {
requestHomeChip('prototype');
requestHomeChip('deck');
expect(consumePendingHomeChip()).toBe('deck');
});
});