import { render, screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { Mock } from 'ts-mockery'; import { Settings } from '../../src/settings/reducers/settings'; import { VisitsSettings } from '../../src/settings/VisitsSettings'; describe('', () => { const setVisitsSettings = jest.fn(); const setUp = (settings: Partial = {}) => ({ user: userEvent.setup(), ...render((settings)} setVisitsSettings={setVisitsSettings} />), }); afterEach(jest.clearAllMocks); it('renders expected components', () => { setUp(); expect(screen.getByRole('heading')).toHaveTextContent('Visits'); expect(screen.getByText('Default interval to load on visits sections:')).toBeInTheDocument(); }); it.each([ [Mock.all(), 'Last 30 days'], [Mock.of({ visits: {} }), 'Last 30 days'], [ Mock.of({ visits: { defaultInterval: 'last7Days', }, }), 'Last 7 days', ], [ Mock.of({ visits: { defaultInterval: 'today', }, }), 'Today', ], ])('sets expected interval as active', (settings, expectedInterval) => { setUp(settings); expect(screen.getByRole('button')).toHaveTextContent(expectedInterval); }); 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 })); }; await selectOption('Last 7 days'); await selectOption('Last 180 days'); await selectOption('Yesterday'); expect(setVisitsSettings).toHaveBeenCalledTimes(3); expect(setVisitsSettings).toHaveBeenNthCalledWith(1, { defaultInterval: 'last7Days' }); expect(setVisitsSettings).toHaveBeenNthCalledWith(2, { defaultInterval: 'last180Days' }); expect(setVisitsSettings).toHaveBeenNthCalledWith(3, { defaultInterval: 'yesterday' }); }); });