mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 08:00:06 +00:00
test(components): cover map, collections, settings, journey and admin
This commit is contained in:
@@ -0,0 +1,312 @@
|
||||
// FE-W4SNB-001 to FE-W4SNB-029
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { act } from '@testing-library/react'
|
||||
import { render, screen, fireEvent, waitFor } from '../../../tests/helpers/render'
|
||||
import { useSystemNoticeStore, type SystemNoticeDTO } from '../../store/systemNoticeStore'
|
||||
import { registerNoticeAction } from './noticeActions'
|
||||
import { BannerRenderer, ToastRenderer } from './SystemNoticeBanner'
|
||||
|
||||
const navigate = vi.fn()
|
||||
vi.mock('react-router-dom', async () => {
|
||||
const actual = await vi.importActual<typeof import('react-router-dom')>('react-router-dom')
|
||||
return { ...actual, useNavigate: () => navigate }
|
||||
})
|
||||
|
||||
function notice(overrides: Partial<SystemNoticeDTO> & { id: string }): SystemNoticeDTO {
|
||||
return {
|
||||
display: 'banner',
|
||||
severity: 'info',
|
||||
titleKey: 'Maintenance',
|
||||
bodyKey: 'Back at noon.',
|
||||
dismissible: true,
|
||||
...overrides,
|
||||
} as SystemNoticeDTO
|
||||
}
|
||||
|
||||
const dismiss = vi.fn()
|
||||
|
||||
let realMatchMedia: typeof window.matchMedia
|
||||
let realRaf: typeof globalThis.requestAnimationFrame
|
||||
let realResizeObserver: typeof globalThis.ResizeObserver
|
||||
|
||||
beforeEach(() => {
|
||||
navigate.mockClear()
|
||||
dismiss.mockClear()
|
||||
useSystemNoticeStore.setState({ notices: [], loaded: true, dismiss })
|
||||
window.__addToast = vi.fn(() => 1)
|
||||
realMatchMedia = window.matchMedia
|
||||
realRaf = globalThis.requestAnimationFrame
|
||||
realResizeObserver = globalThis.ResizeObserver
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
delete window.__addToast
|
||||
document.documentElement.style.removeProperty('--banner-stack-h')
|
||||
window.matchMedia = realMatchMedia
|
||||
globalThis.requestAnimationFrame = realRaf
|
||||
globalThis.ResizeObserver = realResizeObserver
|
||||
})
|
||||
|
||||
describe('BannerRenderer', () => {
|
||||
it('FE-W4SNB-001: renders nothing without banners', () => {
|
||||
const { container } = render(<BannerRenderer notices={[]} />)
|
||||
expect(container).toBeEmptyDOMElement()
|
||||
})
|
||||
|
||||
it('FE-W4SNB-002: shows at most the two highest-priority banners', () => {
|
||||
render(<BannerRenderer notices={[notice({ id: 'a' }), notice({ id: 'b' }), notice({ id: 'c' })]} />)
|
||||
|
||||
expect(screen.getAllByRole('status')).toHaveLength(2)
|
||||
})
|
||||
|
||||
it('FE-W4SNB-003: a warn banner stays a polite status', () => {
|
||||
render(<BannerRenderer notices={[notice({ id: 'a', severity: 'warn' })]} />)
|
||||
const banner = screen.getByRole('status')
|
||||
|
||||
expect(banner).toHaveAttribute('aria-live', 'polite')
|
||||
expect(banner.className).toContain('bg-amber-50')
|
||||
})
|
||||
|
||||
it('FE-W4SNB-004: a critical banner is an assertive alert', () => {
|
||||
render(<BannerRenderer notices={[notice({ id: 'a', severity: 'critical' })]} />)
|
||||
const banner = screen.getByRole('alert')
|
||||
|
||||
expect(banner).toHaveAttribute('aria-live', 'assertive')
|
||||
expect(banner.className).toContain('bg-rose-50')
|
||||
})
|
||||
|
||||
it('FE-W4SNB-005: an unknown severity falls back to the info styling', () => {
|
||||
render(<BannerRenderer notices={[notice({ id: 'a', severity: 'weird' as SystemNoticeDTO['severity'] })]} />)
|
||||
|
||||
expect(screen.getByRole('status').className).toContain('bg-white')
|
||||
})
|
||||
|
||||
it('FE-W4SNB-006: hides the body when it repeats the title', () => {
|
||||
render(<BannerRenderer notices={[notice({ id: 'a', titleKey: 'Same', bodyKey: 'Same' })]} />)
|
||||
|
||||
expect(screen.getAllByText('Same')).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('FE-W4SNB-007: the close button dismisses the notice', () => {
|
||||
render(<BannerRenderer notices={[notice({ id: 'a' })]} />)
|
||||
|
||||
fireEvent.click(screen.getByLabelText('Dismiss: Maintenance'))
|
||||
|
||||
expect(dismiss).toHaveBeenCalledWith('a')
|
||||
})
|
||||
|
||||
it('FE-W4SNB-008: a non-dismissible banner has no close button', () => {
|
||||
render(<BannerRenderer notices={[notice({ id: 'a', dismissible: false })]} />)
|
||||
|
||||
expect(screen.queryByLabelText(/^Dismiss:/)).toBeNull()
|
||||
})
|
||||
|
||||
it('FE-W4SNB-009: a nav CTA routes in-app and dismisses the banner', () => {
|
||||
render(<BannerRenderer notices={[notice({ id: 'a', cta: { kind: 'nav', href: '/settings', labelKey: 'Open settings' } })]} />)
|
||||
|
||||
fireEvent.click(screen.getByRole('link', { name: 'Open settings' }))
|
||||
|
||||
expect(navigate).toHaveBeenCalledWith('/settings')
|
||||
expect(dismiss).toHaveBeenCalledWith('a')
|
||||
})
|
||||
|
||||
it('FE-W4SNB-010: a nav CTA on a sticky banner does not dismiss it', () => {
|
||||
render(
|
||||
<BannerRenderer notices={[notice({ id: 'a', dismissible: false, cta: { kind: 'nav', href: '/settings', labelKey: 'Open settings' } })]} />,
|
||||
)
|
||||
|
||||
fireEvent.click(screen.getByRole('link', { name: 'Open settings' }))
|
||||
|
||||
expect(navigate).toHaveBeenCalledWith('/settings')
|
||||
expect(dismiss).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('FE-W4SNB-011: a link CTA opens a hardened new tab', () => {
|
||||
const open = vi.spyOn(window, 'open').mockImplementation(() => null)
|
||||
render(<BannerRenderer notices={[notice({ id: 'a', cta: { kind: 'link', href: 'https://trek.dev', labelKey: 'Read more' } })]} />)
|
||||
|
||||
fireEvent.click(screen.getByRole('link', { name: 'Read more' }))
|
||||
|
||||
expect(open).toHaveBeenCalledWith('https://trek.dev', '_blank', 'noopener,noreferrer')
|
||||
expect(dismiss).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('FE-W4SNB-012: an action CTA runs the registered handler and dismisses by default', () => {
|
||||
const handler = vi.fn(() => {})
|
||||
registerNoticeAction('open-support', handler)
|
||||
render(<BannerRenderer notices={[notice({ id: 'a', cta: { kind: 'action', actionId: 'open-support', labelKey: 'Support' } })]} />)
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Support' }))
|
||||
|
||||
expect(handler).toHaveBeenCalledWith({ navigate })
|
||||
expect(dismiss).toHaveBeenCalledWith('a')
|
||||
})
|
||||
|
||||
it('FE-W4SNB-013: an action CTA can opt out of dismissing', () => {
|
||||
registerNoticeAction('stay-open', () => {})
|
||||
render(
|
||||
<BannerRenderer notices={[notice({ id: 'a', cta: { kind: 'action', actionId: 'stay-open', labelKey: 'Support', dismissOnAction: false } })]} />,
|
||||
)
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Support' }))
|
||||
|
||||
expect(dismiss).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('FE-W4SNB-014: resets the reported stack height on unmount', () => {
|
||||
const { unmount } = render(<BannerRenderer notices={[notice({ id: 'a' })]} />)
|
||||
|
||||
unmount()
|
||||
|
||||
expect(document.documentElement.style.getPropertyValue('--banner-stack-h')).toBe('0px')
|
||||
})
|
||||
|
||||
it('FE-W4SNB-015: fades the banner in on the next frame', async () => {
|
||||
render(<BannerRenderer notices={[notice({ id: 'a' })]} />)
|
||||
expect(screen.getByRole('status').parentElement?.className).toContain('opacity-0')
|
||||
|
||||
await waitFor(() => expect(screen.getByRole('status').parentElement?.className).toContain('opacity-100'))
|
||||
})
|
||||
|
||||
it('FE-W4SNB-025: reduced motion drops the slide and fades only', () => {
|
||||
window.matchMedia = ((query: string) => ({
|
||||
matches: query.includes('prefers-reduced-motion'),
|
||||
media: query,
|
||||
onchange: null,
|
||||
addListener: () => {},
|
||||
removeListener: () => {},
|
||||
addEventListener: () => {},
|
||||
removeEventListener: () => {},
|
||||
dispatchEvent: () => false,
|
||||
})) as unknown as typeof window.matchMedia
|
||||
|
||||
render(<BannerRenderer notices={[notice({ id: 'a' })]} />)
|
||||
|
||||
expect(screen.getByRole('status').parentElement?.className).toContain('transition-opacity')
|
||||
expect(screen.getByRole('status').parentElement?.className).not.toContain('transition-all')
|
||||
})
|
||||
|
||||
it('FE-W4SNB-026: a browser without matchMedia keeps the full transition', () => {
|
||||
;(window as unknown as { matchMedia?: unknown }).matchMedia = undefined
|
||||
|
||||
render(<BannerRenderer notices={[notice({ id: 'a' })]} />)
|
||||
|
||||
expect(screen.getByRole('status').parentElement?.className).toContain('transition-all')
|
||||
})
|
||||
|
||||
it('FE-W4SNB-027: without requestAnimationFrame the banner mounts already visible', () => {
|
||||
Reflect.deleteProperty(globalThis, 'requestAnimationFrame')
|
||||
|
||||
render(<BannerRenderer notices={[notice({ id: 'a' })]} />)
|
||||
|
||||
expect(screen.getByRole('status').parentElement?.className).toContain('opacity-100')
|
||||
})
|
||||
|
||||
it('FE-W4SNB-028: publishes the measured stack height as a CSS variable', () => {
|
||||
let resize: ResizeObserverCallback | null = null
|
||||
class CapturingResizeObserver {
|
||||
observe = vi.fn()
|
||||
unobserve = vi.fn()
|
||||
disconnect = vi.fn()
|
||||
constructor(cb: ResizeObserverCallback) { resize = cb }
|
||||
}
|
||||
globalThis.ResizeObserver = CapturingResizeObserver as unknown as typeof globalThis.ResizeObserver
|
||||
|
||||
render(<BannerRenderer notices={[notice({ id: 'a' })]} />)
|
||||
|
||||
const stack = screen.getByRole('status').parentElement?.parentElement as HTMLElement
|
||||
Object.defineProperty(stack, 'offsetHeight', { configurable: true, value: 64 })
|
||||
|
||||
act(() => {
|
||||
resize?.([], {} as ResizeObserver)
|
||||
})
|
||||
|
||||
expect(document.documentElement.style.getPropertyValue('--banner-stack-h')).toBe('64px')
|
||||
})
|
||||
})
|
||||
|
||||
describe('ToastRenderer', () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers({ shouldAdvanceTime: true })
|
||||
})
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
it('FE-W4SNB-016: fires an info toast joining title and body', () => {
|
||||
render(<ToastRenderer notices={[notice({ id: 't1', display: 'toast' })]} />)
|
||||
|
||||
expect(window.__addToast).toHaveBeenCalledWith('Maintenance: Back at noon.', 'info', 6000)
|
||||
})
|
||||
|
||||
it('FE-W4SNB-017: a warn toast uses the warning variant and a longer duration', () => {
|
||||
render(<ToastRenderer notices={[notice({ id: 't1', display: 'toast', severity: 'warn' })]} />)
|
||||
|
||||
expect(window.__addToast).toHaveBeenCalledWith('Maintenance: Back at noon.', 'warning', 9000)
|
||||
})
|
||||
|
||||
it('FE-W4SNB-018: uses the title alone when the body repeats it', () => {
|
||||
render(<ToastRenderer notices={[notice({ id: 't1', display: 'toast', titleKey: 'Same', bodyKey: 'Same' })]} />)
|
||||
|
||||
expect(window.__addToast).toHaveBeenCalledWith('Same', 'info', 6000)
|
||||
})
|
||||
|
||||
it('FE-W4SNB-019: dismisses the notice once the toast has run its course', () => {
|
||||
render(<ToastRenderer notices={[notice({ id: 't1', display: 'toast' })]} />)
|
||||
|
||||
act(() => { vi.advanceTimersByTime(6500) })
|
||||
|
||||
expect(dismiss).toHaveBeenCalledWith('t1')
|
||||
})
|
||||
|
||||
it('FE-W4SNB-020: refuses a critical toast and dismisses it instead', () => {
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
||||
render(<ToastRenderer notices={[notice({ id: 't1', display: 'toast', severity: 'critical' })]} />)
|
||||
|
||||
expect(window.__addToast).not.toHaveBeenCalled()
|
||||
expect(dismiss).toHaveBeenCalledWith('t1')
|
||||
expect(String(warn.mock.calls[0][0])).toContain('t1')
|
||||
})
|
||||
|
||||
it('FE-W4SNB-021: fires each notice only once across rerenders', () => {
|
||||
const notices = [notice({ id: 't1', display: 'toast' })]
|
||||
const { rerender } = render(<ToastRenderer notices={notices} />)
|
||||
|
||||
rerender(<ToastRenderer notices={[...notices]} />)
|
||||
|
||||
expect(window.__addToast).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('FE-W4SNB-022: retries on the next frame while the toast host is not registered yet', () => {
|
||||
delete window.__addToast
|
||||
const raf = vi.spyOn(window, 'requestAnimationFrame').mockImplementation(() => 0)
|
||||
|
||||
render(<ToastRenderer notices={[notice({ id: 't1', display: 'toast' })]} />)
|
||||
|
||||
expect(raf).toHaveBeenCalled()
|
||||
expect(dismiss).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('FE-W4SNB-023: drops the toast after ten failed frames', () => {
|
||||
delete window.__addToast
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
||||
vi.spyOn(window, 'requestAnimationFrame').mockImplementation((cb: FrameRequestCallback) => { cb(0); return 0 })
|
||||
|
||||
render(<ToastRenderer notices={[notice({ id: 't1', display: 'toast' })]} />)
|
||||
|
||||
expect(String(warn.mock.calls[warn.mock.calls.length - 1][0])).toContain('never registered')
|
||||
})
|
||||
|
||||
it('FE-W4SNB-029: an unmapped severity falls back to the info variant', () => {
|
||||
render(<ToastRenderer notices={[notice({ id: 't1', display: 'toast', severity: 'legendary' as SystemNoticeDTO['severity'] })]} />)
|
||||
|
||||
expect(window.__addToast).toHaveBeenCalledWith('Maintenance: Back at noon.', 'info', 6000)
|
||||
})
|
||||
|
||||
it('FE-W4SNB-024: renders no DOM of its own', () => {
|
||||
const { container } = render(<ToastRenderer notices={[notice({ id: 't1', display: 'toast' })]} />)
|
||||
|
||||
expect(container).toBeEmptyDOMElement()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,155 @@
|
||||
// FE-W4SNH-001 to FE-W4SNH-008
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { render, screen, act } from '../../../tests/helpers/render'
|
||||
import { useSystemNoticeStore, type SystemNoticeDTO } from '../../store/systemNoticeStore'
|
||||
|
||||
vi.mock('./SystemNoticeModal.js', () => ({
|
||||
ModalRenderer: ({ notices }: { notices: SystemNoticeDTO[] }) =>
|
||||
<div data-testid="modals">{notices.map(n => n.id).join(',')}</div>,
|
||||
}))
|
||||
vi.mock('./SystemNoticeBanner.js', () => ({
|
||||
BannerRenderer: ({ notices }: { notices: SystemNoticeDTO[] }) =>
|
||||
<div data-testid="banners">{notices.map(n => n.id).join(',')}</div>,
|
||||
ToastRenderer: ({ notices }: { notices: SystemNoticeDTO[] }) =>
|
||||
<div data-testid="toasts">{notices.map(n => n.id).join(',')}</div>,
|
||||
}))
|
||||
|
||||
import { SystemNoticeHost } from './SystemNoticeHost'
|
||||
|
||||
function notice(overrides: Partial<SystemNoticeDTO> & { id: string }): SystemNoticeDTO {
|
||||
return {
|
||||
display: 'banner',
|
||||
severity: 'info',
|
||||
titleKey: 'Title',
|
||||
bodyKey: 'Body',
|
||||
dismissible: true,
|
||||
...overrides,
|
||||
} as SystemNoticeDTO
|
||||
}
|
||||
|
||||
let mqListeners: Array<(e: MediaQueryListEvent) => void> = []
|
||||
|
||||
function stubMatchMedia(matches: boolean, { supported = true } = {}) {
|
||||
mqListeners = []
|
||||
Object.defineProperty(window, 'matchMedia', {
|
||||
writable: true,
|
||||
configurable: true,
|
||||
value: supported
|
||||
? vi.fn((query: string) => ({
|
||||
matches,
|
||||
media: query,
|
||||
addEventListener: (_ev: string, fn: (e: MediaQueryListEvent) => void) => { mqListeners.push(fn) },
|
||||
removeEventListener: (_ev: string, fn: (e: MediaQueryListEvent) => void) => {
|
||||
mqListeners = mqListeners.filter(l => l !== fn)
|
||||
},
|
||||
}))
|
||||
: undefined,
|
||||
})
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
stubMatchMedia(false)
|
||||
useSystemNoticeStore.setState({ notices: [], loaded: true, fetching: false })
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
describe('SystemNoticeHost', () => {
|
||||
it('FE-W4SNH-001: renders nothing until the store has loaded', () => {
|
||||
useSystemNoticeStore.setState({ notices: [], loaded: false, fetching: true })
|
||||
const { container } = render(<SystemNoticeHost />)
|
||||
|
||||
expect(container).toBeEmptyDOMElement()
|
||||
})
|
||||
|
||||
it('FE-W4SNH-002: triggers a cold-session fetch when nothing is loaded yet', () => {
|
||||
const fetch = vi.fn(async () => {})
|
||||
useSystemNoticeStore.setState({ notices: [], loaded: false, fetching: false, fetch })
|
||||
|
||||
render(<SystemNoticeHost />)
|
||||
|
||||
expect(fetch).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('FE-W4SNH-003: does not refetch when authStore already loaded the notices', () => {
|
||||
const fetch = vi.fn(async () => {})
|
||||
useSystemNoticeStore.setState({ notices: [], loaded: true, fetching: false, fetch })
|
||||
|
||||
render(<SystemNoticeHost />)
|
||||
|
||||
expect(fetch).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('FE-W4SNH-004: routes each notice to the renderer matching its display', () => {
|
||||
useSystemNoticeStore.setState({
|
||||
loaded: true,
|
||||
notices: [
|
||||
notice({ id: 'b1', display: 'banner' }),
|
||||
notice({ id: 'm1', display: 'modal' }),
|
||||
notice({ id: 't1', display: 'toast' }),
|
||||
notice({ id: 'b2', display: 'banner' }),
|
||||
],
|
||||
})
|
||||
|
||||
render(<SystemNoticeHost />)
|
||||
|
||||
expect(screen.getByTestId('banners')).toHaveTextContent('b1,b2')
|
||||
expect(screen.getByTestId('modals')).toHaveTextContent('m1')
|
||||
expect(screen.getByTestId('toasts')).toHaveTextContent('t1')
|
||||
})
|
||||
|
||||
it('FE-W4SNH-005: hides desktopOnly notices on a mobile viewport', () => {
|
||||
stubMatchMedia(true)
|
||||
useSystemNoticeStore.setState({
|
||||
loaded: true,
|
||||
notices: [
|
||||
notice({ id: 'thanks', display: 'modal', desktopOnly: true }),
|
||||
notice({ id: 'outage', display: 'modal' }),
|
||||
],
|
||||
})
|
||||
|
||||
render(<SystemNoticeHost />)
|
||||
|
||||
expect(screen.getByTestId('modals')).toHaveTextContent('outage')
|
||||
expect(screen.getByTestId('modals')).not.toHaveTextContent('thanks')
|
||||
})
|
||||
|
||||
it('FE-W4SNH-006: keeps desktopOnly notices on a desktop viewport', () => {
|
||||
useSystemNoticeStore.setState({
|
||||
loaded: true,
|
||||
notices: [notice({ id: 'thanks', display: 'modal', desktopOnly: true })],
|
||||
})
|
||||
|
||||
render(<SystemNoticeHost />)
|
||||
|
||||
expect(screen.getByTestId('modals')).toHaveTextContent('thanks')
|
||||
})
|
||||
|
||||
it('FE-W4SNH-007: reacts to a viewport change reported by matchMedia', () => {
|
||||
useSystemNoticeStore.setState({
|
||||
loaded: true,
|
||||
notices: [notice({ id: 'thanks', display: 'modal', desktopOnly: true })],
|
||||
})
|
||||
|
||||
render(<SystemNoticeHost />)
|
||||
expect(screen.getByTestId('modals')).toHaveTextContent('thanks')
|
||||
|
||||
act(() => { mqListeners.forEach(fn => fn({ matches: true } as MediaQueryListEvent)) })
|
||||
|
||||
expect(screen.getByTestId('modals')).toBeEmptyDOMElement()
|
||||
})
|
||||
|
||||
it('FE-W4SNH-008: falls back to the desktop layout when matchMedia is unavailable', () => {
|
||||
stubMatchMedia(false, { supported: false })
|
||||
useSystemNoticeStore.setState({
|
||||
loaded: true,
|
||||
notices: [notice({ id: 'thanks', display: 'modal', desktopOnly: true })],
|
||||
})
|
||||
|
||||
render(<SystemNoticeHost />)
|
||||
|
||||
expect(screen.getByTestId('modals')).toHaveTextContent('thanks')
|
||||
})
|
||||
})
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,65 @@
|
||||
// FE-W4SNA-001 to FE-W4SNA-005
|
||||
import { describe, it, expect, vi, afterEach } from 'vitest'
|
||||
import type { NavigateFunction } from 'react-router-dom'
|
||||
import { registerNoticeAction, runNoticeAction } from './noticeActions'
|
||||
|
||||
const navigate = vi.fn() as unknown as NavigateFunction
|
||||
const ctx = { navigate }
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
describe('noticeActions', () => {
|
||||
it('FE-W4SNA-001: runs a registered handler with the navigate context', () => {
|
||||
const handler = vi.fn(() => {})
|
||||
registerNoticeAction('open-settings', handler)
|
||||
|
||||
runNoticeAction('open-settings', ctx)
|
||||
|
||||
expect(handler).toHaveBeenCalledWith(ctx)
|
||||
})
|
||||
|
||||
it('FE-W4SNA-002: logs and does nothing for an unknown id', () => {
|
||||
const err = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
|
||||
expect(() => runNoticeAction('never-registered', ctx)).not.toThrow()
|
||||
expect(err).toHaveBeenCalledTimes(1)
|
||||
expect(String(err.mock.calls[0][0])).toContain('never-registered')
|
||||
})
|
||||
|
||||
it('FE-W4SNA-003: a later registration replaces the handler for the same id', () => {
|
||||
const first = vi.fn(() => {})
|
||||
const second = vi.fn(() => {})
|
||||
registerNoticeAction('dup', first)
|
||||
registerNoticeAction('dup', second)
|
||||
|
||||
runNoticeAction('dup', ctx)
|
||||
|
||||
expect(first).not.toHaveBeenCalled()
|
||||
expect(second).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('FE-W4SNA-004: fires an async handler without blocking the caller', async () => {
|
||||
let resolved = false
|
||||
registerNoticeAction('async-cta', async () => {
|
||||
await Promise.resolve()
|
||||
resolved = true
|
||||
})
|
||||
|
||||
expect(runNoticeAction('async-cta', ctx)).toBeUndefined()
|
||||
expect(resolved).toBe(false)
|
||||
|
||||
await Promise.resolve()
|
||||
await Promise.resolve()
|
||||
expect(resolved).toBe(true)
|
||||
})
|
||||
|
||||
it('FE-W4SNA-005: hands the real navigate through to the handler', () => {
|
||||
registerNoticeAction('go-billing', c => { c.navigate('/settings/billing') })
|
||||
|
||||
runNoticeAction('go-billing', ctx)
|
||||
|
||||
expect(navigate).toHaveBeenCalledWith('/settings/billing')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user