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

57 lines
2 KiB
TypeScript
Raw Normal View History

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, defaultDarkTheme = false) => renderWithEvents(
<UserInterfaceSettings
settings={fromPartial({ ui })}
setUiSettings={setUiSettings}
_matchMedia={vi.fn().mockReturnValue({ matches: defaultDarkTheme })}
/>,
);
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([
[{ theme: 'dark' as const }, true, true],
[{ theme: 'dark' as const }, false, true],
[{ theme: 'light' as const }, true, false],
[{ theme: 'light' as const }, false, false],
[undefined, false, false],
[undefined, true, true],
])('toggles switch if theme is dark', (ui, defaultDarkTheme, expectedChecked) => {
setUp(ui, defaultDarkTheme);
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 const }],
[{ theme: 'light' as const }],
[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 const, 'dark' as const],
['dark' as const, 'light' as const],
])('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
});
});