|
| 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 | + |
| 14 | +import '@testing-library/jest-dom/vitest'; |
| 15 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 16 | +import { render, screen, cleanup } from '@testing-library/react'; |
| 17 | +import userEvent from '@testing-library/user-event'; |
| 18 | + |
| 19 | +vi.mock('@object-ui/i18n', async (importOriginal) => ({ |
| 20 | + ...(await importOriginal<Record<string, unknown>>()), |
| 21 | + useObjectTranslation: () => ({ |
| 22 | + t: (key: string, options?: Record<string, unknown>) => String(options?.defaultValue ?? key), |
| 23 | + }), |
| 24 | +})); |
| 25 | + |
| 26 | +vi.mock('react-router-dom', async (importOriginal) => ({ |
| 27 | + ...(await importOriginal<Record<string, unknown>>()), |
| 28 | + useNavigate: () => vi.fn(), |
| 29 | +})); |
| 30 | + |
| 31 | +let authState: Record<string, unknown>; |
| 32 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 33 | + ...(await importOriginal<Record<string, unknown>>()), |
| 34 | + useAuth: () => authState, |
| 35 | +})); |
| 36 | + |
| 37 | +import { NATIVE_HOST_GLOBAL } from '@object-ui/mobile'; |
| 38 | +import { WorkspaceSwitcher } from '../WorkspaceSwitcher'; |
| 39 | + |
| 40 | +const ORGS = [ |
| 41 | + { id: 'org_a', name: 'Plant A', slug: 'plant-a' }, |
| 42 | + { id: 'org_b', name: 'Plant B', slug: 'plant-b' }, |
| 43 | +]; |
| 44 | + |
| 45 | +const BASE_METHODS = ['getPushToken', 'haptic', 'openExternal', 'navigate', 'resetServer']; |
| 46 | + |
| 47 | +function installBridge(capabilities: string[]) { |
| 48 | + const showConnectionSwitcher = vi.fn(async () => null); |
| 49 | + (window as unknown as Record<string, unknown>)[NATIVE_HOST_GLOBAL] = { |
| 50 | + version: 1, |
| 51 | + platform: 'ios', |
| 52 | + appVersion: '2.1.0', |
| 53 | + capabilities, |
| 54 | + safeArea: { top: 47, bottom: 34, left: 0, right: 0 }, |
| 55 | + has: (capability: string) => capabilities.includes(capability), |
| 56 | + getPushToken: vi.fn(async () => null), |
| 57 | + haptic: vi.fn(async () => null), |
| 58 | + openExternal: vi.fn(async () => null), |
| 59 | + navigate: vi.fn(async () => null), |
| 60 | + resetServer: vi.fn(async () => null), |
| 61 | + showConnectionSwitcher, |
| 62 | + ready: vi.fn(), |
| 63 | + routeChanged: vi.fn(), |
| 64 | + }; |
| 65 | + return showConnectionSwitcher; |
| 66 | +} |
| 67 | + |
| 68 | +beforeEach(() => { |
| 69 | + delete (window as unknown as Record<string, unknown>)[NATIVE_HOST_GLOBAL]; |
| 70 | + authState = { |
| 71 | + organizations: ORGS, |
| 72 | + activeOrganization: ORGS[0], |
| 73 | + switchOrganization: vi.fn(), |
| 74 | + getAuthConfig: vi.fn().mockResolvedValue({ features: { multiOrgEnabled: true } }), |
| 75 | + }; |
| 76 | +}); |
| 77 | + |
| 78 | +afterEach(() => { |
| 79 | + cleanup(); |
| 80 | + delete (window as unknown as Record<string, unknown>)[NATIVE_HOST_GLOBAL]; |
| 81 | +}); |
| 82 | + |
| 83 | +describe('WorkspaceSwitcher in the mobile shell', () => { |
| 84 | + it('with showConnectionSwitcher announced, the button opens the native switcher and no web menu', async () => { |
| 85 | + const showConnectionSwitcher = installBridge([...BASE_METHODS, 'showConnectionSwitcher']); |
| 86 | + render(<WorkspaceSwitcher />); |
| 87 | + |
| 88 | + const button = screen.getByTestId('workspace-switcher'); |
| 89 | + expect(button).toHaveAttribute('data-native-switcher'); |
| 90 | + expect(button).not.toHaveAttribute('aria-haspopup'); |
| 91 | + |
| 92 | + await userEvent.click(button); |
| 93 | + |
| 94 | + expect(showConnectionSwitcher).toHaveBeenCalledTimes(1); |
| 95 | + expect(screen.queryByRole('menu')).not.toBeInTheDocument(); |
| 96 | + expect(screen.queryByText('Plant B')).not.toBeInTheDocument(); |
| 97 | + expect(authState.switchOrganization).not.toHaveBeenCalled(); |
| 98 | + }); |
| 99 | + |
| 100 | + it('control: with no bridge, the button opens the web organization menu', async () => { |
| 101 | + render(<WorkspaceSwitcher />); |
| 102 | + |
| 103 | + const button = screen.getByTestId('workspace-switcher'); |
| 104 | + expect(button).not.toHaveAttribute('data-native-switcher'); |
| 105 | + |
| 106 | + await userEvent.click(button); |
| 107 | + |
| 108 | + expect(await screen.findByRole('menu')).toBeInTheDocument(); |
| 109 | + expect(screen.getByText('Switch organization')).toBeInTheDocument(); |
| 110 | + expect(screen.getByText('Plant B')).toBeInTheDocument(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('control: a shell without the capability keeps the web menu and never calls the native switcher', async () => { |
| 114 | + const showConnectionSwitcher = installBridge(BASE_METHODS); |
| 115 | + render(<WorkspaceSwitcher />); |
| 116 | + |
| 117 | + const button = screen.getByTestId('workspace-switcher'); |
| 118 | + expect(button).not.toHaveAttribute('data-native-switcher'); |
| 119 | + |
| 120 | + await userEvent.click(button); |
| 121 | + |
| 122 | + expect(await screen.findByRole('menu')).toBeInTheDocument(); |
| 123 | + expect(screen.getByText('Plant B')).toBeInTheDocument(); |
| 124 | + expect(showConnectionSwitcher).not.toHaveBeenCalled(); |
| 125 | + }); |
| 126 | +}); |
0 commit comments