|
| 1 | +import * as React from 'react'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { spy } from 'sinon'; |
| 4 | +import { act } from '@mui/internal-test-utils'; |
| 5 | +import { ToggleButton } from '@base_ui/react/ToggleButton'; |
| 6 | +import { createRenderer, describeConformance } from '#test-utils'; |
| 7 | + |
| 8 | +describe('<ToggleButton />', () => { |
| 9 | + const { render } = createRenderer(); |
| 10 | + |
| 11 | + describeConformance(<ToggleButton />, () => ({ |
| 12 | + refInstanceof: window.HTMLButtonElement, |
| 13 | + render, |
| 14 | + })); |
| 15 | + |
| 16 | + describe('pressed state', () => { |
| 17 | + it('controlled', async () => { |
| 18 | + function App() { |
| 19 | + const [pressed, setPressed] = React.useState(false); |
| 20 | + return ( |
| 21 | + <div> |
| 22 | + <input type="checkbox" checked={pressed} onChange={() => setPressed(!pressed)} /> |
| 23 | + <ToggleButton pressed={pressed} />; |
| 24 | + </div> |
| 25 | + ); |
| 26 | + } |
| 27 | + |
| 28 | + const { getByRole } = await render(<App />); |
| 29 | + const checkbox = getByRole('checkbox'); |
| 30 | + const button = getByRole('button'); |
| 31 | + |
| 32 | + expect(button).to.have.attribute('aria-pressed', 'false'); |
| 33 | + await act(async () => { |
| 34 | + checkbox.click(); |
| 35 | + }); |
| 36 | + |
| 37 | + expect(button).to.have.attribute('aria-pressed', 'true'); |
| 38 | + |
| 39 | + await act(async () => { |
| 40 | + checkbox.click(); |
| 41 | + }); |
| 42 | + |
| 43 | + expect(button).to.have.attribute('aria-pressed', 'false'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('uncontrolled', async () => { |
| 47 | + const { getByRole } = await render(<ToggleButton defaultPressed={false} />); |
| 48 | + |
| 49 | + const button = getByRole('button'); |
| 50 | + |
| 51 | + expect(button).to.have.attribute('aria-pressed', 'false'); |
| 52 | + await act(async () => { |
| 53 | + button.click(); |
| 54 | + }); |
| 55 | + |
| 56 | + expect(button).to.have.attribute('aria-pressed', 'true'); |
| 57 | + |
| 58 | + await act(async () => { |
| 59 | + button.click(); |
| 60 | + }); |
| 61 | + |
| 62 | + expect(button).to.have.attribute('aria-pressed', 'false'); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('prop: onPressedChange', () => { |
| 67 | + it('is called when the pressed state changes', async () => { |
| 68 | + const handlePressed = spy(); |
| 69 | + const { getByRole } = await render( |
| 70 | + <ToggleButton defaultPressed={false} onPressedChange={handlePressed} />, |
| 71 | + ); |
| 72 | + |
| 73 | + const button = getByRole('button'); |
| 74 | + |
| 75 | + await act(async () => { |
| 76 | + button.click(); |
| 77 | + }); |
| 78 | + |
| 79 | + expect(handlePressed.callCount).to.equal(1); |
| 80 | + expect(handlePressed.firstCall.args[0]).to.equal(true); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('prop: disabled', () => { |
| 85 | + it('disables the component', async () => { |
| 86 | + const handlePressed = spy(); |
| 87 | + const { getByRole } = await render(<ToggleButton disabled onPressedChange={handlePressed} />); |
| 88 | + |
| 89 | + const button = getByRole('button'); |
| 90 | + expect(button).to.have.attribute('disabled'); |
| 91 | + expect(button).to.have.attribute('aria-pressed', 'false'); |
| 92 | + |
| 93 | + await act(async () => { |
| 94 | + button.click(); |
| 95 | + }); |
| 96 | + |
| 97 | + expect(handlePressed.callCount).to.equal(0); |
| 98 | + expect(button).to.have.attribute('aria-pressed', 'false'); |
| 99 | + }); |
| 100 | + }); |
| 101 | +}); |
0 commit comments