Repository navigation
Expand file tree
/
Copy pathObjectView.createChartView-11576.test.tsx
More file actions
266 lines (244 loc) · 10.7 KB
/
Copy pathObjectView.createChartView-11576.test.tsx
File metadata and controls
266 lines (244 loc) · 10.7 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
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* objectui#11576, the dialog's SECOND persisting door: `ObjectView`'s
* `handleViewCreate`, reached from the view switcher's New view. The first
* door (`ObjectDataPage.buildSaveAsViewSpec`, "Save as view") is pinned in
* `CreateViewDialog.chartBinding-11576.test.tsx`; the two doors assemble the
* spec separately, so each is pinned on its own.
*
* The real `ObjectView` and the real dialog run; only the metadata client is a
* stub, so the body asserted is the one `createRuntimeMetadata` hands
* `metadataClient.save`. Then the saved view is mounted as the object's list
* view and the `object-chart` node `ObjectView` builds for it is captured: a
* created chart view must render through the ADR-0021 dataset branch, with the
* binding the dialog wrote.
*
* Direction, written before the run: on the unmodified tree the door case goes
* RED (no dataset picker; the saved block carries `xAxisField` /
* `yAxisFields` and fails `ListViewSchema`), and the render case goes RED
* because it renders what the door saved.
*/
import * as React from 'react';
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
// `ObjectView` reaches a dynamic `import('@objectstack/lint')`: pay its load at
// collection, never inside a case's clocked window (`check:test-source-alias`).
import '@objectstack/lint';
import { render, cleanup, act, screen, fireEvent, waitFor, within } from '@testing-library/react';
import { MemoryRouter, Routes, Route } from 'react-router-dom';
import { ListViewSchema, ViewItemSchema } from '@objectstack/spec/ui';
vi.mock('@object-ui/permissions', async (importOriginal) => {
const actual = await importOriginal<typeof import('@object-ui/permissions')>();
// Stable identities: `ListView` names `perms` in its fetch dependencies.
const perms = {
check: () => ({ allowed: true }),
checkField: () => true,
getFieldPermissions: () => [],
getRowFilter: () => undefined,
getObjectApiOperations: () => undefined,
roles: [],
isLoaded: false,
hasCapabilities: () => true,
can: () => true,
cannot: () => false,
};
const fieldPerms = { canRead: () => true, canWrite: () => true, permissions: [] };
return { ...actual, usePermissions: () => perms, useFieldPermissions: () => fieldPerms };
});
vi.mock('@object-ui/auth', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
useAuth: () => ({ user: { id: 'u1', name: 'Ada' }, activeOrganization: null }),
// The add-view button and the dialog are admin-only.
useWorkspaceAdminStatus: () => ({ isAdmin: true, isResolved: true }),
createAuthenticatedFetch: () => vi.fn(),
}));
vi.mock('@object-ui/collaboration', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
useRealtimeSubscription: () => ({ lastMessage: null }),
useConflictResolution: () => ({ hasConflicts: false, resolveAllConflicts: () => {} }),
}));
vi.mock('sonner', () => ({
toast: Object.assign(vi.fn(), {
success: vi.fn(), error: vi.fn(), info: vi.fn(),
warning: vi.fn(), loading: vi.fn(), dismiss: vi.fn(),
}),
}));
vi.mock('./MetadataInspector', () => ({
MetadataPanel: () => null,
useMetadataInspector: () => ({ showDebug: false, toggle: () => {} }),
}));
vi.mock('./RecordDetailView', () => ({ RecordDetailView: () => null }));
/** The `object-chart` node `ObjectView` builds, captured on its way in. */
let capturedChartSchema: any = null;
vi.mock('@object-ui/plugin-charts', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
ObjectChart: (props: any) => {
capturedChartSchema = props.schema;
return null;
},
ChartRenderer: () => null,
}));
/** A served dataset over `crm_deal`, and one over another object. */
const DATASETS = [
{
name: 'deal_metrics',
label: 'Deal metrics',
object: 'crm_deal',
dimensions: [{ name: 'stage', field: 'stage' }],
measures: [
{ name: 'total_amount', aggregate: 'sum', field: 'amount' },
{ name: 'deal_count', aggregate: 'count' },
],
},
{
name: 'account_metrics',
label: 'Account metrics',
object: 'crm_account',
dimensions: [{ name: 'industry', field: 'industry' }],
measures: [{ name: 'account_count', aggregate: 'count' }],
},
];
// The metadata seam's client: `createRuntimeMetadata` writes through `save`;
// the dialog's catalog reads through `list` / `get`.
const metadataClient = {
save: vi.fn(async () => ({})),
get: vi.fn(async (type: string, name: string) =>
type === 'dataset' ? (DATASETS.find((d) => d.name === name) ?? null) : null),
list: vi.fn(async (type: string) => (type === 'dataset' ? DATASETS : [])),
};
vi.mock('./metadata-admin/useMetadata', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
useMetadataClient: () => metadataClient,
}));
import { ObjectView } from './ObjectView';
import { ExpressionProvider } from '../providers/ExpressionProvider';
const OBJECT_NAME = 'crm_deal';
const FIELDS = {
id: { type: 'text', label: 'Id' },
name: { type: 'text', label: 'Name' },
stage: { type: 'select', label: 'Stage', options: [{ value: 'open', label: 'Open' }] },
amount: { type: 'number', label: 'Amount' },
};
function makeDataSource() {
return {
find: vi.fn(async () => ({ data: [], total: 0 })),
findOne: vi.fn(async () => null),
create: vi.fn(async () => ({})),
update: vi.fn(async () => ({})),
delete: vi.fn(async () => ({})),
getObjectSchema: vi.fn(async () => ({ name: OBJECT_NAME, fields: {} })),
} as any;
}
function mount(listViews: Record<string, unknown>) {
render(
<ExpressionProvider user={{ id: 'u1', name: 'Ada', profile: 'admin' }}>
<MemoryRouter initialEntries={[`/apps/demo/${OBJECT_NAME}`]}>
<Routes>
<Route
path="/apps/:appName/:objectName/*"
element={
<ObjectView
dataSource={makeDataSource()}
objects={[{ name: OBJECT_NAME, label: 'Deal', fields: FIELDS, listViews }]}
onEdit={() => {}}
/>
}
/>
</Routes>
</MemoryRouter>
</ExpressionProvider>,
);
}
const settle = () => act(() => new Promise<void>((resolve) => setTimeout(resolve, 300)));
/** The config pick for `key`: the shared Select's trigger (objectui#11865). */
const picker = (key: string) => screen.getByTestId(`create-view-required-${key}`);
/** Open `key`'s picker from the keyboard and return the options it lists. */
async function openPicker(key: string): Promise<HTMLElement[]> {
fireEvent.keyDown(picker(key), { key: 'ArrowDown' });
return within(await screen.findByRole('listbox')).getAllByRole('option');
}
/** The labels `key`'s picker lists, in order; it is closed again after. */
async function optionLabels(key: string): Promise<string[]> {
const labels = (await openPicker(key)).map((o) => o.textContent ?? '');
fireEvent.keyDown(screen.getByRole('listbox'), { key: 'Escape' });
await waitFor(() => expect(screen.queryByRole('listbox')).toBeNull());
return labels;
}
/** Create a chart view through the Create View dialog; return the saved body. */
async function createChartViewThroughTheSwitcher() {
mount({ [`${OBJECT_NAME}.all`]: { name: `${OBJECT_NAME}.all`, label: 'All', type: 'grid', columns: ['name'] } });
await settle();
// The Create View dialog, from the view switcher's Manage views (its
// 「Add view」 creates in place since objectstack#35553).
fireEvent.click(screen.getByTestId('list-view-switcher'));
fireEvent.click(screen.getByTestId('list-view-switcher-manage'));
fireEvent.click(await screen.findByTestId('manage-views-add'));
const card = await screen.findByTestId('create-view-type-chart');
await waitFor(() => expect((card as HTMLButtonElement).disabled).toBe(false));
fireEvent.click(card);
await waitFor(() => expect(picker('values')).toBeEnabled());
expect((await optionLabels('dataset')).filter((label) => label.includes('account_metrics'))).toEqual([]);
const measure = (await openPicker('values')).find((o) => o.textContent === 'total_amount');
if (!measure) throw new Error('the measure picker lists no "total_amount"');
fireEvent.click(measure);
await waitFor(() => expect(screen.queryByRole('listbox')).toBeNull());
await act(async () => {
fireEvent.click(screen.getByTestId('create-view-submit'));
});
await waitFor(() => expect(metadataClient.save).toHaveBeenCalledTimes(1));
const [type, , body, opts] = metadataClient.save.mock.calls[0] as unknown as [string, string, any, any];
expect(type).toBe('view');
expect(opts).toEqual({ mode: 'draft', ifNoneMatch: '*' });
return body;
}
beforeEach(() => {
cleanup();
capturedChartSchema = null;
vi.stubGlobal(
'fetch',
vi.fn(async () =>
new Response(JSON.stringify({ data: [] }), {
status: 200,
headers: { 'content-type': 'application/json' },
}),
),
);
});
afterEach(() => {
vi.unstubAllGlobals();
vi.clearAllMocks();
});
describe('ObjectView.handleViewCreate saves the ADR-0021 chart block the dialog wrote (objectui#11576)', () => {
it('DOOR 2: the saved config passes the spec ListViewSchema, the envelope the ViewItem gate, and no legacy key rides', async () => {
const body = await createChartViewThroughTheSwitcher();
// The spec's verdict first: it is the claim this pin exists for.
const listView = ListViewSchema.safeParse(body.config);
expect(listView.success, JSON.stringify(listView.error?.issues)).toBe(true);
const item = ViewItemSchema.safeParse(body);
expect(item.success, JSON.stringify(item.error?.issues)).toBe(true);
expect(body.config.chart).toEqual({
chartType: 'bar',
dataset: 'deal_metrics',
values: ['total_amount'],
dimensions: ['stage'],
});
expect(Object.keys(body.config.chart)).not.toContain('xAxisField');
expect(Object.keys(body.config.chart)).not.toContain('yAxisFields');
});
it('the saved view renders on the object page through the dataset branch', async () => {
const body = await createChartViewThroughTheSwitcher();
cleanup();
capturedChartSchema = null;
mount({ [body.name]: { name: body.name, label: body.label, ...body.config } });
await waitFor(() => expect(capturedChartSchema?.type).toBe('object-chart'));
expect(capturedChartSchema).toMatchObject({
dataset: 'deal_metrics',
values: ['total_amount'],
dimensions: ['stage'],
chartType: 'bar',
});
// The dataset branch carries no object binding of its own; the legacy
// inline branch would have (`objectName` + `aggregate`).
expect(capturedChartSchema.objectName).toBeUndefined();
expect(capturedChartSchema.aggregate).toBeUndefined();
});
});