|
| 1 | +import { render } from '@testing-library/react'; |
| 2 | +import { createRef } from 'react'; |
| 3 | +import { Panel, type PanelTitleProps } from '..'; |
| 4 | +import { renderClient, renderServer } from '#util/components'; |
| 5 | + |
| 6 | +describe('Panel', () => { |
| 7 | + it('matches snapshot', async () => { |
| 8 | + const { container } = await renderClient( |
| 9 | + <Panel> |
| 10 | + <Panel.Title>Booking complete</Panel.Title> |
| 11 | + We have sent you a confirmation email |
| 12 | + </Panel>, |
| 13 | + { className: 'nhsuk-panel' }, |
| 14 | + ); |
| 15 | + |
| 16 | + expect(container).toMatchSnapshot(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('matches snapshot (via server)', async () => { |
| 20 | + const { container, element } = await renderServer( |
| 21 | + <Panel> |
| 22 | + <Panel.Title>Booking complete</Panel.Title> |
| 23 | + We have sent you a confirmation email |
| 24 | + </Panel>, |
| 25 | + { className: 'nhsuk-panel' }, |
| 26 | + ); |
| 27 | + |
| 28 | + expect(container).toMatchSnapshot('server'); |
| 29 | + |
| 30 | + await renderClient(element, { |
| 31 | + className: 'nhsuk-panel', |
| 32 | + hydrate: true, |
| 33 | + container, |
| 34 | + }); |
| 35 | + |
| 36 | + expect(container).toMatchSnapshot('client'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('forwards refs', async () => { |
| 40 | + const ref = createRef<HTMLDivElement>(); |
| 41 | + |
| 42 | + const { modules } = await renderClient( |
| 43 | + <Panel ref={ref}> |
| 44 | + <Panel.Title>Booking complete</Panel.Title> |
| 45 | + We have sent you a confirmation email |
| 46 | + </Panel>, |
| 47 | + { className: 'nhsuk-panel' }, |
| 48 | + ); |
| 49 | + |
| 50 | + const [panelEl] = modules; |
| 51 | + |
| 52 | + expect(ref.current).toBe(panelEl); |
| 53 | + expect(ref.current).toHaveClass('nhsuk-panel'); |
| 54 | + }); |
| 55 | + |
| 56 | + it.each<PanelTitleProps | undefined>([ |
| 57 | + undefined, |
| 58 | + { headingLevel: 'h1' }, |
| 59 | + { headingLevel: 'h2' }, |
| 60 | + { headingLevel: 'h3' }, |
| 61 | + { headingLevel: 'h4' }, |
| 62 | + ])('renders heading level $headingLevel if specified', (props) => { |
| 63 | + const { container } = render( |
| 64 | + <Panel> |
| 65 | + <Panel.Title {...props}>Booking complete</Panel.Title> |
| 66 | + We have sent you a confirmation email |
| 67 | + </Panel>, |
| 68 | + ); |
| 69 | + |
| 70 | + const title = container.querySelector('.nhsuk-panel__title'); |
| 71 | + |
| 72 | + expect(title).toHaveProperty('tagName', props?.headingLevel?.toUpperCase() ?? 'H1'); |
| 73 | + }); |
| 74 | +}); |
0 commit comments