|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 3 | +import { IntlProvider } from '@edx/frontend-platform/i18n'; |
| 4 | +import { getConfig } from '@edx/frontend-platform'; |
| 5 | + |
| 6 | +import AuthenticatedUserDropdown from './AuthenticatedUserDropdown'; |
| 7 | +import messages from './messages'; |
| 8 | + |
| 9 | +jest.mock('@edx/frontend-platform', () => ({ |
| 10 | + getConfig: jest.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +const configMock = { |
| 14 | + LMS_BASE_URL: 'https://lms.example.com', |
| 15 | + ACCOUNT_PROFILE_URL: 'https://accounts.example.com', |
| 16 | + ACCOUNT_SETTINGS_URL: 'https://accounts.example.com/settings', |
| 17 | + ORDER_HISTORY_URL: 'https://lms.example.com/orders', |
| 18 | + LOGOUT_URL: 'https://lms.example.com/logout', |
| 19 | +}; |
| 20 | + |
| 21 | +describe('AuthenticatedUserDropdown', () => { |
| 22 | + const username = 'testuser'; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + getConfig.mockReturnValue(configMock); |
| 26 | + }); |
| 27 | + |
| 28 | + const renderComponent = () => { |
| 29 | + render( |
| 30 | + <IntlProvider locale="en"> |
| 31 | + <AuthenticatedUserDropdown username={username} /> |
| 32 | + </IntlProvider>, |
| 33 | + ); |
| 34 | + }; |
| 35 | + |
| 36 | + it('renders username in toggle button', () => { |
| 37 | + renderComponent(); |
| 38 | + expect(screen.getByText(username)).toBeInTheDocument(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('renders dropdown items after toggle click', async () => { |
| 42 | + renderComponent(); |
| 43 | + |
| 44 | + const toggleButton = screen.getByRole('button', { name: 'User Options' }); |
| 45 | + await fireEvent.click(toggleButton); |
| 46 | + |
| 47 | + expect(screen.getByText(messages.dashboard.defaultMessage)).toHaveAttribute('href', `${configMock.LMS_BASE_URL}/dashboard`); |
| 48 | + expect(screen.getByText(messages.profile.defaultMessage)).toHaveAttribute('href', `${configMock.ACCOUNT_PROFILE_URL}/u/${username}`); |
| 49 | + expect(screen.getByText(messages.account.defaultMessage)).toHaveAttribute('href', configMock.ACCOUNT_SETTINGS_URL); |
| 50 | + expect(screen.getByText(messages.orderHistory.defaultMessage)).toHaveAttribute('href', configMock.ORDER_HISTORY_URL); |
| 51 | + expect(screen.getByText(messages.signOut.defaultMessage)).toHaveAttribute('href', configMock.LOGOUT_URL); |
| 52 | + }); |
| 53 | + |
| 54 | + it('loops focus from last to first and vice versa with Tab and Shift+Tab', async () => { |
| 55 | + renderComponent(); |
| 56 | + |
| 57 | + const toggleButton = screen.getByRole('button', { name: 'User Options' }); |
| 58 | + await fireEvent.click(toggleButton); |
| 59 | + |
| 60 | + const menuItems = await screen.findAllByRole('menuitem'); |
| 61 | + const firstItem = menuItems[0]; |
| 62 | + const lastItem = menuItems[menuItems.length - 1]; |
| 63 | + |
| 64 | + lastItem.focus(); |
| 65 | + expect(lastItem).toHaveFocus(); |
| 66 | + |
| 67 | + fireEvent.keyDown(lastItem, { key: 'Tab' }); |
| 68 | + expect(firstItem).toHaveFocus(); |
| 69 | + |
| 70 | + firstItem.focus(); |
| 71 | + expect(firstItem).toHaveFocus(); |
| 72 | + |
| 73 | + fireEvent.keyDown(firstItem, { key: 'Tab', shiftKey: true }); |
| 74 | + expect(lastItem).toHaveFocus(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('focuses next link when Tab is pressed on middle item', async () => { |
| 78 | + renderComponent(); |
| 79 | + |
| 80 | + const toggleButton = screen.getByRole('button', { name: 'User Options' }); |
| 81 | + await fireEvent.click(toggleButton); |
| 82 | + |
| 83 | + const menuItems = await screen.findAllByRole('menuitem'); |
| 84 | + const secondItem = menuItems[1]; |
| 85 | + const thirdItem = menuItems[2]; |
| 86 | + |
| 87 | + secondItem.focus(); |
| 88 | + expect(secondItem).toHaveFocus(); |
| 89 | + |
| 90 | + Object.defineProperty(secondItem, 'nextElementSibling', { |
| 91 | + value: thirdItem, |
| 92 | + configurable: true, |
| 93 | + }); |
| 94 | + Object.defineProperty(thirdItem, 'tagName', { |
| 95 | + value: 'A', |
| 96 | + configurable: true, |
| 97 | + }); |
| 98 | + |
| 99 | + fireEvent.keyDown(secondItem, { key: 'Tab' }); |
| 100 | + |
| 101 | + expect(thirdItem).toHaveFocus(); |
| 102 | + }); |
| 103 | +}); |
0 commit comments