Files
accounted/lib/ui-state/__tests__/client.test.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

62 lines
2.0 KiB
TypeScript

/**
* Tests for the client-side ui_state helpers: persistence POST shape,
* silent failure, and last-used split-button mode resolution.
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { persistUiState, rememberCreateMode, resolveInitialMode } from '../client'
const fetchMock = vi.fn()
beforeEach(() => {
vi.clearAllMocks()
fetchMock.mockResolvedValue({ ok: true })
vi.stubGlobal('fetch', fetchMock)
})
afterEach(() => {
vi.unstubAllGlobals()
})
describe('persistUiState', () => {
it('POSTs the patch to /api/user/ui-state', () => {
persistUiState({ nav_collapsed: true })
expect(fetchMock).toHaveBeenCalledWith('/api/user/ui-state', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ nav_collapsed: true }),
})
})
it('swallows network failures', () => {
fetchMock.mockRejectedValue(new Error('offline'))
expect(() => persistUiState({ nav_collapsed: false })).not.toThrow()
})
})
describe('rememberCreateMode', () => {
it('nests the mode under the surface key', () => {
rememberCreateMode('bookkeeping', 'mall')
const body = JSON.parse(fetchMock.mock.calls[0][1].body)
expect(body).toEqual({ create_mode: { bookkeeping: 'mall' } })
})
})
describe('resolveInitialMode', () => {
const keys = ['tomt', 'mall', 'assistent'] as const
it('returns the persisted mode when valid', () => {
const uiState = { create_mode: { bookkeeping: 'mall' } }
expect(resolveInitialMode(uiState, 'bookkeeping', keys, 'tomt')).toBe('mall')
})
it('falls back when the persisted mode is stale', () => {
const uiState = { create_mode: { bookkeeping: 'removed-mode' } }
expect(resolveInitialMode(uiState, 'bookkeeping', keys, 'tomt')).toBe('tomt')
})
it('falls back when nothing is persisted', () => {
expect(resolveInitialMode(undefined, 'bookkeeping', keys, 'tomt')).toBe('tomt')
expect(resolveInitialMode({}, 'other', keys, 'assistent')).toBe('assistent')
})
})