Ir para o conteúdo principal

Perguntas Frequentes

[Tradução Beta Não Oficial]

Esta página foi traduzida por PageTurner AI (beta). Não é oficialmente endossada pelo projeto. Encontrou um erro? Reportar problema →


Como testar upload de arquivos?

Use o utilitário upload do @testing-library/user-event. Ele funciona bem tanto no jsdom quanto no happy-dom.

test('upload file', async () => {
const user = userEvent.setup()

await render(<Uploader />)
const file = new File(['hello'], 'hello.png', {type: 'image/png'})
const input = screen.getByLabelText(/upload file/i)

await user.upload(input, file)

expect(input.files[0]).toBe(file)
expect(input.files.item(0)).toBe(file)
expect(input.files).toHaveLength(1)
})