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

[Unit-test]: File Uploader #17791

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
Loading