Skip to content

Commit

Permalink
feat(file-uploader): increase unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
divya-281 committed Oct 18, 2024
1 parent 4312ae4 commit db6f1de
Showing 1 changed file with 64 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
* LICENSE file in the root directory of this source tree.
*/

import { getByLabel, getByText } from '@carbon/test-utils/dom';
import { act, render } from '@testing-library/react';
import React from 'react';
import { getByLabel, getByText } from '@carbon/test-utils/dom';

import FileUploader from '../';
import { uploadFiles } from '../test-helpers';
import React from 'react';
import { Simulate } from 'react-dom/test-utils';
import { uploadFiles } from '../test-helpers';

const iconDescription = 'test description';
const requiredProps = {
Expand Down Expand Up @@ -106,4 +107,64 @@ describe('FileUploader', () => {
const complete = getByLabel(container, iconDescription);
expect(edit).not.toEqual(complete);
});
it('should disable file input when `disabled` prop is true', () => {
const { container } = render(
<FileUploader {...requiredProps} disabled buttonLabel="disabled upload" />
);
const input = container.querySelector('input');
expect(input).toBeDisabled();
});
it('should render with different button kinds', () => {
const buttonKinds = ['primary', 'secondary', 'danger', 'ghost'];
buttonKinds.forEach((kind) => {
const { container } = render(
<FileUploader {...requiredProps} buttonKind={kind} />
);
const button = container.querySelector('button');
expect(button).toHaveClass(`cds--btn--${kind}`);
});
});
it('should trigger `onDelete` when a file is removed', () => {
const onDelete = jest.fn();
const { container } = render(
<FileUploader
{...requiredProps}
filenameStatus="edit"
onDelete={onDelete}
/>
);
const input = container.querySelector('input');

act(() => {
uploadFiles(input, [
new File(['test'], 'test.png', { type: 'image/png' }),
]);
});

const removeFileButton = getByLabel(
container,
'test description - test.png'
);

act(() => {
Simulate.click(removeFileButton);
});

expect(onDelete).toHaveBeenCalledTimes(1);
});
it('should trigger `onChange` when files are selected', () => {
const onChange = jest.fn();
const { container } = render(
<FileUploader {...requiredProps} onChange={onChange} />
);
const input = container.querySelector('input');

act(() => {
uploadFiles(input, [
new File(['test'], 'test.png', { type: 'image/png' }),
]);
});

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

0 comments on commit db6f1de

Please sign in to comment.