|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#27459 — the Interfaces pillar holds an item just added to a |
| 5 | + * dashboard's own list property unsent until it is filled in, instead of |
| 6 | + * autosaving it for the draft door to refuse. |
| 7 | + * |
| 8 | + * The card: Properties → Global Filters → "Add item", then a Label edit. The |
| 9 | + * autosave PUT `globalFilters[N] = {}`, the door answered 422 on |
| 10 | + * `globalFilters.N.field`, the Label save carried the same item and was refused |
| 11 | + * too, and the Label edit was gone after a reload. The pillar now holds the |
| 12 | + * dashboard as it holds a widget waiting for its binding (objectui#11910): |
| 13 | + * nothing is sent, a neutral line names the input with a "Show me", and |
| 14 | + * filling the item in lets the next autosave send every edit kept meanwhile. |
| 15 | + * |
| 16 | + * Mounted with a metadata client double that records every save, a stub |
| 17 | + * canvas and the real `DashboardDefaultInspector` (its spec-driven form). |
| 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 | +import { DashboardSchema } from '@objectstack/spec/ui'; |
| 26 | + |
| 27 | +const NAV = [{ id: 'nav_sales', type: 'dashboard', label: 'Sales', dashboardName: 'sales_dash' }]; |
| 28 | + |
| 29 | +/** One bound widget and no list property yet: the dashboard parses with the spec as served. */ |
| 30 | +const SALES_DASH = { |
| 31 | + name: 'sales_dash', |
| 32 | + label: 'Sales', |
| 33 | + widgets: [ |
| 34 | + { |
| 35 | + id: 'pipeline', |
| 36 | + type: 'metric', |
| 37 | + title: 'Pipeline', |
| 38 | + dataset: 'sales_ds', |
| 39 | + values: ['revenue'], |
| 40 | + layout: { x: 0, y: 0, w: 3, h: 2 }, |
| 41 | + }, |
| 42 | + ], |
| 43 | +}; |
| 44 | + |
| 45 | +const mockClient = { |
| 46 | + withPreviewDrafts: vi.fn((): unknown => mockClient), |
| 47 | + save: vi.fn(async (_type: string, _name: string, _body: Record<string, unknown>) => ({ |
| 48 | + success: true, |
| 49 | + version: 'v1', |
| 50 | + state: 'draft', |
| 51 | + })), |
| 52 | + list: vi.fn(async (type: string) => { |
| 53 | + if (type === 'app') return [{ name: 'acme_app', label: 'Acme' }]; |
| 54 | + if (type === 'dashboard') return [SALES_DASH]; |
| 55 | + return []; |
| 56 | + }), |
| 57 | + listDrafts: vi.fn(async () => []), |
| 58 | + layered: vi.fn(async (type: string) => { |
| 59 | + if (type === 'app') return { effective: { name: 'acme_app', label: 'Acme', active: true, navigation: NAV } }; |
| 60 | + if (type === 'dashboard') return { effective: JSON.parse(JSON.stringify(SALES_DASH)) }; |
| 61 | + return { code: null, overlay: null, overlayScope: null, effective: null }; |
| 62 | + }), |
| 63 | + getDraft: vi.fn(async () => null), |
| 64 | + get: vi.fn(async () => undefined), |
| 65 | +}; |
| 66 | + |
| 67 | +vi.mock('../metadata-admin/useMetadata', async (importOriginal) => { |
| 68 | + const mod = await importOriginal<typeof import('../metadata-admin/useMetadata')>(); |
| 69 | + return { ...mod, useMetadataClient: () => mockClient, useMetadataTypes: () => ({ entries: [] }) }; |
| 70 | +}); |
| 71 | + |
| 72 | +vi.mock('./packages-io', async (importOriginal) => { |
| 73 | + const mod = await importOriginal<typeof import('./packages-io')>(); |
| 74 | + return { ...mod, fetchPackages: vi.fn(async () => []) }; |
| 75 | +}); |
| 76 | + |
| 77 | +vi.mock('@object-ui/react', async (importOriginal) => { |
| 78 | + const mod = await importOriginal<typeof import('@object-ui/react')>(); |
| 79 | + return { ...mod, useAdapter: () => ({}) }; |
| 80 | +}); |
| 81 | + |
| 82 | +import { InterfacesPillar } from './StudioDesignSurface'; |
| 83 | +import { registerMetadataPreview } from '../metadata-admin/preview-registry'; |
| 84 | +import { registerMetadataDefaultInspector } from '../metadata-admin/default-inspector-registry'; |
| 85 | +import { DashboardDefaultInspector } from '../metadata-admin/inspectors/DashboardDefaultInspector'; |
| 86 | +import { t, tFormat } from '../metadata-admin/i18n'; |
| 87 | + |
| 88 | +// A stub canvas: the leaf needs a registered preview to be draft-editable, |
| 89 | +// and the canvas's own drawing is not what this file pins. |
| 90 | +registerMetadataPreview('dashboard', () => null); |
| 91 | +registerMetadataDefaultInspector('dashboard', DashboardDefaultInspector); |
| 92 | + |
| 93 | +beforeEach(() => { |
| 94 | + for (const fn of Object.values(mockClient)) fn.mockClear(); |
| 95 | +}); |
| 96 | +afterEach(cleanup); |
| 97 | + |
| 98 | +function mountPillar(onHeldEditChange?: (clause: string | null) => void) { |
| 99 | + return render( |
| 100 | + <MemoryRouter initialEntries={['/studio/com.acme.app/interfaces']}> |
| 101 | + <InterfacesPillar packageId="com.acme.app" onHeldEditChange={onHeldEditChange} /> |
| 102 | + </MemoryRouter>, |
| 103 | + ); |
| 104 | +} |
| 105 | + |
| 106 | +function dashboardSaves(): Array<Record<string, unknown>> { |
| 107 | + return mockClient.save.mock.calls.filter((c) => c[0] === 'dashboard').map((c) => c[2]); |
| 108 | +} |
| 109 | + |
| 110 | +/** Past the autosave's 1.5 s debounce. */ |
| 111 | +async function outlastDebounce(): Promise<void> { |
| 112 | + await act(async () => { |
| 113 | + await new Promise((r) => setTimeout(r, 2300)); |
| 114 | + }); |
| 115 | +} |
| 116 | + |
| 117 | +/** The dashboard's properties are open, with its Filters section expanded: its Global Filters row is drawn. */ |
| 118 | +async function ready(): Promise<HTMLElement> { |
| 119 | + const inspector = await screen.findByTestId('if-inspector', undefined, { timeout: 8000 }); |
| 120 | + fireEvent.click(await within(inspector).findByRole('button', { name: /^Filters/ }, { timeout: 8000 })); |
| 121 | + await within(inspector).findByText('Global Filters', { selector: 'label' }, { timeout: 8000 }); |
| 122 | + return inspector; |
| 123 | +} |
| 124 | + |
| 125 | +/** The form row of the list property labelled `label`: its label and its repeater. */ |
| 126 | +function listRow(inspector: HTMLElement, label: string): HTMLElement { |
| 127 | + const row = within(inspector).getByText(label, { selector: 'label' }).parentElement?.parentElement; |
| 128 | + if (!row) throw new Error(`no form row for ${label}`); |
| 129 | + return row; |
| 130 | +} |
| 131 | + |
| 132 | +/** The dashboard's own Label input commits on blur. */ |
| 133 | +function editLabel(inspector: HTMLElement, value: string): void { |
| 134 | + const input = within(inspector).getByDisplayValue('Sales'); |
| 135 | + fireEvent.change(input, { target: { value } }); |
| 136 | + fireEvent.blur(input); |
| 137 | +} |
| 138 | + |
| 139 | +const clause = tFormat('engine.studio.held.listItemNeedsInput', 'en', { input: 'Field', n: 1, list: 'Global Filters' }); |
| 140 | + |
| 141 | +describe('an item just added to a dashboard list property is held until it is filled in (objectstack#27459)', () => { |
| 142 | + it('Global Filters → Add item, then a Label edit: nothing is sent, no red strip, the line names the Field', async () => { |
| 143 | + const report = vi.fn(); |
| 144 | + mountPillar(report); |
| 145 | + const inspector = await ready(); |
| 146 | + fireEvent.click(within(listRow(inspector, 'Global Filters')).getByRole('button', { name: t('engine.form.addItem', 'en') })); |
| 147 | + editLabel(inspector, 'Sales v2'); |
| 148 | + await outlastDebounce(); |
| 149 | + |
| 150 | + expect(dashboardSaves(), 'the unfinished item is not sent, nor the Label edit with it').toEqual([]); |
| 151 | + expect(screen.queryByTestId('studio-refusal'), 'no red strip on the normal path').toBeNull(); |
| 152 | + expect(screen.getByTestId('studio-held-message')).toHaveTextContent( |
| 153 | + tFormat('engine.studio.held.line', 'en', { clause }), |
| 154 | + ); |
| 155 | + // Publish's half: the surface is told what is held. |
| 156 | + expect(report).toHaveBeenLastCalledWith(clause); |
| 157 | + }); |
| 158 | + |
| 159 | + it('"Show me" focuses the list in the dashboard properties', async () => { |
| 160 | + mountPillar(); |
| 161 | + const inspector = await ready(); |
| 162 | + const row = listRow(inspector, 'Global Filters'); |
| 163 | + fireEvent.click(within(row).getByRole('button', { name: t('engine.form.addItem', 'en') })); |
| 164 | + const show = await within(await screen.findByTestId('studio-held')).findByRole('button', { |
| 165 | + name: t('engine.studio.refusal.show', 'en'), |
| 166 | + }); |
| 167 | + (document.activeElement as HTMLElement | null)?.blur(); |
| 168 | + fireEvent.click(show); |
| 169 | + await waitFor(() => expect(row.contains(document.activeElement)).toBe(true)); |
| 170 | + }); |
| 171 | + |
| 172 | + it('filling the Field in sends one save carrying the item and the Label edit kept meanwhile, and clears the line', async () => { |
| 173 | + const report = vi.fn(); |
| 174 | + mountPillar(report); |
| 175 | + const inspector = await ready(); |
| 176 | + const row = listRow(inspector, 'Global Filters'); |
| 177 | + fireEvent.click(within(row).getByRole('button', { name: t('engine.form.addItem', 'en') })); |
| 178 | + editLabel(inspector, 'Sales v2'); |
| 179 | + await outlastDebounce(); |
| 180 | + expect(dashboardSaves()).toEqual([]); |
| 181 | + |
| 182 | + const field = within(row).getByLabelText('Field'); |
| 183 | + fireEvent.change(field, { target: { value: 'stage' } }); |
| 184 | + fireEvent.blur(field); |
| 185 | + await waitFor(() => expect(dashboardSaves()).toHaveLength(1), { timeout: 4000 }); |
| 186 | + const [sent] = dashboardSaves(); |
| 187 | + expect(sent.label).toBe('Sales v2'); |
| 188 | + expect(sent.globalFilters).toEqual([expect.objectContaining({ field: 'stage' })]); |
| 189 | + expect( |
| 190 | + DashboardSchema.safeParse(JSON.parse(JSON.stringify(sent))).success, |
| 191 | + 'the body sent is one the door accepts', |
| 192 | + ).toBe(true); |
| 193 | + await waitFor(() => expect(screen.queryByTestId('studio-held')).toBeNull()); |
| 194 | + expect(screen.queryByTestId('studio-refusal')).toBeNull(); |
| 195 | + expect(report).toHaveBeenLastCalledWith(null); |
| 196 | + }); |
| 197 | + |
| 198 | + // CONTROL: an edit to a dashboard with no unfinished item saves at once. |
| 199 | + it('a Label edit alone saves at once, with no line', async () => { |
| 200 | + mountPillar(); |
| 201 | + const inspector = await ready(); |
| 202 | + editLabel(inspector, 'Sales v2'); |
| 203 | + await waitFor(() => expect(dashboardSaves()).toHaveLength(1), { timeout: 4000 }); |
| 204 | + expect(dashboardSaves()[0].label).toBe('Sales v2'); |
| 205 | + expect(screen.queryByTestId('studio-held')).toBeNull(); |
| 206 | + }); |
| 207 | +}); |
0 commit comments