import { renderToString } from 'react-dom/server'; import { render } from '@testing-library/react'; import { Edit } from '@transferwise/icons'; import { mockMatchMedia, within } from '../test-utils'; import Provider from '../Provider'; import en from '../i18n/en.json'; import { ListItem } from './ListItem'; mockMatchMedia(); describe('ListItem server rendering and hydration', () => { beforeEach(() => { jest.spyOn(console, 'error').mockImplementation((message: string) => { // React 18 warns about the existing layout effects when rendering on the server. if (!message.startsWith('Warning: useLayoutEffect does nothing on the server')) { throw new Error(message); } }); }); afterEach(() => { jest.restoreAllMocks(); }); const controls = [ { name: 'navigation link', control: }, { name: 'navigation button', control: }, { name: 'navigation without indicator', control: , }, { name: 'button', control: Pay }, { name: 'button link', control: Pay }, { name: 'icon button', control: ( ), }, { name: 'icon button link', control: ( ), }, { name: 'checkbox', control: }, { name: 'radio', control: }, { name: 'switch', control: }, ]; describe.each(['active', 'inactive'] as const)('%s spotlight', (spotlight) => { it.each(controls)( 'is present before hydration and unchanged afterwards for $name', ({ control }) => { const element = ( ); const container = document.createElement('div'); container.innerHTML = renderToString(element); const item = within(container).getByRole('listitem'); expect(item).toHaveClass( 'wds-list-item-interactive', 'wds-list-item-spotlight', `wds-list-item-spotlight-${spotlight}`, ); expect(item.querySelector('.wds-list-item-spotlight__border') !== null).toBe( spotlight === 'inactive', ); const serverMarkup = container.innerHTML; render(element, { container, hydrate: true }); expect(within(container).getByRole('listitem')).toBe(item); expect(container.innerHTML).toBe(serverMarkup); }, ); it.each([ { name: 'no control', control: undefined }, { name: 'partial button', control: Pay, }, { name: 'partial icon button', control: ( ), }, ])('is absent before and after hydration for $name', ({ control }) => { const element = ( ); const container = document.createElement('div'); container.innerHTML = renderToString(element); expect(within(container).getByRole('listitem')).not.toHaveClass('wds-list-item-spotlight'); expect(container.querySelector('.wds-list-item-spotlight__border')).toBeNull(); const serverMarkup = container.innerHTML; render(element, { container, hydrate: true }); expect(container.innerHTML).toBe(serverMarkup); }); }); });