Skip to content

fireEvent.drop is not including event.dataTransfer #339

Closed
@christopherschroer

Description

@christopherschroer
  • react-testing-library version: 6.0.1
  • react version: 16.8.1
  • node version: 8.9.4
  • npm version: 5.6.0
  • jest version: 24.0.0

Relevant code or config:

Below is a simplified, generic version of my code

export const FileDropZone = props => {

    const handleDrop = useCallback(
        event => {
            event.stopPropagation();
            event.preventDefault();
            handleFiles(event.dataTransfer.files);
        },
        [handleFiles]
    );

    // Rest of code

    return (
        <div
            onDragEnter={handleDragEnter}
            onDragLeave={handleDragLeave}
            onDrop={handleDrop}
        >
            Drag File Here
        </div>
    );
}

describe('FileDropZone', () => {
    test('should handle a file being dropped in the drop zone', () => {
        const file = new File(['(⌐□_□)'], 'chucknorris.png', {
            type: 'image/png',
        })
        const {getByText, queryByText} = render(<FileDropZone {...props} />);
        expect(queryByText('chucknorris.png')).toBeNull();
        const dropZone = getByText('Drag File Here');
        act(() => {
            fireEvent.drop(dropZone, {dataTransfer: {files: [file]}});
        });
        expect(queryByText('chucknorris.png')).toBeInTheDocument();
    })
})

What you did:

I am trying to test a drop event that is used for uploading files via drag-and-drop

What happened:

When I run my unit test, the event is firing, but I get the following error

TypeError: Cannot read property 'files' of undefined

When I do a console.log of the event it shows that event.dataTransfer is undefined.

Reproduction:

Unable to provide a link to code repository due to the repo being private

Problem description:

Unable to test file upload via drag-and-drop using react-testing-library

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions