|
| 1 | +import React from 'react'; |
| 2 | +import { Factory } from 'rosie'; |
| 3 | +import { |
| 4 | + initializeTestStore, |
| 5 | + render, |
| 6 | + screen, |
| 7 | + fireEvent, |
| 8 | + waitFor, |
| 9 | +} from '@src/setupTest'; |
| 10 | +import userEvent from '@testing-library/user-event'; |
| 11 | +import SidebarContext from '../SidebarContext'; |
| 12 | +import SidebarBase from './SidebarBase'; |
| 13 | +import messages from '../../messages'; |
| 14 | +import { useSidebarFocusAndKeyboard } from './hooks/useSidebarFocusAndKeyboard'; |
| 15 | + |
| 16 | +jest.mock('./hooks/useSidebarFocusAndKeyboard'); |
| 17 | + |
| 18 | +const SIDEBAR_ID = 'test-sidebar'; |
| 19 | + |
| 20 | +const mockUseSidebarFocusAndKeyboard = useSidebarFocusAndKeyboard; |
| 21 | + |
| 22 | +describe('SidebarBase (Refactored)', () => { |
| 23 | + let mockContextValue; |
| 24 | + const courseMetadata = Factory.build('courseMetadata'); |
| 25 | + const user = userEvent.setup(); |
| 26 | + |
| 27 | + let mockCloseBtnRef; |
| 28 | + let mockBackBtnRef; |
| 29 | + let mockHandleClose; |
| 30 | + let mockHandleKeyDown; |
| 31 | + let mockHandleBackBtnKeyDown; |
| 32 | + |
| 33 | + const renderSidebar = (contextProps = {}, componentProps = {}) => { |
| 34 | + const fullContextValue = { ...mockContextValue, ...contextProps }; |
| 35 | + const defaultProps = { |
| 36 | + title: 'Test Sidebar Title', |
| 37 | + ariaLabel: 'Test Sidebar Aria Label', |
| 38 | + sidebarId: SIDEBAR_ID, |
| 39 | + className: 'test-class', |
| 40 | + children: <div>Sidebar Content</div>, |
| 41 | + ...componentProps, |
| 42 | + }; |
| 43 | + return render( |
| 44 | + <SidebarContext.Provider value={fullContextValue}> |
| 45 | + <SidebarBase {...defaultProps} /> |
| 46 | + </SidebarContext.Provider>, |
| 47 | + ); |
| 48 | + }; |
| 49 | + |
| 50 | + beforeEach(async () => { |
| 51 | + await initializeTestStore({ |
| 52 | + courseMetadata, |
| 53 | + excludeFetchCourse: true, |
| 54 | + excludeFetchSequence: true, |
| 55 | + }); |
| 56 | + |
| 57 | + mockContextValue = { |
| 58 | + courseId: courseMetadata.id, |
| 59 | + toggleSidebar: jest.fn(), |
| 60 | + shouldDisplayFullScreen: false, |
| 61 | + currentSidebar: null, |
| 62 | + }; |
| 63 | + |
| 64 | + mockCloseBtnRef = React.createRef(); |
| 65 | + mockBackBtnRef = React.createRef(); |
| 66 | + mockHandleClose = jest.fn(); |
| 67 | + mockHandleKeyDown = jest.fn(); |
| 68 | + mockHandleBackBtnKeyDown = jest.fn(); |
| 69 | + |
| 70 | + mockUseSidebarFocusAndKeyboard.mockReturnValue({ |
| 71 | + closeBtnRef: mockCloseBtnRef, |
| 72 | + backBtnRef: mockBackBtnRef, |
| 73 | + handleClose: mockHandleClose, |
| 74 | + handleKeyDown: mockHandleKeyDown, |
| 75 | + handleBackBtnKeyDown: mockHandleBackBtnKeyDown, |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + afterEach(() => { |
| 80 | + jest.clearAllMocks(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should render children, title, and close button when visible', () => { |
| 84 | + renderSidebar({ currentSidebar: SIDEBAR_ID }); |
| 85 | + expect(screen.getByText('Sidebar Content')).toBeInTheDocument(); |
| 86 | + expect(screen.getByText('Test Sidebar Title')).toBeInTheDocument(); |
| 87 | + expect(screen.getByRole('button', { name: messages.closeNotificationTrigger.defaultMessage })).toBeInTheDocument(); |
| 88 | + expect(screen.getByTestId(`sidebar-${SIDEBAR_ID}`)).toBeInTheDocument(); |
| 89 | + expect(screen.getByTestId(`sidebar-${SIDEBAR_ID}`)).not.toHaveClass('d-none'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should be hidden via CSS class when not the current sidebar', () => { |
| 93 | + renderSidebar({ currentSidebar: 'another-sidebar-id' }); |
| 94 | + const sidebarElement = screen.queryByTestId(`sidebar-${SIDEBAR_ID}`); |
| 95 | + expect(sidebarElement).toBeInTheDocument(); |
| 96 | + expect(sidebarElement).toHaveClass('d-none'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should hide title bar when showTitleBar prop is false', () => { |
| 100 | + renderSidebar({ currentSidebar: SIDEBAR_ID }, { showTitleBar: false }); |
| 101 | + expect(screen.queryByText('Test Sidebar Title')).not.toBeInTheDocument(); |
| 102 | + expect(screen.queryByRole('button', { name: messages.closeNotificationTrigger.defaultMessage })).not.toBeInTheDocument(); |
| 103 | + expect(screen.getByText('Sidebar Content')).toBeInTheDocument(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should render back button instead of close button in fullscreen', () => { |
| 107 | + renderSidebar({ currentSidebar: SIDEBAR_ID, shouldDisplayFullScreen: true }); |
| 108 | + expect(screen.getByRole('button', { name: messages.responsiveCloseNotificationTray.defaultMessage })).toBeInTheDocument(); |
| 109 | + expect(screen.queryByRole('button', { name: messages.closeNotificationTrigger.defaultMessage })).not.toBeInTheDocument(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should call handleClose from hook on close button click', async () => { |
| 113 | + renderSidebar({ currentSidebar: SIDEBAR_ID }); |
| 114 | + const closeButton = screen.getByRole('button', { name: messages.closeNotificationTrigger.defaultMessage }); |
| 115 | + await user.click(closeButton); |
| 116 | + expect(mockHandleClose).toHaveBeenCalledTimes(1); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should call handleClose from hook on fullscreen back button click', async () => { |
| 120 | + renderSidebar({ currentSidebar: SIDEBAR_ID, shouldDisplayFullScreen: true }); |
| 121 | + const backButton = screen.getByRole('button', { name: messages.responsiveCloseNotificationTray.defaultMessage }); |
| 122 | + await user.click(backButton); |
| 123 | + expect(mockHandleClose).toHaveBeenCalledTimes(1); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should call handleKeyDown from hook on standard close button keydown', () => { |
| 127 | + renderSidebar({ currentSidebar: SIDEBAR_ID }); |
| 128 | + const closeButton = screen.getByRole('button', { name: messages.closeNotificationTrigger.defaultMessage }); |
| 129 | + fireEvent.keyDown(closeButton, { key: 'Tab' }); |
| 130 | + expect(mockHandleKeyDown).toHaveBeenCalledTimes(1); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should call handleBackBtnKeyDown from hook on fullscreen back button keydown', () => { |
| 134 | + renderSidebar({ currentSidebar: SIDEBAR_ID, shouldDisplayFullScreen: true }); |
| 135 | + const backButton = screen.getByRole('button', { name: messages.responsiveCloseNotificationTray.defaultMessage }); |
| 136 | + fireEvent.keyDown(backButton, { key: 'Enter' }); |
| 137 | + expect(mockHandleBackBtnKeyDown).toHaveBeenCalledTimes(1); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should call toggleSidebar(null) upon receiving a "close" postMessage event', async () => { |
| 141 | + renderSidebar({ currentSidebar: SIDEBAR_ID }); |
| 142 | + |
| 143 | + fireEvent(window, new MessageEvent('message', { |
| 144 | + data: { type: 'learning.events.sidebar.close' }, |
| 145 | + })); |
| 146 | + |
| 147 | + await waitFor(() => { |
| 148 | + expect(mockContextValue.toggleSidebar).toHaveBeenCalledTimes(1); |
| 149 | + expect(mockContextValue.toggleSidebar).toHaveBeenCalledWith(null); |
| 150 | + }); |
| 151 | + }); |
| 152 | +}); |
0 commit comments