|
| 1 | +import { createElement } from 'react' |
| 2 | +import { describe, expect, test } from 'vitest' |
| 3 | +import { renderToStaticMarkup } from 'react-dom/server' |
| 4 | +import { load } from 'cheerio' |
| 5 | +import { SidebarExpandIcon } from '@primer/octicons-react' |
| 6 | + |
| 7 | +import { IconButton } from '@/frame/components/ui/IconButton' |
| 8 | +import type { IconButtonProps } from '@/frame/components/ui/IconButton' |
| 9 | + |
| 10 | +const LABEL = 'Collapse sidebar' |
| 11 | + |
| 12 | +function render(props: Partial<IconButtonProps> = {}) { |
| 13 | + const html = renderToStaticMarkup( |
| 14 | + createElement(IconButton, { icon: SidebarExpandIcon, 'aria-label': LABEL, ...props }), |
| 15 | + ) |
| 16 | + return load(html) |
| 17 | +} |
| 18 | + |
| 19 | +describe('IconButton', () => { |
| 20 | + test('labels the button through its tooltip, without a second aria-label', () => { |
| 21 | + const $ = render() |
| 22 | + const button = $('button') |
| 23 | + expect(button.attr('aria-label')).toBeUndefined() |
| 24 | + const labelId = button.attr('aria-labelledby') |
| 25 | + expect(labelId).toBeTruthy() |
| 26 | + const tooltip = $(`[id="${labelId}"]`) |
| 27 | + expect(tooltip.text()).toBe(LABEL) |
| 28 | + expect(tooltip.attr('aria-hidden')).toBe('true') |
| 29 | + }) |
| 30 | + |
| 31 | + test('uses aria-label and renders no tooltip when the tooltip is off', () => { |
| 32 | + const $ = render({ tooltip: false }) |
| 33 | + expect($('button').attr('aria-label')).toBe(LABEL) |
| 34 | + expect($('button').attr('aria-labelledby')).toBeUndefined() |
| 35 | + expect($('div').length).toBe(0) |
| 36 | + }) |
| 37 | + |
| 38 | + test('renders no tooltip for an aria-hidden button', () => { |
| 39 | + const $ = render({ 'aria-hidden': true, tabIndex: -1 }) |
| 40 | + expect($('button').attr('aria-label')).toBe(LABEL) |
| 41 | + expect($('button').attr('aria-hidden')).toBe('true') |
| 42 | + expect($('div').length).toBe(0) |
| 43 | + }) |
| 44 | + |
| 45 | + test('is a type=button and passes through button attributes', () => { |
| 46 | + const $ = render({ |
| 47 | + 'data-testid': 'sidebar-collapse-toggle', |
| 48 | + 'aria-expanded': true, |
| 49 | + tabIndex: 0, |
| 50 | + className: 'consumer', |
| 51 | + } as Partial<IconButtonProps>) |
| 52 | + const button = $('button') |
| 53 | + expect(button.attr('type')).toBe('button') |
| 54 | + expect(button.attr('data-testid')).toBe('sidebar-collapse-toggle') |
| 55 | + expect(button.attr('aria-expanded')).toBe('true') |
| 56 | + expect(button.attr('tabindex')).toBe('0') |
| 57 | + expect(button.hasClass('consumer')).toBe(true) |
| 58 | + }) |
| 59 | + |
| 60 | + test('defaults to the default variant at medium size', () => { |
| 61 | + const $ = render() |
| 62 | + expect($('button').attr('data-variant')).toBe('default') |
| 63 | + expect($('button').attr('data-size')).toBe('medium') |
| 64 | + expect(render({ variant: 'invisible', size: 'small' })('button').attr('data-size')).toBe( |
| 65 | + 'small', |
| 66 | + ) |
| 67 | + }) |
| 68 | +}) |
0 commit comments