|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 3 | +import { IntlProvider, defineMessages } from 'react-intl'; |
| 4 | +import { getConfig } from '@edx/frontend-platform'; |
| 5 | +import AuthenticatedUserDropdown from './AuthenticatedUserDropdown'; |
| 6 | + |
| 7 | +jest.mock('@edx/frontend-platform', () => ({ |
| 8 | + getConfig: jest.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +jest.mock('../plugin-slots/LearningUserMenuSlot', () => ({ items }) => ( |
| 12 | + <div> |
| 13 | + {items.map((item) => ( |
| 14 | + <a key={item.href} href={item.href}>{item.message}</a> |
| 15 | + ))} |
| 16 | + </div> |
| 17 | +)); |
| 18 | + |
| 19 | +const messages = defineMessages({ |
| 20 | + dashboard: { defaultMessage: 'Dashboard' }, |
| 21 | + profile: { defaultMessage: 'Profile' }, |
| 22 | + account: { defaultMessage: 'Account Settings' }, |
| 23 | + orderHistory: { defaultMessage: 'Order History' }, |
| 24 | + signOut: { defaultMessage: 'Sign Out' }, |
| 25 | + userOptionsDropdownLabel: { defaultMessage: 'User Options' }, |
| 26 | +}); |
| 27 | + |
| 28 | +const configMock = { |
| 29 | + LMS_BASE_URL: 'https://lms.example.com', |
| 30 | + ACCOUNT_PROFILE_URL: 'https://accounts.example.com', |
| 31 | + ACCOUNT_SETTINGS_URL: 'https://accounts.example.com/settings', |
| 32 | + ORDER_HISTORY_URL: 'https://lms.example.com/orders', |
| 33 | + LOGOUT_URL: 'https://lms.example.com/logout', |
| 34 | +}; |
| 35 | + |
| 36 | +describe('AuthenticatedUserDropdown', () => { |
| 37 | + const username = 'testuser'; |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + getConfig.mockReturnValue(configMock); |
| 41 | + }); |
| 42 | + |
| 43 | + const renderComponent = () => { |
| 44 | + render( |
| 45 | + <IntlProvider locale="en" messages={Object.fromEntries( |
| 46 | + Object.entries(messages).map(([key, value]) => [key, value.defaultMessage]) |
| 47 | + )}> |
| 48 | + <AuthenticatedUserDropdown username={username} /> |
| 49 | + </IntlProvider> |
| 50 | + ); |
| 51 | + }; |
| 52 | + |
| 53 | + it('renders username in toggle button', () => { |
| 54 | + renderComponent(); |
| 55 | + expect(screen.getByText(username)).toBeInTheDocument(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('renders dropdown items after toggle click', async () => { |
| 59 | + renderComponent(); |
| 60 | + |
| 61 | + const toggleButton = screen.getByRole('button', { name: 'User Options' }); |
| 62 | + await fireEvent.click(toggleButton); |
| 63 | + |
| 64 | + expect(screen.getByText('Dashboard')).toHaveAttribute('href', `${configMock.LMS_BASE_URL}/dashboard`); |
| 65 | + expect(screen.getByText('Profile')).toHaveAttribute('href', `${configMock.ACCOUNT_PROFILE_URL}/u/${username}`); |
| 66 | + expect(screen.getByText('Account')).toHaveAttribute('href', configMock.ACCOUNT_SETTINGS_URL); |
| 67 | + expect(screen.getByText('Order History')).toHaveAttribute('href', configMock.ORDER_HISTORY_URL); |
| 68 | + expect(screen.getByText('Sign Out')).toHaveAttribute('href', configMock.LOGOUT_URL); |
| 69 | + }); |
| 70 | +}); |
0 commit comments