Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
electroluxcode opened this issue Jul 14, 2024 · 2 comments · May be fixed by #1328
Open

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

electroluxcode opened this issue Jul 14, 2024 · 2 comments · May be fixed by #1328

Comments

@electroluxcode
Copy link

electroluxcode commented Jul 14, 2024

  • @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

@electroluxcode electroluxcode changed the title fireEvent.drop API behaves inconsistently with browser behavior fireEvent.drop API behaves not as expected Jul 14, 2024
@electroluxcode electroluxcode changed the title fireEvent.drop API behaves not as expected fireEvent.drop API behaves not as expected about preventDefault in Browser and js env Jul 15, 2024
electroluxcode added a commit to electroluxcode/dom-testing-library that referenced this issue Jul 15, 2024
@electroluxcode electroluxcode linked a pull request Jul 15, 2024 that will close this issue
4 tasks
@MatanBobi
Copy link
Member

Hi @electroluxcode, thanks for taking the time to open this.
fireEvent is a very low level API, all it does is dispatch the event based on the configuration.
I think you should be trying your use case with user-event instead.
To top that, I'm not sure I understand the reasoning behind calling e.preventDefault() in onDragStart (afaik, usually calling e.preventDefault in drag scenarios only has meaning in onDragOver or onDragEnter to mark that an item can be dropped somewhere).
I'm also not sure the browser's behavior is following the spec and I couldn't find any mention about that. Are you familiar with any spec that defines this behavior?

@electroluxcode
Copy link
Author

electroluxcode commented Jul 16, 2024

Hi @electroluxcode, thanks for taking the time to open this. fireEvent is a very low level API, all it does is dispatch the event based on the configuration. I think you should be trying your use case with user-event instead. To top that, I'm not sure I understand the reasoning behind calling e.preventDefault() in onDragStart (afaik, usually calling e.preventDefault in drag scenarios only has meaning in onDragOver or onDragEnter to mark that an item can be dropped somewhere). I'm also not sure the browser's behavior is following the spec and I couldn't find any mention about that. Are you familiar with any spec that defines this behavior?

@MatanBobi Thank you very much for your reply. The reason I call e.preventDefault() in onDragStart is to avoid dragging some page elements that shouldn't be draggable. You can refer to

https://github.com/react-dnd/react-dnd/blob/7c88c37489a53b5ac98699c46a506a8e085f1c03/packages/backend-html5/src/HTML5BackendImpl.ts#L551-L553

This is a snippet of react-dnd code where this logic is encapsulated within the onDragStart method.
I wanted to write test cases for this code snippet, but found that the onDragStart event behaves unexpectedly during testing.

and after reading your reply, I looked into user-event, but it seems there are no methods related to dragStart or dragOver.

and regarding the browser's behavior you mentioned, you can refer to https://html.spec.whatwg.org/multipage/dnd.html#dndevents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants