|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file PageControl.test.tsx |
| 5 | + * @input vitest (jsdom), Testing Library, PageControl |
| 6 | + * @output Proves the control's semantics (one named slider reading "Page N |
| 7 | + * of M"), its keyboard (arrows mirrored in RTL, Home, End), its measured |
| 8 | + * geometry and alphas, the width cap, hidesForSinglePage, and a tap's step |
| 9 | + * to either side of the current indicator |
| 10 | + * @position Testing; the real-touch proofs are |
| 11 | + * __tests__/PageControl.interaction.browser.spec.ts |
| 12 | + */ |
| 13 | + |
| 14 | +import {describe, expect, it, vi} from 'vitest'; |
| 15 | +import {fireEvent, render, screen} from '@testing-library/react'; |
| 16 | +import {PageControl} from './PageControl'; |
| 17 | + |
| 18 | +function renderControl( |
| 19 | + props: Partial<React.ComponentProps<typeof PageControl>> = {}, |
| 20 | +) { |
| 21 | + const onChange = vi.fn(); |
| 22 | + const result = render( |
| 23 | + <PageControl count={5} value={2} onChange={onChange} {...props} />, |
| 24 | + ); |
| 25 | + return {onChange, ...result}; |
| 26 | +} |
| 27 | + |
| 28 | +describe('PageControl', () => { |
| 29 | + it('is one slider named "Page indicators" that reads "Page 3 of 5"', () => { |
| 30 | + renderControl(); |
| 31 | + const slider = screen.getByRole('slider', {name: 'Page indicators'}); |
| 32 | + expect(slider).toHaveAttribute('aria-valuemin', '1'); |
| 33 | + expect(slider).toHaveAttribute('aria-valuemax', '5'); |
| 34 | + expect(slider).toHaveAttribute('aria-valuenow', '3'); |
| 35 | + expect(slider).toHaveAttribute('aria-valuetext', 'Page 3 of 5'); |
| 36 | + expect(slider).toHaveAttribute('tabindex', '0'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('takes a custom label', () => { |
| 40 | + renderControl({label: 'Onboarding'}); |
| 41 | + expect( |
| 42 | + screen.getByRole('slider', {name: 'Onboarding'}), |
| 43 | + ).toBeInTheDocument(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('arrow keys, Home and End ask for a page', () => { |
| 47 | + const {onChange} = renderControl(); |
| 48 | + const slider = screen.getByRole('slider'); |
| 49 | + fireEvent.keyDown(slider, {key: 'ArrowRight'}); |
| 50 | + expect(onChange).toHaveBeenLastCalledWith(3); |
| 51 | + fireEvent.keyDown(slider, {key: 'ArrowLeft'}); |
| 52 | + expect(onChange).toHaveBeenLastCalledWith(1); |
| 53 | + fireEvent.keyDown(slider, {key: 'ArrowUp'}); |
| 54 | + expect(onChange).toHaveBeenLastCalledWith(3); |
| 55 | + fireEvent.keyDown(slider, {key: 'ArrowDown'}); |
| 56 | + expect(onChange).toHaveBeenLastCalledWith(1); |
| 57 | + fireEvent.keyDown(slider, {key: 'Home'}); |
| 58 | + expect(onChange).toHaveBeenLastCalledWith(0); |
| 59 | + fireEvent.keyDown(slider, {key: 'End'}); |
| 60 | + expect(onChange).toHaveBeenLastCalledWith(4); |
| 61 | + }); |
| 62 | + |
| 63 | + it('mirrors the horizontal arrows in RTL', () => { |
| 64 | + const onChange = vi.fn(); |
| 65 | + // jsdom does not inherit `direction`: set it on the control itself. |
| 66 | + render( |
| 67 | + <PageControl |
| 68 | + count={5} |
| 69 | + value={2} |
| 70 | + onChange={onChange} |
| 71 | + style={{direction: 'rtl'}} |
| 72 | + />, |
| 73 | + ); |
| 74 | + const slider = screen.getByRole('slider'); |
| 75 | + fireEvent.keyDown(slider, {key: 'ArrowRight'}); |
| 76 | + expect(onChange).toHaveBeenLastCalledWith(1); |
| 77 | + fireEvent.keyDown(slider, {key: 'ArrowLeft'}); |
| 78 | + expect(onChange).toHaveBeenLastCalledWith(3); |
| 79 | + }); |
| 80 | + |
| 81 | + it('asks for nothing past the first or last page', () => { |
| 82 | + const {onChange} = renderControl({value: 4}); |
| 83 | + fireEvent.keyDown(screen.getByRole('slider'), {key: 'ArrowRight'}); |
| 84 | + expect(onChange).not.toHaveBeenCalled(); |
| 85 | + }); |
| 86 | + |
| 87 | + it("draws UIPageControl's geometry: 9.67 px indicators, the current one opaque, the others at 45 %", () => { |
| 88 | + renderControl(); |
| 89 | + const slider = screen.getByRole('slider'); |
| 90 | + expect(slider.style.height).toBe('25.67px'); |
| 91 | + expect(slider.style.width).toBe('108.35px'); |
| 92 | + const indicators = Array.from( |
| 93 | + slider.firstElementChild?.children ?? [], |
| 94 | + ) as HTMLElement[]; |
| 95 | + expect(indicators).toHaveLength(5); |
| 96 | + expect(indicators[0].style.width).toBe('9.67px'); |
| 97 | + expect(indicators.map(i => i.style.opacity)).toEqual([ |
| 98 | + '0.45', |
| 99 | + '0.45', |
| 100 | + '1', |
| 101 | + '0.45', |
| 102 | + '0.45', |
| 103 | + ]); |
| 104 | + }); |
| 105 | + |
| 106 | + it('never grows wider than 161.3 px; past that the row shifts to keep the current page in view', () => { |
| 107 | + renderControl({count: 50, value: 25}); |
| 108 | + const slider = screen.getByRole('slider'); |
| 109 | + expect(slider.style.width).toBe('161.3px'); |
| 110 | + const strip = slider.firstElementChild as HTMLElement; |
| 111 | + expect( |
| 112 | + Number.parseFloat(strip.style.getPropertyValue('--_page-control-offset')), |
| 113 | + ).toBeGreaterThan(0); |
| 114 | + }); |
| 115 | + |
| 116 | + it('with hidesForSinglePage, renders nothing for one page', () => { |
| 117 | + renderControl({count: 1, value: 0, hidesForSinglePage: true}); |
| 118 | + expect(screen.queryByRole('slider')).toBeNull(); |
| 119 | + }); |
| 120 | +}); |
0 commit comments