-
Notifications
You must be signed in to change notification settings - Fork 671
Expand file tree
/
Copy pathLoad3dViewerContent.test.ts
More file actions
357 lines (312 loc) · 10.5 KB
/
Copy pathLoad3dViewerContent.test.ts
File metadata and controls
357 lines (312 loc) · 10.5 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import { render, screen } from '@testing-library/vue'
import userEvent from '@testing-library/user-event'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { ref } from 'vue'
import { createI18n } from 'vue-i18n'
import Load3dViewerContent from '@/components/load3d/Load3dViewerContent.vue'
import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
import { createMockLGraphNode } from '@/utils/__tests__/litegraphTestUtils'
class NoopMutationObserver {
observe() {}
disconnect() {}
takeRecords(): MutationRecord[] {
return []
}
}
const {
viewerState,
dragState,
capturedDragOptions,
dialogCloseMock,
serviceSourceLoad3d,
getLoad3dAsyncMock
} = vi.hoisted(() => ({
viewerState: {
current: null as ReturnType<typeof buildViewerStub> | null
},
dragState: {
current: null as ReturnType<typeof buildDragStub> | null
},
capturedDragOptions: {
current: null as { onModelDrop?: (file: File) => Promise<void> } | null
},
dialogCloseMock: vi.fn(),
serviceSourceLoad3d: {
current: null as unknown
},
getLoad3dAsyncMock: vi.fn()
}))
function buildViewerStub() {
return {
backgroundColor: ref('#282828'),
showGrid: ref(true),
cameraType: ref('perspective'),
fov: ref(75),
lightIntensity: ref(1),
backgroundImage: ref(''),
hasBackgroundImage: ref(false),
backgroundRenderMode: ref('tiled'),
upDirection: ref('original'),
materialMode: ref('original'),
gizmoEnabled: ref(false),
gizmoMode: ref('translate'),
isPreview: ref(false),
isStandaloneMode: ref(false),
canUseGizmo: ref(true),
canUseLighting: ref(true),
canExport: ref(true),
sourceFormat: ref<string | null>(null),
materialModes: ref(['original', 'normal', 'wireframe']),
animations: ref<Array<{ name: string; index: number }>>([]),
playing: ref(false),
selectedSpeed: ref(1),
selectedAnimation: ref(0),
animationProgress: ref(0),
animationDuration: ref(0),
initializeViewer: vi.fn<AnyMockProcedure>(async () => undefined),
initializeStandaloneViewer: vi.fn<AnyMockProcedure>(async () => undefined),
exportModel: vi.fn(),
handleResize: vi.fn(),
handleMouseEnter: vi.fn(),
handleMouseLeave: vi.fn(),
restoreInitialState: vi.fn(),
refreshViewport: vi.fn(),
handleBackgroundImageUpdate: vi.fn(),
handleModelDrop: vi.fn<AnyMockProcedure>(async () => undefined),
handleSeek: vi.fn(),
resetGizmoTransform: vi.fn()
}
}
function buildDragStub() {
return {
isDragging: ref(false),
dragMessage: ref(''),
handleDragOver: vi.fn(),
handleDragLeave: vi.fn(),
handleDrop: vi.fn()
}
}
vi.mock('@/composables/useLoad3dViewer', () => ({
useLoad3dViewer: () => viewerState.current
}))
vi.mock('@/composables/useLoad3dDrag', () => ({
useLoad3dDrag: (opts: { onModelDrop?: (file: File) => Promise<void> }) => {
capturedDragOptions.current = opts
return dragState.current
}
}))
vi.mock('@/services/load3dService', () => ({
useLoad3dService: () => ({
getOrCreateViewerSync: () => viewerState.current,
getLoad3dAsync: getLoad3dAsyncMock
})
}))
vi.mock('@/stores/dialogStore', () => ({
useDialogStore: () => ({ closeDialog: dialogCloseMock })
}))
const i18n = createI18n({
legacy: false,
locale: 'en',
messages: {
en: {
g: { cancel: 'Cancel' }
}
}
})
type RenderOptions = {
node?: LGraphNode
modelUrl?: string
viewerOverrides?: Partial<ReturnType<typeof buildViewerStub>>
dragOverrides?: Partial<ReturnType<typeof buildDragStub>>
}
const MOCK_NODE = createMockLGraphNode({ id: 'node-1', type: 'Load3D' })
async function renderViewerContent(options: RenderOptions = {}) {
const viewerStub = buildViewerStub()
if (options.viewerOverrides) {
Object.assign(viewerStub, options.viewerOverrides)
}
viewerState.current = viewerStub
const dragStub = buildDragStub()
if (options.dragOverrides) {
Object.assign(dragStub, options.dragOverrides)
}
dragState.current = dragStub
getLoad3dAsyncMock.mockResolvedValue(serviceSourceLoad3d.current)
const result = render(Load3dViewerContent, {
props: {
node: options.node,
modelUrl: options.modelUrl
},
global: {
plugins: [i18n],
stubs: {
AnimationControls: {
name: 'AnimationControls',
template: '<div data-testid="animation-controls" />'
},
CameraControls: {
name: 'CameraControls',
template: '<div data-testid="camera-controls" />'
},
ExportControls: {
name: 'ExportControls',
template: '<div data-testid="export-controls" />'
},
GizmoControls: {
name: 'GizmoControls',
template: '<div data-testid="gizmo-controls" />'
},
LightControls: {
name: 'LightControls',
template: '<div data-testid="light-controls" />'
},
ModelControls: {
name: 'ModelControls',
template: '<div data-testid="model-controls" />'
},
SceneControls: {
name: 'SceneControls',
template: '<div data-testid="scene-controls" />'
},
Button: {
name: 'Button',
template: '<button type="button"><slot /></button>'
}
}
}
})
return {
...result,
viewer: viewerStub,
drag: dragStub,
user: userEvent.setup()
}
}
describe('Load3dViewerContent', () => {
beforeEach(() => {
vi.stubGlobal('MutationObserver', NoopMutationObserver)
viewerState.current = null
dragState.current = null
capturedDragOptions.current = null
serviceSourceLoad3d.current = null
})
describe('initialization', () => {
it('invokes initializeStandaloneViewer when a modelUrl is provided without a node', async () => {
const { viewer } = await renderViewerContent({
modelUrl: 'api/view?filename=cube.glb'
})
await vi.waitFor(() =>
expect(viewer.initializeStandaloneViewer).toHaveBeenCalledWith(
expect.any(HTMLElement),
'api/view?filename=cube.glb'
)
)
expect(viewer.initializeViewer).not.toHaveBeenCalled()
})
it('invokes initializeViewer with the source load3d when a node is provided', async () => {
const source = { id: 'source-load3d' }
serviceSourceLoad3d.current = source
const { viewer } = await renderViewerContent({ node: MOCK_NODE })
await vi.waitFor(() =>
expect(viewer.initializeViewer).toHaveBeenCalledWith(
expect.any(HTMLElement),
source
)
)
expect(getLoad3dAsyncMock).toHaveBeenCalledWith(MOCK_NODE)
expect(viewer.initializeStandaloneViewer).not.toHaveBeenCalled()
})
it('skips initializeViewer if the source load3d cannot be resolved', async () => {
serviceSourceLoad3d.current = null
const { viewer } = await renderViewerContent({ node: MOCK_NODE })
await vi.waitFor(() =>
expect(getLoad3dAsyncMock).toHaveBeenCalledWith(MOCK_NODE)
)
expect(viewer.initializeViewer).not.toHaveBeenCalled()
})
})
describe('capability gating', () => {
it('hides LightControls when canUseLighting is false', async () => {
await renderViewerContent({
node: MOCK_NODE,
viewerOverrides: { canUseLighting: ref(false) }
})
expect(screen.queryByTestId('light-controls')).not.toBeInTheDocument()
})
it('hides GizmoControls when canUseGizmo is false', async () => {
await renderViewerContent({
node: MOCK_NODE,
viewerOverrides: { canUseGizmo: ref(false) }
})
expect(screen.queryByTestId('gizmo-controls')).not.toBeInTheDocument()
})
it('hides ExportControls when canExport is false', async () => {
await renderViewerContent({
node: MOCK_NODE,
viewerOverrides: { canExport: ref(false) }
})
expect(screen.queryByTestId('export-controls')).not.toBeInTheDocument()
})
it('renders all capability-gated controls when all flags are true', async () => {
await renderViewerContent({ node: MOCK_NODE })
expect(screen.getByTestId('light-controls')).toBeInTheDocument()
expect(screen.getByTestId('gizmo-controls')).toBeInTheDocument()
expect(screen.getByTestId('export-controls')).toBeInTheDocument()
})
})
describe('animation controls', () => {
it('hides AnimationControls when the animation list is empty', async () => {
await renderViewerContent({ node: MOCK_NODE })
expect(screen.queryByTestId('animation-controls')).not.toBeInTheDocument()
})
it('shows AnimationControls when animations are present', async () => {
await renderViewerContent({
node: MOCK_NODE,
viewerOverrides: {
animations: ref([{ name: 'idle', index: 0 }])
}
})
expect(screen.getByTestId('animation-controls')).toBeInTheDocument()
})
})
describe('drag overlay', () => {
it('is hidden by default', async () => {
await renderViewerContent({ node: MOCK_NODE })
expect(screen.queryByText(/drag/i)).not.toBeInTheDocument()
})
it('renders the drag message when useLoad3dDrag reports dragging', async () => {
await renderViewerContent({
node: MOCK_NODE,
dragOverrides: {
isDragging: ref(true),
dragMessage: ref('Drop to load')
}
})
expect(screen.getByText('Drop to load')).toBeInTheDocument()
})
})
describe('drag integration', () => {
it('routes a dropped file through useLoad3dDrag back to viewer.handleModelDrop', async () => {
const { viewer } = await renderViewerContent({ node: MOCK_NODE })
const file = new File(['cube'], 'cube.glb')
await capturedDragOptions.current!.onModelDrop!(file)
expect(viewer.handleModelDrop).toHaveBeenCalledWith(file)
})
})
describe('cancel button', () => {
it('closes the dialog in node mode and restores initial viewer state', async () => {
const { user, viewer } = await renderViewerContent({ node: MOCK_NODE })
await user.click(screen.getByRole('button', { name: /Cancel/ }))
expect(viewer.restoreInitialState).toHaveBeenCalledOnce()
expect(dialogCloseMock).toHaveBeenCalledOnce()
})
it('closes the dialog in standalone mode without touching initial state', async () => {
const { user, viewer } = await renderViewerContent({
modelUrl: 'api/view?filename=cube.glb'
})
await user.click(screen.getByRole('button', { name: /Cancel/ }))
expect(viewer.restoreInitialState).not.toHaveBeenCalled()
expect(dialogCloseMock).toHaveBeenCalledOnce()
})
})
})