|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#29022 — the Interfaces pillar holds a report switched to Joined |
| 5 | + * in its Properties until its first block is added, instead of autosaving a |
| 6 | + * body the draft door refuses. |
| 7 | + * |
| 8 | + * The card: Report type = Joined sent `type: 'joined'` with the dataset and |
| 9 | + * measures cleared and no `blocks` (422 at `blocks`); a Description edit then |
| 10 | + * sent the same body plus the edit (422 again), and a reload dropped both with |
| 11 | + * no prompt. Now nothing is sent, a neutral line names the Blocks list with a |
| 12 | + * "Show me", and the surface is told what is held (its leave guard and Publish |
| 13 | + * read that). |
| 14 | + * |
| 15 | + * Mounted as `InterfacesPillar.reportHeld-27785.test.tsx` mounts it: a metadata |
| 16 | + * client double that records every save, a stub canvas and the real |
| 17 | + * `ReportDefaultInspector` with its type picker. |
| 18 | + */ |
| 19 | + |
| 20 | +import '@testing-library/jest-dom/vitest'; |
| 21 | +import * as React from 'react'; |
| 22 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 23 | +import { act, cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'; |
| 24 | +import { MemoryRouter } from 'react-router-dom'; |
| 25 | + |
| 26 | +const NAV = [{ id: 'nav_rep', type: 'report', label: 'Sales report', reportName: 'sales_report' }]; |
| 27 | + |
| 28 | +const SUMMARY_REPORT = { |
| 29 | + name: 'sales_report', |
| 30 | + label: 'Sales report', |
| 31 | + type: 'summary', |
| 32 | + dataset: 'sales_ds', |
| 33 | + rows: ['stage'], |
| 34 | + values: ['revenue'], |
| 35 | +}; |
| 36 | + |
| 37 | +const DATASETS = [ |
| 38 | + { |
| 39 | + name: 'sales_ds', |
| 40 | + label: 'Sales', |
| 41 | + object: 'opportunity', |
| 42 | + dimensions: [{ name: 'stage', label: 'Stage' }], |
| 43 | + measures: [{ name: 'revenue', label: 'Revenue', aggregate: 'sum' }], |
| 44 | + }, |
| 45 | +]; |
| 46 | + |
| 47 | +/** The report the pillar opens, as served: it parses with the spec. */ |
| 48 | +let stored: Record<string, unknown> = SUMMARY_REPORT; |
| 49 | + |
| 50 | +const mockClient = { |
| 51 | + withPreviewDrafts: vi.fn((): unknown => mockClient), |
| 52 | + save: vi.fn(async (_type: string, _name: string, _body: Record<string, unknown>) => ({ |
| 53 | + success: true, |
| 54 | + version: 'v1', |
| 55 | + state: 'draft', |
| 56 | + })), |
| 57 | + list: vi.fn(async (type: string) => { |
| 58 | + if (type === 'app') return [{ name: 'acme_app', label: 'Acme' }]; |
| 59 | + if (type === 'dataset') return DATASETS; |
| 60 | + if (type === 'report') return [stored]; |
| 61 | + return []; |
| 62 | + }), |
| 63 | + listDrafts: vi.fn(async () => []), |
| 64 | + layered: vi.fn(async (type: string) => { |
| 65 | + if (type === 'app') return { effective: { name: 'acme_app', label: 'Acme', active: true, navigation: NAV } }; |
| 66 | + if (type === 'report') return { effective: JSON.parse(JSON.stringify(stored)) }; |
| 67 | + return { code: null, overlay: null, overlayScope: null, effective: null }; |
| 68 | + }), |
| 69 | + getDraft: vi.fn(async () => null), |
| 70 | + get: vi.fn(async (type: string, name: string) => |
| 71 | + type === 'dataset' ? DATASETS.find((d) => d.name === name) : undefined, |
| 72 | + ), |
| 73 | +}; |
| 74 | + |
| 75 | +vi.mock('../metadata-admin/useMetadata', async (importOriginal) => { |
| 76 | + const mod = await importOriginal<typeof import('../metadata-admin/useMetadata')>(); |
| 77 | + return { ...mod, useMetadataClient: () => mockClient, useMetadataTypes: () => ({ entries: [] }) }; |
| 78 | +}); |
| 79 | + |
| 80 | +vi.mock('./packages-io', async (importOriginal) => { |
| 81 | + const mod = await importOriginal<typeof import('./packages-io')>(); |
| 82 | + return { ...mod, fetchPackages: vi.fn(async () => []) }; |
| 83 | +}); |
| 84 | + |
| 85 | +vi.mock('@object-ui/react', async (importOriginal) => { |
| 86 | + const mod = await importOriginal<typeof import('@object-ui/react')>(); |
| 87 | + return { ...mod, useAdapter: () => ({}) }; |
| 88 | +}); |
| 89 | + |
| 90 | +import { InterfacesPillar } from './StudioDesignSurface'; |
| 91 | +import { registerMetadataPreview } from '../metadata-admin/preview-registry'; |
| 92 | +import { registerMetadataDefaultInspector } from '../metadata-admin/default-inspector-registry'; |
| 93 | +import { ReportDefaultInspector } from '../metadata-admin/inspectors/ReportDefaultInspector'; |
| 94 | +import { t, tFormat } from '../metadata-admin/i18n'; |
| 95 | + |
| 96 | +// A stub canvas: the leaf needs a registered preview to be draft-editable, |
| 97 | +// and the canvas's own drawing is not what this file pins. |
| 98 | +registerMetadataPreview('report', () => null); |
| 99 | +registerMetadataDefaultInspector('report', ReportDefaultInspector); |
| 100 | + |
| 101 | +beforeEach(() => { |
| 102 | + stored = SUMMARY_REPORT; |
| 103 | + for (const fn of Object.values(mockClient)) fn.mockClear(); |
| 104 | +}); |
| 105 | +afterEach(cleanup); |
| 106 | + |
| 107 | +const L = (key: string) => t(key, 'en'); |
| 108 | + |
| 109 | +function mountPillar(onHeldEditChange?: (clause: string | null) => void) { |
| 110 | + return render( |
| 111 | + <MemoryRouter initialEntries={['/studio/com.acme.app/interfaces']}> |
| 112 | + <InterfacesPillar packageId="com.acme.app" onHeldEditChange={onHeldEditChange} /> |
| 113 | + </MemoryRouter>, |
| 114 | + ); |
| 115 | +} |
| 116 | + |
| 117 | +function reportSaves(): Array<Record<string, unknown>> { |
| 118 | + return mockClient.save.mock.calls.filter((c) => c[0] === 'report').map((c) => c[2]); |
| 119 | +} |
| 120 | + |
| 121 | +/** Past the autosave's 1.5 s debounce. */ |
| 122 | +async function outlastDebounce(): Promise<void> { |
| 123 | + await act(async () => { |
| 124 | + await new Promise((r) => setTimeout(r, 2300)); |
| 125 | + }); |
| 126 | +} |
| 127 | + |
| 128 | +/** The report's properties are open, with the list property labelled `list` drawn. */ |
| 129 | +async function ready(list: string): Promise<HTMLElement> { |
| 130 | + const inspector = await screen.findByTestId('if-inspector', undefined, { timeout: 8000 }); |
| 131 | + await within(inspector).findByText(list, { selector: 'label' }, { timeout: 8000 }); |
| 132 | + return inspector; |
| 133 | +} |
| 134 | + |
| 135 | +/** The form row of the list property labelled `label`: its label and its repeater. */ |
| 136 | +function listRow(inspector: HTMLElement, label: string): HTMLElement { |
| 137 | + const row = within(inspector).getByText(label, { selector: 'label' }).parentElement?.parentElement; |
| 138 | + if (!row) throw new Error(`no form row for ${label}`); |
| 139 | + return row; |
| 140 | +} |
| 141 | + |
| 142 | +/** The report's own Label input. */ |
| 143 | +function editLabel(inspector: HTMLElement, value: string): void { |
| 144 | + const input = within(inspector).getByLabelText(L('engine.inspector.report.label')); |
| 145 | + fireEvent.change(input, { target: { value } }); |
| 146 | + fireEvent.blur(input); |
| 147 | +} |
| 148 | + |
| 149 | +/** Picks `option` in the inspector select labelled `label`. */ |
| 150 | +async function pick(inspector: HTMLElement, label: string, option: string | RegExp): Promise<void> { |
| 151 | + fireEvent.keyDown(within(inspector).getByRole('combobox', { name: label }), { key: 'ArrowDown' }); |
| 152 | + await waitFor(() => expect(screen.queryAllByRole('option').length).toBeGreaterThan(0)); |
| 153 | + fireEvent.click(screen.getByRole('option', { name: option })); |
| 154 | +} |
| 155 | + |
| 156 | +const heldLine = (clause: string) => tFormat('engine.studio.held.line', 'en', { clause }); |
| 157 | +const joinedClause = tFormat('engine.studio.held.reportJoinedNeedsBlock', 'en', { list: 'Blocks' }); |
| 158 | + |
| 159 | +describe('a report switched to Joined is held until its first block is added (objectstack#29022)', () => { |
| 160 | + it('Report type = Joined, then a Label edit: nothing is sent, no red strip, the line names Blocks', async () => { |
| 161 | + const report = vi.fn(); |
| 162 | + mountPillar(report); |
| 163 | + const inspector = await ready('Order'); |
| 164 | + await pick(inspector, L('engine.inspector.report.type'), L('engine.inspector.report.type.joined')); |
| 165 | + editLabel(inspector, 'Sales v2'); |
| 166 | + await outlastDebounce(); |
| 167 | + |
| 168 | + expect(reportSaves(), 'the joined report without blocks is not sent, nor the Label edit with it').toEqual([]); |
| 169 | + expect(screen.queryByTestId('studio-refusal'), 'no red strip on the normal path').toBeNull(); |
| 170 | + expect(screen.getByTestId('studio-held-message')).toHaveTextContent(heldLine(joinedClause)); |
| 171 | + // The surface's half: its leave guard and Publish read the held clause. |
| 172 | + expect(report).toHaveBeenLastCalledWith(joinedClause); |
| 173 | + }); |
| 174 | + |
| 175 | + it('"Show me" focuses the Blocks list in the report properties', async () => { |
| 176 | + mountPillar(); |
| 177 | + const inspector = await ready('Order'); |
| 178 | + await pick(inspector, L('engine.inspector.report.type'), L('engine.inspector.report.type.joined')); |
| 179 | + await within(inspector).findByText('Blocks', { selector: 'label' }); |
| 180 | + const row = listRow(inspector, 'Blocks'); |
| 181 | + const show = await within(await screen.findByTestId('studio-held')).findByRole('button', { |
| 182 | + name: L('engine.studio.refusal.show'), |
| 183 | + }); |
| 184 | + (document.activeElement as HTMLElement | null)?.blur(); |
| 185 | + fireEvent.click(show); |
| 186 | + await waitFor(() => expect(row.contains(document.activeElement)).toBe(true)); |
| 187 | + }); |
| 188 | + |
| 189 | + it('Blocks → Add item hands the hold to the new block, still unsent', async () => { |
| 190 | + const report = vi.fn(); |
| 191 | + mountPillar(report); |
| 192 | + const inspector = await ready('Order'); |
| 193 | + await pick(inspector, L('engine.inspector.report.type'), L('engine.inspector.report.type.joined')); |
| 194 | + await within(inspector).findByText('Blocks', { selector: 'label' }); |
| 195 | + fireEvent.click(within(listRow(inspector, 'Blocks')).getByRole('button', { name: L('engine.form.addItem') })); |
| 196 | + await outlastDebounce(); |
| 197 | + |
| 198 | + const clause = tFormat('engine.studio.held.listItemNeedsInput', 'en', { input: 'Name', n: 1, list: 'Blocks' }); |
| 199 | + expect(reportSaves()).toEqual([]); |
| 200 | + expect(screen.queryByTestId('studio-refusal')).toBeNull(); |
| 201 | + expect(screen.getByTestId('studio-held-message')).toHaveTextContent(heldLine(clause)); |
| 202 | + expect(report).toHaveBeenLastCalledWith(clause); |
| 203 | + }); |
| 204 | + |
| 205 | + // CONTROL: a type switch the door accepts saves at once. |
| 206 | + it('Report type = Tabular saves at once, with no line', async () => { |
| 207 | + mountPillar(); |
| 208 | + const inspector = await ready('Order'); |
| 209 | + await pick(inspector, L('engine.inspector.report.type'), L('engine.inspector.report.type.tabular')); |
| 210 | + await waitFor(() => expect(reportSaves()).toHaveLength(1), { timeout: 4000 }); |
| 211 | + expect(reportSaves()[0].type).toBe('tabular'); |
| 212 | + expect(screen.queryByTestId('studio-held')).toBeNull(); |
| 213 | + }); |
| 214 | +}); |
0 commit comments