2022-07-10 00:03:21 +03:00
|
|
|
import { screen } from '@testing-library/react';
|
2023-04-13 23:47:13 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2023-02-18 13:11:01 +03:00
|
|
|
import { formatISO } from 'date-fns';
|
2022-04-30 13:01:50 +03:00
|
|
|
import { MemoryRouter } from 'react-router-dom';
|
2023-08-02 10:01:44 +03:00
|
|
|
import type { MercureBoundProps } from '../../src/mercure/helpers/boundToMercureHub';
|
2023-08-04 12:16:01 +03:00
|
|
|
import { SettingsProvider } from '../../src/utils/settings';
|
2023-08-02 10:01:44 +03:00
|
|
|
import { DomainVisits as createDomainVisits } from '../../src/visits/DomainVisits';
|
|
|
|
import type { DomainVisits } from '../../src/visits/reducers/domainVisits';
|
2022-07-10 20:44:49 +03:00
|
|
|
import { renderWithEvents } from '../__helpers__/setUpTest';
|
2022-04-30 13:01:50 +03:00
|
|
|
|
2023-05-27 12:57:26 +03:00
|
|
|
vi.mock('react-router-dom', async () => ({
|
|
|
|
...(await vi.importActual<any>('react-router-dom')),
|
|
|
|
useParams: vi.fn().mockReturnValue({ domain: 'foo.com_DEFAULT' }),
|
2022-04-30 13:01:50 +03:00
|
|
|
}));
|
|
|
|
|
|
|
|
describe('<DomainVisits />', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const exportVisits = vi.fn();
|
|
|
|
const getDomainVisits = vi.fn();
|
|
|
|
const cancelGetDomainVisits = vi.fn();
|
2023-04-13 23:47:13 +03:00
|
|
|
const domainVisits = fromPartial<DomainVisits>({ visits: [{ date: formatISO(new Date()) }] });
|
|
|
|
const DomainVisits = createDomainVisits(fromPartial({ exportVisits }));
|
2022-07-10 00:03:21 +03:00
|
|
|
const setUp = () => renderWithEvents(
|
|
|
|
<MemoryRouter>
|
2023-08-04 12:16:01 +03:00
|
|
|
<SettingsProvider value={fromPartial({})}>
|
|
|
|
<DomainVisits
|
|
|
|
{...fromPartial<MercureBoundProps>({ mercureInfo: {} })}
|
|
|
|
getDomainVisits={getDomainVisits}
|
|
|
|
cancelGetDomainVisits={cancelGetDomainVisits}
|
|
|
|
domainVisits={domainVisits}
|
|
|
|
/>
|
|
|
|
</SettingsProvider>
|
2022-07-10 00:03:21 +03:00
|
|
|
</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());
|
|
|
|
});
|
|
|
|
});
|