2022-07-10 00:03:21 +03:00
|
|
|
import { screen } from '@testing-library/react';
|
2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { Settings } from '../../src/settings/reducers/settings';
|
2021-12-25 12:49:12 +03:00
|
|
|
import { VisitsSettings } from '../../src/settings/VisitsSettings';
|
2022-07-10 20:44:49 +03:00
|
|
|
import { renderWithEvents } from '../__helpers__/setUpTest';
|
2021-03-06 19:21:23 +03:00
|
|
|
|
2021-12-25 12:49:12 +03:00
|
|
|
describe('<VisitsSettings />', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const setVisitsSettings = vi.fn();
|
2022-07-10 00:03:21 +03:00
|
|
|
const setUp = (settings: Partial<Settings> = {}) => renderWithEvents(
|
2023-04-13 22:48:29 +03:00
|
|
|
<VisitsSettings settings={fromPartial(settings)} setVisitsSettings={setVisitsSettings} />,
|
2022-07-10 00:03:21 +03:00
|
|
|
);
|
2021-03-06 19:21:23 +03:00
|
|
|
|
2023-05-27 12:57:26 +03:00
|
|
|
afterEach(vi.clearAllMocks);
|
2021-03-06 19:21:23 +03:00
|
|
|
|
|
|
|
it('renders expected components', () => {
|
2022-06-08 19:24:21 +03:00
|
|
|
setUp();
|
2021-03-06 19:21:23 +03:00
|
|
|
|
2022-06-08 19:24:21 +03:00
|
|
|
expect(screen.getByRole('heading')).toHaveTextContent('Visits');
|
|
|
|
expect(screen.getByText('Default interval to load on visits sections:')).toBeInTheDocument();
|
2022-12-22 14:25:25 +03:00
|
|
|
expect(screen.getByText(/^Exclude bots wherever possible/)).toBeInTheDocument();
|
2021-03-06 19:21:23 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
2023-04-13 22:48:29 +03:00
|
|
|
[fromPartial<Settings>({}), 'Last 30 days'],
|
|
|
|
[fromPartial<Settings>({ visits: {} }), 'Last 30 days'],
|
2021-03-06 19:21:23 +03:00
|
|
|
[
|
2023-04-13 22:48:29 +03:00
|
|
|
fromPartial<Settings>({
|
2021-03-06 19:21:23 +03:00
|
|
|
visits: {
|
|
|
|
defaultInterval: 'last7Days',
|
|
|
|
},
|
|
|
|
}),
|
2022-06-08 19:24:21 +03:00
|
|
|
'Last 7 days',
|
2021-03-06 19:21:23 +03:00
|
|
|
],
|
|
|
|
[
|
2023-04-13 22:48:29 +03:00
|
|
|
fromPartial<Settings>({
|
2021-03-06 19:21:23 +03:00
|
|
|
visits: {
|
|
|
|
defaultInterval: 'today',
|
|
|
|
},
|
|
|
|
}),
|
2022-06-08 19:24:21 +03:00
|
|
|
'Today',
|
2021-03-06 19:21:23 +03:00
|
|
|
],
|
|
|
|
])('sets expected interval as active', (settings, expectedInterval) => {
|
2022-06-08 19:24:21 +03:00
|
|
|
setUp(settings);
|
|
|
|
expect(screen.getByRole('button')).toHaveTextContent(expectedInterval);
|
2021-03-06 19:21:23 +03:00
|
|
|
});
|
|
|
|
|
2022-06-08 19:24:21 +03:00
|
|
|
it('invokes setVisitsSettings when interval changes', async () => {
|
|
|
|
const { user } = setUp();
|
|
|
|
const selectOption = async (name: string) => {
|
|
|
|
await user.click(screen.getByRole('button'));
|
|
|
|
await user.click(screen.getByRole('menuitem', { name }));
|
|
|
|
};
|
2021-03-06 19:21:23 +03:00
|
|
|
|
2022-06-08 19:24:21 +03:00
|
|
|
await selectOption('Last 7 days');
|
|
|
|
await selectOption('Last 180 days');
|
|
|
|
await selectOption('Yesterday');
|
2021-03-06 19:21:23 +03:00
|
|
|
|
|
|
|
expect(setVisitsSettings).toHaveBeenCalledTimes(3);
|
|
|
|
expect(setVisitsSettings).toHaveBeenNthCalledWith(1, { defaultInterval: 'last7Days' });
|
2021-12-22 22:08:28 +03:00
|
|
|
expect(setVisitsSettings).toHaveBeenNthCalledWith(2, { defaultInterval: 'last180Days' });
|
2021-03-06 19:21:23 +03:00
|
|
|
expect(setVisitsSettings).toHaveBeenNthCalledWith(3, { defaultInterval: 'yesterday' });
|
|
|
|
});
|
2022-12-22 14:25:25 +03:00
|
|
|
|
|
|
|
it.each([
|
|
|
|
[
|
2023-04-13 22:48:29 +03:00
|
|
|
fromPartial<Settings>({}),
|
2022-12-22 14:25:25 +03:00
|
|
|
/The visits coming from potential bots will be included.$/,
|
|
|
|
/The visits coming from potential bots will be excluded.$/,
|
|
|
|
],
|
|
|
|
[
|
2023-04-13 22:48:29 +03:00
|
|
|
fromPartial<Settings>({ visits: { excludeBots: false } }),
|
2022-12-22 14:25:25 +03:00
|
|
|
/The visits coming from potential bots will be included.$/,
|
|
|
|
/The visits coming from potential bots will be excluded.$/,
|
|
|
|
],
|
|
|
|
[
|
2023-04-13 22:48:29 +03:00
|
|
|
fromPartial<Settings>({ visits: { excludeBots: true } }),
|
2022-12-22 14:25:25 +03:00
|
|
|
/The visits coming from potential bots will be excluded.$/,
|
|
|
|
/The visits coming from potential bots will be included.$/,
|
|
|
|
],
|
|
|
|
])('displays expected helper text for exclude bots control', (settings, expectedText, notExpectedText) => {
|
|
|
|
setUp(settings);
|
|
|
|
|
|
|
|
const visitsComponent = screen.getByText(/^Exclude bots wherever possible/);
|
|
|
|
|
|
|
|
expect(visitsComponent).toHaveTextContent(expectedText);
|
|
|
|
expect(visitsComponent).not.toHaveTextContent(notExpectedText);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('invokes setVisitsSettings when bot exclusion is toggled', async () => {
|
|
|
|
const { user } = setUp();
|
|
|
|
|
|
|
|
await user.click(screen.getByText(/^Exclude bots wherever possible/));
|
|
|
|
expect(setVisitsSettings).toHaveBeenCalledWith(expect.objectContaining({ excludeBots: true }));
|
|
|
|
});
|
2021-03-06 19:21:23 +03:00
|
|
|
});
|