|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#37444 — the object list route loads no authoring code before its |
| 5 | + * first screen. |
| 6 | + * |
| 7 | + * ## What changed, and what this file pins about it |
| 8 | + * |
| 9 | + * `objectViewViewCrud.tsx` and `objectViewViewSharing.ts` imported the package |
| 10 | + * ask (`./metadata-admin/PackageCreateAsk`) and the designer strings |
| 11 | + * (`./metadata-admin/i18n`) statically, and the ask's dialog was rendered, |
| 12 | + * closed, on every list. Through them the list route's graph held the package |
| 13 | + * page's create dialog, `SchemaForm` and the Studio catalogue. The view config |
| 14 | + * panel and the create-view dialog, both `lazy()`, were also drawn closed on |
| 15 | + * every list, so their chunks were fetched before the list's first screen too. |
| 16 | + * The bundle half (which chunks a list load fetches, and the LCP it costs) is |
| 17 | + * measured on the built console and recorded on the PR. What a unit test CAN |
| 18 | + * pin is that none of those modules is evaluated until someone asks for it. |
| 19 | + * |
| 20 | + * ## The instrument: counted mock factories |
| 21 | + * |
| 22 | + * `vi.mock(path, factory)` runs `factory` the first time the module is |
| 23 | + * imported, and not before. Each count below is the unit-level image of "its |
| 24 | + * chunk was fetched". A STATIC import of one of them anywhere in |
| 25 | + * `ObjectView.tsx`'s graph runs its factory while this file imports |
| 26 | + * `ObjectView`, before any case starts, and the first case reds. |
| 27 | + * |
| 28 | + * ⚠️ The module registry is per FILE, so a load cannot be undone between |
| 29 | + * cases. The case that needs every module unloaded runs first, and each later |
| 30 | + * case asserts the count it STARTS from, so a reorder fails loudly. |
| 31 | + */ |
| 32 | + |
| 33 | +import '@testing-library/jest-dom/vitest'; |
| 34 | +import * as React from 'react'; |
| 35 | +import { describe, it, expect, vi } from 'vitest'; |
| 36 | +import { act, render, screen, waitFor, fireEvent } from '@testing-library/react'; |
| 37 | +import { MemoryRouter } from 'react-router-dom'; |
| 38 | + |
| 39 | +const loads = vi.hoisted(() => ({ |
| 40 | + packageAsk: 0, |
| 41 | + packagesPage: 0, |
| 42 | + schemaForm: 0, |
| 43 | + designerStrings: 0, |
| 44 | + viewConfigPanel: 0, |
| 45 | + createViewDialog: 0, |
| 46 | +})); |
| 47 | + |
| 48 | +const toastError = vi.hoisted(() => vi.fn()); |
| 49 | + |
| 50 | +vi.mock('sonner', async (importOriginal) => ({ |
| 51 | + ...(await importOriginal<Record<string, unknown>>()), |
| 52 | + toast: { error: toastError, success: vi.fn(), info: vi.fn(), warning: vi.fn(), message: vi.fn() }, |
| 53 | +})); |
| 54 | + |
| 55 | +vi.mock('../providers/MetadataProvider', async (importOriginal) => ({ |
| 56 | + ...(await importOriginal<Record<string, unknown>>()), |
| 57 | + useMetadata: () => ({ apps: [] }), |
| 58 | +})); |
| 59 | + |
| 60 | +// A stand-in dialog: open, it offers one package; the case answers with it. |
| 61 | +vi.mock('./metadata-admin/PackageCreateAsk', () => { |
| 62 | + loads.packageAsk += 1; |
| 63 | + return { |
| 64 | + PackageCreateAskDialog: ({ open, onAnswer }: { open: boolean; onAnswer: (id: string | null) => void }) => ( |
| 65 | + <div data-testid="stub-package-ask" data-open={String(open)}> |
| 66 | + <button type="button" data-testid="stub-package-ask-pick" onClick={() => onAnswer('com.example.pick')} /> |
| 67 | + </div> |
| 68 | + ), |
| 69 | + runtimeCreateRefusalText: (notSaved: { reason: string }, kind: string, locale: string) => |
| 70 | + notSaved.reason === 'cancelled' ? null : `refused ${kind} in ${locale}`, |
| 71 | + }; |
| 72 | +}); |
| 73 | + |
| 74 | +vi.mock('./metadata-admin/PackagesPage', async (importOriginal) => { |
| 75 | + loads.packagesPage += 1; |
| 76 | + return importOriginal<Record<string, unknown>>(); |
| 77 | +}); |
| 78 | + |
| 79 | +vi.mock('./metadata-admin/SchemaForm', async (importOriginal) => { |
| 80 | + loads.schemaForm += 1; |
| 81 | + return importOriginal<Record<string, unknown>>(); |
| 82 | +}); |
| 83 | + |
| 84 | +vi.mock('./metadata-admin/i18n', async (importOriginal) => { |
| 85 | + loads.designerStrings += 1; |
| 86 | + return importOriginal<Record<string, unknown>>(); |
| 87 | +}); |
| 88 | + |
| 89 | +vi.mock('./ViewConfigPanel', async (importOriginal) => { |
| 90 | + loads.viewConfigPanel += 1; |
| 91 | + return importOriginal<Record<string, unknown>>(); |
| 92 | +}); |
| 93 | + |
| 94 | +vi.mock('./CreateViewDialog', async (importOriginal) => { |
| 95 | + loads.createViewDialog += 1; |
| 96 | + return importOriginal<Record<string, unknown>>(); |
| 97 | +}); |
| 98 | + |
| 99 | +// `objectViewViewCrud` reaches a dynamic `import('@objectstack/lint')`: pay its |
| 100 | +// load at collection, never inside a case's clocked window. |
| 101 | +import '@objectstack/lint'; |
| 102 | +import './ObjectView'; |
| 103 | +import { reportViewNotSaved, useRuntimeCreateApp } from './objectViewViewCrud'; |
| 104 | +import { RuntimeCreateNotSavedError } from './runtime-metadata-persistence'; |
| 105 | + |
| 106 | +type CreateApp = ReturnType<typeof useRuntimeCreateApp>; |
| 107 | + |
| 108 | +function AskHost({ onApp }: { onApp: (app: CreateApp) => void }) { |
| 109 | + const app = useRuntimeCreateApp(); |
| 110 | + onApp(app); |
| 111 | + return app.askDialog; |
| 112 | +} |
| 113 | + |
| 114 | +describe('objectstack#37444 — the object list route loads no authoring code before it is asked', () => { |
| 115 | + it('importing the list route evaluates none of the authoring modules', () => { |
| 116 | + expect(loads).toEqual({ |
| 117 | + packageAsk: 0, |
| 118 | + packagesPage: 0, |
| 119 | + schemaForm: 0, |
| 120 | + designerStrings: 0, |
| 121 | + viewConfigPanel: 0, |
| 122 | + createViewDialog: 0, |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + it('the package ask loads its dialog on the first ask, answers it, and keeps it mounted closed', async () => { |
| 127 | + expect(loads.packageAsk).toBe(0); |
| 128 | + let app: CreateApp | null = null; |
| 129 | + render( |
| 130 | + <MemoryRouter> |
| 131 | + <AskHost onApp={(next) => { app = next; }} /> |
| 132 | + </MemoryRouter>, |
| 133 | + ); |
| 134 | + // Nothing asked yet: the dialog module is not loaded and nothing is drawn. |
| 135 | + expect(loads.packageAsk).toBe(0); |
| 136 | + expect(screen.queryByTestId('stub-package-ask')).toBeNull(); |
| 137 | + |
| 138 | + let answer: Promise<string | null> | undefined; |
| 139 | + act(() => { |
| 140 | + answer = app!.askPackage({ preselect: null }); |
| 141 | + }); |
| 142 | + await waitFor(() => expect(screen.getByTestId('stub-package-ask')).toHaveAttribute('data-open', 'true')); |
| 143 | + expect(loads.packageAsk).toBe(1); |
| 144 | + |
| 145 | + fireEvent.click(screen.getByTestId('stub-package-ask-pick')); |
| 146 | + await expect(answer!).resolves.toBe('com.example.pick'); |
| 147 | + // Answered, it closes but stays mounted, so it closes the way it always did. |
| 148 | + await waitFor(() => expect(screen.getByTestId('stub-package-ask')).toHaveAttribute('data-open', 'false')); |
| 149 | + }); |
| 150 | + |
| 151 | + it('a refused view create says why once its words have loaded; a cancelled one says nothing', async () => { |
| 152 | + toastError.mockClear(); |
| 153 | + expect(reportViewNotSaved(new Error('other'), 'en')).toBe(false); |
| 154 | + expect(reportViewNotSaved(new RuntimeCreateNotSavedError('system-app'), 'zh-CN')).toBe(true); |
| 155 | + // The words are in the designer strings, loaded on the refusal: the toast follows. |
| 156 | + await waitFor(() => expect(toastError).toHaveBeenCalledTimes(1)); |
| 157 | + expect(toastError).toHaveBeenCalledWith('refused view in zh-CN'); |
| 158 | + expect(reportViewNotSaved(new RuntimeCreateNotSavedError('cancelled'), 'en')).toBe(true); |
| 159 | + await act(() => new Promise<void>((resolve) => setTimeout(resolve, 50))); |
| 160 | + expect(toastError).toHaveBeenCalledTimes(1); |
| 161 | + }); |
| 162 | +}); |
0 commit comments