Migrated more tests to react testing lib

This commit is contained in:
Alejandro Celaya
2022-06-10 21:31:13 +02:00
parent 8bf1a9d023
commit 54fe849efd
3 changed files with 114 additions and 93 deletions

View File

@@ -1,46 +1,50 @@
import { shallow } from 'enzyme';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { MemoryRouter } from 'react-router-dom';
import { Mock } from 'ts-mockery';
import { formatISO } from 'date-fns';
import { OrphanVisits as createOrphanVisits } from '../../src/visits/OrphanVisits';
import { MercureBoundProps } from '../../src/mercure/helpers/boundToMercureHub';
import { VisitsInfo } from '../../src/visits/types';
import { VisitsStats } from '../../src/visits/VisitsStats';
import { Visit, VisitsInfo } from '../../src/visits/types';
import { Settings } from '../../src/settings/reducers/settings';
import { ReportExporter } from '../../src/common/services/ReportExporter';
import { SelectedServer } from '../../src/servers/data';
import { VisitsHeader } from '../../src/visits/VisitsHeader';
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: jest.fn().mockReturnValue(jest.fn()),
useParams: jest.fn().mockReturnValue({}),
}));
describe('<OrphanVisits />', () => {
const getOrphanVisits = jest.fn();
const exportVisits = jest.fn();
const orphanVisits = Mock.of<VisitsInfo>({ visits: [Mock.of<Visit>({ date: formatISO(new Date()) })] });
const OrphanVisits = createOrphanVisits(Mock.of<ReportExporter>({ exportVisits }));
const setUp = () => ({
user: userEvent.setup(),
...render(
<MemoryRouter>
<OrphanVisits
{...Mock.of<MercureBoundProps>({ mercureInfo: {} })}
getOrphanVisits={getOrphanVisits}
orphanVisits={orphanVisits}
cancelGetOrphanVisits={jest.fn()}
settings={Mock.all<Settings>()}
selectedServer={Mock.all<SelectedServer>()}
/>
</MemoryRouter>,
),
});
it('wraps visits stats and header', () => {
const getOrphanVisits = jest.fn();
const cancelGetOrphanVisits = jest.fn();
const orphanVisits = Mock.all<VisitsInfo>();
const OrphanVisits = createOrphanVisits(Mock.all<ReportExporter>());
setUp();
expect(screen.getByRole('heading', { name: 'Orphan visits' })).toBeInTheDocument();
expect(getOrphanVisits).toHaveBeenCalled();
});
const wrapper = shallow(
<OrphanVisits
{...Mock.of<MercureBoundProps>({ mercureInfo: {} })}
getOrphanVisits={getOrphanVisits}
orphanVisits={orphanVisits}
cancelGetOrphanVisits={cancelGetOrphanVisits}
settings={Mock.all<Settings>()}
selectedServer={Mock.all<SelectedServer>()}
/>,
).dive();
const stats = wrapper.find(VisitsStats);
const header = wrapper.find(VisitsHeader);
it('exports visits when clicking the button', async () => {
const { user } = setUp();
const btn = screen.getByRole('button', { name: 'Export (1)' });
expect(stats).toHaveLength(1);
expect(header).toHaveLength(1);
expect(stats.prop('cancelGetVisits')).toEqual(cancelGetOrphanVisits);
expect(stats.prop('visitsInfo')).toEqual(orphanVisits);
expect(stats.prop('isOrphanVisits')).toEqual(true);
expect(header.prop('visits')).toEqual(orphanVisits.visits);
expect(header.prop('goBack')).toEqual(expect.any(Function));
expect(exportVisits).not.toHaveBeenCalled();
expect(btn).toBeInTheDocument();
await user.click(btn);
expect(exportVisits).toHaveBeenCalledWith('orphan_visits.csv', expect.anything());
});
});