Migrated CreateShortUrl test to react testing library

This commit is contained in:
Alejandro Celaya 2022-06-08 18:28:16 +02:00
parent e538f2a3bb
commit 105254d053
2 changed files with 17 additions and 26 deletions

View file

@ -55,7 +55,6 @@ export const CreateShortUrl = (
mode={basicMode ? 'create-basic' : 'create'} mode={basicMode ? 'create-basic' : 'create'}
onSave={async (data: ShortUrlData) => { onSave={async (data: ShortUrlData) => {
resetCreateShortUrl(); resetCreateShortUrl();
return createShortUrl(data); return createShortUrl(data);
}} }}
/> />

View file

@ -1,21 +1,17 @@
import { shallow, ShallowWrapper } from 'enzyme'; import { render, screen } from '@testing-library/react';
import { Mock } from 'ts-mockery'; import { Mock } from 'ts-mockery';
import { CreateShortUrl as createShortUrlsCreator } from '../../src/short-urls/CreateShortUrl'; import { CreateShortUrl as createShortUrlsCreator } from '../../src/short-urls/CreateShortUrl';
import { ShortUrlCreation } from '../../src/short-urls/reducers/shortUrlCreation'; import { ShortUrlCreation } from '../../src/short-urls/reducers/shortUrlCreation';
import { Settings } from '../../src/settings/reducers/settings'; import { Settings } from '../../src/settings/reducers/settings';
describe('<CreateShortUrl />', () => { describe('<CreateShortUrl />', () => {
let wrapper: ShallowWrapper; const ShortUrlForm = () => <span>ShortUrlForm</span>;
const ShortUrlForm = () => null; const CreateShortUrlResult = () => <span>CreateShortUrlResult</span>;
const CreateShortUrlResult = () => null;
const shortUrlCreation = { validateUrls: true }; const shortUrlCreation = { validateUrls: true };
const shortUrlCreationResult = Mock.all<ShortUrlCreation>(); const shortUrlCreationResult = Mock.all<ShortUrlCreation>();
const createShortUrl = jest.fn(async () => Promise.resolve()); const createShortUrl = jest.fn(async () => Promise.resolve());
beforeEach(() => {
const CreateShortUrl = createShortUrlsCreator(ShortUrlForm, CreateShortUrlResult); const CreateShortUrl = createShortUrlsCreator(ShortUrlForm, CreateShortUrlResult);
const setUp = () => render(
wrapper = shallow(
<CreateShortUrl <CreateShortUrl
shortUrlCreationResult={shortUrlCreationResult} shortUrlCreationResult={shortUrlCreationResult}
createShortUrl={createShortUrl} createShortUrl={createShortUrl}
@ -24,15 +20,11 @@ describe('<CreateShortUrl />', () => {
settings={Mock.of<Settings>({ shortUrlCreation })} settings={Mock.of<Settings>({ shortUrlCreation })}
/>, />,
); );
});
afterEach(() => wrapper.unmount());
afterEach(jest.clearAllMocks);
it('renders a ShortUrlForm with a computed initial state', () => { it('renders computed initial state', () => {
const form = wrapper.find(ShortUrlForm); setUp();
const result = wrapper.find(CreateShortUrlResult);
expect(form).toHaveLength(1); expect(screen.getByText('ShortUrlForm')).toBeInTheDocument();
expect(result).toHaveLength(1); expect(screen.getByText('CreateShortUrlResult')).toBeInTheDocument();
}); });
}); });