Repository navigation
Expand file tree
/
Copy pathStudioCustomizeControls.customize-28562.test.tsx
More file actions
268 lines (239 loc) · 12 KB
/
Copy pathStudioCustomizeControls.customize-28562.test.tsx
File metadata and controls
268 lines (239 loc) · 12 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
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* objectstack#28562 (ADR-0146 D2, D3) — the pieces Studio's Customize door is
* built from, each pinned on its own:
*
* - which types Studio customizes, asked of the spec's classification;
* - the override declaration a document carries, parsed by the spec's schema;
* - the body a customization's draft is written with: no `_` keys, the
* declaration of the base, an existing declaration kept as it is, and
* nothing at all when there is no version to declare;
* - whether the ensure door refuses this caller, from the posture and the
* platform-operator flag the server publishes;
* - the ensure door's client (`packages-io.ts#ensureCustomizationPackage`):
* the pair's id from the answer, 201 and 200 told apart, a refusal
* rejected with the server's own sentence, and the package list told only
* when a pair was created;
* - what the package list says of a base: its version, its listed pair;
* - the Customize button's reasons and the override badge.
*/
import '@testing-library/jest-dom/vitest';
import * as React from 'react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { cleanup, fireEvent, render, screen } from '@testing-library/react';
import {
StudioCustomizeButton,
StudioOverrideChip,
customizeDoorWalled,
declaredOverrideOf,
isStudioCustomizeType,
overrideDraftBody,
} from './StudioCustomizeControls';
import {
ensureCustomizationPackage,
listedCustomizationPairOf,
listedPackageVersion,
parseListedPackageManifests,
} from './packages-io';
import { t, tFormat } from '../metadata-admin/i18n';
const BASE = 'com.example.hotcrm';
const PAIR = 'com.example.hotcrm.custom';
afterEach(() => {
cleanup();
vi.unstubAllGlobals();
});
describe('the types Studio customizes (objectstack#28562)', () => {
// objectstack#30059 — the app, whose navigation the Interfaces rail customizes.
// objectstack#30063 — the page, edited in its designer as a dashboard is.
it.each(['view', 'dashboard', 'report', 'page', 'app'])('%s is customized', (type) => {
expect(isStudioCustomizeType(type)).toBe(true);
});
it.each(['object', 'flow', 'translation', '', undefined, null])(
'%s is not: no pillar here customizes it, or it is not overridable',
(type) => {
expect(isStudioCustomizeType(type as string | undefined)).toBe(false);
},
);
});
describe('the declaration a document carries (objectstack#28562)', () => {
it('reads a declaration of this base', () => {
expect(declaredOverrideOf({ name: 'pipeline', overrides: { package: BASE, basedOn: '4.0.0' } }, BASE)).toEqual({
package: BASE,
basedOn: '4.0.0',
});
});
it.each([
['no declaration', { name: 'pipeline' }],
['a declaration of another base', { overrides: { package: 'com.example.other', basedOn: '1.0.0' } }],
['a declaration with no version', { overrides: { package: BASE } }],
['not a document', null],
])('%s declares no override of the base', (_case, doc) => {
expect(declaredOverrideOf(doc, BASE)).toBeNull();
});
});
describe('the body a customization is written with (objectstack#28562)', () => {
it('drops the top-level `_` keys and declares the override of the base at its version', () => {
const doc = { name: 'pipeline', label: 'Pipeline', widgets: [{ id: 'w1', _note: 'kept below the top' }], _packageId: BASE, _lock: 'full' };
expect(overrideDraftBody(doc, BASE, '4.0.0')).toStrictEqual({
name: 'pipeline',
label: 'Pipeline',
widgets: [{ id: 'w1', _note: 'kept below the top' }],
overrides: { package: BASE, basedOn: '4.0.0' },
});
});
it('keeps a declaration of the base the document already carries, its version included', () => {
const doc = { name: 'pipeline', overrides: { package: BASE, basedOn: '3.2.0' } };
expect(overrideDraftBody(doc, BASE, '4.0.0')?.overrides).toEqual({ package: BASE, basedOn: '3.2.0' });
});
it('declares nothing it would have to guess: no version and no declaration is no body', () => {
expect(overrideDraftBody({ name: 'pipeline' }, BASE, null)).toBeNull();
});
it('with no version, a document that already declares the base is still written', () => {
const doc = { name: 'pipeline', overrides: { package: BASE, basedOn: '3.2.0' } };
expect(overrideDraftBody(doc, BASE, null)).toStrictEqual(doc);
});
});
describe('whether the ensure door refuses this caller (objectstack#28562)', () => {
it.each([
['group', false, true],
['isolated', false, true],
['group', true, false],
['isolated', true, false],
['single', false, false],
[undefined, false, false],
] as const)('posture %s, platform operator %s: walled %s', (posture, isPlatformOperator, walled) => {
expect(customizeDoorWalled({ posture, isPlatformOperator })).toBe(walled);
});
});
function json(body: unknown, status: number): Response {
return new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } });
}
const answer = (created: boolean) => ({
success: true,
data: { basePackageId: BASE, packageId: PAIR, namespace: 'hotcrm_custom', dependencyRange: '^4', created, writable: true },
});
describe('the ensure door client (objectstack#28562)', () => {
it('POSTs no body to the base’s door, and names the pair from the answer; a created pair tells the package list', async () => {
const fetchMock = vi.fn(async () => json(answer(true), 201));
vi.stubGlobal('fetch', fetchMock);
const changed = vi.fn();
window.addEventListener('objectui:packages-changed', changed);
try {
await expect(ensureCustomizationPackage(BASE)).resolves.toEqual({
basePackageId: BASE,
packageId: PAIR,
namespace: 'hotcrm_custom',
created: true,
writable: true,
});
} finally {
window.removeEventListener('objectui:packages-changed', changed);
}
expect(fetchMock).toHaveBeenCalledTimes(1);
const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit];
expect(url).toBe(`/api/v1/packages/${BASE}/ensure-customization`);
expect(init.method).toBe('POST');
expect(init.body).toBeUndefined();
expect(changed).toHaveBeenCalledTimes(1);
});
it('a pair that was there (200) is named the same way, and the package list is not told', async () => {
vi.stubGlobal('fetch', vi.fn(async () => json(answer(false), 200)));
const changed = vi.fn();
window.addEventListener('objectui:packages-changed', changed);
try {
await expect(ensureCustomizationPackage(BASE)).resolves.toMatchObject({ packageId: PAIR, created: false });
} finally {
window.removeEventListener('objectui:packages-changed', changed);
}
expect(changed).not.toHaveBeenCalled();
});
it('a refusal rejects with the server’s own sentence', async () => {
const sentence = 'Only the platform operator may create a customization package under the group posture.';
vi.stubGlobal('fetch', vi.fn(async () => json({ success: false, error: { code: 'FORBIDDEN', message: sentence } }, 403)));
await expect(ensureCustomizationPackage(BASE)).rejects.toMatchObject({ message: `${sentence} (FORBIDDEN)` });
});
it('an answer its declaration refuses names no pair', async () => {
vi.stubGlobal('fetch', vi.fn(async () => json({ success: true, data: { packageId: PAIR } }, 201)));
await expect(ensureCustomizationPackage(BASE)).rejects.toThrow(BASE);
});
});
describe('what the package list says of a base (objectstack#28562)', () => {
const listed = (withPair: boolean, version?: string) =>
parseListedPackageManifests({
data: [
{ manifest: { id: BASE, name: 'HotCRM', ...(version ? { version } : {}) }, writable: false },
...(withPair ? [{ manifest: { id: PAIR, name: 'HotCRM customizations', version: '0.0.0' }, writable: true }] : []),
],
});
it('the version its manifest declares, else none', () => {
expect(listedPackageVersion(listed(false, '4.0.0'), BASE)).toBe('4.0.0');
expect(listedPackageVersion(listed(false), BASE)).toBeNull();
expect(listedPackageVersion(listed(false, '4.0.0'), 'com.example.unlisted')).toBeNull();
});
it('its pair when the list lists one, else none: the read never spells a pair that is not there', () => {
expect(listedCustomizationPairOf(listed(true, '4.0.0'), BASE)).toBe(PAIR);
expect(listedCustomizationPairOf(listed(false, '4.0.0'), BASE)).toBeNull();
});
});
describe('the Customize button and the override badge (objectstack#28562)', () => {
it('offered: enabled, titled with the base, and pressing it calls back', () => {
const onCustomize = vi.fn();
render(<StudioCustomizeButton block={null} baseLabel="HotCRM" locale="en" onCustomize={onCustomize} />);
const button = screen.getByTestId('studio-customize');
expect(button).toBeEnabled();
expect(button).toHaveTextContent(t('engine.studio.customize.action', 'en'));
expect(button).toHaveAttribute('title', tFormat('engine.studio.customize.actionTitle', 'en', { package: 'HotCRM' }));
button.click();
expect(onCustomize).toHaveBeenCalledTimes(1);
expect(screen.queryByTestId('studio-customize-blocked')).toBeNull();
});
it.each([
['walled', t('engine.studio.customize.walled', 'en')],
['noVersion', tFormat('engine.studio.customize.noVersion', 'en', { package: 'HotCRM' })],
] as const)('blocked (%s): disabled, and its wrapper says why', (block, reason) => {
const onCustomize = vi.fn();
render(<StudioCustomizeButton block={block} baseLabel="HotCRM" locale="en" onCustomize={onCustomize} />);
const button = screen.getByTestId('studio-customize');
expect(button).toBeDisabled();
const wrapper = screen.getByTestId('studio-customize-blocked');
expect(wrapper).toHaveAttribute('data-block', block);
expect(wrapper).toHaveAttribute('title', reason);
expect(document.getElementById(button.getAttribute('aria-describedby')!)).toHaveTextContent(reason);
});
it('the badge names the base; pressed, its popover says the version the override is based on', () => {
render(<StudioOverrideChip baseLabel="HotCRM" basedOn="4.0.0" locale="en" />);
const chip = screen.getByTestId('studio-override-chip');
expect(chip).toHaveTextContent(tFormat('engine.studio.customize.overrides', 'en', { package: 'HotCRM' }));
fireEvent.click(chip);
expect(screen.getByTestId('studio-override-popover-facts')).toHaveTextContent(
tFormat('engine.studio.customize.overridesTitle', 'en', { package: 'HotCRM', basedOn: '4.0.0' }),
);
});
});
describe('the override mark is where Restore is asked (objectstack#29692)', () => {
it('its popover offers Restore package original; pressing it asks the host once and closes the popover', () => {
const onRestore = vi.fn();
render(<StudioOverrideChip baseLabel="HotCRM" basedOn="4.0.0" locale="en" onRestore={onRestore} />);
fireEvent.click(screen.getByTestId('studio-override-chip'));
const restore = screen.getByTestId('studio-override-restore');
expect(restore).toHaveTextContent(t('engine.studio.customize.restore', 'en'));
fireEvent.click(restore);
expect(onRestore).toHaveBeenCalledTimes(1);
expect(screen.queryByTestId('studio-override-popover')).toBeNull();
});
it('disabled while the host cannot restore: pressing it asks nothing', () => {
const onRestore = vi.fn();
render(<StudioOverrideChip baseLabel="HotCRM" basedOn="4.0.0" locale="en" onRestore={onRestore} restoreDisabled />);
fireEvent.click(screen.getByTestId('studio-override-chip'));
const restore = screen.getByTestId('studio-override-restore');
expect(restore).toBeDisabled();
fireEvent.click(restore);
expect(onRestore).not.toHaveBeenCalled();
});
it('CONTROL: without onRestore the popover states the facts and offers no Restore', () => {
render(<StudioOverrideChip baseLabel="HotCRM" basedOn="4.0.0" locale="en" />);
fireEvent.click(screen.getByTestId('studio-override-chip'));
expect(screen.getByTestId('studio-override-popover')).toBeInTheDocument();
expect(screen.queryByTestId('studio-override-restore')).toBeNull();
});
});