React Testing Library - Fileupload component

UI Components for React
Post Reply
chipun
Posts: 3
Joined: 06 Apr 2023, 15:18

06 Apr 2023, 15:25

Hello,

I tried to test a file upload component with React Testing Library (RTL) and TypeScript. It seems I cannot upload any file to the file upload component when I ran the test. How do you test accessing the FileUploadHandlerEvent of fileupload component?

Code: Select all

    it('should display the selected file name', async () => {
        const { getByTestId } = render(<FileUpload />);

        const fileInput = getByTestId('file-input');
        const file = new File(['test file content'], 'test.txt', {
            type: 'text/plain',
        });
        fireEvent.change(fileInput, { target: { files: [file] } });

        const fileName = getByTestId('file-name');
        expect(fileName.textContent).toBe('test.txt');
    });

Post Reply

Return to “PrimeReact”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 21 guests