|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The account menu stays inside a phone screen in every interface language |
| 5 | + * (objectstack#36041). |
| 6 | + * |
| 7 | + * The menu sizes to its longest line. At 375 px, with the desktop notification |
| 8 | + * hint showing (a blocked or unsupported Notification API, as on iOS Safari), |
| 9 | + * the German, French, Russian, Spanish and Portuguese hints widened it past the |
| 10 | + * screen, and the language control rode off the right edge with it: the |
| 11 | + * language list could not be opened at all. Below `md` the menu is now capped |
| 12 | + * at the screen less a 1 rem gutter each side, so long lines wrap. |
| 13 | + * |
| 14 | + * The language list is its own dropdown under the globe, opening below it (or |
| 15 | + * above, where only that fits), never to the side; with the globe back on the |
| 16 | + * screen it opens on the screen. The list itself is shared with the sign-in |
| 17 | + * page and is unchanged. |
| 18 | + * |
| 19 | + * jsdom has no layout, so what is pinned here is what the menu asks for: the |
| 20 | + * phone-only cap on the content, the list's placement, and that every language |
| 21 | + * row picks its language. The 375 px readings in all eleven languages, before |
| 22 | + * and after, are in the pull request. |
| 23 | + */ |
| 24 | + |
| 25 | +import '@testing-library/jest-dom/vitest'; |
| 26 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 27 | +import { act, fireEvent, render, screen, waitFor, within } from '@testing-library/react'; |
| 28 | +import React from 'react'; |
| 29 | +import { MemoryRouter } from 'react-router-dom'; |
| 30 | +import { I18nProvider } from '@object-ui/i18n'; |
| 31 | +import { SidebarProvider } from '@object-ui/components'; |
| 32 | +import { PLATFORM_LOCALE_TAGS } from '@objectstack/spec/system'; |
| 33 | + |
| 34 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 35 | + ...(await importOriginal<Record<string, unknown>>()), |
| 36 | + useAuth: () => ({ |
| 37 | + user: { id: 'u1', name: 'Lin Yue', email: 'linyue@example.com' }, |
| 38 | + signOut: vi.fn(), |
| 39 | + isAuthEnabled: true, |
| 40 | + organizations: [], |
| 41 | + activeOrganization: null, |
| 42 | + isOrganizationsLoading: false, |
| 43 | + getAuthConfig: undefined, |
| 44 | + }), |
| 45 | + getUserInitials: () => 'LY', |
| 46 | +})); |
| 47 | + |
| 48 | +vi.mock('../../providers/MetadataProvider', () => ({ |
| 49 | + useMetadata: () => ({ apps: [] }), |
| 50 | +})); |
| 51 | + |
| 52 | +import { AccountMenu } from '../AccountMenu'; |
| 53 | + |
| 54 | +/** The phone-only cap: the screen less a 1 rem gutter each side. */ |
| 55 | +const PHONE_CAP = 'max-md:max-w-[calc(100vw-2rem)]'; |
| 56 | + |
| 57 | +const classesOf = (el: Element) => el.className.split(/\s+/); |
| 58 | + |
| 59 | +function mount(trigger: 'avatar' | 'sidebar', language = 'de') { |
| 60 | + const menu = <AccountMenu trigger={trigger} />; |
| 61 | + return render( |
| 62 | + <I18nProvider config={{ defaultLanguage: language, detectBrowserLanguage: false }} persistLanguage={false}> |
| 63 | + <MemoryRouter>{trigger === 'sidebar' ? <SidebarProvider>{menu}</SidebarProvider> : menu}</MemoryRouter> |
| 64 | + </I18nProvider>, |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +/** Open the account menu from its trigger and return its content. */ |
| 69 | +function openAccountMenu(): HTMLElement { |
| 70 | + const trigger = screen.getAllByRole('button', { name: 'Kontomenü' })[0]; |
| 71 | + fireEvent.keyDown(trigger, { key: 'Enter' }); |
| 72 | + return screen.getAllByRole('menu')[0]; |
| 73 | +} |
| 74 | + |
| 75 | +/** Open the language list from the globe inside the account menu. */ |
| 76 | +function openLanguageList(menu: HTMLElement): HTMLElement { |
| 77 | + const globe = within(menu).getByRole('button', { name: 'Sprache' }); |
| 78 | + fireEvent.keyDown(globe, { key: 'Enter' }); |
| 79 | + const menus = screen.getAllByRole('menu'); |
| 80 | + return menus[menus.length - 1]; |
| 81 | +} |
| 82 | + |
| 83 | +beforeEach(() => { |
| 84 | + document.documentElement.lang = ''; |
| 85 | +}); |
| 86 | + |
| 87 | +describe('the account menu at phone width (objectstack#36041)', () => { |
| 88 | + it('caps the menu at the screen less the gutter below md, and only below md', () => { |
| 89 | + mount('avatar'); |
| 90 | + const classes = classesOf(openAccountMenu()); |
| 91 | + expect(classes).toContain(PHONE_CAP); |
| 92 | + // Desktop keeps today's width: no unscoped max width rides on the content. |
| 93 | + expect(classes.filter((c) => c.startsWith('max-w-'))).toEqual([]); |
| 94 | + }); |
| 95 | + |
| 96 | + it('opens the language list below the globe, not to the side', () => { |
| 97 | + mount('avatar'); |
| 98 | + const list = openLanguageList(openAccountMenu()); |
| 99 | + expect(['bottom', 'top']).toContain(list.getAttribute('data-side')); |
| 100 | + expect(within(list).getAllByRole('menuitem')).toHaveLength(PLATFORM_LOCALE_TAGS.length); |
| 101 | + }); |
| 102 | + |
| 103 | + it('picks every language from the list', async () => { |
| 104 | + const { container } = mount('avatar'); |
| 105 | + const picked: string[] = []; |
| 106 | + for (let i = 0; i < PLATFORM_LOCALE_TAGS.length; i++) { |
| 107 | + // The trigger is found by its role in the DOM, not its name: the menu |
| 108 | + // relabels in each language it picks. |
| 109 | + const trigger = container.querySelector('button[aria-haspopup="menu"]'); |
| 110 | + if (!trigger) throw new Error('no account menu trigger'); |
| 111 | + fireEvent.keyDown(trigger, { key: 'Enter' }); |
| 112 | + const menu = screen.getAllByRole('menu')[0]; |
| 113 | + const globe = within(menu) |
| 114 | + .getAllByRole('button') |
| 115 | + .find((b) => b.getAttribute('aria-haspopup') === 'menu' && b.querySelector('.lucide-globe')); |
| 116 | + if (!globe) throw new Error('no language control in the account menu'); |
| 117 | + fireEvent.keyDown(globe, { key: 'Enter' }); |
| 118 | + const menus = screen.getAllByRole('menu'); |
| 119 | + const rows = within(menus[menus.length - 1]).getAllByRole('menuitem'); |
| 120 | + expect(rows).toHaveLength(PLATFORM_LOCALE_TAGS.length); |
| 121 | + await act(async () => { |
| 122 | + fireEvent.click(rows[i]); |
| 123 | + }); |
| 124 | + // Close whatever is still open before the next round. |
| 125 | + for (const open of screen.queryAllByRole('menu')) fireEvent.keyDown(open, { key: 'Escape' }); |
| 126 | + await waitFor(() => expect(screen.queryAllByRole('menu')).toHaveLength(0)); |
| 127 | + picked.push(document.documentElement.lang); |
| 128 | + } |
| 129 | + expect([...picked].sort()).toEqual([...PLATFORM_LOCALE_TAGS].sort()); |
| 130 | + }); |
| 131 | + |
| 132 | + it('CONTROL — the desktop sidebar trigger keeps its placement: above, aligned to the start', () => { |
| 133 | + mount('sidebar'); |
| 134 | + fireEvent.keyDown(screen.getByTestId('sidebar-account-menu'), { key: 'Enter' }); |
| 135 | + const menu = screen.getAllByRole('menu')[0]; |
| 136 | + expect(menu.getAttribute('data-side')).toBe('top'); |
| 137 | + expect(menu.getAttribute('data-align')).toBe('start'); |
| 138 | + }); |
| 139 | +}); |
0 commit comments