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

50 lines
1.7 KiB
TypeScript
Raw Normal View History

import { screen } from '@testing-library/react';
2021-02-16 21:31:16 +03:00
import { Mock } from 'ts-mockery';
import { Settings, UiSettings } from '../../src/settings/reducers/settings';
import { UserInterfaceSettings } from '../../src/settings/UserInterfaceSettings';
2021-02-16 21:31:16 +03:00
import { Theme } from '../../src/utils/theme';
2022-07-10 20:44:49 +03:00
import { renderWithEvents } from '../__helpers__/setUpTest';
2021-02-16 21:31:16 +03:00
describe('<UserInterfaceSettings />', () => {
2021-02-16 21:31:16 +03:00
const setUiSettings = jest.fn();
const setUp = (ui?: UiSettings) => renderWithEvents(
<UserInterfaceSettings settings={Mock.of<Settings>({ ui })} setUiSettings={setUiSettings} />,
);
2021-02-16 21:31:16 +03:00
afterEach(jest.clearAllMocks);
it.each([
2022-03-26 14:17:42 +03:00
[{ theme: 'dark' as Theme }, true],
[{ theme: 'light' as Theme }, false],
[undefined, false],
])('toggles switch if theme is dark', (ui, expectedChecked) => {
setUp(ui);
2021-02-16 21:31:16 +03:00
if (expectedChecked) {
expect(screen.getByLabelText('Use dark theme.')).toBeChecked();
} else {
expect(screen.getByLabelText('Use dark theme.')).not.toBeChecked();
}
2021-02-16 21:31:16 +03:00
});
it.each([
[{ theme: 'dark' as Theme }],
[{ theme: 'light' as Theme }],
[undefined],
])('shows different icons based on theme', (ui) => {
setUp(ui);
expect(screen.getByRole('img', { hidden: true })).toMatchSnapshot();
});
2021-02-16 21:31:16 +03:00
it.each([
['light' as Theme, 'dark' as Theme],
['dark' as Theme, 'light' as Theme],
])('invokes setUiSettings when theme toggle value changes', async (initialTheme, expectedTheme) => {
const { user } = setUp({ theme: initialTheme });
2021-02-16 21:31:16 +03:00
expect(setUiSettings).not.toHaveBeenCalled();
await user.click(screen.getByLabelText('Use dark theme.'));
expect(setUiSettings).toHaveBeenCalledWith({ theme: expectedTheme });
2021-02-16 21:31:16 +03:00
});
});