import { describe, it, expect } from 'vitest'; import { expectTag } from '@shared/testing/expect-tag'; import { BesluitState, reduce, initial } from './besluit.machine'; const editingWith = (besluit: string, toelichting = ''): BesluitState => ({ tag: 'Editing', draft: { besluit, toelichting }, errors: {}, }); describe('besluit reduce', () => { it('SetField updates the draft while editing', () => { const s = reduce(initial, { tag: 'SetField', key: 'besluit', value: 'Goedkeuren' }); expect(expectTag(s, 'Editing').draft.besluit).toBe('Goedkeuren'); }); it('Submit with no besluit chosen stays Editing and reports a field error', () => { const s = reduce(editingWith(''), { tag: 'Submit' }); expect(expectTag(s, 'Editing').errors.besluit).toBeTruthy(); }); it('Submit Afwijzen without a toelichting stays Editing and reports a field error', () => { const s = reduce(editingWith('Afwijzen'), { tag: 'Submit' }); expect(expectTag(s, 'Editing').errors.toelichting).toBeTruthy(); }); it('Submit Goedkeuren with no toelichting moves to Submitting (optional there)', () => { const s = reduce(editingWith('Goedkeuren'), { tag: 'Submit' }); expect(expectTag(s, 'Submitting').data).toEqual({ besluit: 'Goedkeuren', toelichting: undefined, }); }); it('Submit Afwijzen with a toelichting moves to Submitting with the trimmed value', () => { const s = reduce(editingWith('Afwijzen', ' niet erkend '), { tag: 'Submit' }); expect(expectTag(s, 'Submitting').data).toEqual({ besluit: 'Afwijzen', toelichting: 'niet erkend', }); }); it('SubmitConfirmed maps Submitting to Submitted', () => { const submitting = reduce(editingWith('Goedkeuren'), { tag: 'Submit' }); expect(reduce(submitting, { tag: 'SubmitConfirmed' }).tag).toBe('Submitted'); }); it('SubmitFailed maps Submitting to Failed with the error', () => { const submitting = reduce(editingWith('Goedkeuren'), { tag: 'Submit' }); const failed = reduce(submitting, { tag: 'SubmitFailed', error: 'boom' }); expect(failed).toMatchObject({ tag: 'Failed', error: 'boom' }); }); it('Retry re-submits a failure', () => { const submitting = reduce(editingWith('Goedkeuren'), { tag: 'Submit' }); const failed = reduce(submitting, { tag: 'SubmitFailed', error: 'boom' }); expect(reduce(failed, { tag: 'Retry' }).tag).toBe('Submitting'); }); it('Reset returns to the initial editing state', () => { const submitting = reduce(editingWith('Goedkeuren'), { tag: 'Submit' }); expect(reduce(submitting, { tag: 'Reset' })).toEqual(initial); }); });