import React from 'react';
import {render, screen} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {MantineProvider} from '@mantine/core';
import {IconMoodSmile} from '@tabler/icons-react';
import {ThemeContext} from '../../contexts/contexts';
import {cssVariablesResolver, mantineTheme, semanticColorNames} from '../../themes/mantine';
import {themeChoices} from '../../themes/names';
import {
ActionInput,
Button,
Confirm,
Header,
Icon,
IconStack,
Pagination,
SearchBox,
TabBar,
tabClassName,
IconButton,
Label,
Message,
Panel,
Progress,
resolveIconName,
Status,
Table,
ThemePicker,
Toggle,
} from './index';
// The components need Mantine's provider, exactly as ThemeProvider supplies it.
const renderUI = (ui) => render(
{ui}
);
describe('Icon', () => {
it('renders a Semantic name as an SVG', () => {
const {container} = renderUI();
expect(container.querySelector('svg')).toBeInTheDocument();
});
it('accepts a Tabler component directly', () => {
const {container} = renderUI();
expect(container.querySelector('svg')).toBeInTheDocument();
});
it('maps every Semantic icon name the app uses', () => {
// A name that falls through renders a fallback glyph, leaving a hole in the UI.
const names = ['trash', 'warning sign', 'download', 'refresh', 'plus', 'edit', 'wifi',
'search', 'lock', 'check', 'upload', 'play', 'folder', 'filter', 'file text',
'external', 'disk', 'circle notched', 'terminal', 'eye'];
const unmapped = names.filter(name => !resolveIconName(name));
expect(unmapped).toEqual([]);
});
it('hides decorative icons from assistive technology but names labelled ones', () => {
const {container} = renderUI(<>>);
const [decorative, labelled] = [...container.querySelectorAll('svg')];
expect(decorative).toHaveAttribute('aria-hidden', 'true');
expect(labelled).toHaveAttribute('aria-label', 'Delete');
expect(labelled).not.toHaveAttribute('aria-hidden');
});
it('maps every icon the theme choices ask for', () => {
// Regression: the map was built by grepping literal name='...' strings, so icons
// passed through a variable (the nav bar's picker) were missed and three of the
// five theme choices rendered a fallback glyph.
const unmapped = themeChoices.filter(choice => !resolveIconName(choice.icon));
expect(unmapped.map(c => `${c.text}: ${c.icon}`)).toEqual([]);
});
it('reports an unknown name instead of failing silently', () => {
const error = jest.spyOn(console, 'error').mockImplementation(() => {});
renderUI();
expect(error).toHaveBeenCalledWith(expect.stringContaining('not a real icon'));
error.mockRestore();
});
});
describe('Button', () => {
it('renders a role as a themed button', () => {
renderUI();
expect(screen.getByRole('button', {name: 'Download'})).toBeInTheDocument();
});
it('marks destructive buttons so night mode can restyle them', () => {
// The dashed-outline treatment lives in CSS keyed on this class; without it a
// Delete button would stay filled red in night mode.
renderUI();
expect(screen.getByRole('button', {name: 'Delete'})).toHaveClass('wrolpi-button-danger');
});
it('lets a call site override the role colors', () => {
renderUI();
expect(screen.getByRole('button', {name: 'Odd one out'})).toHaveAttribute('data-variant', 'outline');
});
it('renders an icon alongside the label', () => {
const {container} = renderUI();
expect(container.querySelector('svg')).toBeInTheDocument();
expect(screen.getByRole('button', {name: 'Save'})).toBeInTheDocument();
});
it('gives icon-only buttons an accessible name', () => {
renderUI();
expect(screen.getByRole('button', {name: 'Delete channel'})).toBeInTheDocument();
});
});
describe('Message', () => {
it('announces errors assertively and other kinds politely', () => {
renderUI(<>>);
expect(screen.getByRole('alert')).toHaveTextContent('Failed');
expect(screen.getByRole('status')).toHaveTextContent('Working');
});
it('gives errors the dashed danger border in every theme', () => {
renderUI(Try again);
expect(screen.getByRole('alert')).toHaveClass('wrolpi-message-error');
});
it('dismisses when asked', async () => {
const onDismiss = jest.fn();
renderUI();
await userEvent.click(screen.getByRole('button', {name: 'Dismiss'}));
expect(onDismiss).toHaveBeenCalled();
});
it('offers no dismiss button when the message cannot be cleared', () => {
renderUI();
expect(screen.queryByRole('button', {name: 'Dismiss'})).not.toBeInTheDocument();
});
it('renders a leading icon when given one', () => {
const {container} = renderUI();
expect(container.querySelector('.wrolpi-message-icon svg')).toBeInTheDocument();
});
});
describe('Header', () => {
it('renders the requested heading level', () => {
renderUI(Browser Extension);
expect(screen.getByRole('heading', {level: 1, name: 'Browser Extension'})).toBeInTheDocument();
});
it('defaults to h3 so a call site that omits the level still nests sanely', () => {
renderUI(Firefox);
expect(screen.getByRole('heading', {level: 3, name: 'Firefox'})).toBeInTheDocument();
});
it('takes its size from the level, not from the call site', () => {
// The class is what ui.css keys the type scale on; a call site passing its own
// font-size is the thing this component exists to prevent.
const {container} = renderUI(Install steps);
expect(container.querySelector('.wrolpi-header-h4')).toBeInTheDocument();
});
it('renders an icon and a subheader', () => {
const {container} = renderUI(
This WROLPi's URL
);
expect(container.querySelector('svg')).toBeInTheDocument();
expect(screen.getByText('Paste this into the extension')).toBeInTheDocument();
});
});
describe('ActionInput', () => {
it('joins the input and its action into one control', () => {
const {container} = renderUI(
Copy}/>
);
expect(container.querySelector('.wrolpi-action-input')).toBeInTheDocument();
expect(screen.getByRole('textbox', {name: 'URL'})).toHaveValue('https://wrolpi.local');
expect(screen.getByRole('button', {name: 'Copy'})).toBeInTheDocument();
});
it('is a plain input when no action is given', () => {
const {container} = renderUI();
expect(container.querySelector('.wrolpi-action-input')).not.toBeInTheDocument();
expect(screen.getByRole('textbox', {name: 'URL'})).toBeInTheDocument();
});
});
describe('Progress', () => {
it('reports its value to assistive technology', () => {
renderUI(