Files
TREK/client/src/components/Settings/ToggleSwitch.test.tsx
T
Maurice fbba4fd73b fix(tours): refresh the wanderer watch after a fixed token and truly disable its switch offline
The watch row only re-read its state when the connection came or went, so after
a passing connection test or a newly saved token it kept saying "Token refused"
until a reload. The connection card now hands it a key built from the saved
connection and its last check, and the row re-reads the watch whenever that
changes.

Without a connection the switch was only dimmed: the button stayed in the tab
order and was announced as active. ToggleSwitch gains a disabled prop that
disables the button and marks it aria-disabled, and the reason stays reachable
through a focusable wrapper carrying the tooltip.
2026-10-20 15:35:32 +02:00

90 lines
3.6 KiB
TypeScript

import React from 'react';
import { render, screen } from '../../../tests/helpers/render';
import userEvent from '@testing-library/user-event';
import { resetAllStores } from '../../../tests/helpers/store';
import ToggleSwitch from './ToggleSwitch';
beforeEach(() => {
resetAllStores();
vi.clearAllMocks();
});
describe('ToggleSwitch', () => {
it('FE-COMP-TOGGLESWITCH-001: renders a button', () => {
render(<ToggleSwitch on={false} onToggle={() => {}} />);
expect(screen.getByRole('button')).toBeInTheDocument();
});
it('FE-COMP-TOGGLESWITCH-002: knob is positioned left when on is false', () => {
render(<ToggleSwitch on={false} onToggle={() => {}} />);
const button = screen.getByRole('button');
const knob = button.querySelector('span')!;
expect(knob.style.left).toBe('2px');
});
it('FE-COMP-TOGGLESWITCH-003: knob is positioned right when on is true', () => {
render(<ToggleSwitch on={true} onToggle={() => {}} />);
const button = screen.getByRole('button');
const knob = button.querySelector('span')!;
expect(knob.style.left).toBe('22px');
});
it('FE-COMP-TOGGLESWITCH-004: background uses accent variable when on is true', () => {
render(<ToggleSwitch on={true} onToggle={() => {}} />);
const button = screen.getByRole('button');
expect(button.style.background).toContain('var(--accent');
});
it('FE-COMP-TOGGLESWITCH-005: background uses border-primary variable when on is false', () => {
render(<ToggleSwitch on={false} onToggle={() => {}} />);
const button = screen.getByRole('button');
expect(button.style.background).toContain('var(--border-primary');
});
it('FE-COMP-TOGGLESWITCH-006: clicking the button calls onToggle', async () => {
const user = userEvent.setup();
const onToggle = vi.fn();
render(<ToggleSwitch on={false} onToggle={onToggle} />);
await user.click(screen.getByRole('button'));
expect(onToggle).toHaveBeenCalledTimes(1);
});
it('FE-COMP-TOGGLESWITCH-007: clicking does not change visual state without parent update', async () => {
const user = userEvent.setup();
render(<ToggleSwitch on={false} onToggle={() => {}} />);
const button = screen.getByRole('button');
await user.click(button);
expect(button.querySelector('span')!.style.left).toBe('2px');
});
it('FE-COMP-TOGGLESWITCH-008: re-renders correctly when on prop changes from false to true', () => {
const { rerender } = render(<ToggleSwitch on={false} onToggle={() => {}} />);
const button = screen.getByRole('button');
expect(button.querySelector('span')!.style.left).toBe('2px');
rerender(<ToggleSwitch on={true} onToggle={() => {}} />);
expect(button.querySelector('span')!.style.left).toBe('22px');
});
it('FE-COMP-TOGGLESWITCH-009: a disabled switch is out of reach and says so', async () => {
const user = userEvent.setup();
const onToggle = vi.fn();
render(<ToggleSwitch on={false} onToggle={onToggle} disabled />);
const button = screen.getByRole('button');
expect(button).toBeDisabled();
expect(button).toHaveAttribute('aria-disabled', 'true');
expect(button.style.cursor).toBe('not-allowed');
await user.click(button);
await user.tab();
expect(onToggle).not.toHaveBeenCalled();
expect(button).not.toHaveFocus();
});
it('FE-COMP-TOGGLESWITCH-010: an enabled switch keeps the pointer and no aria-disabled', () => {
render(<ToggleSwitch on={false} onToggle={() => {}} />);
const button = screen.getByRole('button');
expect(button).toBeEnabled();
expect(button).not.toHaveAttribute('aria-disabled');
expect(button.style.cursor).toBe('pointer');
});
});