Skip to content

Commit f247c2c

Browse files
author
Alexander
committed
Покрываем тестами
1 parent 5c7b809 commit f247c2c

File tree

6 files changed

+1099
-1
lines changed

6 files changed

+1099
-1
lines changed
Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
import type { CanvasFullState } from '../../../../src/editor/history-manager'
2+
import {
3+
cloneState,
4+
prepareStatesForDiff,
5+
stableStringify
6+
} from '../../../../src/editor/history-manager/diff-normalization'
7+
import { createHistoryCanvasState } from '../../../test-utils/history-helpers'
8+
9+
describe('history-manager/diff-normalization', () => {
10+
it('stableStringify возвращает одинаковую строку для объектов с разным порядком ключей', () => {
11+
const firstValue = {
12+
b: 2,
13+
a: 1,
14+
nested: {
15+
d: 4,
16+
c: 3
17+
}
18+
}
19+
const secondValue = {
20+
nested: {
21+
c: 3,
22+
d: 4
23+
},
24+
a: 1,
25+
b: 2
26+
}
27+
28+
expect(stableStringify({ value: firstValue })).toBe(stableStringify({ value: secondValue }))
29+
})
30+
31+
it('cloneState создаёт независимую глубокую копию состояния', () => {
32+
const state = createHistoryCanvasState({
33+
overrides: {
34+
objects: [{
35+
id: 'object-1',
36+
left: 10,
37+
customData: {
38+
nested: {
39+
value: 1
40+
}
41+
}
42+
}] as CanvasFullState['objects']
43+
}
44+
})
45+
46+
const clonedState = cloneState({ state })
47+
const [clonedObject] = clonedState.objects as Array<Record<string, unknown>>
48+
const clonedCustomData = clonedObject.customData as {
49+
nested: {
50+
value: number
51+
}
52+
}
53+
54+
clonedCustomData.nested.value = 5
55+
56+
const [originalObject] = state.objects as Array<Record<string, unknown>>
57+
const originalCustomData = originalObject.customData as {
58+
nested: {
59+
value: number
60+
}
61+
}
62+
63+
expect(originalCustomData.nested.value).toBe(1)
64+
})
65+
66+
it('prepareStatesForDiff игнорирует backgroundColor если фон текста выключен', () => {
67+
const prevState = createHistoryCanvasState({
68+
overrides: {
69+
objects: [{
70+
id: 'text-1',
71+
type: 'textbox',
72+
backgroundOpacity: 0,
73+
backgroundColor: '#ffffff',
74+
textBackgroundColor: ''
75+
}] as CanvasFullState['objects']
76+
}
77+
})
78+
const nextState = createHistoryCanvasState({
79+
overrides: {
80+
objects: [{
81+
id: 'text-1',
82+
type: 'textbox',
83+
backgroundOpacity: 0,
84+
backgroundColor: '#000000',
85+
textBackgroundColor: '#111111'
86+
}] as CanvasFullState['objects']
87+
}
88+
})
89+
90+
const normalized = prepareStatesForDiff({
91+
prevState,
92+
nextState
93+
})
94+
const [prevTextObject] = normalized.prevState.objects as Array<Record<string, unknown>>
95+
const [nextTextObject] = normalized.nextState.objects as Array<Record<string, unknown>>
96+
97+
expect(prevTextObject.backgroundColor).toBeNull()
98+
expect(nextTextObject.backgroundColor).toBeNull()
99+
expect(prevTextObject.textBackgroundColor).toBeNull()
100+
expect(nextTextObject.textBackgroundColor).toBeNull()
101+
})
102+
103+
it('prepareStatesForDiff сохраняет backgroundColor если фон текста включен', () => {
104+
const prevState = createHistoryCanvasState({
105+
overrides: {
106+
objects: [{
107+
id: 'text-1',
108+
type: 'textbox',
109+
backgroundOpacity: 1,
110+
backgroundColor: '#ffffff'
111+
}] as CanvasFullState['objects']
112+
}
113+
})
114+
const nextState = createHistoryCanvasState({
115+
overrides: {
116+
objects: [{
117+
id: 'text-1',
118+
type: 'textbox',
119+
backgroundOpacity: 1,
120+
backgroundColor: '#000000'
121+
}] as CanvasFullState['objects']
122+
}
123+
})
124+
125+
const normalized = prepareStatesForDiff({
126+
prevState,
127+
nextState
128+
})
129+
const [prevTextObject] = normalized.prevState.objects as Array<Record<string, unknown>>
130+
const [nextTextObject] = normalized.nextState.objects as Array<Record<string, unknown>>
131+
132+
expect(prevTextObject.backgroundColor).toBe('#ffffff')
133+
expect(nextTextObject.backgroundColor).toBe('#000000')
134+
})
135+
136+
it('prepareStatesForDiff игнорирует resize canvas без изменения монтажной области', () => {
137+
const prevState = createHistoryCanvasState({
138+
overrides: {
139+
width: 800,
140+
height: 600,
141+
objects: [{
142+
id: 'montage-area',
143+
type: 'rect',
144+
width: 400,
145+
height: 300,
146+
left: 100,
147+
top: 50
148+
}] as CanvasFullState['objects']
149+
}
150+
})
151+
const nextState = createHistoryCanvasState({
152+
overrides: {
153+
width: 900,
154+
height: 700,
155+
objects: [{
156+
id: 'montage-area',
157+
type: 'rect',
158+
width: 400,
159+
height: 300,
160+
left: 100,
161+
top: 50
162+
}] as CanvasFullState['objects']
163+
}
164+
})
165+
166+
const normalized = prepareStatesForDiff({
167+
prevState,
168+
nextState
169+
})
170+
171+
expect(normalized.nextState.width).toBe(800)
172+
expect(normalized.nextState.height).toBe(600)
173+
})
174+
175+
it('prepareStatesForDiff игнорирует одинаковый сдвиг монтажной области и пользовательского объекта', () => {
176+
const prevState = createHistoryCanvasState({
177+
overrides: {
178+
objects: [
179+
{
180+
id: 'montage-area',
181+
type: 'rect',
182+
left: 100,
183+
top: 50,
184+
width: 400,
185+
height: 300
186+
},
187+
{
188+
id: 'object-1',
189+
type: 'rect',
190+
left: 120,
191+
top: 70
192+
}
193+
] as CanvasFullState['objects']
194+
}
195+
})
196+
const nextState = createHistoryCanvasState({
197+
overrides: {
198+
objects: [
199+
{
200+
id: 'montage-area',
201+
type: 'rect',
202+
left: 140,
203+
top: 90,
204+
width: 400,
205+
height: 300
206+
},
207+
{
208+
id: 'object-1',
209+
type: 'rect',
210+
left: 160,
211+
top: 110
212+
}
213+
] as CanvasFullState['objects']
214+
}
215+
})
216+
217+
const normalized = prepareStatesForDiff({
218+
prevState,
219+
nextState
220+
})
221+
const [, normalizedObject] = normalized.nextState.objects as Array<Record<string, unknown>>
222+
223+
expect(normalizedObject.left).toBe(120)
224+
expect(normalizedObject.top).toBe(70)
225+
})
226+
227+
it('prepareStatesForDiff сохраняет реальное изменение объекта поверх общего сдвига', () => {
228+
const prevState = createHistoryCanvasState({
229+
overrides: {
230+
objects: [
231+
{
232+
id: 'montage-area',
233+
type: 'rect',
234+
left: 100,
235+
top: 50,
236+
width: 400,
237+
height: 300
238+
},
239+
{
240+
id: 'object-1',
241+
type: 'rect',
242+
left: 120,
243+
top: 70
244+
}
245+
] as CanvasFullState['objects']
246+
}
247+
})
248+
const nextState = createHistoryCanvasState({
249+
overrides: {
250+
objects: [
251+
{
252+
id: 'montage-area',
253+
type: 'rect',
254+
left: 140,
255+
top: 90,
256+
width: 400,
257+
height: 300
258+
},
259+
{
260+
id: 'object-1',
261+
type: 'rect',
262+
left: 165,
263+
top: 118
264+
}
265+
] as CanvasFullState['objects']
266+
}
267+
})
268+
269+
const normalized = prepareStatesForDiff({
270+
prevState,
271+
nextState
272+
})
273+
const [, normalizedObject] = normalized.nextState.objects as Array<Record<string, unknown>>
274+
275+
expect(normalizedObject.left).toBe(125)
276+
expect(normalizedObject.top).toBe(78)
277+
})
278+
})

0 commit comments

Comments
 (0)