Skip to content

fireEvent.drop API behaves not as expected about preventDefault in Browser and js env #1327

Open
@electroluxcode

Description

@electroluxcode
  • @testing-library/dom 9.3.4 version:
  • Testing Framework and version: null
  • DOM Environment: 20.0.3

Reproduction:

import { fireEvent, render,act } from '@testing-library/react'
import type { FC } from 'react'

function tick(): Promise<void> {
	return new Promise((resolve) => {
		setTimeout(resolve, 0)
	})
}

async function fireDragDrop(source: Element) {
	await act(async () => {
		fireEvent.dragStart(source)
		fireEvent.dragEnter(source)
		fireEvent.dragOver(source)
		fireEvent.drop(source)
		await tick()
	})
}

const Container: FC = (function Container() {
	return (
	<>
		<div draggable="true"
		data-testid={'Other1'}
		onDragStart={(e)=>{
			e.preventDefault()
			console.log("ondragstart")
		}}
		onDragOver={()=>{
			console.log("onDragOver")
		}}>
		other1
		</div>
	</>
	)
})

describe('drag', () => {
	it('prevent', async () => {
		const rendered = render(<Container />)
		const box1 = (await rendered.findAllByTestId('Other1'))[0]
		await fireDragDrop(box1)
	})
})

What you did:

run it in brower and testing env

Problem description:

when I run this component in the browser, onDragOver isn't executed due to e.preventDefault() in the onDragStart test case,

but onDragOver is executed in testing env after onDragStart

Expect

ii should prevent onDragOver fn in test env

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions