import { render, screen, waitFor } from '@testing-library/react'; import { ThemeProvider, ThemeProviderProps } from './ThemeProvider'; import { useState } from 'react'; describe('ThemeProvider', () => { it('tests local theme provider (isNotRootProvider) has not affect on root theme (on html element)', () => { render(
local-theme-provider
, ); // global theme isn't affected (in this case isn't set) expect(document.documentElement).not.toHaveClass( 'np-theme-personal', 'np-theme-personal--dark', ); // only local theme being set // eslint-disable-next-line testing-library/no-node-access expect(screen.getByText('local-theme-provider').parentElement).toHaveClass( 'np-theme-personal np-theme-personal--dark', ); }); it('tests root provider set root theme (on html element) and it is not affected by nested provider(s)', () => { render(
root-dark-theme
nested-forest-green
nested-personal
, ); // root provider set dark theme and it wasn't changed by two nested providers expect(document.documentElement).toHaveClass('np-theme-personal np-theme-personal--dark'); // eslint-disable-next-line testing-library/no-node-access expect(screen.getByText('root-dark-theme').parentElement).toHaveClass( 'np-theme-personal np-theme-personal--dark', ); // eslint-disable-next-line testing-library/no-node-access expect(screen.getByText('nested-forest-green').parentElement).toHaveClass( 'np-theme-personal np-theme-personal--forest-green', ); // eslint-disable-next-line testing-library/no-node-access expect(screen.getByText('nested-personal').parentElement).toHaveClass('np-theme-personal'); }); it('tests deprecated light theme normalization', () => { render(
light
personal
personal-dark
, ); // root 'light' theme is normalized to 'personal' expect(document.documentElement).toHaveClass('np-theme-personal'); // 'light' theme is normalized to 'personal' // eslint-disable-next-line testing-library/no-node-access expect(screen.getByText('light').parentElement).toHaveClass('np-theme-personal'); // eslint-disable-next-line testing-library/no-node-access expect(screen.getByText('personal').parentElement).toHaveClass('np-theme-personal'); // eslint-disable-next-line testing-library/no-node-access expect(screen.getByText('personal-dark').parentElement).toHaveClass( 'np-theme-personal', 'np-theme-personal--dark', ); }); it('updates theming setting based of props updates', async () => { const TestComponent = () => { const [theme, setTheme] = useState('personal'); const [screenMode, setScreenMode] = useState('light'); return ( <>
content
); }; render(); // eslint-disable-next-line testing-library/no-node-access expect(screen.getByText('content').parentElement).toHaveClass('np-theme-personal'); // Change to business theme screen.getByText('Change to business').click(); await waitFor(() => { // eslint-disable-next-line testing-library/no-node-access expect(screen.getByText('content').parentElement).toHaveClass( 'np-theme-personal', 'np-theme-business', ); }); // Change to dark mode screen.getByText('Change to dark').click(); await waitFor(() => { // eslint-disable-next-line testing-library/no-node-access expect(screen.getByText('content').parentElement).toHaveClass( 'np-theme-personal', 'np-theme-business', 'np-theme-business--dark', ); }); }); it('sets platform themes', () => { render(
personal
platform
platform--forest-green
, ); // eslint-disable-next-line testing-library/no-node-access expect(screen.getByText('personal').parentElement).toHaveClass('np-theme-personal'); // eslint-disable-next-line testing-library/no-node-access expect(screen.getByText('platform').parentElement).toHaveClass( 'np-theme-personal', 'np-theme-platform', ); // eslint-disable-next-line testing-library/no-node-access expect(screen.getByText('platform--forest-green').parentElement).toHaveClass( 'np-theme-personal', 'np-theme-platform--forest-green', ); }); it('sets business themes', () => { render(
business
business-dark
business--bright-green
business--forest-green
, ); // eslint-disable-next-line testing-library/no-node-access expect(screen.getByText('business').parentElement).toHaveClass( 'np-theme-personal', 'np-theme-business', ); // eslint-disable-next-line testing-library/no-node-access expect(screen.getByText('business-dark').parentElement).toHaveClass( 'np-theme-personal', 'np-theme-business', 'np-theme-business--dark', ); // eslint-disable-next-line testing-library/no-node-access expect(screen.getByText('business--bright-green').parentElement).toHaveClass( 'np-theme-personal', 'np-theme-business--bright-green', ); // eslint-disable-next-line testing-library/no-node-access expect(screen.getByText('business--forest-green').parentElement).toHaveClass( 'np-theme-personal', 'np-theme-business--forest-green', ); }); });