Skip to content

Commit

Permalink
test(FileUploader): fix failing tests and update snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
Shankar-CodeJunkie committed Oct 11, 2024
1 parent 3110b90 commit 0e5c7ab
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
5 changes: 2 additions & 3 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3314,9 +3314,7 @@ Map {
"render": [Function],
},
"FileUploader" => Object {
"contextType": Object {
"$$typeof": Symbol(react.context),
},
"$$typeof": Symbol(react.forward_ref),
"propTypes": Object {
"accept": Object {
"args": Array [
Expand Down Expand Up @@ -3396,6 +3394,7 @@ Map {
"type": "oneOf",
},
},
"render": [Function],
},
"FileUploaderButton" => Object {
"propTypes": Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ const FileUploader = React.forwardRef(
if (filenameStatus === 'edit') {
evt.stopPropagation();
const filteredArray = state.fileNames.filter(
(filename) => filename !== nodes[index].innerText.trim()
(filename) => filename !== nodes[index]?.innerText?.trim()
);

updateState({ fileNames: filteredArray });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { act, render } from '@testing-library/react';
import React from 'react';
import FileUploader from '../';
import { uploadFiles } from '../test-helpers';
import { Simulate } from 'react-dom/test-utils';

const iconDescription = 'test description';
const requiredProps = {
Expand Down Expand Up @@ -48,22 +49,38 @@ describe('FileUploader', () => {

it('should clear all uploaded files when `clearFiles` is called on a ref', () => {
const ref = React.createRef();
const { container } = render(<FileUploader {...requiredProps} ref={ref} />);
const onClick = jest.fn();
let requiredProps1 = {
...requiredProps,
filenameStatus: 'edit',
};
const fileUpload = render(
<FileUploader {...requiredProps1} ref={ref} onClick={onClick} />
);
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
const input = container.querySelector('input');
const input = fileUpload.container.querySelector('input');

const filename = 'test.png';
act(() => {
uploadFiles(input, [new File(['test'], filename, { type: 'image/png' })]);
});
expect(getByText(fileUpload.container, filename)).toBeInstanceOf(
HTMLElement
);

// eslint-disable-next-line testing-library/prefer-screen-queries
expect(getByText(container, filename)).toBeInstanceOf(HTMLElement);
const onDelete = jest.fn();
const description = 'test-description';
// eslint-disable-next-line testing-library/render-result-naming-convention

let removeFile = getByLabel(
fileUpload.container,
'test description - test.png'
);
act(() => {
ref.current.clearFiles();
Simulate.click(removeFile);
});
// eslint-disable-next-line testing-library/prefer-screen-queries
expect(getByText(container, filename)).not.toBeInstanceOf(HTMLElement);

expect(onClick).toHaveBeenCalledTimes(1);
});

it('should synchronize the filename status state when its prop changes', () => {
Expand Down

0 comments on commit 0e5c7ab

Please sign in to comment.