-
Notifications
You must be signed in to change notification settings - Fork 295
feat: improved accessibility of notification tray #1817
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
Draft
filippovskii09
wants to merge
7
commits into
openedx:master
Choose a base branch
from
raccoongang:filippovskii/feat/course-unit-notifications-tray
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+785
−29
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
220d14e
feat: improved accessibility of notification tray
filippovskii09 c6fc88b
fix: upgrate test coverage & fix disccusion & some refactor
filippovskii09 8eb8ab0
fix: fix lint errors
filippovskii09 5199cee
fix: remove extra comments & add description for handleKeyDown
filippovskii09 217a46c
fix: refactor after review
filippovskii09 d23f8a2
fix: some refactor
filippovskii09 ca3a43c
fix: some formating and refactor
filippovskii09 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
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,154 @@ | ||
| import { Factory } from 'rosie'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { createRef } from 'react'; | ||
|
|
||
| import { | ||
| initializeTestStore, | ||
| render, | ||
| screen, | ||
| fireEvent, | ||
| waitFor, | ||
| } from '@src/setupTest'; | ||
| import messages from '@src/courseware/course/messages'; | ||
| import SidebarContext from '../SidebarContext'; | ||
| import SidebarBase from './SidebarBase'; | ||
| import { useSidebarFocusAndKeyboard } from './hooks/useSidebarFocusAndKeyboard'; | ||
|
|
||
| jest.mock('./hooks/useSidebarFocusAndKeyboard'); | ||
|
|
||
| const SIDEBAR_ID = 'test-sidebar'; | ||
|
|
||
| const mockUseSidebarFocusAndKeyboard = useSidebarFocusAndKeyboard; | ||
|
|
||
| describe('SidebarBase', () => { | ||
| let mockContextValue; | ||
| const courseMetadata = Factory.build('courseMetadata'); | ||
| const user = userEvent.setup(); | ||
|
|
||
| let mockCloseBtnRef; | ||
| let mockBackBtnRef; | ||
| let mockHandleClose; | ||
| let mockHandleKeyDown; | ||
| let mockHandleBackBtnKeyDown; | ||
|
|
||
| const defaultComponentProps = { | ||
| title: 'Test Sidebar Title', | ||
| ariaLabel: 'Test Sidebar Aria Label', | ||
| sidebarId: SIDEBAR_ID, | ||
| className: 'test-class', | ||
| children: <div>Sidebar Content</div>, | ||
| }; | ||
|
|
||
| const renderSidebar = (contextProps = {}, componentProps = {}) => { | ||
| const fullContextValue = { ...mockContextValue, ...contextProps }; | ||
| const defaultProps = { ...defaultComponentProps, ...componentProps }; | ||
| return render( | ||
| <SidebarContext.Provider value={fullContextValue}> | ||
| <SidebarBase {...defaultProps} /> | ||
| </SidebarContext.Provider>, | ||
| ); | ||
| }; | ||
|
|
||
| beforeEach(async () => { | ||
| await initializeTestStore({ | ||
| courseMetadata, | ||
| excludeFetchCourse: true, | ||
| excludeFetchSequence: true, | ||
| }); | ||
|
|
||
| mockContextValue = { | ||
| courseId: courseMetadata.id, | ||
| toggleSidebar: jest.fn(), | ||
| shouldDisplayFullScreen: false, | ||
| currentSidebar: null, | ||
| }; | ||
|
|
||
| mockCloseBtnRef = createRef(); | ||
| mockBackBtnRef = createRef(); | ||
| mockHandleClose = jest.fn(); | ||
| mockHandleKeyDown = jest.fn(); | ||
| mockHandleBackBtnKeyDown = jest.fn(); | ||
|
|
||
| mockUseSidebarFocusAndKeyboard.mockReturnValue({ | ||
| closeBtnRef: mockCloseBtnRef, | ||
| backBtnRef: mockBackBtnRef, | ||
| handleClose: mockHandleClose, | ||
| handleKeyDown: mockHandleKeyDown, | ||
| handleBackBtnKeyDown: mockHandleBackBtnKeyDown, | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should render children, title, and close button when visible', () => { | ||
| renderSidebar({ currentSidebar: SIDEBAR_ID }); | ||
| expect(screen.getByText('Sidebar Content')).toBeInTheDocument(); | ||
| expect(screen.getByText(defaultComponentProps.title)).toBeInTheDocument(); | ||
| expect(screen.getByRole('button', { name: messages.closeNotificationTrigger.defaultMessage })).toBeInTheDocument(); | ||
| expect(screen.getByTestId(`sidebar-${SIDEBAR_ID}`)).toBeInTheDocument(); | ||
| expect(screen.getByTestId(`sidebar-${SIDEBAR_ID}`)).not.toHaveClass('d-none'); | ||
| }); | ||
|
|
||
| it('should be hidden via CSS class when not the current sidebar', () => { | ||
| renderSidebar({ currentSidebar: 'another-sidebar-id' }); | ||
| const sidebarElement = screen.queryByTestId(`sidebar-${SIDEBAR_ID}`); | ||
| expect(sidebarElement).toBeInTheDocument(); | ||
| expect(sidebarElement).toHaveClass('d-none'); | ||
| }); | ||
|
|
||
| it('should hide title bar when showTitleBar prop is false', () => { | ||
| renderSidebar({ currentSidebar: SIDEBAR_ID }, { showTitleBar: false }); | ||
| expect(screen.queryByText(defaultComponentProps.title)).not.toBeInTheDocument(); | ||
| expect(screen.queryByRole('button', { name: messages.closeNotificationTrigger.defaultMessage })).not.toBeInTheDocument(); | ||
| expect(screen.getByText('Sidebar Content')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should render back button instead of close button in fullscreen', () => { | ||
| renderSidebar({ currentSidebar: SIDEBAR_ID, shouldDisplayFullScreen: true }); | ||
| expect(screen.getByRole('button', { name: messages.responsiveCloseNotificationTray.defaultMessage })).toBeInTheDocument(); | ||
| expect(screen.queryByRole('button', { name: messages.closeNotificationTrigger.defaultMessage })).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should call handleClose from hook on close button click', async () => { | ||
| renderSidebar({ currentSidebar: SIDEBAR_ID }); | ||
| const closeButton = screen.getByRole('button', { name: messages.closeNotificationTrigger.defaultMessage }); | ||
| await user.click(closeButton); | ||
| expect(mockHandleClose).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should call handleClose from hook on fullscreen back button click', async () => { | ||
| renderSidebar({ currentSidebar: SIDEBAR_ID, shouldDisplayFullScreen: true }); | ||
| const backButton = screen.getByRole('button', { name: messages.responsiveCloseNotificationTray.defaultMessage }); | ||
| await user.click(backButton); | ||
| expect(mockHandleClose).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should call handleKeyDown from hook on standard close button keydown', () => { | ||
| renderSidebar({ currentSidebar: SIDEBAR_ID }); | ||
| const closeButton = screen.getByRole('button', { name: messages.closeNotificationTrigger.defaultMessage }); | ||
| fireEvent.keyDown(closeButton, { key: 'Tab' }); | ||
| expect(mockHandleKeyDown).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should call handleBackBtnKeyDown from hook on fullscreen back button keydown', () => { | ||
| renderSidebar({ currentSidebar: SIDEBAR_ID, shouldDisplayFullScreen: true }); | ||
| const backButton = screen.getByRole('button', { name: messages.responsiveCloseNotificationTray.defaultMessage }); | ||
| fireEvent.keyDown(backButton, { key: 'Enter' }); | ||
| expect(mockHandleBackBtnKeyDown).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should call toggleSidebar(null) upon receiving a "close" postMessage event', async () => { | ||
| renderSidebar({ currentSidebar: SIDEBAR_ID }); | ||
|
|
||
| fireEvent(window, new MessageEvent('message', { | ||
| data: { type: 'learning.events.sidebar.close' }, | ||
| })); | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockContextValue.toggleSidebar).toHaveBeenCalledTimes(1); | ||
| expect(mockContextValue.toggleSidebar).toHaveBeenCalledWith(null); | ||
| }); | ||
| }); | ||
| }); |
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
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 |
|---|---|---|
| @@ -1,27 +1,37 @@ | ||
| import PropTypes from 'prop-types'; | ||
| import React from 'react'; | ||
| import { forwardRef } from 'react'; | ||
|
|
||
| const SidebarTriggerBase = ({ | ||
| const SidebarTriggerBase = forwardRef(({ | ||
| onClick, | ||
| onKeyDown, | ||
| ariaLabel, | ||
| children, | ||
| }) => ( | ||
| isOpenNotificationStatusBar, | ||
| sectionId, | ||
| }, ref) => ( | ||
| <button | ||
| className="border border-light-400 bg-transparent align-items-center align-content-center d-flex notification-btn" | ||
| className="border border-light-400 bg-transparent align-items-center align-content-center d-flex notification-btn sidebar-trigger-btn" | ||
| type="button" | ||
| onClick={onClick} | ||
| onKeyDown={onKeyDown} | ||
| aria-label={ariaLabel} | ||
| aria-expanded={isOpenNotificationStatusBar} | ||
| aria-controls={sectionId} | ||
| ref={ref} | ||
| > | ||
| <div className="icon-container d-flex position-relative align-items-center"> | ||
| {children} | ||
| </div> | ||
| </button> | ||
| ); | ||
| )); | ||
|
|
||
| SidebarTriggerBase.propTypes = { | ||
| onClick: PropTypes.func.isRequired, | ||
| onKeyDown: PropTypes.func.isRequired, | ||
| ariaLabel: PropTypes.string.isRequired, | ||
| children: PropTypes.element.isRequired, | ||
| isOpenNotificationStatusBar: PropTypes.bool.isRequired, | ||
| sectionId: PropTypes.string.isRequired, | ||
| }; | ||
|
|
||
| export default SidebarTriggerBase; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[clarify]: Is there any reason why sidebarId was removed from deps? Also, do I need to remove // eslint-disable-next-line react-hooks/exhaustive-deps?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no reason, removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My question is related to the fact that
sidebarIdwas previously included in the dependency array. Why was it removed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because
sidebarIdit was an unnecessary dependency