|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#37444 — an object page mounts the record-form modal the first |
| 5 | + * time it opens, so a list's first screen loads no form code. |
| 6 | + * |
| 7 | + * `AppContent` drew a closed `LazyModalForm` on every object page (the route's |
| 8 | + * object is always the form's default object), and a lazy component that is |
| 9 | + * drawn loads its chunk even closed: the whole of `@object-ui/plugin-form` was |
| 10 | + * fetched before every list's first screen. The bundle half is measured on the |
| 11 | + * built console and recorded on the PR; this file pins that the form module is |
| 12 | + * not evaluated until the form opens, and that it opens as before. |
| 13 | + * |
| 14 | + * The instrument: a counted `vi.mock` factory, which runs the first time the |
| 15 | + * module is imported. The module registry is per FILE, so the case that needs |
| 16 | + * it unloaded runs first, and the later case asserts the count it starts from. |
| 17 | + */ |
| 18 | + |
| 19 | +import '@testing-library/jest-dom/vitest'; |
| 20 | +import { describe, it, expect, vi, afterEach, afterAll } from 'vitest'; |
| 21 | +import React from 'react'; |
| 22 | +import { render, cleanup, waitFor, screen } from '@testing-library/react'; |
| 23 | +import { MemoryRouter, Routes, Route } from 'react-router-dom'; |
| 24 | +import { createI18n, I18nProvider } from '@object-ui/i18n'; |
| 25 | +// The console layout's draft bar reaches a dynamic `@objectstack/lint` import; |
| 26 | +// its first load is paid here, during collection, not inside a case's clock. |
| 27 | +import '@objectstack/lint'; |
| 28 | + |
| 29 | +const { loads, modalOpen } = vi.hoisted(() => ({ loads: { form: 0 }, modalOpen: [] as boolean[] })); |
| 30 | + |
| 31 | +vi.mock('@object-ui/plugin-form', async (importOriginal) => { |
| 32 | + loads.form += 1; |
| 33 | + return { |
| 34 | + ...(await importOriginal<Record<string, unknown>>()), |
| 35 | + ModalForm: ({ schema }: { schema: { open?: boolean; objectName?: string } }) => { |
| 36 | + modalOpen.push(!!schema.open); |
| 37 | + return <div data-testid="modal-form" data-object={schema.objectName} data-open={String(!!schema.open)} />; |
| 38 | + }, |
| 39 | + ObjectForm: () => null, |
| 40 | + }; |
| 41 | +}); |
| 42 | + |
| 43 | +vi.mock('@object-ui/plugin-designer', async (importOriginal) => ({ |
| 44 | + ...(await importOriginal<typeof import('@object-ui/plugin-designer')>()), |
| 45 | + CreateAppPage: () => <div data-testid="create-app-page" />, |
| 46 | + EditAppPage: () => <div data-testid="edit-app-page" />, |
| 47 | + DashboardDesignPage: () => <div data-testid="dashboard-design-page" />, |
| 48 | +})); |
| 49 | + |
| 50 | +vi.mock('../../layout/ConsoleLayout', () => ({ |
| 51 | + ConsoleLayout: ({ children }: { children?: React.ReactNode }) => ( |
| 52 | + <div data-testid="console-layout">{children}</div> |
| 53 | + ), |
| 54 | +})); |
| 55 | +vi.mock('../../chrome/CommandPalette', () => ({ CommandPalette: () => null })); |
| 56 | +vi.mock('../../chrome/KeyboardShortcutsDialog', () => ({ KeyboardShortcutsDialog: () => null })); |
| 57 | +vi.mock('../../chrome/OnboardingWalkthrough', () => ({ OnboardingWalkthrough: () => null })); |
| 58 | +vi.mock('../../views/ObjectView', () => ({ ObjectView: () => <div data-testid="object-view" /> })); |
| 59 | +vi.mock('../../views/RecordDetailView', () => ({ RecordDetailView: () => <div data-testid="record-page" /> })); |
| 60 | + |
| 61 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 62 | + ...(await importOriginal<Record<string, unknown>>()), |
| 63 | + useAuth: () => ({ |
| 64 | + user: { id: 'u1', name: 'Ada', email: 'ada@example.com', role: 'user' }, |
| 65 | + getAuthConfig: async () => ({ features: {} }), |
| 66 | + activeOrganization: null, |
| 67 | + }), |
| 68 | + useWorkspaceAdminStatus: () => ({ isAdmin: true, isResolved: true }), |
| 69 | +})); |
| 70 | + |
| 71 | +const dataSourceStub = { |
| 72 | + onConnectionStateChange: () => () => {}, |
| 73 | + getConnectionState: () => 'connected', |
| 74 | +}; |
| 75 | +vi.mock('../../providers/AdapterProvider', async (importOriginal) => ({ |
| 76 | + ...(await importOriginal<Record<string, unknown>>()), |
| 77 | + useAdapter: () => dataSourceStub, |
| 78 | +})); |
| 79 | + |
| 80 | +const METADATA = { |
| 81 | + apps: [{ name: 'crm', label: 'CRM', isDefault: true, navigation: [] }], |
| 82 | + objects: [{ name: 'showcase_task', label: 'Task', fields: { subject: { type: 'text', label: 'Subject' } } }], |
| 83 | + loading: false, |
| 84 | + ensureType: undefined, |
| 85 | + error: null, |
| 86 | + refresh: async () => {}, |
| 87 | +}; |
| 88 | +vi.mock('../../providers/MetadataProvider', async (importOriginal) => ({ |
| 89 | + ...(await importOriginal<Record<string, unknown>>()), |
| 90 | + useMetadata: () => METADATA, |
| 91 | +})); |
| 92 | + |
| 93 | +const ACTION_RUNNER = { execute: vi.fn(), runner: { registerHandler: () => {}, getContext: () => ({}) } }; |
| 94 | +vi.mock('@object-ui/react', async (importOriginal) => ({ |
| 95 | + ...(await importOriginal<Record<string, unknown>>()), |
| 96 | + useActionRunner: () => ACTION_RUNNER, |
| 97 | + useGlobalUndo: () => {}, |
| 98 | + useMutationInvalidationBridge: () => {}, |
| 99 | +})); |
| 100 | + |
| 101 | +// An edit dialog reads the approvals of the record it edits; none runs here. |
| 102 | +vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({ data: [] }), { |
| 103 | + status: 200, |
| 104 | + headers: { 'Content-Type': 'application/json' }, |
| 105 | +}))); |
| 106 | +afterAll(() => vi.unstubAllGlobals()); |
| 107 | + |
| 108 | +import { AppContent } from '../AppContent'; |
| 109 | + |
| 110 | +function mountAt(url: string) { |
| 111 | + const i18n = createI18n({ defaultLanguage: 'en', detectBrowserLanguage: false }); |
| 112 | + render( |
| 113 | + <I18nProvider instance={i18n}> |
| 114 | + <MemoryRouter initialEntries={[url]}> |
| 115 | + <Routes> |
| 116 | + <Route path="/apps/:appName/*" element={<AppContent />} /> |
| 117 | + </Routes> |
| 118 | + </MemoryRouter> |
| 119 | + </I18nProvider>, |
| 120 | + ); |
| 121 | +} |
| 122 | + |
| 123 | +afterEach(() => cleanup()); |
| 124 | + |
| 125 | +describe('objectstack#37444 — an object page mounts the record form when it opens', () => { |
| 126 | + it('a list with no form open loads no form module and draws no modal', async () => { |
| 127 | + expect(loads.form).toBe(0); |
| 128 | + mountAt('/apps/crm/showcase_task'); |
| 129 | + await screen.findByTestId('console-layout'); |
| 130 | + // Long enough for a drawn lazy modal to have asked for its chunk and drawn. |
| 131 | + await new Promise<void>((resolve) => setTimeout(resolve, 200)); |
| 132 | + expect(screen.queryByTestId('modal-form')).toBeNull(); |
| 133 | + expect(loads.form).toBe(0); |
| 134 | + }); |
| 135 | + |
| 136 | + it('opening the create form loads it and draws it open', async () => { |
| 137 | + expect(loads.form).toBe(0); |
| 138 | + mountAt('/apps/crm/showcase_task?form=new'); |
| 139 | + const modal = await screen.findByTestId('modal-form'); |
| 140 | + expect(modal).toHaveAttribute('data-object', 'showcase_task'); |
| 141 | + await waitFor(() => expect(modal).toHaveAttribute('data-open', 'true')); |
| 142 | + expect(loads.form).toBe(1); |
| 143 | + // It is never drawn closed first: the opening render is the first one. |
| 144 | + expect(modalOpen[0]).toBe(true); |
| 145 | + }); |
| 146 | +}); |
0 commit comments