79215322

Date: 2024-11-22 14:08:54
Score: 1
Natty:
Report link

For my use case, using the dropzone defined in @Lin Du's answer I needed to do some additional work.

import { createEvent, fireEvent, render, screen, waitFor } from '@testing-library/react';

describe('Dropzone tests', () => {
 test('handles multiple file drops', async () => {
   render(<App />);

   const mockFiles = [
     new File(['file1'], 'image1.png', { type: 'image/png' }),
     new File(['file2'], 'image2.png', { type: 'image/png' }),
     new File(['file3'], 'image3.jpg', { type: 'image/jpeg' }),
     new File(['file4'], 'image4.jpg', { type: 'image/jpeg' }),
     new File(['file5'], 'image5.gif', { type: 'image/gif' }),
     new File(['file6'], 'image6.jpg', { type: 'image/jpeg' }),
   ];

   const dropzone = screen.getByTestId('dropzone');
   const dropEvent = createEvent.drop(dropzone);
   
   Object.defineProperty(dropEvent, 'dataTransfer', {
     value: {
       files: mockFiles,
       items: mockFiles.map(file => ({
         kind: 'file',
         type: file.type,
         getAsFile: () => file,
       })),
       types: ['Files'],
     },
   });

   fireEvent(dropzone, dropEvent);

   await waitFor(() => {
     mockFiles.forEach(file => {
       expect(screen.getByText(file.name)).toBeInTheDocument();
     });
   });
 });
});
Reasons:
  • Blacklisted phrase (0.5): I need
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Lin
  • Low reputation (1):
Posted by: Sami