Open History to see the last commit
1import { expect, it } from 'vitest'
2import { checkpointCommit, commitMutation, commitSynchronously, currentCommit, onCommitFailure, onCommitRollback, prepareCommit, queueBeforeMutation, queueCommitEffects, queueMutation, queueProp, queueText, rewindCommit } from '../packages/redact/src/dom/commit'
3
4it('preserves interleaved text and generic mutation order', () => {
5 const first = document.createTextNode('old'), second = document.createTextNode('other')
6 const events: string[] = []
7 const plan = prepareCommit(() => {
8 queueBeforeMutation(() => events.push(`before:${first.data}:${second.data}`))
9 queueText(first, 'one')
10 queueMutation(() => events.push(`first:${first.data}`))
11 queueText(second, '')
12 queueMutation(() => { events.push(`second:${second.data}`); second.data = 'generic' })
13 queueText(first, 'two')
14 queueMutation(() => events.push(`last:${first.data}:${second.data}`))
15 queueCommitEffects(() => events.push(`effect:${first.data}:${second.data}`))
16 })
17 expect([first.data, second.data]).toEqual(['old', 'other'])
18 commitMutation(plan)
19 commitMutation(plan)
20 expect(events).toEqual(['before:old:other', 'first:one', 'second:', 'last:two:generic', 'effect:two:generic'])
21})
22
23it('truncates mixed text records and functions at nested checkpoints', () => {
24 const text = document.createTextNode('old')
25 const events: string[] = []
26 const plan = prepareCommit(() => {
27 queueMutation(() => events.push('kept'))
28 queueText(text, 'prefix')
29 const outer = checkpointCommit()
30 onCommitRollback(() => events.push('undo-outer'))
31 queueText(text, 'discarded-outer')
32 queueMutation(() => events.push('discarded-outer'))
33 const inner = checkpointCommit()
34 onCommitRollback(() => events.push('undo-inner'))
35 queueMutation(() => events.push('discarded-inner'))
36 queueText(text, 'discarded-inner')
37 queueBeforeMutation(() => events.push('discarded-before'))
38 queueCommitEffects(() => events.push('discarded-effect'))
39 rewindCommit(inner)
40 queueMutation(() => events.push('also-discarded'))
41 rewindCommit(outer)
42 queueMutation(() => events.push(`prefix:${text.data}`))
43 queueText(text, 'final')
44 queueMutation(() => events.push(`final:${text.data}`))
45 })
46 expect(text.data).toBe('old')
47 commitMutation(plan)
48 expect(events).toEqual(['undo-inner', 'undo-outer', 'kept', 'prefix:prefix', 'final:final'])
49 expect(text.data).toBe('final')
50})
51
52it('does not reuse an active synchronous plan during nested mutation commits', () => {
53 const outer = document.createTextNode('outer-old'), inner = document.createTextNode('inner-old')
54 const events: string[] = []
55 commitSynchronously(() => {
56 queueBeforeMutation(() => events.push('outer-before'))
57 queueMutation(() => {
58 events.push('outer-start')
59 commitSynchronously(() => {
60 queueBeforeMutation(() => events.push('inner-before'))
61 queueText(inner, 'inner-new')
62 queueMutation(() => events.push(`inner-mutate:${inner.data}`))
63 queueCommitEffects(() => events.push('inner-effect'))
64 })
65 queueCommitEffects(() => events.push('outer-added-effect'))
66 events.push('outer-end')
67 })
68 queueText(outer, 'outer-new')
69 queueMutation(() => events.push(`outer-mutate:${outer.data}:${inner.data}`))
70 queueCommitEffects(() => events.push('outer-effect'))
71 })
72 expect(events).toEqual(['outer-before', 'outer-start', 'inner-before', 'inner-mutate:inner-new', 'inner-effect', 'outer-end', 'outer-mutate:outer-new:inner-new', 'outer-effect', 'outer-added-effect'])
73 expect(currentCommit).toBeNull()
74 commitSynchronously(() => queueText(outer, 'next'))
75 expect([outer.data, inner.data]).toEqual(['next', 'inner-new'])
76})
77
78it('clears failed preparation before reusing a plan, including reentrant recovery', () => {
79 const discarded = document.createTextNode('untouched'), recovered = document.createTextNode('old')
80 const events: string[] = []
81 expect(() => commitSynchronously(() => {
82 queueBeforeMutation(() => events.push('discarded-before'))
83 queueText(discarded, 'discarded')
84 queueCommitEffects(() => events.push('discarded-effect'))
85 onCommitFailure(() => commitSynchronously(() => {
86 queueText(recovered, 'recovered')
87 queueCommitEffects(() => events.push('recovery'))
88 }))
89 throw Error('prepare failed')
90 })).toThrow('prepare failed')
91 expect(currentCommit).toBeNull()
92 commitSynchronously(() => {
93 queueBeforeMutation(() => events.push('next-before'))
94 queueText(recovered, 'next')
95 queueCommitEffects(() => events.push('next-effect'))
96 })
97 expect(discarded.data).toBe('untouched')
98 expect(recovered.data).toBe('next')
99 expect(events).toEqual(['recovery', 'next-before', 'next-effect'])
100})
101
102it('isolates a nested mutation failure from the parent plan and the next commit', () => {
103 const outer = document.createTextNode('old'), inner = document.createTextNode('old')
104 const events: string[] = []
105 commitSynchronously(() => {
106 queueMutation(() => {
107 expect(() => commitSynchronously(() => {
108 queueText(inner, 'committed-before-error')
109 queueMutation(() => { throw Error('mutation failed') })
110 queueText(inner, 'discarded-after-error')
111 queueCommitEffects(() => events.push('discarded-effect'))
112 })).toThrow('mutation failed')
113 queueCommitEffects(() => events.push('parent-effect'))
114 })
115 queueText(outer, 'parent-complete')
116 })
117 commitSynchronously(() => queueMutation(() => events.push('next')))
118 expect([outer.data, inner.data]).toEqual(['parent-complete', 'committed-before-error'])
119 expect(events).toEqual(['parent-effect', 'next'])
120 expect(currentCommit).toBeNull()
121})
122
123it('preserves property, text and function record boundaries including function-valued props', () => {
124 const button = document.createElement('button'), text = document.createTextNode('old')
125 button.appendChild(text)
126 button.setAttribute('data-state', 'old')
127 const events: string[] = []
128 const plan = prepareCommit(() => {
129 queueProp(button, 'data-state', 'one', 'old', false)
130 queueText(text, 'one')
131 queueMutation(() => events.push(`${button.getAttribute('data-state')}:${text.data}`))
132 queueProp(button, 'onClick', () => events.push('clicked'), undefined, false)
133 queueProp(button, 'disabled', true, false, false)
134 queueMutation(() => events.push(`disabled:${button.disabled}`))
135 queueText(text, 'two')
136 queueProp(button, 'disabled', false, true, false)
137 queueProp(button, 'data-state', null, 'one', false)
138 queueMutation(() => events.push(`${button.hasAttribute('data-state')}:${text.data}:${button.disabled}`))
139 })
140 expect(button.textContent).toBe('old')
141 expect(button.getAttribute('data-state')).toBe('old')
142 commitMutation(plan)
143 expect(events).toEqual(['one:one', 'disabled:true', 'false:two:false'])
144 button.click()
145 expect(events).toEqual(['one:one', 'disabled:true', 'false:two:false', 'clicked'])
146})
147
148it('rewinds property records without leaving operand fragments in the mutation tape', () => {
149 const div = document.createElement('div'), text = document.createTextNode('old')
150 div.appendChild(text)
151 const events: string[] = []
152 const plan = prepareCommit(() => {
153 queueText(text, 'prefix')
154 queueProp(div, 'title', 'kept', undefined, false)
155 const checkpoint = checkpointCommit()
156 queueProp(div, 'title', 'discarded', 'kept', false)
157 queueText(text, 'discarded')
158 queueMutation(() => events.push('discarded'))
159 rewindCommit(checkpoint)
160 queueMutation(() => events.push(`${div.title}:${text.data}`))
161 queueProp(div, 'title', 'final', 'kept', false)
162 queueText(text, 'final')
163 })
164 commitMutation(plan)
165 expect(events).toEqual(['kept:prefix'])
166 expect([div.title, text.data]).toEqual(['final', 'final'])
167})