-
-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathRenderControlsPanel.test.tsx
More file actions
257 lines (220 loc) · 8.04 KB
/
Copy pathRenderControlsPanel.test.tsx
File metadata and controls
257 lines (220 loc) · 8.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import { screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { http, HttpResponse } from 'msw'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { RenderControlsPanel } from '@/components/panels/RenderControlsPanel'
import * as sceneActions from '@/lib/io/scene'
import { usePreferencesStore } from '@/lib/stores/preferencesStore'
import { useSelectionStore } from '@/lib/stores/selectionStore'
import { renderWithQuery } from '../helpers'
import { server } from '../msw/server'
vi.mock('@tanstack/react-virtual', () => ({
useVirtualizer: ({ count }: { count: number }) => ({
getTotalSize: () => count * 28,
getVirtualItems: () =>
Array.from({ length: count }, (_, i) => ({
index: i,
start: i * 28,
end: (i + 1) * 28,
size: 28,
key: i,
})),
measure: vi.fn(),
}),
}))
vi.mock('@/lib/io/scene', async () => {
const actual = await vi.importActual<any>('@/lib/io/scene')
return {
...actual,
applyOp: vi.fn(),
queueAutoRender: vi.fn(),
}
})
function sceneWithTextNodes(nodes: any[]) {
const nodeMap: any = {}
nodes.forEach((n) => {
nodeMap[n.id] = {
id: n.id,
transform: { x: 0, y: 0, width: 10, height: 10, rotationDeg: 0 },
visible: true,
kind: { text: n.kind?.text ?? { style: { fontFamilies: ['Arial'] } } },
}
})
return {
epoch: 1,
scene: {
pages: {
p1: { id: 'p1', name: 'P1', nodes: nodeMap },
},
project: { name: 'Proj' },
},
}
}
describe('RenderControlsPanel Font Assignment', () => {
beforeEach(() => {
useSelectionStore.getState().setPage('p1')
useSelectionStore.getState().clear()
usePreferencesStore.getState().setDefaultFont('Arial')
vi.clearAllMocks()
server.use(
http.get('/api/v1/fonts', () =>
HttpResponse.json([
{ familyName: 'Arial', postScriptName: 'Arial', source: 'system', cached: true },
{ familyName: 'Roboto', postScriptName: 'Roboto', source: 'system', cached: true },
{ familyName: 'Custom', postScriptName: 'Custom', source: 'system', cached: true },
]),
),
http.get('/api/v1/scene.json', () =>
HttpResponse.json(
sceneWithTextNodes([
{ id: 't1', kind: { text: { style: { fontFamilies: ['Arial'] } } } },
{ id: 't2', kind: { text: { style: { fontFamilies: ['Arial'] } } } },
]),
),
),
)
})
it('applying a font to a singular text box only updates that box', async () => {
renderWithQuery(<RenderControlsPanel />)
// Select node t1
useSelectionStore.getState().select('t1', false)
// Open font select
const trigger = await screen.findByTestId('render-font-select')
await userEvent.click(trigger)
// Pick "Roboto"
const option = await screen.findByText('Roboto')
await userEvent.click(option)
// Verify applyOp was called for t1
await waitFor(() => expect(sceneActions.applyOp).toHaveBeenCalled())
const lastOp = (sceneActions.applyOp as any).mock.calls[0][0]
expect(lastOp).toHaveProperty('updateNode')
expect(lastOp.updateNode.id).toBe('t1')
expect(lastOp.updateNode.patch.data.text.style.fontFamilies).toEqual(['Roboto'])
})
it('bulk applying a font change (with selection) updates all selected boxes', async () => {
renderWithQuery(<RenderControlsPanel />)
// Select both nodes
useSelectionStore.getState().selectMany(['t1', 't2'])
// Open font select
const trigger = await screen.findByTestId('render-font-select')
await userEvent.click(trigger)
// Pick "Roboto"
const option = await screen.findByText('Roboto')
await userEvent.click(option)
// Verify applyOp was called with a batch
await waitFor(() => expect(sceneActions.applyOp).toHaveBeenCalled())
const lastOp = (sceneActions.applyOp as any).mock.calls[0][0]
expect(lastOp).toHaveProperty('batch')
expect(lastOp.batch.ops).toHaveLength(2)
})
it('changing global font (no selection) updates defaultFont in preferences', async () => {
renderWithQuery(<RenderControlsPanel />)
// No selection
// Open font select
const trigger = await screen.findByTestId('render-font-select')
await userEvent.click(trigger)
// Pick "Custom"
const option = await screen.findByText('Custom')
await userEvent.click(option)
// Verify default font changed
expect(usePreferencesStore.getState().defaultFont).toBe('Custom')
})
it('shows auto when a selected block has no manual font size override', async () => {
server.use(
http.get('/api/v1/scene.json', () =>
HttpResponse.json(
sceneWithTextNodes([
{
id: 't1',
kind: {
text: {
style: { fontFamilies: ['Arial'] },
fontPrediction: { fontSizePx: 66, strokeWidthPx: 0, textColor: [0, 0, 0] },
detectedFontSizePx: 30,
},
},
},
]),
),
),
)
renderWithQuery(<RenderControlsPanel />)
useSelectionStore.getState().select('t1', false)
const input = (await screen.findByTestId('render-font-size')) as HTMLInputElement
await waitFor(() => expect(input.value).toBe(''))
expect(input).toHaveAttribute('placeholder', 'auto')
})
it('typing a font size with no selection applies the size to all text boxes', async () => {
renderWithQuery(<RenderControlsPanel />)
// No selection — global scope
const input = (await screen.findByTestId('render-font-size')) as HTMLInputElement
expect(input).not.toBeDisabled()
await userEvent.clear(input)
await userEvent.type(input, '24')
await waitFor(() => expect(sceneActions.applyOp).toHaveBeenCalled())
const op = (sceneActions.applyOp as any).mock.calls.at(-1)[0]
expect(op).toHaveProperty('batch')
expect(op.batch.ops).toHaveLength(2)
for (const sub of op.batch.ops) {
expect(sub.updateNode.patch.data.text.style.fontSize).toBe(24)
}
})
it('clearing the font size with no selection resets all boxes to auto', async () => {
server.use(
http.get('/api/v1/scene.json', () =>
HttpResponse.json(
sceneWithTextNodes([
{
id: 't1',
kind: { text: { style: { fontFamilies: ['Arial'], fontSize: 20 } } },
},
{
id: 't2',
kind: { text: { style: { fontFamilies: ['Arial'], fontSize: 20 } } },
},
]),
),
),
)
renderWithQuery(<RenderControlsPanel />)
const input = (await screen.findByTestId('render-font-size')) as HTMLInputElement
// Type a value first so the buffer has content to clear.
await userEvent.type(input, '30')
await userEvent.clear(input)
await waitFor(() => {
const calls = (sceneActions.applyOp as any).mock.calls
expect(calls.length).toBeGreaterThan(0)
const op = calls.at(-1)[0]
expect(op).toHaveProperty('batch')
for (const sub of op.batch.ops) {
expect(sub.updateNode.patch.data.text.style.fontSize).toBeNull()
}
})
})
it('opening the font color picker commits effective black as an explicit color', async () => {
server.use(
http.get('/api/v1/scene.json', () =>
HttpResponse.json(
sceneWithTextNodes([
{
id: 't1',
kind: {
text: {
fontPrediction: { fontSizePx: 66, strokeWidthPx: 0, textColor: [0, 0, 0] },
},
},
},
]),
),
),
)
renderWithQuery(<RenderControlsPanel />)
useSelectionStore.getState().select('t1', false)
const trigger = await screen.findByTestId('render-color-trigger')
await userEvent.click(trigger)
await waitFor(() => expect(sceneActions.applyOp).toHaveBeenCalled())
const op = (sceneActions.applyOp as any).mock.calls[0][0]
expect(op.updateNode.id).toBe('t1')
expect(op.updateNode.patch.data.text.style.color).toEqual([0, 0, 0, 255])
})
})