|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectui#13433 — a `type: 'form'` action fired inside an app opened its form |
| 5 | + * outside the app: on the app-less `/forms/:name`, headed by the raw qualified |
| 6 | + * view name (`showcase_task.form`), with Submit as the only button. |
| 7 | + * |
| 8 | + * Pinned here, on the console half of the fix (the navigator half — which |
| 9 | + * route a form hop takes — is pinned in app-shell, beside |
| 10 | + * `hostInConsolePath`'s two callers): |
| 11 | + * |
| 12 | + * - the app route table serves `/apps/:appName/forms/:name` with the same |
| 13 | + * internal `FormPage`, and a submit there lands on the record in the SAME |
| 14 | + * app the URL names; |
| 15 | + * - a form view that names itself nowhere is headed by its object's label; |
| 16 | + * an authored view label or form `title` still wins; |
| 17 | + * - the internal form has a Cancel: back to the page the user came from |
| 18 | + * when there is one, else the edited record, else the route's fallback; |
| 19 | + * - the public `/f/:slug` form has none (controls: an anonymous visitor has |
| 20 | + * no console to return to). |
| 21 | + */ |
| 22 | + |
| 23 | +import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'; |
| 24 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 25 | +import userEvent from '@testing-library/user-event'; |
| 26 | +import { MemoryRouter, Route, Routes, useLocation } from 'react-router-dom'; |
| 27 | +import { createI18n } from '@object-ui/i18n'; |
| 28 | +import { MetadataCtx } from '@object-ui/react'; |
| 29 | +import { FormPage } from './FormPage'; |
| 30 | +import { appRoutes } from '../AppContent'; |
| 31 | + |
| 32 | +vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } })); |
| 33 | + |
| 34 | +/** A container's default form: no view label, no form `title` — what Log Time opens. */ |
| 35 | +const UNLABELLED_VIEW = { |
| 36 | + name: 'showcase_task.form', |
| 37 | + object: 'showcase_task', |
| 38 | + viewKind: 'form', |
| 39 | + config: { |
| 40 | + type: 'simple', |
| 41 | + sections: [{ name: 'task', label: 'Task details', fields: ['title'] }], |
| 42 | + }, |
| 43 | +}; |
| 44 | + |
| 45 | +const OBJECT_SCHEMA = { |
| 46 | + name: 'showcase_task', |
| 47 | + label: 'Task', |
| 48 | + fields: { title: { type: 'text', label: 'Title' } }, |
| 49 | +}; |
| 50 | + |
| 51 | +const GET_RESPONSE = { |
| 52 | + object: 'showcase_task', |
| 53 | + id: 'task-42', |
| 54 | + record: { id: 'task-42', title: 'Write the report' }, |
| 55 | +}; |
| 56 | + |
| 57 | +const POST_RESPONSE = { |
| 58 | + object: 'showcase_task', |
| 59 | + id: 'task-77', |
| 60 | + record: { id: 'task-77', title: 'New task' }, |
| 61 | +}; |
| 62 | + |
| 63 | +function stubFetch(view: unknown = UNLABELLED_VIEW) { |
| 64 | + return vi.fn(async (url: string, init?: RequestInit) => { |
| 65 | + const method = (init?.method ?? 'GET').toUpperCase(); |
| 66 | + const u = String(url); |
| 67 | + let body: unknown; |
| 68 | + if (u.includes('/meta/view/')) body = view; |
| 69 | + else if (u.includes('/meta/object/')) body = OBJECT_SCHEMA; |
| 70 | + else if (method === 'GET' && u.includes('/data/showcase_task/task-42')) body = GET_RESPONSE; |
| 71 | + else if (method === 'POST' && u.includes('/data/showcase_task')) body = POST_RESPONSE; |
| 72 | + else throw new Error(`unstubbed fetch: ${method} ${u}`); |
| 73 | + return { |
| 74 | + ok: true, |
| 75 | + status: 200, |
| 76 | + statusText: 'OK', |
| 77 | + json: async () => body, |
| 78 | + text: async () => JSON.stringify(body), |
| 79 | + } as unknown as Response; |
| 80 | + }); |
| 81 | +} |
| 82 | + |
| 83 | +function LocationProbe() { |
| 84 | + const location = useLocation(); |
| 85 | + return <div data-testid="location">{`${location.pathname}${location.search}`}</div>; |
| 86 | +} |
| 87 | + |
| 88 | +const where = () => screen.getByTestId('location').textContent; |
| 89 | + |
| 90 | +const recordPath = (objectName: string, recordId: string) => |
| 91 | + `/apps/com.example.showcase/${objectName}/record/${recordId}`; |
| 92 | + |
| 93 | +/** The internal form on the app-less route, optionally with a page behind it. */ |
| 94 | +function renderInternal({ url, behind }: { url: string; behind?: string }) { |
| 95 | + const entries = behind ? [behind, url] : [url]; |
| 96 | + return render( |
| 97 | + <MemoryRouter initialEntries={entries} initialIndex={entries.length - 1}> |
| 98 | + <LocationProbe /> |
| 99 | + <Routes> |
| 100 | + <Route |
| 101 | + path="/forms/:name" |
| 102 | + element={<FormPage mode="internal" recordPath={recordPath} cancelPath="/home" />} |
| 103 | + /> |
| 104 | + <Route path="/f/:slug" element={<FormPage mode="public" />} /> |
| 105 | + <Route path="*" element={<div data-testid="elsewhere" />} /> |
| 106 | + </Routes> |
| 107 | + </MemoryRouter>, |
| 108 | + ); |
| 109 | +} |
| 110 | + |
| 111 | +beforeEach(() => { |
| 112 | + createI18n({ defaultLanguage: 'en', detectBrowserLanguage: false }); |
| 113 | +}); |
| 114 | + |
| 115 | +afterEach(() => { |
| 116 | + vi.unstubAllGlobals(); |
| 117 | +}); |
| 118 | + |
| 119 | +describe('the heading of an internal form (objectui#13433)', () => { |
| 120 | + it('a form view that names itself nowhere is headed by its object, not its raw name', async () => { |
| 121 | + vi.stubGlobal('fetch', stubFetch()); |
| 122 | + renderInternal({ url: '/forms/showcase_task.form' }); |
| 123 | + |
| 124 | + // RED before the fix: the heading read `showcase_task.form`. |
| 125 | + expect(await screen.findByRole('heading', { level: 1 })).toHaveTextContent(/^Task$/); |
| 126 | + expect(screen.queryByText('showcase_task.form')).toBeNull(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('an authored form `title` heads the form', async () => { |
| 130 | + vi.stubGlobal('fetch', stubFetch({ ...UNLABELLED_VIEW, config: { ...UNLABELLED_VIEW.config, title: 'Log time' } })); |
| 131 | + renderInternal({ url: '/forms/showcase_task.form' }); |
| 132 | + |
| 133 | + expect(await screen.findByRole('heading', { level: 1 })).toHaveTextContent(/^Log time$/); |
| 134 | + }); |
| 135 | + |
| 136 | + it('control: the view’s own label still wins', async () => { |
| 137 | + vi.stubGlobal('fetch', stubFetch({ ...UNLABELLED_VIEW, label: 'Quick edit', config: { ...UNLABELLED_VIEW.config, title: 'Log time' } })); |
| 138 | + renderInternal({ url: '/forms/showcase_task.form' }); |
| 139 | + |
| 140 | + expect(await screen.findByRole('heading', { level: 1 })).toHaveTextContent(/^Quick edit$/); |
| 141 | + }); |
| 142 | +}); |
| 143 | + |
| 144 | +describe('Cancel on an internal form (objectui#13433)', () => { |
| 145 | + it('goes back to the page the form was opened from', async () => { |
| 146 | + vi.stubGlobal('fetch', stubFetch()); |
| 147 | + renderInternal({ |
| 148 | + behind: '/apps/com.example.showcase/showcase_task/record/task-42?tab=details', |
| 149 | + url: '/forms/showcase_task.form?recordId=task-42&recordObject=showcase_task', |
| 150 | + }); |
| 151 | + |
| 152 | + await userEvent.click(await screen.findByRole('button', { name: 'Cancel' })); |
| 153 | + |
| 154 | + await waitFor(() => expect(where()).toBe('/apps/com.example.showcase/showcase_task/record/task-42?tab=details')); |
| 155 | + }); |
| 156 | + |
| 157 | + it('opened cold on an edit, lands on the record it edits', async () => { |
| 158 | + vi.stubGlobal('fetch', stubFetch()); |
| 159 | + renderInternal({ url: '/forms/showcase_task.form?recordId=task-42&recordObject=showcase_task' }); |
| 160 | + |
| 161 | + await userEvent.click(await screen.findByRole('button', { name: 'Cancel' })); |
| 162 | + |
| 163 | + await waitFor(() => expect(where()).toBe('/apps/com.example.showcase/showcase_task/record/task-42')); |
| 164 | + }); |
| 165 | + |
| 166 | + it('opened cold on a create, lands on the route’s fallback', async () => { |
| 167 | + vi.stubGlobal('fetch', stubFetch()); |
| 168 | + renderInternal({ url: '/forms/showcase_task.form' }); |
| 169 | + |
| 170 | + await userEvent.click(await screen.findByRole('button', { name: 'Cancel' })); |
| 171 | + |
| 172 | + await waitFor(() => expect(where()).toBe('/home')); |
| 173 | + }); |
| 174 | + |
| 175 | + it('writes nothing', async () => { |
| 176 | + const fetchSpy = stubFetch(); |
| 177 | + vi.stubGlobal('fetch', fetchSpy); |
| 178 | + renderInternal({ url: '/forms/showcase_task.form?recordId=task-42&recordObject=showcase_task' }); |
| 179 | + |
| 180 | + await userEvent.click(await screen.findByRole('button', { name: 'Cancel' })); |
| 181 | + |
| 182 | + await waitFor(() => expect(screen.getByTestId('elsewhere')).toBeInTheDocument()); |
| 183 | + const writes = fetchSpy.mock.calls.filter(([, init]) => (init?.method ?? 'GET').toUpperCase() !== 'GET'); |
| 184 | + expect(writes).toEqual([]); |
| 185 | + }); |
| 186 | + |
| 187 | + it('control: the public form renders no Cancel', async () => { |
| 188 | + vi.stubGlobal( |
| 189 | + 'fetch', |
| 190 | + vi.fn(async () => ({ |
| 191 | + ok: true, |
| 192 | + status: 200, |
| 193 | + statusText: 'OK', |
| 194 | + json: async () => ({ object: 'showcase_task', form: UNLABELLED_VIEW.config, objectSchema: OBJECT_SCHEMA }), |
| 195 | + text: async () => '', |
| 196 | + }) as unknown as Response), |
| 197 | + ); |
| 198 | + renderInternal({ url: '/f/contact-us' }); |
| 199 | + |
| 200 | + expect(await screen.findByRole('button', { name: 'Submit' })).toBeInTheDocument(); |
| 201 | + expect(screen.queryByRole('button', { name: 'Cancel' })).toBeNull(); |
| 202 | + }); |
| 203 | +}); |
| 204 | + |
| 205 | +describe('the in-app form route, /apps/:appName/forms/:name (objectui#13433)', () => { |
| 206 | + const METADATA = { |
| 207 | + apps: [ |
| 208 | + { name: 'showcase', _packageId: 'com.example.showcase' }, |
| 209 | + { name: 'crm', _packageId: 'com.acme.crm' }, |
| 210 | + ], |
| 211 | + objects: [], |
| 212 | + dashboards: [], |
| 213 | + reports: [], |
| 214 | + pages: [], |
| 215 | + loading: false, |
| 216 | + error: null, |
| 217 | + refresh: async () => {}, |
| 218 | + invalidate: () => {}, |
| 219 | + ensureType: async () => [], |
| 220 | + getItem: async () => null, |
| 221 | + getItemsByType: () => [], |
| 222 | + } as any; |
| 223 | + |
| 224 | + /** The app route table as `AppContent` mounts it, under `/apps/:appName/*`. */ |
| 225 | + function renderInApp(url: string) { |
| 226 | + return render( |
| 227 | + <MetadataCtx.Provider value={METADATA}> |
| 228 | + <MemoryRouter initialEntries={[url]}> |
| 229 | + <LocationProbe /> |
| 230 | + <Routes> |
| 231 | + <Route |
| 232 | + path="/apps/:appName/*" |
| 233 | + element={ |
| 234 | + <Routes> |
| 235 | + {appRoutes} |
| 236 | + <Route path=":objectName/record/:recordId" element={<div data-testid="record-page" />} /> |
| 237 | + <Route path="*" element={<div data-testid="app-not-found" />} /> |
| 238 | + </Routes> |
| 239 | + } |
| 240 | + /> |
| 241 | + </Routes> |
| 242 | + </MemoryRouter> |
| 243 | + </MetadataCtx.Provider>, |
| 244 | + ); |
| 245 | + } |
| 246 | + |
| 247 | + it('renders the internal form inside the app the URL names', async () => { |
| 248 | + vi.stubGlobal('fetch', stubFetch()); |
| 249 | + renderInApp('/apps/com.example.showcase/forms/showcase_task.form?recordId=task-42&recordObject=showcase_task'); |
| 250 | + |
| 251 | + // RED without the route: the app table's catch-all answers instead. |
| 252 | + expect(await screen.findByLabelText(/Title/)).toHaveValue('Write the report'); |
| 253 | + expect(screen.queryByTestId('app-not-found')).toBeNull(); |
| 254 | + }); |
| 255 | + |
| 256 | + it('a submit lands on the written record in the same app — the second app, not the first', async () => { |
| 257 | + vi.stubGlobal('fetch', stubFetch()); |
| 258 | + renderInApp('/apps/com.acme.crm/forms/showcase_task.form'); |
| 259 | + |
| 260 | + await userEvent.type(await screen.findByLabelText(/Title/), 'New task'); |
| 261 | + await userEvent.click(screen.getByRole('button', { name: 'Submit' })); |
| 262 | + |
| 263 | + await waitFor(() => expect(where()).toBe('/apps/com.acme.crm/showcase_task/record/task-77')); |
| 264 | + }); |
| 265 | + |
| 266 | + it('opened cold on a create, Cancel lands on the app’s home', async () => { |
| 267 | + vi.stubGlobal('fetch', stubFetch()); |
| 268 | + renderInApp('/apps/com.acme.crm/forms/showcase_task.form'); |
| 269 | + |
| 270 | + await userEvent.click(await screen.findByRole('button', { name: 'Cancel' })); |
| 271 | + |
| 272 | + await waitFor(() => expect(where()).toBe('/apps/com.acme.crm')); |
| 273 | + }); |
| 274 | +}); |
0 commit comments