|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#26845 — the two departures objectstack#26705 left open in the |
| 5 | + * Data pillar: creating an object, and opening another object from the |
| 6 | + * Pending changes sheet. Both load another object into the one buffer a held |
| 7 | + * edit lives in, so both ask the shared departure question first. |
| 8 | + * |
| 9 | + * New object asks before its dialog opens, so Cancel costs nothing and the |
| 10 | + * create itself asks nothing. A Pending changes request for another object |
| 11 | + * asks before the pillar applies it; one for the open object drops nothing |
| 12 | + * and asks nothing. |
| 13 | + * |
| 14 | + * Rendered through the REAL `StudioDesignSurface` on the real route shape, as |
| 15 | + * in `StudioDesignSurface.heldDataLeave-26705.test.tsx`; the sheet is a stand-in |
| 16 | + * that asks the host for an object exactly as its security block does |
| 17 | + * (`useSurfaceNavigator`). |
| 18 | + */ |
| 19 | + |
| 20 | +import '@testing-library/jest-dom/vitest'; |
| 21 | +import * as React from 'react'; |
| 22 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 23 | +import { render, screen, fireEvent, cleanup, waitFor, within } from '@testing-library/react'; |
| 24 | +import { MemoryRouter, Route, Routes } from 'react-router-dom'; |
| 25 | + |
| 26 | +const PKG = 'com.acme.app'; |
| 27 | + |
| 28 | +const OBJECTS: Record<string, Record<string, unknown>> = { |
| 29 | + acme_task: { name: 'acme_task', label: 'Task', fields: [{ name: 'title', label: 'Title', type: 'text' }] }, |
| 30 | + acme_note: { name: 'acme_note', label: 'Note', fields: [{ name: 'body', label: 'Body', type: 'text' }] }, |
| 31 | +}; |
| 32 | + |
| 33 | +const mockClient = vi.hoisted(() => ({ |
| 34 | + list: vi.fn(async (_type: string) => [] as unknown[]), |
| 35 | + listDrafts: vi.fn(async () => []), |
| 36 | + listTypes: vi.fn(async () => ({ entries: [] })), |
| 37 | + get: vi.fn(async () => null), |
| 38 | + references: vi.fn(async () => []), |
| 39 | + layered: vi.fn(async (_type: string, _name: string) => ({ code: null, overlay: null, overlayScope: null, effective: null as unknown })), |
| 40 | + getDraft: vi.fn(async () => null), |
| 41 | + save: vi.fn(async () => ({ success: true })), |
| 42 | + publishDraft: vi.fn(async () => ({ success: true })), |
| 43 | + publishPackageDrafts: vi.fn(async () => ({ success: true, failed: [] })), |
| 44 | + publish: vi.fn(async () => ({ success: true })), |
| 45 | + reset: vi.fn(async () => ({})), |
| 46 | +})); |
| 47 | + |
| 48 | +vi.mock('../metadata-admin/useMetadata', async (importOriginal) => { |
| 49 | + const mod = await importOriginal<typeof import('../metadata-admin/useMetadata')>(); |
| 50 | + return { ...mod, useMetadataClient: () => mockClient, useMetadataTypes: () => ({ entries: [] }) }; |
| 51 | +}); |
| 52 | + |
| 53 | +vi.mock('./packages-io', async (importOriginal) => { |
| 54 | + const mod = await importOriginal<typeof import('./packages-io')>(); |
| 55 | + return { |
| 56 | + ...mod, |
| 57 | + fetchPackages: vi.fn(async () => [{ id: 'com.acme.app', name: 'Acme', writable: true, namespace: 'acme' }]), |
| 58 | + }; |
| 59 | +}); |
| 60 | + |
| 61 | +vi.mock('@object-ui/react', async (importOriginal) => { |
| 62 | + const mod = await importOriginal<typeof import('@object-ui/react')>(); |
| 63 | + return { ...mod, useAdapter: () => dataSource }; |
| 64 | +}); |
| 65 | + |
| 66 | +vi.mock('./StudioAiCopilot', () => ({ StudioChatDock: () => null })); |
| 67 | +// Stands in for the Pending changes sheet: its security block asks the host to |
| 68 | +// open the object a draft names, through the surface channel. |
| 69 | +vi.mock('../../preview/DraftChangesPanel', async () => { |
| 70 | + const { createElement } = await import('react'); |
| 71 | + const { useSurfaceNavigator } = await import('./surfaceDeepLinkChannel'); |
| 72 | + return { |
| 73 | + DraftChangesPanel: () => { |
| 74 | + const openSurface = useSurfaceNavigator(); |
| 75 | + return createElement( |
| 76 | + 'div', |
| 77 | + null, |
| 78 | + ['acme_task', 'acme_note'].map((name) => |
| 79 | + createElement( |
| 80 | + 'button', |
| 81 | + { key: name, type: 'button', onClick: () => openSurface?.({ type: 'object', name }) }, |
| 82 | + `Pending object/${name}`, |
| 83 | + ), |
| 84 | + ), |
| 85 | + ); |
| 86 | + }, |
| 87 | + }; |
| 88 | +}); |
| 89 | + |
| 90 | +import { StudioDesignSurface } from './StudioDesignSurface'; |
| 91 | +import { addFieldFromFormToolbar } from './__tests__/dataPillarAddField'; |
| 92 | +import { createEmptyDataSource } from './__tests__/emptyDataSource'; |
| 93 | +import { registerBuiltinInspectors } from '../metadata-admin/inspectors'; |
| 94 | +import { t } from '../metadata-admin/i18n'; |
| 95 | + |
| 96 | +const dataSource = createEmptyDataSource(); |
| 97 | +registerBuiltinInspectors(); |
| 98 | + |
| 99 | +vi.stubGlobal( |
| 100 | + 'fetch', |
| 101 | + vi.fn(async () => new Response('null', { status: 404, headers: { 'content-type': 'application/json' } })), |
| 102 | +); |
| 103 | + |
| 104 | +window.matchMedia = ((query: string) => ({ |
| 105 | + matches: false, |
| 106 | + media: query, |
| 107 | + onchange: null, |
| 108 | + addEventListener: () => {}, |
| 109 | + removeEventListener: () => {}, |
| 110 | + addListener: () => {}, |
| 111 | + removeListener: () => {}, |
| 112 | + dispatchEvent: () => false, |
| 113 | +})) as unknown as typeof window.matchMedia; |
| 114 | +(globalThis as { ResizeObserver?: unknown }).ResizeObserver = |
| 115 | + (globalThis as { ResizeObserver?: unknown }).ResizeObserver ?? |
| 116 | + class { |
| 117 | + observe() {} |
| 118 | + unobserve() {} |
| 119 | + disconnect() {} |
| 120 | + }; |
| 121 | + |
| 122 | +// This jsdom environment ships no window.confirm — install a mock outright. |
| 123 | +let confirmSpy: ReturnType<typeof vi.fn>; |
| 124 | +const LEAVE_QUESTION = t('engine.edit.unsavedLeaveConfirm', 'en'); |
| 125 | + |
| 126 | +beforeEach(() => { |
| 127 | + for (const fn of Object.values(mockClient)) (fn as unknown as { mockClear: () => void }).mockClear(); |
| 128 | + mockClient.list.mockImplementation(async (type: string) => |
| 129 | + type === 'object' ? Object.values(OBJECTS).map((o) => ({ name: o.name, label: o.label, _packageId: PKG })) : [], |
| 130 | + ); |
| 131 | + mockClient.layered.mockImplementation(async (type: string, name: string) => ({ |
| 132 | + code: null, |
| 133 | + overlay: null, |
| 134 | + overlayScope: null, |
| 135 | + effective: type === 'object' && OBJECTS[name] ? JSON.parse(JSON.stringify(OBJECTS[name])) : null, |
| 136 | + })); |
| 137 | + confirmSpy = vi.fn(); |
| 138 | + window.confirm = confirmSpy as unknown as typeof window.confirm; |
| 139 | +}); |
| 140 | + |
| 141 | +afterEach(() => { |
| 142 | + cleanup(); |
| 143 | + localStorage.clear(); |
| 144 | + window.history.replaceState(null, '', '/'); |
| 145 | +}); |
| 146 | + |
| 147 | +/** The object the header names: its api-name chip. */ |
| 148 | +async function openObjectIs(name: string): Promise<void> { |
| 149 | + await waitFor( |
| 150 | + () => expect(within(screen.getByTestId('data-object-header')).getByTestId('studio-item-api-name')).toHaveTextContent(name), |
| 151 | + { timeout: 8000 }, |
| 152 | + ); |
| 153 | +} |
| 154 | + |
| 155 | +async function openTask(): Promise<void> { |
| 156 | + // The shell's link guard reads the window's URL to skip a link to the page |
| 157 | + // already shown; `BrowserRouter` keeps the two equal, so this does too. |
| 158 | + window.history.replaceState(null, '', `/studio/${PKG}/data`); |
| 159 | + render( |
| 160 | + <MemoryRouter initialEntries={[`/studio/${PKG}/data`]}> |
| 161 | + <Routes> |
| 162 | + <Route path="/studio/:packageId/:tab" element={<StudioDesignSurface />} /> |
| 163 | + </Routes> |
| 164 | + </MemoryRouter>, |
| 165 | + ); |
| 166 | + await openObjectIs('acme_task'); |
| 167 | +} |
| 168 | + |
| 169 | +/** The card's repro: add a Picklist "Priority" with no option yet; the autosave holds it. */ |
| 170 | +async function holdAPicklist(): Promise<void> { |
| 171 | + fireEvent.click(await screen.findByRole('button', { name: 'Form' })); |
| 172 | + await addFieldFromFormToolbar(undefined, { label: 'Priority', type: /^Picklist$/ }); |
| 173 | + await screen.findByTestId('studio-held', undefined, { timeout: 8000 }); |
| 174 | +} |
| 175 | + |
| 176 | +const newObject = () => fireEvent.click(within(screen.getByTestId('studio-rail-data')).getByTestId('studio-rail-add')); |
| 177 | +const pendingRequest = (name: string) => fireEvent.click(screen.getByRole('button', { name: `Pending object/${name}` })); |
| 178 | + |
| 179 | +describe('a held Data field guards create and Pending changes (objectstack#26845)', () => { |
| 180 | + it('New object asks; Cancel opens no dialog and keeps the held field, OK opens it and the create asks nothing', async () => { |
| 181 | + await openTask(); |
| 182 | + await holdAPicklist(); |
| 183 | + |
| 184 | + confirmSpy.mockReturnValueOnce(false); |
| 185 | + newObject(); |
| 186 | + expect(confirmSpy).toHaveBeenCalledTimes(1); |
| 187 | + expect(confirmSpy).toHaveBeenCalledWith(LEAVE_QUESTION); |
| 188 | + expect(screen.queryByRole('dialog')).toBeNull(); |
| 189 | + expect(screen.getByTestId('studio-held')).toBeInTheDocument(); |
| 190 | + |
| 191 | + confirmSpy.mockReturnValueOnce(true); |
| 192 | + newObject(); |
| 193 | + expect(confirmSpy).toHaveBeenCalledTimes(2); |
| 194 | + const dialog = await screen.findByRole('dialog'); |
| 195 | + fireEvent.change(within(dialog).getByLabelText(t('engine.studio.data.nameLabel', 'en')), { |
| 196 | + target: { value: 'Ticket' }, |
| 197 | + }); |
| 198 | + fireEvent.click(within(dialog).getByRole('button', { name: t('engine.studio.createDraft', 'en') })); |
| 199 | + await openObjectIs('acme_ticket'); |
| 200 | + expect(confirmSpy).toHaveBeenCalledTimes(2); |
| 201 | + }); |
| 202 | + |
| 203 | + it('a Pending changes request for another object asks; Cancel keeps the held field, OK opens the object', async () => { |
| 204 | + await openTask(); |
| 205 | + await holdAPicklist(); |
| 206 | + |
| 207 | + confirmSpy.mockReturnValueOnce(false); |
| 208 | + pendingRequest('acme_note'); |
| 209 | + await waitFor(() => expect(confirmSpy).toHaveBeenCalledTimes(1)); |
| 210 | + expect(confirmSpy).toHaveBeenCalledWith(LEAVE_QUESTION); |
| 211 | + await openObjectIs('acme_task'); |
| 212 | + expect(screen.getByTestId('studio-held')).toBeInTheDocument(); |
| 213 | + |
| 214 | + // A request for the open object drops nothing, so it asks nothing. |
| 215 | + pendingRequest('acme_task'); |
| 216 | + await openObjectIs('acme_task'); |
| 217 | + expect(screen.getByTestId('studio-held')).toBeInTheDocument(); |
| 218 | + expect(confirmSpy).toHaveBeenCalledTimes(1); |
| 219 | + |
| 220 | + confirmSpy.mockReturnValueOnce(true); |
| 221 | + pendingRequest('acme_note'); |
| 222 | + await waitFor(() => expect(confirmSpy).toHaveBeenCalledTimes(2)); |
| 223 | + await openObjectIs('acme_note'); |
| 224 | + expect(screen.queryByTestId('studio-held')).toBeNull(); |
| 225 | + }); |
| 226 | + |
| 227 | + // CONTROL: nothing held, nothing asked. |
| 228 | + it('with no edit held, New object and a Pending changes request never ask', async () => { |
| 229 | + await openTask(); |
| 230 | + |
| 231 | + pendingRequest('acme_note'); |
| 232 | + await openObjectIs('acme_note'); |
| 233 | + newObject(); |
| 234 | + await screen.findByRole('dialog'); |
| 235 | + expect(confirmSpy).not.toHaveBeenCalled(); |
| 236 | + }); |
| 237 | +}); |
0 commit comments