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