2022-07-10 00:03:21 +03:00
|
|
|
import { screen } from '@testing-library/react';
|
2022-04-30 13:01:50 +03:00
|
|
|
import { MemoryRouter } from 'react-router-dom';
|
|
|
|
import { Mock } from 'ts-mockery';
|
|
|
|
import { formatISO } from 'date-fns';
|
|
|
|
import { DomainVisits as createDomainVisits } from '../../src/visits/DomainVisits';
|
|
|
|
import { ReportExporter } from '../../src/common/services/ReportExporter';
|
|
|
|
import { MercureBoundProps } from '../../src/mercure/helpers/boundToMercureHub';
|
|
|
|
import { DomainVisits } from '../../src/visits/reducers/domainVisits';
|
|
|
|
import { Settings } from '../../src/settings/reducers/settings';
|
|
|
|
import { SelectedServer } from '../../src/servers/data';
|
|
|
|
import { Visit } from '../../src/visits/types';
|
2022-07-10 20:44:49 +03:00
|
|
|
import { renderWithEvents } from '../__helpers__/setUpTest';
|
2022-04-30 13:01:50 +03:00
|
|
|
|
|
|
|
jest.mock('react-router-dom', () => ({
|
|
|
|
...jest.requireActual('react-router-dom'),
|
|
|
|
useParams: jest.fn().mockReturnValue({ domain: 'foo.com_DEFAULT' }),
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe('<DomainVisits />', () => {
|
|
|
|
const exportVisits = jest.fn();
|
|
|
|
const getDomainVisits = jest.fn();
|
|
|
|
const cancelGetDomainVisits = jest.fn();
|
|
|
|
const domainVisits = Mock.of<DomainVisits>({ visits: [Mock.of<Visit>({ date: formatISO(new Date()) })] });
|
|
|
|
const DomainVisits = createDomainVisits(Mock.of<ReportExporter>({ exportVisits }));
|
2022-07-10 00:03:21 +03:00
|
|
|
const setUp = () => renderWithEvents(
|
|
|
|
<MemoryRouter>
|
|
|
|
<DomainVisits
|
|
|
|
{...Mock.of<MercureBoundProps>({ mercureInfo: {} })}
|
|
|
|
getDomainVisits={getDomainVisits}
|
|
|
|
cancelGetDomainVisits={cancelGetDomainVisits}
|
|
|
|
domainVisits={domainVisits}
|
|
|
|
settings={Mock.all<Settings>()}
|
|
|
|
selectedServer={Mock.all<SelectedServer>()}
|
|
|
|
/>
|
|
|
|
</MemoryRouter>,
|
|
|
|
);
|
2022-04-30 13:01:50 +03:00
|
|
|
|
|
|
|
it('wraps visits stats and header', () => {
|
2022-05-11 20:18:43 +03:00
|
|
|
setUp();
|
2022-04-30 13:01:50 +03:00
|
|
|
expect(screen.getByRole('heading', { name: '"foo.com" visits' })).toBeInTheDocument();
|
2022-11-12 11:18:37 +03:00
|
|
|
expect(getDomainVisits).toHaveBeenCalledWith(expect.objectContaining({ domain: 'DEFAULT' }));
|
2022-04-30 13:01:50 +03:00
|
|
|
});
|
|
|
|
|
2022-05-11 20:18:43 +03:00
|
|
|
it('exports visits when clicking the button', async () => {
|
|
|
|
const { user } = setUp();
|
2022-04-30 13:01:50 +03:00
|
|
|
const btn = screen.getByRole('button', { name: 'Export (1)' });
|
|
|
|
|
|
|
|
expect(exportVisits).not.toHaveBeenCalled();
|
|
|
|
expect(btn).toBeInTheDocument();
|
|
|
|
|
2022-05-11 20:18:43 +03:00
|
|
|
await user.click(btn);
|
2022-04-30 13:01:50 +03:00
|
|
|
expect(exportVisits).toHaveBeenCalledWith('domain_foo.com_visits.csv', expect.anything());
|
|
|
|
});
|
|
|
|
});
|