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

51 lines
1.8 KiB
TypeScript
Raw Normal View History

import type { Theme } from '@shlinkio/shlink-frontend-kit';
import { screen } from '@testing-library/react';
import { fromPartial } from '@total-typescript/shoehorn';
import type { UiSettings } from '../../src/settings/reducers/settings';
import { UserInterfaceSettings } from '../../src/settings/UserInterfaceSettings';
2023-09-30 11:45:52 +03:00
import { checkAccessibility } from '../__helpers__/accessibility';
2022-07-10 20:44:49 +03:00
import { renderWithEvents } from '../__helpers__/setUpTest';
2021-02-16 21:31:16 +03:00
describe('<UserInterfaceSettings />', () => {
2023-05-27 12:57:26 +03:00
const setUiSettings = vi.fn();
const setUp = (ui?: UiSettings) => renderWithEvents(
<UserInterfaceSettings settings={fromPartial({ ui })} setUiSettings={setUiSettings} />,
);
2021-02-16 21:31:16 +03:00
2023-09-30 11:45:52 +03:00
it('passes a11y checks', () => checkAccessibility(setUp()));
2021-02-16 21:31:16 +03:00
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
});
});