Migrated QrFormatDropdown test to react testing library

This commit is contained in:
Alejandro Celaya 2022-06-13 21:38:39 +02:00
parent 3201830b27
commit eb9ec4ec31

View file

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