2021-03-06 19:21:23 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
|
|
|
import { Mock } from 'ts-mockery';
|
|
|
|
import { Settings } from '../../src/settings/reducers/settings';
|
2021-12-25 12:49:12 +03:00
|
|
|
import { VisitsSettings } from '../../src/settings/VisitsSettings';
|
2021-03-06 19:21:23 +03:00
|
|
|
import { SimpleCard } from '../../src/utils/SimpleCard';
|
|
|
|
import { DateIntervalSelector } from '../../src/utils/dates/DateIntervalSelector';
|
2022-03-07 18:27:25 +03:00
|
|
|
import { LabeledFormGroup } from '../../src/utils/forms/LabeledFormGroup';
|
2021-03-06 19:21:23 +03:00
|
|
|
|
2021-12-25 12:49:12 +03:00
|
|
|
describe('<VisitsSettings />', () => {
|
2021-03-06 19:21:23 +03:00
|
|
|
let wrapper: ShallowWrapper;
|
|
|
|
const setVisitsSettings = jest.fn();
|
|
|
|
const createWrapper = (settings: Partial<Settings> = {}) => {
|
2021-12-25 12:49:12 +03:00
|
|
|
wrapper = shallow(<VisitsSettings settings={Mock.of<Settings>(settings)} setVisitsSettings={setVisitsSettings} />);
|
2021-03-06 19:21:23 +03:00
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
|
|
|
afterEach(jest.clearAllMocks);
|
|
|
|
afterEach(() => wrapper?.unmount());
|
|
|
|
|
|
|
|
it('renders expected components', () => {
|
|
|
|
const wrapper = createWrapper();
|
|
|
|
|
|
|
|
expect(wrapper.find(SimpleCard).prop('title')).toEqual('Visits');
|
2022-03-07 18:27:25 +03:00
|
|
|
expect(wrapper.find(LabeledFormGroup).prop('label')).toEqual('Default interval to load on visits sections:');
|
2021-03-06 19:21:23 +03:00
|
|
|
expect(wrapper.find(DateIntervalSelector)).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[Mock.all<Settings>(), 'last30Days'],
|
|
|
|
[Mock.of<Settings>({ visits: {} }), 'last30Days'],
|
2021-03-06 19:21:23 +03:00
|
|
|
[
|
|
|
|
Mock.of<Settings>({
|
|
|
|
visits: {
|
|
|
|
defaultInterval: 'last7Days',
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
'last7Days',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
Mock.of<Settings>({
|
|
|
|
visits: {
|
|
|
|
defaultInterval: 'today',
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
'today',
|
|
|
|
],
|
|
|
|
])('sets expected interval as active', (settings, expectedInterval) => {
|
|
|
|
const wrapper = createWrapper(settings);
|
|
|
|
|
|
|
|
expect(wrapper.find(DateIntervalSelector).prop('active')).toEqual(expectedInterval);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('invokes setVisitsSettings when interval changes', () => {
|
|
|
|
const wrapper = createWrapper();
|
|
|
|
const selector = wrapper.find(DateIntervalSelector);
|
|
|
|
|
|
|
|
selector.simulate('change', 'last7Days');
|
2021-12-22 22:08:28 +03:00
|
|
|
selector.simulate('change', 'last180Days');
|
2021-03-06 19:21:23 +03:00
|
|
|
selector.simulate('change', 'yesterday');
|
|
|
|
|
|
|
|
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' });
|
|
|
|
});
|
|
|
|
});
|