shlink-web-client/test/settings/VisitsSettings.test.tsx

63 lines
2 KiB
TypeScript
Raw Normal View History

import { screen } from '@testing-library/react';
2021-03-06 19:21:23 +03:00
import { Mock } from 'ts-mockery';
import { Settings } from '../../src/settings/reducers/settings';
import { VisitsSettings } from '../../src/settings/VisitsSettings';
import { renderWithEvents } from '../__mocks__/setUpTest';
2021-03-06 19:21:23 +03:00
describe('<VisitsSettings />', () => {
2021-03-06 19:21:23 +03:00
const setVisitsSettings = jest.fn();
const setUp = (settings: Partial<Settings> = {}) => renderWithEvents(
<VisitsSettings settings={Mock.of<Settings>(settings)} setVisitsSettings={setVisitsSettings} />,
);
2021-03-06 19:21:23 +03:00
afterEach(jest.clearAllMocks);
it('renders expected components', () => {
setUp();
2021-03-06 19:21:23 +03:00
expect(screen.getByRole('heading')).toHaveTextContent('Visits');
expect(screen.getByText('Default interval to load on visits sections:')).toBeInTheDocument();
2021-03-06 19:21:23 +03:00
});
it.each([
[Mock.all<Settings>(), 'Last 30 days'],
[Mock.of<Settings>({ visits: {} }), 'Last 30 days'],
2021-03-06 19:21:23 +03:00
[
Mock.of<Settings>({
visits: {
defaultInterval: 'last7Days',
},
}),
'Last 7 days',
2021-03-06 19:21:23 +03:00
],
[
Mock.of<Settings>({
visits: {
defaultInterval: 'today',
},
}),
'Today',
2021-03-06 19:21:23 +03:00
],
])('sets expected interval as active', (settings, expectedInterval) => {
setUp(settings);
expect(screen.getByRole('button')).toHaveTextContent(expectedInterval);
2021-03-06 19:21:23 +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
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' });
expect(setVisitsSettings).toHaveBeenNthCalledWith(2, { defaultInterval: 'last180Days' });
2021-03-06 19:21:23 +03:00
expect(setVisitsSettings).toHaveBeenNthCalledWith(3, { defaultInterval: 'yesterday' });
});
});