Files
management/Management/tests/source-upload.test.ts

51 lines
1.9 KiB
TypeScript

import { act, createElement } from 'react'
import { createRoot } from 'react-dom/client'
import { expect, it, vi } from 'vitest'
import { ComponentSourceUpload } from '../src/components/ComponentSourceUpload'
it('keeps URDF selectable while STEP parses and prevents duplicate pickers until cancel or selection', async () => {
vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true)
const host = document.createElement('div')
document.body.append(host)
const root = createRoot(host)
const onFiles = vi.fn()
const click = vi.spyOn(HTMLInputElement.prototype, 'click').mockImplementation(() => {})
try {
await act(async () =>
root.render(
createElement(
'div',
{},
createElement(ComponentSourceUpload, { kind: 'step', busy: true, onFiles }),
createElement(ComponentSourceUpload, { kind: 'urdf', busy: false, onFiles }),
),
),
)
const buttons = host.querySelectorAll('button')
const inputs = host.querySelectorAll('input')
expect(buttons[0].disabled).toBe(true)
expect(buttons[1].disabled).toBe(false)
await act(async () => {
buttons[1].click()
buttons[1].click()
buttons[1].click()
})
expect(click).toHaveBeenCalledTimes(1)
expect(buttons[1].disabled).toBe(true)
await act(async () => inputs[1].dispatchEvent(new Event('cancel')))
expect(buttons[1].disabled).toBe(false)
await act(async () => buttons[1].click())
expect(click).toHaveBeenCalledTimes(2)
const file = new File(['<robot/>'], 'test.urdf')
Object.defineProperty(inputs[1], 'files', { value: [file], configurable: true })
await act(async () => inputs[1].dispatchEvent(new Event('change', { bubbles: true })))
expect(onFiles).toHaveBeenCalledWith([file])
expect(buttons[1].disabled).toBe(false)
} finally {
await act(async () => root.unmount())
host.remove()
click.mockRestore()
vi.unstubAllGlobals()
}
})