|
| 1 | +import React from 'react' |
| 2 | +import '@testing-library/jest-dom/extend-expect' |
| 3 | +import {render} from '@testing-library/react' |
| 4 | +import {EyeIcon, FileCodeIcon, PeopleIcon} from '@primer/octicons-react' |
| 5 | +import userEvent from '@testing-library/user-event' |
| 6 | +import {behavesAsComponent, checkExports, checkStoriesForAxeViolations} from '../utils/testing' |
| 7 | +import {SegmentedControl} from '.' // TODO: update import when we move this to the global index |
| 8 | + |
| 9 | +const segmentData = [ |
| 10 | + {label: 'Preview', iconLabel: 'EyeIcon', icon: () => <EyeIcon aria-label="EyeIcon" />}, |
| 11 | + {label: 'Raw', iconLabel: 'FileCodeIcon', icon: () => <FileCodeIcon aria-label="FileCodeIcon" />}, |
| 12 | + {label: 'Blame', iconLabel: 'PeopleIcon', icon: () => <PeopleIcon aria-label="PeopleIcon" />} |
| 13 | +] |
| 14 | + |
| 15 | +// TODO: improve test coverage |
| 16 | +describe('SegmentedControl', () => { |
| 17 | + behavesAsComponent({ |
| 18 | + Component: SegmentedControl, |
| 19 | + toRender: () => ( |
| 20 | + <SegmentedControl aria-label="File view"> |
| 21 | + <SegmentedControl.Button selected>Preview</SegmentedControl.Button> |
| 22 | + <SegmentedControl.Button>Raw</SegmentedControl.Button> |
| 23 | + <SegmentedControl.Button>Blame</SegmentedControl.Button> |
| 24 | + </SegmentedControl> |
| 25 | + ) |
| 26 | + }) |
| 27 | + |
| 28 | + checkExports('SegmentedControl', { |
| 29 | + default: undefined, |
| 30 | + SegmentedControl |
| 31 | + }) |
| 32 | + |
| 33 | + it('renders with a selected segment', () => { |
| 34 | + const {getByText} = render( |
| 35 | + <SegmentedControl aria-label="File view"> |
| 36 | + {segmentData.map(({label}, index) => ( |
| 37 | + <SegmentedControl.Button selected={index === 1} key={label}> |
| 38 | + {label} |
| 39 | + </SegmentedControl.Button> |
| 40 | + ))} |
| 41 | + </SegmentedControl> |
| 42 | + ) |
| 43 | + |
| 44 | + const selectedButton = getByText('Raw').closest('button') |
| 45 | + |
| 46 | + expect(selectedButton?.getAttribute('aria-current')).toBe('true') |
| 47 | + }) |
| 48 | + |
| 49 | + it('renders the first segment as selected if no child has the `selected` prop passed', () => { |
| 50 | + const {getByText} = render( |
| 51 | + <SegmentedControl aria-label="File view"> |
| 52 | + {segmentData.map(({label}) => ( |
| 53 | + <SegmentedControl.Button key={label}>{label}</SegmentedControl.Button> |
| 54 | + ))} |
| 55 | + </SegmentedControl> |
| 56 | + ) |
| 57 | + |
| 58 | + const selectedButton = getByText('Preview').closest('button') |
| 59 | + |
| 60 | + expect(selectedButton?.getAttribute('aria-current')).toBe('true') |
| 61 | + }) |
| 62 | + |
| 63 | + it('renders segments with segment labels that have leading icons', () => { |
| 64 | + const {getByLabelText} = render( |
| 65 | + <SegmentedControl aria-label="File view"> |
| 66 | + {segmentData.map(({label, icon}, index) => ( |
| 67 | + <SegmentedControl.Button selected={index === 0} leadingIcon={icon} key={label}> |
| 68 | + {label} |
| 69 | + </SegmentedControl.Button> |
| 70 | + ))} |
| 71 | + </SegmentedControl> |
| 72 | + ) |
| 73 | + |
| 74 | + for (const datum of segmentData) { |
| 75 | + const iconEl = getByLabelText(datum.iconLabel) |
| 76 | + expect(iconEl).toBeDefined() |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + it('renders segments with accessible icon-only labels', () => { |
| 81 | + const {getByLabelText} = render( |
| 82 | + <SegmentedControl aria-label="File view"> |
| 83 | + {segmentData.map(({label, icon}) => ( |
| 84 | + <SegmentedControl.IconButton icon={icon} aria-label={label} key={label} /> |
| 85 | + ))} |
| 86 | + </SegmentedControl> |
| 87 | + ) |
| 88 | + |
| 89 | + for (const datum of segmentData) { |
| 90 | + const labelledButton = getByLabelText(datum.label) |
| 91 | + expect(labelledButton).toBeDefined() |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + it('calls onChange with index of clicked segment button', () => { |
| 96 | + const handleChange = jest.fn() |
| 97 | + const {getByText} = render( |
| 98 | + <SegmentedControl aria-label="File view" onChange={handleChange}> |
| 99 | + {segmentData.map(({label}, index) => ( |
| 100 | + <SegmentedControl.Button selected={index === 0} key={label}> |
| 101 | + {label} |
| 102 | + </SegmentedControl.Button> |
| 103 | + ))} |
| 104 | + </SegmentedControl> |
| 105 | + ) |
| 106 | + |
| 107 | + const buttonToClick = getByText('Raw').closest('button') |
| 108 | + |
| 109 | + expect(handleChange).not.toHaveBeenCalled() |
| 110 | + if (buttonToClick) { |
| 111 | + userEvent.click(buttonToClick) |
| 112 | + } |
| 113 | + expect(handleChange).toHaveBeenCalledWith(1) |
| 114 | + }) |
| 115 | + |
| 116 | + it('calls segment button onClick if it is passed', () => { |
| 117 | + const handleClick = jest.fn() |
| 118 | + const {getByText} = render( |
| 119 | + <SegmentedControl aria-label="File view"> |
| 120 | + {segmentData.map(({label}, index) => ( |
| 121 | + <SegmentedControl.Button selected={index === 0} onClick={index === 1 ? handleClick : undefined} key={label}> |
| 122 | + {label} |
| 123 | + </SegmentedControl.Button> |
| 124 | + ))} |
| 125 | + </SegmentedControl> |
| 126 | + ) |
| 127 | + |
| 128 | + const buttonToClick = getByText('Raw').closest('button') |
| 129 | + |
| 130 | + expect(handleClick).not.toHaveBeenCalled() |
| 131 | + if (buttonToClick) { |
| 132 | + userEvent.click(buttonToClick) |
| 133 | + } |
| 134 | + expect(handleClick).toHaveBeenCalled() |
| 135 | + }) |
| 136 | +}) |
| 137 | + |
| 138 | +checkStoriesForAxeViolations('examples', '../SegmentedControl/') |
| 139 | +checkStoriesForAxeViolations('fixtures', '../SegmentedControl/') |
0 commit comments