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