Merge pull request #673 from acelaya-forks/feature/tests

Feature/tests
This commit is contained in:
Alejandro Celaya 2022-06-20 08:06:11 +02:00 committed by GitHub
commit d3d2cf72b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 37 deletions

View file

@ -1,47 +1,53 @@
import { shallow, ShallowWrapper } from 'enzyme';
import { DropdownItem } from 'reactstrap';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { QrErrorCorrection } from '../../../../src/utils/helpers/qrCodes';
import { QrErrorCorrectionDropdown } from '../../../../src/short-urls/helpers/qr-codes/QrErrorCorrectionDropdown';
describe('<QrErrorCorrectionDropdown />', () => {
const initialErrorCorrection: QrErrorCorrection = 'Q';
const setErrorCorrection = jest.fn();
let wrapper: ShallowWrapper;
beforeEach(() => {
wrapper = shallow(
const setUp = () => ({
user: userEvent.setup(),
...render(
<QrErrorCorrectionDropdown errorCorrection={initialErrorCorrection} setErrorCorrection={setErrorCorrection} />,
);
),
});
afterEach(() => wrapper?.unmount());
afterEach(jest.clearAllMocks);
it('renders initial state', () => {
const items = wrapper.find(DropdownItem);
it('renders initial state', async () => {
const { user } = setUp();
const btn = screen.getByRole('button');
expect(wrapper.prop('text')).toEqual('Error correction (Q)');
expect(items.at(0).prop('active')).toEqual(false);
expect(items.at(1).prop('active')).toEqual(false);
expect(items.at(2).prop('active')).toEqual(true);
expect(items.at(3).prop('active')).toEqual(false);
expect(btn).toHaveTextContent('Error correction (Q)');
await user.click(btn);
const items = screen.getAllByRole('menuitem');
expect(items[0]).not.toHaveClass('active');
expect(items[1]).not.toHaveClass('active');
expect(items[2]).toHaveClass('active');
expect(items[3]).not.toHaveClass('active');
});
it('invokes callback when items are clicked', () => {
const items = wrapper.find(DropdownItem);
it('invokes callback when items are clicked', async () => {
const { user } = setUp();
const clickItem = async (name: RegExp) => {
await user.click(screen.getByRole('button'));
await user.click(screen.getByRole('menuitem', { name }));
};
expect(setErrorCorrection).not.toHaveBeenCalled();
items.at(0).simulate('click');
await clickItem(/ow/);
expect(setErrorCorrection).toHaveBeenCalledWith('L');
items.at(1).simulate('click');
await clickItem(/edium/);
expect(setErrorCorrection).toHaveBeenCalledWith('M');
items.at(2).simulate('click');
await clickItem(/uartile/);
expect(setErrorCorrection).toHaveBeenCalledWith('Q');
items.at(3).simulate('click');
await clickItem(/igh/);
expect(setErrorCorrection).toHaveBeenCalledWith('H');
});
});

View file

@ -1,37 +1,43 @@
import { shallow, ShallowWrapper } from 'enzyme';
import { DropdownItem } from 'reactstrap';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { QrCodeFormat } from '../../../../src/utils/helpers/qrCodes';
import { QrFormatDropdown } from '../../../../src/short-urls/helpers/qr-codes/QrFormatDropdown';
describe('<QrFormatDropdown />', () => {
const initialFormat: QrCodeFormat = 'svg';
const setFormat = jest.fn();
let wrapper: ShallowWrapper;
beforeEach(() => {
wrapper = shallow(<QrFormatDropdown format={initialFormat} setFormat={setFormat} />);
const setUp = () => ({
user: userEvent.setup(),
...render(<QrFormatDropdown format={initialFormat} setFormat={setFormat} />),
});
afterEach(() => wrapper?.unmount());
afterEach(jest.clearAllMocks);
it('renders initial state', () => {
const items = wrapper.find(DropdownItem);
it('renders initial state', async () => {
const { user } = setUp();
const btn = screen.getByRole('button');
expect(wrapper.prop('text')).toEqual('Format (svg)');
expect(items.at(0).prop('active')).toEqual(false);
expect(items.at(1).prop('active')).toEqual(true);
expect(btn).toHaveTextContent('Format (svg');
await user.click(btn);
const items = screen.getAllByRole('menuitem');
expect(items[0]).not.toHaveClass('active');
expect(items[1]).toHaveClass('active');
});
it('invokes callback when items are clicked', () => {
const items = wrapper.find(DropdownItem);
it('invokes callback when items are clicked', async () => {
const { user } = setUp();
const clickItem = async (name: string) => {
await user.click(screen.getByRole('button'));
await user.click(screen.getByRole('menuitem', { name }));
};
expect(setFormat).not.toHaveBeenCalled();
items.at(0).simulate('click');
await clickItem('PNG');
expect(setFormat).toHaveBeenCalledWith('png');
items.at(1).simulate('click');
await clickItem('SVG');
expect(setFormat).toHaveBeenCalledWith('svg');
});
});