-
Notifications
You must be signed in to change notification settings - Fork 161
[UEPR-483] Action Menu Tests #452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kbangelov
merged 6 commits into
scratchfoundation:release/UEPR-297-accessibility-improvements
from
kbangelov:task/uepr-483-action-menu-tests
Mar 23, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d1d4950
chore: wrote 3 unit tests for action menu
kbangelov d12b4af
chore: updated tests
kbangelov cf4c01f
chore: replaced timeouts with waitFor
kbangelov c12bc3b
Merge branch 'task/uepr-483-choose-a-sprite-and-backdrop-accessibilit…
kbangelov 10eb45b
chore: separated tests and simplified them
kbangelov cec1579
chore: separated and renamed tests
kbangelov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
165 changes: 165 additions & 0 deletions
165
packages/scratch-gui/test/unit/components/action-menu.test.jsx
This file contains hidden or 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,165 @@ | ||
| import '@testing-library/jest-dom'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import {render, screen, fireEvent, waitFor} from '@testing-library/react'; | ||
| import ActionMenu from '../../../src/components/action-menu/action-menu.jsx'; | ||
| import {KEY} from '../../../src/lib/navigation-keys'; | ||
| import React, {act} from 'react'; | ||
|
|
||
| // Mock the CSS module so class names exist | ||
| jest.mock('../../../src/components/action-menu/action-menu.css', () => ({ | ||
| expanded: 'expanded' | ||
| })); | ||
|
|
||
| describe('ActionMenu keyboard navigation', () => { | ||
| const mockOnClick = jest.fn(); | ||
| const mockMoreButtonClick = jest.fn(); | ||
|
|
||
| const defaultProps = { | ||
| title: 'Main Button', | ||
| img: 'main-icon.svg', | ||
| onClick: mockOnClick, | ||
| moreButtons: [ | ||
| {title: 'Button 1', img: 'icon1.svg', onClick: mockMoreButtonClick}, | ||
| {title: 'Button 2', img: 'icon2.svg', onClick: mockMoreButtonClick}, | ||
| {title: 'Button 3', img: 'icon3.svg', onClick: mockMoreButtonClick} | ||
| ] | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| mockOnClick.mockClear(); | ||
| mockMoreButtonClick.mockClear(); | ||
| }); | ||
|
|
||
| test('expands menu upon focus on main button', () => { | ||
| render(<ActionMenu {...defaultProps} />); | ||
| const mainButton = screen.getByRole('button', {name: 'Main Button'}); | ||
|
|
||
| act(() => { | ||
| mainButton.focus(); | ||
| }); | ||
|
|
||
| expect(mainButton.parentElement).toHaveClass('expanded'); | ||
| }); | ||
|
|
||
| test('focuses first item on arrow_down', async () => { | ||
| render(<ActionMenu {...defaultProps} />); | ||
| const mainButton = screen.getByRole('button', {name: 'Main Button'}); | ||
|
|
||
| act(() => { | ||
| mainButton.focus(); | ||
| fireEvent.keyDown(mainButton, {key: KEY.ARROW_DOWN}); | ||
| }); | ||
|
|
||
| await waitFor(() => { | ||
| expect(document.activeElement).toBe(screen.getByRole('button', {name: 'Button 1'})); | ||
| }); | ||
| }); | ||
|
|
||
| test('focuses last item on arrow_up', async () => { | ||
| render(<ActionMenu {...defaultProps} />); | ||
| const mainButton = screen.getByRole('button', {name: 'Main Button'}); | ||
|
|
||
| act(() => { | ||
| mainButton.focus(); | ||
| fireEvent.keyDown(mainButton, {key: KEY.ARROW_UP}); | ||
| }); | ||
|
|
||
| await waitFor(() => { | ||
| expect(document.activeElement).toBe(screen.getByRole('button', {name: 'Button 3'})); | ||
| }); | ||
| }); | ||
|
|
||
| test('cycles from first item to last on arrow_up', async () => { | ||
| render(<ActionMenu {...defaultProps} />); | ||
|
|
||
| const firstItem = screen.getByRole('button', {name: 'Button 1'}); | ||
| const lastItem = screen.getByRole('button', {name: 'Button 3'}); | ||
|
|
||
| act(() => { | ||
| firstItem.focus(); | ||
| fireEvent.keyDown(firstItem, {key: KEY.ARROW_UP}); | ||
| }); | ||
|
|
||
| await waitFor(() => { | ||
| expect(document.activeElement).toBe(lastItem); | ||
| }); | ||
| }); | ||
|
|
||
| test('cycles from last item to first on arrow_down', async () => { | ||
| render(<ActionMenu {...defaultProps} />); | ||
|
|
||
| const firstItem = screen.getByRole('button', {name: 'Button 1'}); | ||
| const lastItem = screen.getByRole('button', {name: 'Button 3'}); | ||
|
|
||
| act(() => { | ||
| lastItem.focus(); | ||
| fireEvent.keyDown(lastItem, {key: KEY.ARROW_DOWN}); | ||
| }); | ||
|
|
||
| await waitFor(() => { | ||
| expect(document.activeElement).toBe(firstItem); | ||
| }); | ||
| }); | ||
|
|
||
| test('focuses to main button on escape', async () => { | ||
| render(<ActionMenu {...defaultProps} />); | ||
| const mainButton = screen.getByRole('button', {name: 'Main Button'}); | ||
| const firstItem = screen.getByRole('button', {name: 'Button 1'}); | ||
|
|
||
| act(() => { | ||
| firstItem.focus(); | ||
| fireEvent.keyDown(firstItem, {key: KEY.ESCAPE}); | ||
| }); | ||
|
|
||
| await waitFor(() => { | ||
| expect(document.activeElement).toBe(mainButton); | ||
| }); | ||
| }); | ||
|
|
||
| test('closes menu and focuses next element on tab', async () => { | ||
| render( | ||
| <> | ||
| <ActionMenu {...defaultProps} /> | ||
| <button>After Menu</button> | ||
| </> | ||
| ); | ||
| const firstItem = screen.getByRole('button', {name: 'Button 1'}); | ||
| const afterButton = screen.getByRole('button', {name: 'After Menu'}); | ||
| const user = userEvent.setup(); | ||
|
|
||
| act(() => { | ||
| firstItem.focus(); | ||
| }); | ||
|
|
||
| await user.tab(); | ||
|
|
||
| await waitFor(() => { | ||
| expect(document.activeElement).toBe(afterButton); | ||
| expect(screen.getByRole('button', {name: 'Main Button'}).parentElement).not.toHaveClass('expanded'); | ||
| }); | ||
| }); | ||
|
|
||
| test('closes menu and focuses previous element on shift + tab', async () => { | ||
| render( | ||
| <> | ||
| <button>Before Menu</button> | ||
| <ActionMenu {...defaultProps} /> | ||
| </> | ||
| ); | ||
|
|
||
| const mainButton = screen.getByRole('button', {name: 'Main Button'}); | ||
| const beforeButton = screen.getByRole('button', {name: 'Before Menu'}); | ||
| const user = userEvent.setup(); | ||
|
|
||
| act(() => { | ||
| mainButton.focus(); | ||
| }); | ||
|
|
||
| await user.tab({shift: true}); | ||
|
|
||
| await waitFor(() => { | ||
| expect(document.activeElement).toBe(beforeButton); | ||
| expect(mainButton.parentElement).not.toHaveClass('expanded'); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.