|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * objectstack#27627 — entering an app loads its metadata bundle, by the app's |
| 11 | + * NAME (the address `GET /meta/app/:name/bundle` takes), on the package-id |
| 12 | + * route the sidebar builds and on the name alias alike; staying in the app |
| 13 | + * asks it once. |
| 14 | + * |
| 15 | + * Measured from a real `AppContent` mount (the |
| 16 | + * `AppContent.recentOnPackageRoute-11818` harness); the adapter's |
| 17 | + * `loadAppBundle` is the observation point. |
| 18 | + */ |
| 19 | + |
| 20 | +import '@testing-library/jest-dom/vitest'; |
| 21 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 22 | +import React from 'react'; |
| 23 | +import { render, cleanup, screen } from '@testing-library/react'; |
| 24 | +import { MemoryRouter, Routes, Route } from 'react-router-dom'; |
| 25 | +import { createI18n, I18nProvider } from '@object-ui/i18n'; |
| 26 | + |
| 27 | +vi.mock('@object-ui/plugin-form', async (importOriginal) => ({ |
| 28 | + ...(await importOriginal<Record<string, unknown>>()), |
| 29 | + ModalForm: () => null, |
| 30 | +})); |
| 31 | + |
| 32 | +vi.mock('@object-ui/plugin-designer', async (importOriginal) => ({ |
| 33 | + ...(await importOriginal<typeof import('@object-ui/plugin-designer')>()), |
| 34 | + CreateAppPage: () => <div data-testid="create-app-page" />, |
| 35 | + EditAppPage: () => <div data-testid="edit-app-page" />, |
| 36 | + DashboardDesignPage: () => <div data-testid="dashboard-design-page" />, |
| 37 | +})); |
| 38 | + |
| 39 | +vi.mock('../../layout/ConsoleLayout', () => ({ |
| 40 | + ConsoleLayout: ({ children }: { children?: React.ReactNode }) => ( |
| 41 | + <div data-testid="console-layout">{children}</div> |
| 42 | + ), |
| 43 | +})); |
| 44 | +vi.mock('../../chrome/CommandPalette', () => ({ CommandPalette: () => null })); |
| 45 | +vi.mock('../../chrome/KeyboardShortcutsDialog', () => ({ KeyboardShortcutsDialog: () => null })); |
| 46 | +vi.mock('../../chrome/OnboardingWalkthrough', () => ({ OnboardingWalkthrough: () => null })); |
| 47 | +vi.mock('../../views/ObjectView', () => ({ ObjectView: () => <div data-testid="object-view" /> })); |
| 48 | +vi.mock('../../views/RecordDetailView', () => ({ RecordDetailView: () => null })); |
| 49 | + |
| 50 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 51 | + ...(await importOriginal<Record<string, unknown>>()), |
| 52 | + useAuth: () => ({ |
| 53 | + user: { id: 'u1', name: 'Ada', email: 'ada@example.com', role: 'user' }, |
| 54 | + getAuthConfig: async () => ({ features: {} }), |
| 55 | + activeOrganization: null, |
| 56 | + }), |
| 57 | + useWorkspaceAdminStatus: () => ({ isAdmin: true, isResolved: true }), |
| 58 | +})); |
| 59 | + |
| 60 | +const { loadAppBundle } = vi.hoisted(() => ({ loadAppBundle: vi.fn(async (_appId: string) => {}) })); |
| 61 | +const dataSourceStub = { |
| 62 | + onConnectionStateChange: () => () => {}, |
| 63 | + getConnectionState: () => 'connected', |
| 64 | + loadAppBundle, |
| 65 | +}; |
| 66 | +vi.mock('../../providers/AdapterProvider', async (importOriginal) => ({ |
| 67 | + ...(await importOriginal<Record<string, unknown>>()), |
| 68 | + useAdapter: () => dataSourceStub, |
| 69 | +})); |
| 70 | + |
| 71 | +/** The showcase app as the catalog serves it: a name AND a package id. */ |
| 72 | +const APPS = [ |
| 73 | + { |
| 74 | + name: 'showcase_app', |
| 75 | + label: 'Showcase', |
| 76 | + _packageId: 'com.example.showcase', |
| 77 | + isDefault: true, |
| 78 | + navigation: [{ id: 'tasks', type: 'object', objectName: 'showcase_task', label: 'Tasks' }], |
| 79 | + }, |
| 80 | +]; |
| 81 | + |
| 82 | +const OBJECTS = [{ name: 'showcase_task', label: 'Task', pluralLabel: 'Tasks', fields: { title: { type: 'text' } } }]; |
| 83 | + |
| 84 | +// One value for the whole run: the route's effects key on these members, and |
| 85 | +// a fresh value per render would re-run them without end. |
| 86 | +const METADATA = { |
| 87 | + apps: APPS, |
| 88 | + objects: OBJECTS, |
| 89 | + loading: false, |
| 90 | + ensureType: undefined, |
| 91 | + error: null, |
| 92 | + refresh: async () => {}, |
| 93 | +}; |
| 94 | +vi.mock('../../providers/MetadataProvider', async (importOriginal) => ({ |
| 95 | + ...(await importOriginal<Record<string, unknown>>()), |
| 96 | + useMetadata: () => METADATA, |
| 97 | +})); |
| 98 | + |
| 99 | +const ACTION_RUNNER = { execute: () => {}, runner: { registerHandler: () => {}, getContext: () => ({}) } }; |
| 100 | +vi.mock('@object-ui/react', async (importOriginal) => ({ |
| 101 | + ...(await importOriginal<Record<string, unknown>>()), |
| 102 | + useActionRunner: () => ACTION_RUNNER, |
| 103 | + useGlobalUndo: () => {}, |
| 104 | + useMutationInvalidationBridge: () => {}, |
| 105 | +})); |
| 106 | + |
| 107 | +import { AppContent } from '../AppContent'; |
| 108 | + |
| 109 | +beforeEach(() => { |
| 110 | + loadAppBundle.mockClear(); |
| 111 | +}); |
| 112 | +afterEach(cleanup); |
| 113 | + |
| 114 | +async function visit(path: string) { |
| 115 | + const i18n = createI18n({ defaultLanguage: 'en', detectBrowserLanguage: false }); |
| 116 | + const view = ( |
| 117 | + <I18nProvider instance={i18n}> |
| 118 | + <MemoryRouter initialEntries={[path]}> |
| 119 | + <Routes> |
| 120 | + <Route path="/apps/:appName/*" element={<AppContent />} /> |
| 121 | + </Routes> |
| 122 | + </MemoryRouter> |
| 123 | + </I18nProvider> |
| 124 | + ); |
| 125 | + const mounted = render(view); |
| 126 | + await screen.findByTestId('object-view', undefined, { timeout: 5000 }); |
| 127 | + return { rerender: () => mounted.rerender(view) }; |
| 128 | +} |
| 129 | + |
| 130 | +describe('AppContent loads the entered app\'s metadata bundle (objectstack#27627)', () => { |
| 131 | + it('on the package-id route, by the app name, once', async () => { |
| 132 | + const { rerender } = await visit('/apps/com.example.showcase/showcase_task'); |
| 133 | + rerender(); |
| 134 | + expect(loadAppBundle.mock.calls).toEqual([['showcase_app']]); |
| 135 | + }); |
| 136 | + |
| 137 | + it('on the name alias, by the same name', async () => { |
| 138 | + await visit('/apps/showcase_app/showcase_task'); |
| 139 | + expect(loadAppBundle.mock.calls.map(([app]) => app)).toEqual(expect.arrayContaining(['showcase_app'])); |
| 140 | + expect(new Set(loadAppBundle.mock.calls.map(([app]) => app))).toEqual(new Set(['showcase_app'])); |
| 141 | + }); |
| 142 | +}); |
0 commit comments