Migrated NonOrphanVisits test to react testing library

This commit is contained in:
Alejandro Celaya 2022-05-01 16:52:33 +02:00
parent a93d1e821d
commit 8edb3dc923

View file

@ -1,13 +1,13 @@
import { shallow } from 'enzyme'; import { fireEvent, render, screen } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import { Mock } from 'ts-mockery'; import { Mock } from 'ts-mockery';
import { formatISO } from 'date-fns';
import { NonOrphanVisits as createNonOrphanVisits } from '../../src/visits/NonOrphanVisits'; import { NonOrphanVisits as createNonOrphanVisits } from '../../src/visits/NonOrphanVisits';
import { MercureBoundProps } from '../../src/mercure/helpers/boundToMercureHub'; import { MercureBoundProps } from '../../src/mercure/helpers/boundToMercureHub';
import { VisitsInfo } from '../../src/visits/types'; import { Visit, VisitsInfo } from '../../src/visits/types';
import VisitsStats from '../../src/visits/VisitsStats';
import { Settings } from '../../src/settings/reducers/settings'; import { Settings } from '../../src/settings/reducers/settings';
import { ReportExporter } from '../../src/common/services/ReportExporter'; import { ReportExporter } from '../../src/common/services/ReportExporter';
import { SelectedServer } from '../../src/servers/data'; import { SelectedServer } from '../../src/servers/data';
import VisitsHeader from '../../src/visits/VisitsHeader';
jest.mock('react-router-dom', () => ({ jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'), ...jest.requireActual('react-router-dom'),
@ -16,31 +16,37 @@ jest.mock('react-router-dom', () => ({
})); }));
describe('<NonOrphanVisits />', () => { describe('<NonOrphanVisits />', () => {
it('wraps visits stats and header', () => { const exportVisits = jest.fn();
const getNonOrphanVisits = jest.fn(); const getNonOrphanVisits = jest.fn();
const cancelGetNonOrphanVisits = jest.fn(); const cancelGetNonOrphanVisits = jest.fn();
const nonOrphanVisits = Mock.all<VisitsInfo>(); const nonOrphanVisits = Mock.of<VisitsInfo>({ visits: [Mock.of<Visit>({ date: formatISO(new Date()) })] });
const NonOrphanVisits = createNonOrphanVisits(Mock.all<ReportExporter>()); const NonOrphanVisits = createNonOrphanVisits(Mock.of<ReportExporter>({ exportVisits }));
const wrapper = shallow( beforeEach(() => render(
<MemoryRouter>
<NonOrphanVisits <NonOrphanVisits
{...Mock.of<MercureBoundProps>({ mercureInfo: {} })} {...Mock.of<MercureBoundProps>({ mercureInfo: {} })}
getNonOrphanVisits={getNonOrphanVisits} getNonOrphanVisits={getNonOrphanVisits}
nonOrphanVisits={nonOrphanVisits}
cancelGetNonOrphanVisits={cancelGetNonOrphanVisits} cancelGetNonOrphanVisits={cancelGetNonOrphanVisits}
nonOrphanVisits={nonOrphanVisits}
settings={Mock.all<Settings>()} settings={Mock.all<Settings>()}
selectedServer={Mock.all<SelectedServer>()} selectedServer={Mock.all<SelectedServer>()}
/>, />
).dive(); </MemoryRouter>,
const stats = wrapper.find(VisitsStats); ));
const header = wrapper.find(VisitsHeader);
expect(stats).toHaveLength(1); it('wraps visits stats and header', () => {
expect(header).toHaveLength(1); expect(screen.getByRole('heading', { name: 'Non-orphan visits' })).toBeInTheDocument();
expect(stats.prop('cancelGetVisits')).toEqual(cancelGetNonOrphanVisits); expect(getNonOrphanVisits).toHaveBeenCalled();
expect(stats.prop('visitsInfo')).toEqual(nonOrphanVisits); });
expect(stats.prop('isOrphanVisits')).not.toBeDefined();
expect(header.prop('visits')).toEqual(nonOrphanVisits.visits); it('exports visits when clicking the button', () => {
expect(header.prop('goBack')).toEqual(expect.any(Function)); const btn = screen.getByRole('button', { name: 'Export (1)' });
expect(exportVisits).not.toHaveBeenCalled();
expect(btn).toBeInTheDocument();
fireEvent.click(btn);
expect(exportVisits).toHaveBeenCalledWith('non_orphan_visits.csv', expect.anything());
}); });
}); });