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-08-19 18:21:34 +03:00
|
|
|
import type { UiSettings } from '../../src/settings/reducers/settings';
|
2021-12-25 12:49:12 +03:00
|
|
|
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
|
|
|
|
2021-12-25 12:49:12 +03:00
|
|
|
describe('<UserInterfaceSettings />', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const setUiSettings = vi.fn();
|
2023-12-19 01:05:17 +03:00
|
|
|
const setUp = (ui?: UiSettings, defaultDarkTheme = false) => renderWithEvents(
|
|
|
|
<UserInterfaceSettings
|
|
|
|
settings={fromPartial({ ui })}
|
|
|
|
setUiSettings={setUiSettings}
|
|
|
|
_matchMedia={vi.fn().mockReturnValue({ matches: defaultDarkTheme })}
|
|
|
|
/>,
|
2022-07-10 00:03:21 +03:00
|
|
|
);
|
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([
|
2023-12-19 01:05:17 +03:00
|
|
|
[{ 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
|
|
|
|
2022-06-07 21:22:30 +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
|
|
|
});
|
|
|
|
|
2021-02-19 20:55:03 +03:00
|
|
|
it.each([
|
2023-12-19 01:05:17 +03:00
|
|
|
[{ theme: 'dark' as const }],
|
|
|
|
[{ theme: 'light' as const }],
|
2022-06-07 21:22:30 +03:00
|
|
|
[undefined],
|
|
|
|
])('shows different icons based on theme', (ui) => {
|
|
|
|
setUp(ui);
|
|
|
|
expect(screen.getByRole('img', { hidden: true })).toMatchSnapshot();
|
2021-02-19 20:55:03 +03:00
|
|
|
});
|
|
|
|
|
2021-02-16 21:31:16 +03:00
|
|
|
it.each([
|
2023-12-19 01:05:17 +03:00
|
|
|
['light' as const, 'dark' as const],
|
|
|
|
['dark' as const, 'light' as const],
|
2022-06-07 21:22:30 +03:00
|
|
|
])('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();
|
2022-06-07 21:22:30 +03:00
|
|
|
await user.click(screen.getByLabelText('Use dark theme.'));
|
|
|
|
expect(setUiSettings).toHaveBeenCalledWith({ theme: expectedTheme });
|
2021-02-16 21:31:16 +03:00
|
|
|
});
|
|
|
|
});
|