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

Can't test using testing library #157

Open
guilhermeccrz opened this issue Jun 10, 2021 · 3 comments
Open

Can't test using testing library #157

guilhermeccrz opened this issue Jun 10, 2021 · 3 comments

Comments

@guilhermeccrz
Copy link

I want to test a upload response using react dropzone upload and testing-library, but i can't. When i try to use userEvent.upload, react dropzone breaks.

 render(<View/>);

        await new Promise((r) => setTimeout(r, 500));

        const infos = within(screen.getByTestId('infos'));
        await waitFor(() => expect(infos.getByText('R$ 2.400,00')).toBeInTheDocument());

        const uploadForm = screen.getAllByTestId('upload-form');
        fireEvent.drop(uploadForm[3], {
            dataTransfer: {
                files: [new File(['(⌐□_□)'], 'chucknorris.png', { type: 'image/png' })]
            }
        });

        await waitFor(() =>
            expect(screen.getByText('chucknorris.jpg enviado com sucesso!')).toBeInTheDocument()
        );

Open Proposal - Upload Files › test document upload success for holerite

TypeError: URL.createObjectURL is not a function

  at node_modules/react-dropzone-uploader/dist/react-dropzone-uploader.js:14:15637
@easynowbaby
Copy link

I'm having the same problem, any solutions?

@chrismatheson
Copy link

@easynowbaby I believe this is a limitation of the jest testing environment (assuming that what your using) jsdom/jsdom#1721 which does not implement these methods.

I did a small work around like so

URL.createObjectURL = jest.fn();
URL.revokeObjectURL = jest.fn();

however I also am noticing some other errors TypeError: t.scroll is not a function which have to do with (probably) jsdom vs real browser differences.

@nihalvaria
Copy link

nihalvaria commented Feb 14, 2022

I solved the TypeError: t.scroll is not a function issue, with the following :

  const defaultDivScroll = window.HTMLDivElement.prototype.scroll;

  beforeEach(() => {
    window.HTMLDivElement.prototype.scroll = jest.fn();
  });

  afterEach(() => {
    window.HTMLDivElement.prototype.scroll = defaultDivScroll;
  });

I used HTMLDivElement because I spread my dropzoneProps over div element :

    <div {...dropzoneProps}>
      {input}
      {previews}
      {submitButton}
    </div>

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

No branches or pull requests

4 participants