|
| 1 | +import type { JSONContent } from '@tiptap/react'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | +import { resolveInitialPolicyContent } from '../resolve-initial-content'; |
| 4 | + |
| 5 | +const publishedContent: JSONContent[] = [ |
| 6 | + { type: 'paragraph', content: [{ type: 'text', text: 'published' }] }, |
| 7 | +]; |
| 8 | +const draftContent: JSONContent[] = [ |
| 9 | + { type: 'paragraph', content: [{ type: 'text', text: 'draft edit' }] }, |
| 10 | +]; |
| 11 | + |
| 12 | +describe('resolveInitialPolicyContent', () => { |
| 13 | + it('seeds from the targeted version when initialVersionId matches a draft (the bug fix)', () => { |
| 14 | + const result = resolveInitialPolicyContent({ |
| 15 | + initialVersionId: 'pv_draft', |
| 16 | + versions: [ |
| 17 | + { id: 'pv_published', content: publishedContent }, |
| 18 | + { id: 'pv_draft', content: draftContent }, |
| 19 | + ], |
| 20 | + policyContent: publishedContent, |
| 21 | + }); |
| 22 | + |
| 23 | + expect(result).toEqual(draftContent); |
| 24 | + }); |
| 25 | + |
| 26 | + it('falls back to policyContent when no initialVersionId is given', () => { |
| 27 | + const result = resolveInitialPolicyContent({ |
| 28 | + initialVersionId: undefined, |
| 29 | + versions: [{ id: 'pv_draft', content: draftContent }], |
| 30 | + policyContent: publishedContent, |
| 31 | + }); |
| 32 | + |
| 33 | + expect(result).toEqual(publishedContent); |
| 34 | + }); |
| 35 | + |
| 36 | + it('falls back to policyContent when initialVersionId matches no known version', () => { |
| 37 | + const result = resolveInitialPolicyContent({ |
| 38 | + initialVersionId: 'pv_missing', |
| 39 | + versions: [{ id: 'pv_draft', content: draftContent }], |
| 40 | + policyContent: publishedContent, |
| 41 | + }); |
| 42 | + |
| 43 | + expect(result).toEqual(publishedContent); |
| 44 | + }); |
| 45 | + |
| 46 | + it('wraps a single (non-array) version content into an array', () => { |
| 47 | + const singleNode: JSONContent = { type: 'paragraph', content: [{ type: 'text', text: 'one' }] }; |
| 48 | + const result = resolveInitialPolicyContent({ |
| 49 | + initialVersionId: 'pv_draft', |
| 50 | + versions: [{ id: 'pv_draft', content: singleNode }], |
| 51 | + policyContent: publishedContent, |
| 52 | + }); |
| 53 | + |
| 54 | + expect(result).toEqual([singleNode]); |
| 55 | + }); |
| 56 | + |
| 57 | + it('wraps a single (non-array) policyContent fallback into an array', () => { |
| 58 | + const singleNode: JSONContent = { type: 'paragraph', content: [{ type: 'text', text: 'pub' }] }; |
| 59 | + const result = resolveInitialPolicyContent({ |
| 60 | + initialVersionId: undefined, |
| 61 | + versions: [], |
| 62 | + policyContent: singleNode, |
| 63 | + }); |
| 64 | + |
| 65 | + expect(result).toEqual([singleNode]); |
| 66 | + }); |
| 67 | + |
| 68 | + it('returns an empty array for an empty draft version (does not fall back)', () => { |
| 69 | + const result = resolveInitialPolicyContent({ |
| 70 | + initialVersionId: 'pv_draft', |
| 71 | + versions: [{ id: 'pv_draft', content: [] }], |
| 72 | + policyContent: publishedContent, |
| 73 | + }); |
| 74 | + |
| 75 | + expect(result).toEqual([]); |
| 76 | + }); |
| 77 | +}); |
0 commit comments