Merge pull request #667 from acelaya-forks/feature/tests-and-more-tests

Feature/tests and more tests
This commit is contained in:
Alejandro Celaya 2022-06-10 20:53:24 +02:00 committed by GitHub
commit 8bf1a9d023
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 38 deletions

View file

@ -4,4 +4,5 @@ import ResizeObserver from 'resize-observer-polyfill';
(global as any).ResizeObserver = ResizeObserver;
(global as any).scrollTo = () => {};
(global as any).prompt = () => {};
(global as any).matchMedia = (media: string) => ({ matches: false, media });

View file

@ -1,27 +1,30 @@
import { shallow, ShallowWrapper } from 'enzyme';
import CopyToClipboard from 'react-copy-to-clipboard';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faCopy as copyIcon } from '@fortawesome/free-regular-svg-icons';
import { render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { CopyToClipboardIcon } from '../../src/utils/CopyToClipboardIcon';
describe('<CopyToClipboardIcon />', () => {
let wrapper: ShallowWrapper;
const onCopy = () => {};
beforeEach(() => {
wrapper = shallow(<CopyToClipboardIcon text="foo" onCopy={onCopy} />);
const onCopy = jest.fn();
const setUp = (text = 'foo') => ({
user: userEvent.setup(),
...render(<CopyToClipboardIcon text={text} onCopy={onCopy} />),
});
afterEach(() => wrapper?.unmount());
afterEach(jest.clearAllMocks);
it('wraps expected components', () => {
const copyToClipboard = wrapper.find(CopyToClipboard);
const icon = wrapper.find(FontAwesomeIcon);
const { container } = setUp();
expect(container).toMatchSnapshot();
});
expect(copyToClipboard).toHaveLength(1);
expect(copyToClipboard.prop('text')).toEqual('foo');
expect(copyToClipboard.prop('onCopy')).toEqual(onCopy);
expect(icon).toHaveLength(1);
expect(icon.prop('icon')).toEqual(copyIcon);
expect(icon.prop('className')).toEqual('ms-2 copy-to-clipboard-icon');
it.each([
['text'],
['bar'],
['baz'],
])('copies content to clipboard when clicked', async (text) => {
const { user, container } = setUp(text);
expect(onCopy).not.toHaveBeenCalled();
container.firstElementChild && await user.click(container.firstElementChild);
expect(onCopy).toHaveBeenCalledWith(text, false);
});
});

View file

@ -1,35 +1,34 @@
import { shallow, ShallowWrapper } from 'enzyme';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Mock } from 'ts-mockery';
import { DateInput, DateInputProps } from '../../src/utils/DateInput';
describe('<DateInput />', () => {
let wrapped: ShallowWrapper;
const createComponent = (props: Partial<DateInputProps> = {}) => {
wrapped = shallow(<DateInput {...Mock.of<DateInputProps>(props)} />);
return wrapped;
};
afterEach(() => wrapped?.unmount());
it('wraps a DatePicker', () => {
wrapped = createComponent();
const setUp = (props: Partial<DateInputProps> = {}) => ({
user: userEvent.setup(),
...render(<DateInput {...Mock.of<DateInputProps>(props)} />),
});
it('shows calendar icon when input is not clearable', () => {
wrapped = createComponent({ isClearable: false });
expect(wrapped.find(FontAwesomeIcon)).toHaveLength(1);
setUp({ isClearable: false });
expect(screen.getByRole('img', { hidden: true })).toBeInTheDocument();
});
it('shows calendar icon when input is clearable but selected value is nil', () => {
wrapped = createComponent({ isClearable: true, selected: null });
expect(wrapped.find(FontAwesomeIcon)).toHaveLength(1);
setUp({ isClearable: true, selected: null });
expect(screen.getByRole('img', { hidden: true })).toBeInTheDocument();
});
it('does not show calendar icon when input is clearable', () => {
wrapped = createComponent({ isClearable: true, selected: new Date() });
expect(wrapped.find(FontAwesomeIcon)).toHaveLength(0);
setUp({ isClearable: true, selected: new Date() });
expect(screen.queryByRole('img', { hidden: true })).not.toBeInTheDocument();
});
it('shows popper on element click', async () => {
const { user, container } = setUp({ placeholderText: 'foo' });
expect(container.querySelector('.react-datepicker')).not.toBeInTheDocument();
await user.click(screen.getByPlaceholderText('foo'));
await waitFor(() => expect(container.querySelector('.react-datepicker')).toBeInTheDocument());
});
});

View file

@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`<CopyToClipboardIcon /> wraps expected components 1`] = `
<div>
<svg
aria-hidden="true"
class="svg-inline--fa fa-copy ms-2 copy-to-clipboard-icon"
data-icon="copy"
data-prefix="far"
focusable="false"
role="img"
viewBox="0 0 512 512"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M502.6 70.63l-61.25-61.25C435.4 3.371 427.2 0 418.7 0H255.1c-35.35 0-64 28.66-64 64l.0195 256C192 355.4 220.7 384 256 384h192c35.2 0 64-28.8 64-64V93.25C512 84.77 508.6 76.63 502.6 70.63zM464 320c0 8.836-7.164 16-16 16H255.1c-8.838 0-16-7.164-16-16L239.1 64.13c0-8.836 7.164-16 16-16h128L384 96c0 17.67 14.33 32 32 32h47.1V320zM272 448c0 8.836-7.164 16-16 16H63.1c-8.838 0-16-7.164-16-16L47.98 192.1c0-8.836 7.164-16 16-16H160V128H63.99c-35.35 0-64 28.65-64 64l.0098 256C.002 483.3 28.66 512 64 512h192c35.2 0 64-28.8 64-64v-32h-47.1L272 448z"
fill="currentColor"
/>
</svg>
</div>
`;