2021-09-20 22:23:39 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
|
|
|
import { Mock } from 'ts-mockery';
|
|
|
|
import { Input } from 'reactstrap';
|
2022-03-07 18:27:25 +03:00
|
|
|
import { FormText } from '../../src/utils/forms/FormText';
|
2021-12-25 12:49:12 +03:00
|
|
|
import {
|
|
|
|
RealTimeUpdatesSettings as RealTimeUpdatesSettingsOptions,
|
|
|
|
Settings,
|
|
|
|
} from '../../src/settings/reducers/settings';
|
|
|
|
import RealTimeUpdatesSettings from '../../src/settings/RealTimeUpdatesSettings';
|
2021-09-20 22:23:39 +03:00
|
|
|
import ToggleSwitch from '../../src/utils/ToggleSwitch';
|
2022-03-07 18:27:25 +03:00
|
|
|
import { LabeledFormGroup } from '../../src/utils/forms/LabeledFormGroup';
|
2021-09-20 22:23:39 +03:00
|
|
|
|
2021-12-25 12:49:12 +03:00
|
|
|
describe('<RealTimeUpdatesSettings />', () => {
|
2021-09-20 22:23:39 +03:00
|
|
|
const toggleRealTimeUpdates = jest.fn();
|
|
|
|
const setRealTimeUpdatesInterval = jest.fn();
|
|
|
|
let wrapper: ShallowWrapper;
|
2021-12-25 12:49:12 +03:00
|
|
|
const createWrapper = (realTimeUpdates: Partial<RealTimeUpdatesSettingsOptions> = {}) => {
|
2021-09-20 22:23:39 +03:00
|
|
|
const settings = Mock.of<Settings>({ realTimeUpdates });
|
|
|
|
|
|
|
|
wrapper = shallow(
|
2021-12-25 12:49:12 +03:00
|
|
|
<RealTimeUpdatesSettings
|
2021-09-20 22:23:39 +03:00
|
|
|
settings={settings}
|
|
|
|
toggleRealTimeUpdates={toggleRealTimeUpdates}
|
|
|
|
setRealTimeUpdatesInterval={setRealTimeUpdatesInterval}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
|
|
|
afterEach(jest.clearAllMocks);
|
|
|
|
afterEach(() => wrapper?.unmount());
|
|
|
|
|
2021-09-20 23:00:34 +03:00
|
|
|
it('renders enabled real time updates as expected', () => {
|
2021-09-20 22:23:39 +03:00
|
|
|
const wrapper = createWrapper({ enabled: true });
|
|
|
|
const toggle = wrapper.find(ToggleSwitch);
|
2022-03-07 18:27:25 +03:00
|
|
|
const label = wrapper.find(LabeledFormGroup);
|
2021-09-20 22:23:39 +03:00
|
|
|
const input = wrapper.find(Input);
|
2022-03-07 18:27:25 +03:00
|
|
|
const formText = wrapper.find(FormText);
|
2021-09-20 22:23:39 +03:00
|
|
|
|
|
|
|
expect(toggle.prop('checked')).toEqual(true);
|
|
|
|
expect(toggle.html()).toContain('processed');
|
|
|
|
expect(toggle.html()).not.toContain('ignored');
|
2022-03-07 18:27:25 +03:00
|
|
|
expect(label.prop('labelClassName')).not.toContain('text-muted');
|
2021-09-20 22:23:39 +03:00
|
|
|
expect(input.prop('disabled')).toEqual(false);
|
2022-03-07 18:27:25 +03:00
|
|
|
expect(formText).toHaveLength(2);
|
2021-09-20 22:23:39 +03:00
|
|
|
});
|
|
|
|
|
2021-09-20 23:00:34 +03:00
|
|
|
it('renders disabled real time updates as expected', () => {
|
2021-09-20 22:23:39 +03:00
|
|
|
const wrapper = createWrapper({ enabled: false });
|
|
|
|
const toggle = wrapper.find(ToggleSwitch);
|
2022-03-07 18:27:25 +03:00
|
|
|
const label = wrapper.find(LabeledFormGroup);
|
2021-09-20 22:23:39 +03:00
|
|
|
const input = wrapper.find(Input);
|
2022-03-07 18:27:25 +03:00
|
|
|
const formText = wrapper.find(FormText);
|
2021-09-20 22:23:39 +03:00
|
|
|
|
|
|
|
expect(toggle.prop('checked')).toEqual(false);
|
|
|
|
expect(toggle.html()).not.toContain('processed');
|
|
|
|
expect(toggle.html()).toContain('ignored');
|
2022-03-07 18:27:25 +03:00
|
|
|
expect(label.prop('labelClassName')).toContain('text-muted');
|
2021-09-20 22:23:39 +03:00
|
|
|
expect(input.prop('disabled')).toEqual(true);
|
2022-03-07 18:27:25 +03:00
|
|
|
expect(formText).toHaveLength(1);
|
2021-09-20 22:23:39 +03:00
|
|
|
});
|
|
|
|
|
2021-09-20 23:00:34 +03:00
|
|
|
it.each([
|
2021-09-20 22:23:39 +03:00
|
|
|
[ 1, 'minute' ],
|
|
|
|
[ 2, 'minutes' ],
|
|
|
|
[ 10, 'minutes' ],
|
|
|
|
[ 100, 'minutes' ],
|
2021-09-20 23:00:34 +03:00
|
|
|
])('shows expected children when interval is greater than 0', (interval, minutesWord) => {
|
2021-09-20 22:23:39 +03:00
|
|
|
const wrapper = createWrapper({ enabled: true, interval });
|
|
|
|
const span = wrapper.find('span');
|
|
|
|
const input = wrapper.find(Input);
|
|
|
|
|
|
|
|
expect(span).toHaveLength(1);
|
|
|
|
expect(span.html()).toEqual(
|
|
|
|
`<span>Updates will be reflected in the UI every <b>${interval}</b> ${minutesWord}.</span>`,
|
|
|
|
);
|
|
|
|
expect(input.prop('value')).toEqual(`${interval}`);
|
|
|
|
});
|
|
|
|
|
2021-09-20 23:00:34 +03:00
|
|
|
it.each([[ undefined ], [ 0 ]])('shows expected children when interval is 0 or undefined', (interval) => {
|
2021-09-20 22:23:39 +03:00
|
|
|
const wrapper = createWrapper({ enabled: true, interval });
|
|
|
|
const span = wrapper.find('span');
|
2022-03-07 18:27:25 +03:00
|
|
|
const formText = wrapper.find(FormText).at(1);
|
2021-09-20 22:23:39 +03:00
|
|
|
const input = wrapper.find(Input);
|
|
|
|
|
|
|
|
expect(span).toHaveLength(0);
|
2022-03-07 18:27:25 +03:00
|
|
|
expect(formText.html()).toContain('Updates will be reflected in the UI as soon as they happen.');
|
2021-09-20 22:23:39 +03:00
|
|
|
expect(input.prop('value')).toEqual('');
|
|
|
|
});
|
|
|
|
|
2021-09-20 23:00:34 +03:00
|
|
|
it('updates real time updates on input change', () => {
|
2021-09-20 22:23:39 +03:00
|
|
|
const wrapper = createWrapper();
|
|
|
|
const input = wrapper.find(Input);
|
|
|
|
|
|
|
|
expect(setRealTimeUpdatesInterval).not.toHaveBeenCalled();
|
|
|
|
input.simulate('change', { target: { value: '10' } });
|
|
|
|
expect(setRealTimeUpdatesInterval).toHaveBeenCalledWith(10);
|
|
|
|
});
|
|
|
|
|
2021-09-20 23:00:34 +03:00
|
|
|
it('toggles real time updates on switch change', () => {
|
2021-09-20 22:23:39 +03:00
|
|
|
const wrapper = createWrapper();
|
|
|
|
const toggle = wrapper.find(ToggleSwitch);
|
|
|
|
|
|
|
|
expect(toggleRealTimeUpdates).not.toHaveBeenCalled();
|
|
|
|
toggle.simulate('change');
|
|
|
|
expect(toggleRealTimeUpdates).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|