Dom-testing-library: Mocked dataTransfer items are overwritten by rtl

Created on 3 Aug 2020  路  8Comments  路  Source: testing-library/dom-testing-library

  • @testing-library/react version: 10.2.1
  • Testing Framework and version: jasmine version 3.5.0
  • Test runner: karma version 4.0.1
  • DOM Environment: chrome version 83

Relevant code or config:

const dragNdropZone = component.getByTestId('drag-n-drop-zone');
const fileName = 'testFileName';

const fileItem = {
  name: fileName,
  size: 100,
  type: 'file',
};

const fileEntry = {
  name: fileName,
  fullPath: `folder_one/folder_two/${fileName}`,
  file: (resolve) => resolve(fileItem),
};

const itemOne = {
  webkitGetAsEntry: () => fileEntry,
};

const dataTransfer = new DataTransfer();

Object.defineProperty(dataTransfer, 'items', {
  value: [itemOne],
});

fireEvent.drop(dragNdropZone, { dataTransfer });

What you did:

I am trying to test drag-and-drop functionality.

What happened:

rtl overrides items in dataTransfer object assigning an empty array to it

Reproduction:

If we check an event object passed to the onDrop handler of dragNdropZone we'll see that e.dataTransfer.files is an empty array even though we assigned an array with one item.

Problem description:

I am not able to mock items as a prop of dataTransfer object and test drag-and-drop functionality properly. NOTE: mocking worked correctly in rtl version 9.2.0

bug released

Most helpful comment

@kentcdodds @delca85 my apology for the late reply. I have the PR, it is WIP since I still need to test it. if anyone has started work on the issue I'll close my PR. otherwise I will be grateful for any advice on testing and work verification.

All 8 comments

That solution looks good to me. But let's make sure to handle the case where DataTransfer is not supported by the environment as well.

Would you be interested in contributing this fix?

@kentcdodds I am sorry, it seems I need to submit the issue to dom-testing-library

I have moved the issue 馃槃

No worries @Snizhana :)

Thanks @marcosvega91!

I would be glad to work on it.

Does it sound good to all of you?

I'd still love to give @Snizhana a chance to work on it if they'd like. If we don't hear anything in the next day or so then go for it @delca85 馃憤 thanks!

@kentcdodds @delca85 my apology for the late reply. I have the PR, it is WIP since I still need to test it. if anyone has started work on the issue I'll close my PR. otherwise I will be grateful for any advice on testing and work verification.

:tada: This issue has been resolved in version 7.22.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

Was this page helpful?
0 / 5 - 0 ratings