-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: splitButton accessibility improvements (#1458)
* feat: splitButton accessibility improvements * feat: splitButton accessibility improvements * feat: splitButton accessibility improvements * refactor: apply code review comments (pairing session) --------- Co-authored-by: Mozafar Haider <[email protected]>
- Loading branch information
1 parent
f11eabb
commit 51a1b14
Showing
2 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { render, fireEvent, cleanup, waitFor } from '@testing-library/react' | ||
import React from 'react' | ||
import '@testing-library/jest-dom/extend-expect' | ||
import { SplitButton } from './split-button.js' | ||
|
||
describe('SplitButton', () => { | ||
afterEach(cleanup) | ||
|
||
it('renders button with children', () => { | ||
const { getByText } = render(<SplitButton>Click me</SplitButton>) | ||
expect(getByText('Click me')).toBeInTheDocument() | ||
}) | ||
|
||
it('toggles dropdown when left button is clicked', () => { | ||
const { getByTestId, queryByTestId } = render(<SplitButton />) | ||
const toggleButton = getByTestId('dhis2-uicore-splitbutton-toggle') | ||
|
||
fireEvent.click(toggleButton) | ||
expect( | ||
queryByTestId('dhis2-uicore-splitbutton-menu') | ||
).toBeInTheDocument() | ||
|
||
fireEvent.click(toggleButton) | ||
expect( | ||
queryByTestId('dhis2-uicore-splitbutton-menu') | ||
).not.toBeInTheDocument() | ||
}) | ||
|
||
it('renders dropdown content when open is true', () => { | ||
const { getByTestId } = render( | ||
<SplitButton component={<div>Dropdown Content</div>} /> | ||
) | ||
|
||
const toggleButton = getByTestId('dhis2-uicore-splitbutton-toggle') | ||
fireEvent.click(toggleButton) | ||
|
||
expect(getByTestId('dhis2-uicore-splitbutton-menu')).toBeInTheDocument() | ||
}) | ||
|
||
it("does not close dropdown 'Enter' key is pressed", async () => { | ||
const { getByTestId } = render( | ||
<SplitButton component={<div>Dropdown Content</div>} /> | ||
) | ||
|
||
const toggleButton = getByTestId('dhis2-uicore-splitbutton-toggle') | ||
fireEvent.click(toggleButton) | ||
expect(getByTestId('dhis2-uicore-splitbutton-menu')).toBeInTheDocument() | ||
|
||
fireEvent.keyDown(document, { key: 'Enter' }) | ||
|
||
// Use waitFor to wait for the DOM to update | ||
await waitFor(() => { | ||
expect( | ||
getByTestId('dhis2-uicore-splitbutton-menu') | ||
).toBeInTheDocument() | ||
}) | ||
}) | ||
|
||
it('closes dropdown when escape key is pressed', async () => { | ||
const { getByTestId, queryByTestId } = render( | ||
<SplitButton component={<div>Dropdown Content</div>} /> | ||
) | ||
|
||
const toggleButton = getByTestId('dhis2-uicore-splitbutton-toggle') | ||
fireEvent.click(toggleButton) | ||
expect(getByTestId('dhis2-uicore-splitbutton-menu')).toBeInTheDocument() | ||
|
||
fireEvent.keyDown(document, { key: 'Escape' }) | ||
|
||
// Use waitFor to wait for the DOM to update | ||
await waitFor(() => { | ||
expect( | ||
queryByTestId('dhis2-uicore-splitbutton-menu') | ||
).not.toBeInTheDocument() | ||
}) | ||
}) | ||
|
||
it('adds title and aria-label attributes to the right button', () => { | ||
const { getByTestId } = render(<SplitButton />) | ||
const toggleButton = getByTestId('dhis2-uicore-splitbutton-toggle') | ||
|
||
expect(toggleButton).toHaveAttribute('title', 'Toggle dropdown') | ||
expect(toggleButton).toHaveAttribute('aria-label', 'Toggle dropdown') | ||
}) | ||
}) |