Scrum4Me/__tests__/stores/user-settings.test.ts
Madhura68 56c55e1813 feat(PBI-79/ST-1334): user-settings pendingSprintDraft-slot
- lib/user-settings.ts: nieuw workflow.pendingSprintDraft veld met
  compacte intent-shape (pbiIntent + per-PBI storyOverrides).
- actions/sprint-draft.ts: setPendingSprintDraftAction +
  clearPendingSprintDraftAction met product-membership-check + Zod-validatie.
- stores/user-settings/store.ts: setPendingSprintDraft / clearPendingSprintDraft
  optimistic acties + fine-grained mutators upsertPbiIntent / upsertStoryOverride.
  Sprint-draft actions worden dynamisch geïmporteerd zodat jsdom-tests
  zonder DATABASE_URL niet falen.
- Tests: nieuwe sprint-draft.test.ts (action-laag), uitbreiding
  user-settings store-tests (5 nieuwe cases) en schema-tests (4 cases).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-11 13:43:32 +02:00

274 lines
8 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
const updateAction = vi.fn()
const setDraftAction = vi.fn()
const clearDraftAction = vi.fn()
vi.mock('@/actions/user-settings', () => ({
updateUserSettingsAction: (...args: unknown[]) => updateAction(...args),
}))
vi.mock('@/actions/sprint-draft', () => ({
setPendingSprintDraftAction: (...args: unknown[]) => setDraftAction(...args),
clearPendingSprintDraftAction: (...args: unknown[]) =>
clearDraftAction(...args),
}))
import { useUserSettingsStore } from '@/stores/user-settings/store'
import type { PendingSprintDraft } from '@/lib/user-settings'
function resetStore() {
useUserSettingsStore.setState((s) => {
s.entities.settings = {}
s.context.hydrated = false
s.context.isDemo = false
s.pendingMutations = {}
})
}
beforeEach(() => {
resetStore()
updateAction.mockReset()
setDraftAction.mockReset()
clearDraftAction.mockReset()
})
afterEach(() => {
resetStore()
})
describe('useUserSettingsStore', () => {
it('hydrate sets entities and context', () => {
useUserSettingsStore.getState().hydrate(
{ views: { sprintBacklog: { sort: 'code' } } },
false,
)
const s = useUserSettingsStore.getState()
expect(s.entities.settings.views?.sprintBacklog?.sort).toBe('code')
expect(s.context.hydrated).toBe(true)
expect(s.context.isDemo).toBe(false)
})
it('setPref updates state optimistically and settles on success', async () => {
useUserSettingsStore.getState().hydrate({}, false)
updateAction.mockResolvedValueOnce({
success: true,
settings: { views: { sprintBacklog: { filterStatus: 'all' } } },
})
await useUserSettingsStore
.getState()
.setPref(['views', 'sprintBacklog', 'filterStatus'], 'all')
const s = useUserSettingsStore.getState()
expect(s.entities.settings.views?.sprintBacklog?.filterStatus).toBe('all')
expect(Object.keys(s.pendingMutations)).toHaveLength(0)
expect(updateAction).toHaveBeenCalledWith({
views: { sprintBacklog: { filterStatus: 'all' } },
})
})
it('setPref rolls back on server error', async () => {
useUserSettingsStore.getState().hydrate(
{ views: { sprintBacklog: { sort: 'code' } } },
false,
)
updateAction.mockResolvedValueOnce({ error: 'boom', code: 422 })
await useUserSettingsStore
.getState()
.setPref(['views', 'sprintBacklog', 'sort'], 'priority')
const s = useUserSettingsStore.getState()
expect(s.entities.settings.views?.sprintBacklog?.sort).toBe('code')
expect(Object.keys(s.pendingMutations)).toHaveLength(0)
})
it('setPref skips server-call for demo accounts', async () => {
useUserSettingsStore.getState().hydrate({}, true)
await useUserSettingsStore
.getState()
.setPref(['devTools', 'debugMode'], true)
const s = useUserSettingsStore.getState()
expect(s.entities.settings.devTools?.debugMode).toBe(true)
expect(updateAction).not.toHaveBeenCalled()
})
it('setPendingSprintDraft persists draft optimistically + calls action', async () => {
useUserSettingsStore.getState().hydrate({}, false)
setDraftAction.mockResolvedValueOnce({ success: true })
const draft: PendingSprintDraft = {
goal: 'Sprint 1',
pbiIntent: { pbiA: 'all' },
storyOverrides: {},
}
await useUserSettingsStore
.getState()
.setPendingSprintDraft('product-1', draft)
const s = useUserSettingsStore.getState()
expect(
s.entities.settings.workflow?.pendingSprintDraft?.['product-1'],
).toMatchObject({ goal: 'Sprint 1' })
expect(setDraftAction).toHaveBeenCalledWith('product-1', draft)
})
it('setPendingSprintDraft rolls back on server error', async () => {
useUserSettingsStore.getState().hydrate({}, false)
setDraftAction.mockResolvedValueOnce({ error: 'boom' })
const draft: PendingSprintDraft = {
goal: 'Sprint X',
pbiIntent: {},
storyOverrides: {},
}
await useUserSettingsStore
.getState()
.setPendingSprintDraft('product-1', draft)
const s = useUserSettingsStore.getState()
expect(s.entities.settings.workflow?.pendingSprintDraft).toBeUndefined()
})
it('clearPendingSprintDraft removes key optimistically', async () => {
useUserSettingsStore.getState().hydrate(
{
workflow: {
pendingSprintDraft: {
'product-1': {
goal: 'Old',
pbiIntent: {},
storyOverrides: {},
},
},
},
},
false,
)
clearDraftAction.mockResolvedValueOnce({ success: true })
await useUserSettingsStore
.getState()
.clearPendingSprintDraft('product-1')
const s = useUserSettingsStore.getState()
expect(
s.entities.settings.workflow?.pendingSprintDraft?.['product-1'],
).toBeUndefined()
expect(clearDraftAction).toHaveBeenCalledWith('product-1')
})
it('upsertPbiIntent updates intent and wipes storyOverrides for that PBI', async () => {
useUserSettingsStore.getState().hydrate(
{
workflow: {
pendingSprintDraft: {
'product-1': {
goal: 'g',
pbiIntent: { pbiA: 'none' },
storyOverrides: {
pbiA: { add: ['s-1'], remove: [] },
pbiB: { add: [], remove: ['s-2'] },
},
},
},
},
},
false,
)
setDraftAction.mockResolvedValue({ success: true })
await useUserSettingsStore
.getState()
.upsertPbiIntent('product-1', 'pbiA', 'all')
const draft =
useUserSettingsStore.getState().entities.settings.workflow
?.pendingSprintDraft?.['product-1']
expect(draft?.pbiIntent.pbiA).toBe('all')
expect(draft?.storyOverrides.pbiA).toBeUndefined()
expect(draft?.storyOverrides.pbiB).toEqual({ add: [], remove: ['s-2'] })
})
it('upsertStoryOverride add adds to add[] and removes from remove[]', async () => {
useUserSettingsStore.getState().hydrate(
{
workflow: {
pendingSprintDraft: {
'product-1': {
goal: 'g',
pbiIntent: {},
storyOverrides: {
pbiA: { add: [], remove: ['story-1'] },
},
},
},
},
},
false,
)
setDraftAction.mockResolvedValue({ success: true })
await useUserSettingsStore
.getState()
.upsertStoryOverride('product-1', 'pbiA', 'story-1', 'add')
const draft =
useUserSettingsStore.getState().entities.settings.workflow
?.pendingSprintDraft?.['product-1']
expect(draft?.storyOverrides.pbiA).toEqual({
add: ['story-1'],
remove: [],
})
})
it('upsertStoryOverride clear removes from both arrays and drops empty entry', async () => {
useUserSettingsStore.getState().hydrate(
{
workflow: {
pendingSprintDraft: {
'product-1': {
goal: 'g',
pbiIntent: {},
storyOverrides: {
pbiA: { add: ['story-1'], remove: [] },
},
},
},
},
},
false,
)
setDraftAction.mockResolvedValue({ success: true })
await useUserSettingsStore
.getState()
.upsertStoryOverride('product-1', 'pbiA', 'story-1', 'clear')
const draft =
useUserSettingsStore.getState().entities.settings.workflow
?.pendingSprintDraft?.['product-1']
expect(draft?.storyOverrides.pbiA).toBeUndefined()
})
it('applyServerPatch merges without optimistic state', () => {
useUserSettingsStore.getState().hydrate(
{ views: { sprintBacklog: { sort: 'code' } } },
false,
)
useUserSettingsStore.getState().applyServerPatch({
views: { sprintBacklog: { sortDir: 'desc' } },
})
const s = useUserSettingsStore.getState()
expect(s.entities.settings.views?.sprintBacklog).toEqual({
sort: 'code',
sortDir: 'desc',
})
expect(Object.keys(s.pendingMutations)).toHaveLength(0)
})
})