question-draft-store.client.spec.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /** Question-composer Session store behavior. */
  2. import { describe, expect, it } from 'vitest'
  3. import { createQuestionDraftStore, type QuestionDraftProgress } from '../src/client/draft-store.ts'
  4. const FIRST: QuestionDraftProgress = {
  5. index: 1,
  6. drafts: [{ selected: ['Fast'], custom: '', skipped: false }],
  7. }
  8. describe('createQuestionDraftStore', () => {
  9. it('keeps one request progress and ignores cleanup from an obsolete request', () => {
  10. const store = createQuestionDraftStore().create('session-one')
  11. store.actions.replace('question:one', FIRST)
  12. expect(store.getSnapshot()).toEqual({ requestKey: 'question:one', progress: FIRST })
  13. store.actions.clear('question:older')
  14. expect(store.getSnapshot()).toEqual({ requestKey: 'question:one', progress: FIRST })
  15. store.actions.clear('question:one')
  16. expect(store.getSnapshot()).toEqual({ progress: { index: 0, drafts: [] } })
  17. })
  18. it('replaces the previous request atomically instead of accumulating drafts', () => {
  19. const store = createQuestionDraftStore().create('session-one')
  20. const second: QuestionDraftProgress = {
  21. index: 0,
  22. drafts: [{ selected: [], custom: 'Careful', skipped: false }],
  23. }
  24. store.actions.replace('question:one', FIRST)
  25. store.actions.replace('question:two', second)
  26. expect(store.getSnapshot()).toEqual({ requestKey: 'question:two', progress: second })
  27. })
  28. })