|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * WorkspaceSwitcher inside the ObjectStack mobile shell. |
| 5 | + * |
| 6 | + * When the shell's bridge (`window.ObjectStackNative`, read through |
| 7 | + * `@object-ui/mobile`) announces `showConnectionSwitcher`, the header's |
| 8 | + * organization button opens the shell's native switcher and the web menu does |
| 9 | + * not open. With no bridge, or a bridge without that capability, the web menu |
| 10 | + * opens exactly as before. The dropdown is the real one, opened by a real |
| 11 | + * click, so "the web menu opens" is observed, not assumed. |
| 12 | + * |
| 13 | + * In the shell the button renders at ONE membership too: the sheet lists the |
| 14 | + * shell's connections across servers, which this server's membership count |
| 15 | + * cannot see. On a walled deployment `CurrentOrganizationIndicator` then steps |
| 16 | + * aside, so the header carries the organization once, with the name shown at |
| 17 | + * the same widths the indicator showed it. |
| 18 | + */ |
| 19 | + |
| 20 | +import '@testing-library/jest-dom/vitest'; |
| 21 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 22 | +import { render, screen, cleanup, waitFor } from '@testing-library/react'; |
| 23 | +import userEvent from '@testing-library/user-event'; |
| 24 | + |
| 25 | +vi.mock('@object-ui/i18n', async (importOriginal) => ({ |
| 26 | + ...(await importOriginal<Record<string, unknown>>()), |
| 27 | + useObjectTranslation: () => ({ |
| 28 | + t: (key: string, options?: Record<string, unknown>) => String(options?.defaultValue ?? key), |
| 29 | + }), |
| 30 | +})); |
| 31 | + |
| 32 | +vi.mock('react-router-dom', async (importOriginal) => ({ |
| 33 | + ...(await importOriginal<Record<string, unknown>>()), |
| 34 | + useNavigate: () => vi.fn(), |
| 35 | +})); |
| 36 | + |
| 37 | +let authState: Record<string, unknown>; |
| 38 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 39 | + ...(await importOriginal<Record<string, unknown>>()), |
| 40 | + useAuth: () => authState, |
| 41 | +})); |
| 42 | + |
| 43 | +import { NATIVE_HOST_GLOBAL } from '@object-ui/mobile'; |
| 44 | +import { WorkspaceSwitcher } from '../WorkspaceSwitcher'; |
| 45 | +import { CurrentOrganizationIndicator } from '../CurrentOrganizationIndicator'; |
| 46 | +import { useTenancyPosture } from '../../hooks/useTenancyPosture'; |
| 47 | + |
| 48 | +const ORGS = [ |
| 49 | + { id: 'org_a', name: 'Plant A', slug: 'plant-a' }, |
| 50 | + { id: 'org_b', name: 'Plant B', slug: 'plant-b' }, |
| 51 | +]; |
| 52 | + |
| 53 | +const BASE_METHODS = ['getPushToken', 'haptic', 'openExternal', 'navigate', 'resetServer']; |
| 54 | + |
| 55 | +function installBridge(capabilities: string[]) { |
| 56 | + const showConnectionSwitcher = vi.fn(async () => null); |
| 57 | + (window as unknown as Record<string, unknown>)[NATIVE_HOST_GLOBAL] = { |
| 58 | + version: 1, |
| 59 | + platform: 'ios', |
| 60 | + appVersion: '2.1.0', |
| 61 | + capabilities, |
| 62 | + safeArea: { top: 47, bottom: 34, left: 0, right: 0 }, |
| 63 | + has: (capability: string) => capabilities.includes(capability), |
| 64 | + getPushToken: vi.fn(async () => null), |
| 65 | + haptic: vi.fn(async () => null), |
| 66 | + openExternal: vi.fn(async () => null), |
| 67 | + navigate: vi.fn(async () => null), |
| 68 | + resetServer: vi.fn(async () => null), |
| 69 | + showConnectionSwitcher, |
| 70 | + ready: vi.fn(), |
| 71 | + routeChanged: vi.fn(), |
| 72 | + }; |
| 73 | + return showConnectionSwitcher; |
| 74 | +} |
| 75 | + |
| 76 | +beforeEach(() => { |
| 77 | + delete (window as unknown as Record<string, unknown>)[NATIVE_HOST_GLOBAL]; |
| 78 | + authState = { |
| 79 | + organizations: ORGS, |
| 80 | + activeOrganization: ORGS[0], |
| 81 | + switchOrganization: vi.fn(), |
| 82 | + getAuthConfig: vi.fn().mockResolvedValue({ features: { multiOrgEnabled: true } }), |
| 83 | + }; |
| 84 | +}); |
| 85 | + |
| 86 | +afterEach(() => { |
| 87 | + cleanup(); |
| 88 | + delete (window as unknown as Record<string, unknown>)[NATIVE_HOST_GLOBAL]; |
| 89 | +}); |
| 90 | + |
| 91 | +describe('WorkspaceSwitcher in the mobile shell', () => { |
| 92 | + it('with showConnectionSwitcher announced, the button opens the native switcher and no web menu', async () => { |
| 93 | + const showConnectionSwitcher = installBridge([...BASE_METHODS, 'showConnectionSwitcher']); |
| 94 | + render(<WorkspaceSwitcher />); |
| 95 | + |
| 96 | + const button = screen.getByTestId('workspace-switcher'); |
| 97 | + expect(button).toHaveAttribute('data-native-switcher'); |
| 98 | + expect(button).not.toHaveAttribute('aria-haspopup'); |
| 99 | + |
| 100 | + await userEvent.click(button); |
| 101 | + |
| 102 | + expect(showConnectionSwitcher).toHaveBeenCalledTimes(1); |
| 103 | + expect(screen.queryByRole('menu')).not.toBeInTheDocument(); |
| 104 | + expect(screen.queryByText('Plant B')).not.toBeInTheDocument(); |
| 105 | + expect(authState.switchOrganization).not.toHaveBeenCalled(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('control: with no bridge, the button opens the web organization menu', async () => { |
| 109 | + render(<WorkspaceSwitcher />); |
| 110 | + |
| 111 | + const button = screen.getByTestId('workspace-switcher'); |
| 112 | + expect(button).not.toHaveAttribute('data-native-switcher'); |
| 113 | + |
| 114 | + await userEvent.click(button); |
| 115 | + |
| 116 | + expect(await screen.findByRole('menu')).toBeInTheDocument(); |
| 117 | + expect(screen.getByText('Switch organization')).toBeInTheDocument(); |
| 118 | + expect(screen.getByText('Plant B')).toBeInTheDocument(); |
| 119 | + }); |
| 120 | + |
| 121 | + it('control: a shell without the capability keeps the web menu and never calls the native switcher', async () => { |
| 122 | + const showConnectionSwitcher = installBridge(BASE_METHODS); |
| 123 | + render(<WorkspaceSwitcher />); |
| 124 | + |
| 125 | + const button = screen.getByTestId('workspace-switcher'); |
| 126 | + expect(button).not.toHaveAttribute('data-native-switcher'); |
| 127 | + |
| 128 | + await userEvent.click(button); |
| 129 | + |
| 130 | + expect(await screen.findByRole('menu')).toBeInTheDocument(); |
| 131 | + expect(screen.getByText('Plant B')).toBeInTheDocument(); |
| 132 | + expect(showConnectionSwitcher).not.toHaveBeenCalled(); |
| 133 | + }); |
| 134 | +}); |
| 135 | + |
| 136 | +describe('WorkspaceSwitcher in the mobile shell at one membership (walled deployment)', () => { |
| 137 | + /** AppHeader's pair, in its order, plus the resolved posture to wait on. */ |
| 138 | + function PostureProbe() { |
| 139 | + return <span data-testid="posture">{String(useTenancyPosture() ?? 'unresolved')}</span>; |
| 140 | + } |
| 141 | + function mountHeaderPair() { |
| 142 | + return render( |
| 143 | + <> |
| 144 | + <WorkspaceSwitcher /> |
| 145 | + <CurrentOrganizationIndicator /> |
| 146 | + <PostureProbe /> |
| 147 | + </>, |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + beforeEach(() => { |
| 152 | + authState = { |
| 153 | + organizations: [ORGS[0]], |
| 154 | + activeOrganization: ORGS[0], |
| 155 | + switchOrganization: vi.fn(), |
| 156 | + getAuthConfig: vi.fn().mockResolvedValue({ features: { multiOrgEnabled: true, tenancyPosture: 'isolated' } }), |
| 157 | + }; |
| 158 | + }); |
| 159 | + |
| 160 | + /** The name span of whichever surface carries the organization. */ |
| 161 | + function nameSpan(container: HTMLElement): HTMLElement { |
| 162 | + const spans = Array.from(container.querySelectorAll('span')).filter((el) => el.textContent === ORGS[0].name); |
| 163 | + expect(spans).toHaveLength(1); |
| 164 | + return spans[0] as HTMLElement; |
| 165 | + } |
| 166 | + |
| 167 | + it('with the capability, the native button renders, opens the sheet, and the read-only indicator steps aside', async () => { |
| 168 | + const showConnectionSwitcher = installBridge([...BASE_METHODS, 'showConnectionSwitcher']); |
| 169 | + const { container } = mountHeaderPair(); |
| 170 | + await waitFor(() => expect(screen.getByTestId('posture')).toHaveTextContent('isolated')); |
| 171 | + |
| 172 | + const button = screen.getByTestId('workspace-switcher'); |
| 173 | + expect(button).toHaveAttribute('data-native-switcher'); |
| 174 | + expect(screen.queryByTestId('current-organization-indicator')).not.toBeInTheDocument(); |
| 175 | + // The organization is drawn once. |
| 176 | + expect(nameSpan(container).closest('[data-testid="workspace-switcher"]')).toBe(button); |
| 177 | + |
| 178 | + await userEvent.click(button); |
| 179 | + expect(showConnectionSwitcher).toHaveBeenCalledTimes(1); |
| 180 | + expect(screen.queryByRole('menu')).not.toBeInTheDocument(); |
| 181 | + }); |
| 182 | + |
| 183 | + it('the native button shows the name at the same widths the indicator did', async () => { |
| 184 | + const plain = mountHeaderPair(); |
| 185 | + await waitFor(() => expect(screen.getByTestId('current-organization-indicator')).toBeInTheDocument()); |
| 186 | + const indicatorNameClass = nameSpan(plain.container).className; |
| 187 | + plain.unmount(); |
| 188 | + |
| 189 | + installBridge([...BASE_METHODS, 'showConnectionSwitcher']); |
| 190 | + const shell = mountHeaderPair(); |
| 191 | + await waitFor(() => expect(screen.getByTestId('posture')).toHaveTextContent('isolated')); |
| 192 | + expect(nameSpan(shell.container).className).toBe(indicatorNameClass); |
| 193 | + }); |
| 194 | + |
| 195 | + it('control: with no bridge, one membership draws no switcher and the read-only indicator, as today', async () => { |
| 196 | + mountHeaderPair(); |
| 197 | + await waitFor(() => expect(screen.getByTestId('current-organization-indicator')).toBeInTheDocument()); |
| 198 | + expect(screen.queryByTestId('workspace-switcher')).not.toBeInTheDocument(); |
| 199 | + }); |
| 200 | + |
| 201 | + it('control: a shell without the capability keeps today\'s single-membership rendering', async () => { |
| 202 | + const showConnectionSwitcher = installBridge(BASE_METHODS); |
| 203 | + mountHeaderPair(); |
| 204 | + await waitFor(() => expect(screen.getByTestId('current-organization-indicator')).toBeInTheDocument()); |
| 205 | + expect(screen.queryByTestId('workspace-switcher')).not.toBeInTheDocument(); |
| 206 | + expect(showConnectionSwitcher).not.toHaveBeenCalled(); |
| 207 | + }); |
| 208 | +}); |
0 commit comments