|
| 1 | +import * as React from 'react'; |
| 2 | +import { fireEvent, render, screen, act } from '@testing-library/react'; |
| 3 | +import { Menu, MenuPopover, MenuTrigger } from '@fluentui/react-components'; |
| 4 | +import { ToggleButton } from './ToggleButton'; |
| 5 | + |
| 6 | +describe('ToggleButton', () => { |
| 7 | + beforeEach(() => { |
| 8 | + console.error = jest.fn(); |
| 9 | + }); |
| 10 | + |
| 11 | + it('should throw error for icon button if no title or aria-label is provided', () => { |
| 12 | + console.error = jest.fn(); |
| 13 | + expect(() => render(<ToggleButton checked icon={<i>X</i>} />)).toThrow( |
| 14 | + 'Icon button must have a title' |
| 15 | + ); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should not throw error for icon button if aria-label is provided', () => { |
| 19 | + console.error = jest.fn(); |
| 20 | + expect(() => |
| 21 | + render(<ToggleButton checked aria-label="label" icon={<i>X</i>} />) |
| 22 | + ).not.toThrow(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should render title', () => { |
| 26 | + jest.useFakeTimers(); |
| 27 | + const { getByRole } = render( |
| 28 | + <ToggleButton checked icon={<i>X</i>} title={'Tooltip'} /> |
| 29 | + ); |
| 30 | + |
| 31 | + const button = getByRole('button'); |
| 32 | + fireEvent.pointerEnter(button); |
| 33 | + act(() => { |
| 34 | + jest.runOnlyPendingTimers(); |
| 35 | + }); |
| 36 | + |
| 37 | + const title = screen.queryByText('Tooltip'); |
| 38 | + expect(title).not.toBeNull(); |
| 39 | + |
| 40 | + expect(title?.textContent).toEqual(button.getAttribute('aria-label')); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should error when attempting to wrap with a menu', () => { |
| 44 | + expect(() => |
| 45 | + render( |
| 46 | + <Menu> |
| 47 | + <MenuTrigger> |
| 48 | + <ToggleButton checked={false} icon={<i>X</i>} /> |
| 49 | + </MenuTrigger> |
| 50 | + <MenuPopover></MenuPopover> |
| 51 | + </Menu> |
| 52 | + ) |
| 53 | + ).toThrow('Icon button must have a title'); |
| 54 | + }); |
| 55 | +}); |
0 commit comments