File size: 6,396 Bytes
d8c5e93 845f5c3 d8c5e93 845f5c3 3cd559b d8c5e93 | 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 | 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('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])
})
})
|