Repository navigation
Expand file tree
/
Copy pathPackageCustomizationsPanel.test.tsx
More file actions
304 lines (270 loc) · 13.4 KB
/
Copy pathPackageCustomizationsPanel.test.tsx
File metadata and controls
304 lines (270 loc) · 13.4 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* objectstack#36908 — an installed app's "Customizations in this environment"
* in its Setup → Packages detail: the removed Studio home's panel
* (objectstack#34714) and its card line (ADR-0146 D3), one app at a time.
* objectstack#37236 — "customized" means overrides + extensions everywhere
* (`customizationTotal`); arrangements are not customizations and are named
* apart, so an app whose only change is a tab arrangement reads "0 customized"
* beside its arrangement count.
*
* The real panel and the real customization reader (`packageCustomizations-io`)
* run under a real router; the wire of the customization route and the
* metadata client's list reads are stubbed. The stubbed answers are shaped by
* the route's declaration (`GetPackageCustomizationsResponseSchema`) inside
* the dispatcher envelope.
*/
import '@testing-library/jest-dom/vitest';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react';
import { MemoryRouter, Route, Routes, useLocation } from 'react-router-dom';
import { loadStudioCatalogue } from '@object-ui/i18n/namespaces/studio';
await loadStudioCatalogue('zh');
/** What the metadata client's `list(type, { packageId })` answers, by type. */
const lists = vi.hoisted(() => ({ byType: {} as Record<string, unknown[]>, asked: [] as string[] }));
vi.mock('../metadata-admin/useMetadata.js', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
useMetadataClient: () => ({
list: async (type: string, options?: { packageId?: string }) => {
lists.asked.push(`${type}@${options?.packageId}`);
return lists.byType[type] ?? [];
},
}),
}));
import { PACKAGE_CUSTOMIZATION_ROWS_SHOWN, PackageCustomizationsPanel } from './PackageCustomizationsPanel';
import { customizationRowsOf, loadCustomizationLabels } from './packageCustomizationRows';
import { t } from '../metadata-admin/i18n';
import type { PackageCustomizations } from './packageCustomizations-io';
const APP = 'app.objectstack.hotcrm';
type Lists = Partial<
Pick<PackageCustomizations, 'overrides' | 'extensions' | 'arrangements' | 'switchedOff' | 'contributions'>
>;
/** A served answer, its counts derived from its lists as the route derives them. */
function served(l: Lists = {}): PackageCustomizations {
const data = {
packageId: APP,
overrides: l.overrides ?? [],
extensions: l.extensions ?? [],
arrangements: l.arrangements ?? [],
switchedOff: l.switchedOff ?? [],
contributions: l.contributions ?? [],
};
return {
...data,
counts: {
overrides: data.overrides.length,
extensions: data.extensions.length,
arrangements: data.arrangements.length,
switchedOff: data.switchedOff.length,
contributions: data.contributions.length,
},
};
}
/** Answer the customization route with `status` and `data`; any other request is refused. */
function stubWire(status: number, data: unknown) {
vi.stubGlobal(
'fetch',
vi.fn(async (input: RequestInfo | URL) => {
const url = String(input);
if (url !== `/api/v1/packages/${APP}/customizations`) throw new Error(`unexpected request ${url}`);
const body =
status === 200
? { success: true, data }
: { success: false, error: { code: 'PERMISSION_DENIED', message: 'You may not read this package.' } };
// `headers`: a transport that reads them finds an empty set, not a throw.
return { ok: status === 200, status, headers: new Headers(), json: async () => body } as Response;
}),
);
}
function Where() {
const { pathname, search } = useLocation();
return <div data-testid="where">{`${pathname}${search}`}</div>;
}
function renderPanel(locale = 'en') {
const onNavigate = vi.fn();
render(
<MemoryRouter initialEntries={['/apps/setup/component/developer/packages?face=admin']}>
<Routes>
<Route
path="/apps/setup/component/developer/packages"
element={<PackageCustomizationsPanel packageId={APP} locale={locale} onNavigate={onNavigate} />}
/>
<Route path="*" element={<Where />} />
</Routes>
</MemoryRouter>,
);
return { onNavigate };
}
const summary = () => screen.getByTestId('pkg-detail-customizations-summary');
const rows = () => screen.queryAllByTestId('pkg-detail-customization');
const tab = (key: string) => screen.getByTestId(`pkg-detail-customizations-tab-${key}`);
afterEach(() => {
cleanup();
vi.unstubAllGlobals();
lists.byType = {};
lists.asked = [];
});
describe('Setup → Packages: an installed app’s customizations (objectstack#36908)', () => {
it('lists one row per change, each saying how it changed the app, and counts them', async () => {
stubWire(
200,
served({
overrides: [{ type: 'view', name: 'account_list' }],
extensions: [{ object: 'account', kind: 'field', name: 'region' }],
arrangements: [{ object: 'account', key: 'relatedLists' }],
switchedOff: [{ type: 'flow', name: 'lead_nurture' }],
contributions: [{ kind: 'field', object: 'account', name: 'x', packageId: 'com.other' }],
}),
);
lists.byType = { view: [{ name: 'account_list', label: 'All accounts' }], object: [{ name: 'account', label: 'Account' }] };
renderPanel();
await waitFor(() => expect(summary()).toHaveAttribute('data-state', 'read'));
// objectstack#37236: the arrangement is named apart, not counted as customized.
expect(summary()).toHaveTextContent('2 customized, 1 rearranged, 1 switched off');
expect(rows().map((r) => r.getAttribute('data-mode'))).toEqual(['changed', 'added', 'arranged', 'switchedOff']);
await waitFor(() => expect(rows()[0]).toHaveTextContent('All accounts'));
expect(within(rows()[2]).getByTestId('pkg-detail-customization-meta')).toHaveTextContent('Rearranged · Tabs');
// The label read asks the app's own lists, one per type with rows.
expect(lists.asked.sort()).toEqual([`flow@${APP}`, `object@${APP}`, `view@${APP}`]);
});
it('objectstack#37236: an arrangement-only app reads 0 customized beside its arrangement count, its rows still listed', async () => {
stubWire(
200,
served({
arrangements: [
{ object: 'account', key: 'relatedLists' },
{ object: 'account', key: 'fieldGroups' },
],
}),
);
renderPanel();
await waitFor(() => expect(summary()).toHaveAttribute('data-state', 'read'));
expect(summary().textContent).toBe('0 customized, 2 rearranged, 0 switched off');
// The rows are unchanged: both arrangements are listed, on their own tab.
expect(tab('arranged')).toHaveTextContent('2');
expect(rows().map((r) => r.getAttribute('data-mode'))).toEqual(['arranged', 'arranged']);
});
it('objectstack#37236: no arrangement, no arrangement segment', async () => {
stubWire(200, served({ overrides: [{ type: 'view', name: 'account_list' }] }));
renderPanel();
await waitFor(() => expect(summary()).toHaveAttribute('data-state', 'read'));
expect(summary().textContent).toBe('1 customized, 0 switched off');
});
it('with no change the section says so in one line and draws no list', async () => {
stubWire(200, served());
renderPanel();
await waitFor(() => expect(summary()).toHaveTextContent(t('engine.packages.customizations.none', 'en')));
expect(rows()).toHaveLength(0);
expect(screen.queryByTestId('pkg-detail-customizations-tab-all')).toBeNull();
});
it('a read that fails says so, never "0", with the server’s sentence as its title', async () => {
stubWire(403, null);
renderPanel();
await waitFor(() => expect(summary()).toHaveAttribute('data-state', 'failed'));
expect(summary()).toHaveTextContent(t('engine.packages.customizations.failed', 'en'));
expect(summary().getAttribute('title')).toContain('You may not read this package.');
expect(summary()).not.toHaveTextContent('0');
});
it('a row opens its item in Studio and closes the sheet; Customize in Studio opens the app', async () => {
stubWire(200, served({ extensions: [{ object: 'account', kind: 'field', name: 'region' }] }));
const { onNavigate } = renderPanel();
expect(screen.getByTestId('pkg-detail-customize')).toHaveAttribute('href', `/studio/${APP}/overview`);
await waitFor(() => expect(rows()).toHaveLength(1));
fireEvent.click(within(rows()[0]).getByRole('link'));
expect(await screen.findByTestId('where')).toHaveTextContent(`/studio/${APP}/metadata/object/account`);
expect(onNavigate).toHaveBeenCalled();
});
it('a switched-off action opens where it is switched: its object’s Actions view (objectstack#34724)', async () => {
stubWire(200, served({ switchedOff: [{ type: 'action', name: 'clone_opportunity' }] }));
lists.byType = { object: [{ name: 'opportunity', actions: [{ name: 'clone_opportunity' }] }] };
renderPanel();
await waitFor(() => expect(rows()).toHaveLength(1));
expect(within(rows()[0]).getByRole('link')).toHaveAttribute(
'href',
// objectstack#36904 — `?action=` on the object designer's address.
`/studio/${APP}/metadata/object/opportunity?action=clone_opportunity`,
);
});
it('tabs narrow by kind and the search by text', async () => {
stubWire(
200,
served({
overrides: [{ type: 'view', name: 'account_list' }],
extensions: [{ object: 'account', kind: 'field', name: 'region' }],
switchedOff: [{ type: 'flow', name: 'lead_nurture' }],
}),
);
renderPanel();
await waitFor(() => expect(rows()).toHaveLength(3));
fireEvent.click(tab('fields'));
expect(rows().map((r) => r.getAttribute('data-mode'))).toEqual(['added']);
fireEvent.click(tab('all'));
fireEvent.change(screen.getByTestId('pkg-detail-customizations-search'), { target: { value: 'nurture' } });
expect(rows().map((r) => r.getAttribute('data-mode'))).toEqual(['switchedOff']);
});
it(`shows the first ${PACKAGE_CUSTOMIZATION_ROWS_SHOWN} rows, the rest behind "Show all"`, async () => {
const overrides = Array.from({ length: PACKAGE_CUSTOMIZATION_ROWS_SHOWN + 2 }, (_, i) => ({ type: 'view', name: `v${i}` }));
stubWire(200, served({ overrides }));
renderPanel();
await waitFor(() => expect(rows()).toHaveLength(PACKAGE_CUSTOMIZATION_ROWS_SHOWN));
fireEvent.click(screen.getByTestId('pkg-detail-customizations-show-all'));
expect(rows()).toHaveLength(PACKAGE_CUSTOMIZATION_ROWS_SHOWN + 2);
});
it('zh-CN: the section speaks the card’s words; an arrangement-only app is 已定制 0 项 beside 调整排列', async () => {
stubWire(200, served({ arrangements: [{ object: 'account', key: 'relatedLists' }] }));
renderPanel('zh');
await waitFor(() => expect(summary()).toHaveAttribute('data-state', 'read'));
expect(summary().textContent).toBe('已定制 0 项,调整排列 1 处,已停用 0 项');
expect(screen.getByText('本环境的定制')).toBeInTheDocument();
expect(within(rows()[0]).getByTestId('pkg-detail-customization-meta')).toHaveTextContent('重新排列 · 页签');
});
});
describe('the section’s rows, count and labels (objectstack#36908)', () => {
const read = served({
overrides: [{ type: 'view', name: 'account_list' }],
arrangements: [
{ object: 'account', key: 'fieldGroups' },
{ object: 'account', key: 'relatedLists' },
],
contributions: [{ kind: 'field', object: 'account', name: 'x', packageId: 'com.other' }],
});
it('one row per served override, extension, arrangement and switched-off item; contributions make none', () => {
expect(customizationRowsOf(APP, read).map((r) => `${r.mode}:${r.type}:${r.name}:${r.arrangement ?? ''}`)).toEqual([
'changed:view:account_list:',
'arranged:object:account:fieldGroups',
'arranged:object:account:relatedLists',
]);
});
it('a switched-off action with no object read opens its own page', () => {
const rowsOf = customizationRowsOf(APP, { ...served(), switchedOff: [{ type: 'action', name: 'export_all' }] });
expect(rowsOf[0].href).toBe(`/studio/${APP}/metadata/action/export_all`);
});
it('labels come from the app’s own list of each type with rows, a field’s from its object; a failed list keeps the names', async () => {
const rowsOf = customizationRowsOf(APP, {
...served(),
overrides: [{ type: 'view', name: 'ticket_list' }],
extensions: [{ object: 'ticket', kind: 'field', name: 'region' }],
switchedOff: [{ type: 'flow', name: 'escalate' }],
});
const asked: string[] = [];
const client = {
list: vi.fn(async (type: string, options?: { packageId?: string }) => {
asked.push(`${type}@${options?.packageId}`);
if (type === 'flow') throw new Error('HTTP 500');
if (type === 'view') return [{ name: 'ticket_list', label: { en: 'All tickets', zh: '全部工单' } }];
if (type === 'object') return [{ name: 'ticket', label: 'Ticket', fields: { region: { label: 'Region' } } }];
return [];
}),
};
expect(await loadCustomizationLabels(client, APP, rowsOf, 'zh')).toEqual({
'view:ticket_list': '全部工单',
'object:ticket': 'Ticket',
'field:ticket.region': 'Region',
});
expect(asked.sort()).toEqual([`flow@${APP}`, `object@${APP}`, `view@${APP}`]);
// No row, no request.
client.list.mockClear();
expect(await loadCustomizationLabels(client, APP, [], 'en')).toEqual({});
expect(client.list).not.toHaveBeenCalled();
});
});