|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 3 | +import '@testing-library/jest-dom/extend-expect'; |
| 4 | +import { IntlProvider } from '@edx/frontend-platform/i18n'; |
| 5 | +import { MemoryRouter } from 'react-router-dom'; |
| 6 | + |
| 7 | +import HeaderBody from './HeaderBody'; |
| 8 | +import messages from './messages'; |
| 9 | + |
| 10 | +const mockOnNavigate = jest.fn(); |
| 11 | +const mockSearchButtonAction = jest.fn(); |
| 12 | +const mockToggleModalPopup = jest.fn(); |
| 13 | +const mockSetModalPopupTarget = jest.fn(); |
| 14 | + |
| 15 | +const defaultProps = { |
| 16 | + studioBaseUrl: 'https://example.com', |
| 17 | + logoutUrl: 'https://example.com/logout', |
| 18 | + onNavigate: mockOnNavigate, |
| 19 | + setModalPopupTarget: mockSetModalPopupTarget, |
| 20 | + toggleModalPopup: mockToggleModalPopup, |
| 21 | + searchButtonAction: mockSearchButtonAction, |
| 22 | + username: 'testuser', |
| 23 | + authenticatedUserAvatar: 'avatar.png', |
| 24 | + isAdmin: true, |
| 25 | + isMobile: false, |
| 26 | + isHiddenMainMenu: false, |
| 27 | + mainMenuDropdowns: [], |
| 28 | + logo: 'logo.png', |
| 29 | + logoAltText: 'Test Logo', |
| 30 | + number: '101', |
| 31 | + org: 'EDX', |
| 32 | + title: 'Test Course', |
| 33 | + outlineLink: '/courses/edx/course-101', |
| 34 | +}; |
| 35 | + |
| 36 | +const RootWrapper = (props) => ( |
| 37 | + <MemoryRouter> |
| 38 | + <IntlProvider locale="en" messages={messages}> |
| 39 | + <HeaderBody {...props} /> |
| 40 | + </IntlProvider> |
| 41 | + </MemoryRouter> |
| 42 | +); |
| 43 | + |
| 44 | +describe('HeaderBody Component', () => { |
| 45 | + afterEach(() => { |
| 46 | + jest.clearAllMocks(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('renders the logo and brand navigation', () => { |
| 50 | + render(<RootWrapper {...defaultProps} />); |
| 51 | + |
| 52 | + const logoImage = screen.getByAltText(defaultProps.logoAltText); |
| 53 | + expect(logoImage).toBeInTheDocument(); |
| 54 | + expect(logoImage).toHaveAttribute('src', defaultProps.logo); |
| 55 | + }); |
| 56 | + |
| 57 | + it('renders course lockup information', () => { |
| 58 | + render(<RootWrapper {...defaultProps} />); |
| 59 | + |
| 60 | + const courseTitle = screen.getByText(defaultProps.title); |
| 61 | + const courseOrgNumber = screen.getByText(`${defaultProps.org} ${defaultProps.number}`); |
| 62 | + |
| 63 | + expect(courseTitle).toBeInTheDocument(); |
| 64 | + expect(courseOrgNumber).toBeInTheDocument(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('renders a course lock-up link with the correct outline URL', () => { |
| 68 | + render(<RootWrapper {...defaultProps} />); |
| 69 | + |
| 70 | + const courseLockUpLink = screen.getByTestId('course-lock-up-block'); |
| 71 | + expect(courseLockUpLink.getAttribute('href')).toBe(defaultProps.outlineLink); |
| 72 | + }); |
| 73 | + |
| 74 | + it('displays search button and triggers searchButtonAction on click', () => { |
| 75 | + render(<RootWrapper {...defaultProps} />); |
| 76 | + |
| 77 | + const searchButton = screen.getByLabelText(messages['header.label.search.nav'].defaultMessage); |
| 78 | + expect(searchButton).toBeInTheDocument(); |
| 79 | + |
| 80 | + fireEvent.click(searchButton); |
| 81 | + expect(mockSearchButtonAction).toHaveBeenCalled(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('displays user menu with username and avatar', () => { |
| 85 | + render(<RootWrapper {...defaultProps} />); |
| 86 | + |
| 87 | + const userMenu = screen.getByText(defaultProps.username); |
| 88 | + const avatarImage = screen.getByAltText(defaultProps.username); |
| 89 | + |
| 90 | + expect(userMenu).toBeInTheDocument(); |
| 91 | + expect(avatarImage).toHaveAttribute('src', defaultProps.authenticatedUserAvatar); |
| 92 | + }); |
| 93 | + |
| 94 | + it('toggles mobile menu popup when button is clicked in mobile view', () => { |
| 95 | + render(<RootWrapper {...defaultProps} isMobile isModalPopupOpen={false} />); |
| 96 | + |
| 97 | + const menuButton = screen.getByTestId('mobile-menu-button'); |
| 98 | + fireEvent.click(menuButton); |
| 99 | + |
| 100 | + expect(mockToggleModalPopup).toHaveBeenCalled(); |
| 101 | + }); |
| 102 | +}); |
0 commit comments