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 13:11:01 +03:00
|
|
|
import { RealTimeUpdatesSettings } from '../../src/settings/RealTimeUpdatesSettings';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type {
|
2021-12-25 12:49:12 +03:00
|
|
|
RealTimeUpdatesSettings as RealTimeUpdatesSettingsOptions,
|
|
|
|
} from '../../src/settings/reducers/settings';
|
2022-07-10 20:44:49 +03:00
|
|
|
import { renderWithEvents } from '../__helpers__/setUpTest';
|
2021-09-20 22:23:39 +03:00
|
|
|
|
2021-12-25 12:49:12 +03:00
|
|
|
describe('<RealTimeUpdatesSettings />', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const toggleRealTimeUpdates = vi.fn();
|
|
|
|
const setRealTimeUpdatesInterval = vi.fn();
|
2022-07-10 00:03:21 +03:00
|
|
|
const setUp = (realTimeUpdates: Partial<RealTimeUpdatesSettingsOptions> = {}) => renderWithEvents(
|
|
|
|
<RealTimeUpdatesSettings
|
2023-04-13 22:48:29 +03:00
|
|
|
settings={fromPartial({ realTimeUpdates })}
|
2022-07-10 00:03:21 +03:00
|
|
|
toggleRealTimeUpdates={toggleRealTimeUpdates}
|
|
|
|
setRealTimeUpdatesInterval={setRealTimeUpdatesInterval}
|
|
|
|
/>,
|
|
|
|
);
|
2021-09-20 22:23:39 +03:00
|
|
|
|
2023-05-27 12:57:26 +03:00
|
|
|
afterEach(vi.clearAllMocks);
|
2021-09-20 22:23:39 +03:00
|
|
|
|
2021-09-20 23:00:34 +03:00
|
|
|
it('renders enabled real time updates as expected', () => {
|
2022-06-06 23:23:21 +03:00
|
|
|
setUp({ enabled: true });
|
|
|
|
|
|
|
|
expect(screen.getByLabelText(/^Enable or disable real-time updates./)).toBeChecked();
|
|
|
|
expect(screen.getByText(/^Real-time updates are currently being/)).toHaveTextContent('processed');
|
|
|
|
expect(screen.getByText(/^Real-time updates are currently being/)).not.toHaveTextContent('ignored');
|
|
|
|
expect(screen.getByText('Real-time updates frequency (in minutes):')).not.toHaveAttribute(
|
|
|
|
'class',
|
|
|
|
expect.stringContaining('text-muted'),
|
|
|
|
);
|
|
|
|
expect(screen.getByLabelText('Real-time updates frequency (in minutes):')).not.toHaveAttribute('disabled');
|
|
|
|
expect(screen.getByText('Updates will be reflected in the UI as soon as they happen.')).toBeInTheDocument();
|
2021-09-20 22:23:39 +03:00
|
|
|
});
|
|
|
|
|
2021-09-20 23:00:34 +03:00
|
|
|
it('renders disabled real time updates as expected', () => {
|
2022-06-06 23:23:21 +03:00
|
|
|
setUp({ enabled: false });
|
|
|
|
|
|
|
|
expect(screen.getByLabelText(/^Enable or disable real-time updates./)).not.toBeChecked();
|
|
|
|
expect(screen.getByText(/^Real-time updates are currently being/)).not.toHaveTextContent('processed');
|
|
|
|
expect(screen.getByText(/^Real-time updates are currently being/)).toHaveTextContent('ignored');
|
|
|
|
expect(screen.getByText('Real-time updates frequency (in minutes):')).toHaveAttribute(
|
|
|
|
'class',
|
|
|
|
expect.stringContaining('text-muted'),
|
|
|
|
);
|
|
|
|
expect(screen.getByLabelText('Real-time updates frequency (in minutes):')).toHaveAttribute('disabled');
|
|
|
|
expect(screen.queryByText('Updates will be reflected in the UI as soon as they happen.')).not.toBeInTheDocument();
|
2021-09-20 22:23:39 +03:00
|
|
|
});
|
|
|
|
|
2021-09-20 23:00:34 +03:00
|
|
|
it.each([
|
2022-03-26 14:17:42 +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) => {
|
2022-06-06 23:23:21 +03:00
|
|
|
setUp({ enabled: true, interval });
|
2021-09-20 22:23:39 +03:00
|
|
|
|
2022-06-06 23:23:21 +03:00
|
|
|
expect(screen.getByText(/^Updates will be reflected in the UI every/)).toHaveTextContent(
|
|
|
|
`${interval} ${minutesWord}`,
|
2021-09-20 22:23:39 +03:00
|
|
|
);
|
2022-06-06 23:23:21 +03:00
|
|
|
expect(screen.getByLabelText('Real-time updates frequency (in minutes):')).toHaveValue(interval);
|
|
|
|
expect(screen.queryByText('Updates will be reflected in the UI as soon as they happen.')).not.toBeInTheDocument();
|
2021-09-20 22:23:39 +03:00
|
|
|
});
|
|
|
|
|
2022-03-26 14:17:42 +03:00
|
|
|
it.each([[undefined], [0]])('shows expected children when interval is 0 or undefined', (interval) => {
|
2022-06-06 23:23:21 +03:00
|
|
|
setUp({ enabled: true, interval });
|
2021-09-20 22:23:39 +03:00
|
|
|
|
2022-06-06 23:23:21 +03:00
|
|
|
expect(screen.queryByText(/^Updates will be reflected in the UI every/)).not.toBeInTheDocument();
|
|
|
|
expect(screen.getByText('Updates will be reflected in the UI as soon as they happen.')).toBeInTheDocument();
|
2021-09-20 22:23:39 +03:00
|
|
|
});
|
|
|
|
|
2022-06-06 23:23:21 +03:00
|
|
|
it('updates real time updates when typing on input', async () => {
|
|
|
|
const { user } = setUp({ enabled: true });
|
2021-09-20 22:23:39 +03:00
|
|
|
|
|
|
|
expect(setRealTimeUpdatesInterval).not.toHaveBeenCalled();
|
2022-06-06 23:23:21 +03:00
|
|
|
await user.type(screen.getByLabelText('Real-time updates frequency (in minutes):'), '5');
|
|
|
|
expect(setRealTimeUpdatesInterval).toHaveBeenCalledWith(5);
|
2021-09-20 22:23:39 +03:00
|
|
|
});
|
|
|
|
|
2022-06-06 23:23:21 +03:00
|
|
|
it('toggles real time updates on switch change', async () => {
|
|
|
|
const { user } = setUp({ enabled: true });
|
2021-09-20 22:23:39 +03:00
|
|
|
|
|
|
|
expect(toggleRealTimeUpdates).not.toHaveBeenCalled();
|
2022-06-06 23:23:21 +03:00
|
|
|
await user.click(screen.getByText(/^Enable or disable real-time updates./));
|
2021-09-20 22:23:39 +03:00
|
|
|
expect(toggleRealTimeUpdates).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|