@larakit/react 0.0.0-stage → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +30 -4
- package/src/Cell.tsx +35 -0
- package/src/Dashboard.tsx +79 -0
- package/src/Field.tsx +132 -0
- package/src/FileInput.tsx +76 -0
- package/src/Form.tsx +116 -0
- package/src/Layout.tsx +250 -0
- package/src/PanelPages.tsx +115 -0
- package/src/Repeater.tsx +46 -0
- package/src/ResourceIndex.tsx +271 -0
- package/src/action-upload.test.tsx +44 -0
- package/src/blocks.tsx +26 -0
- package/src/button-part.test.tsx +42 -0
- package/src/components.test.ts +22 -0
- package/src/components.tsx +435 -0
- package/src/confirm.test.tsx +68 -0
- package/src/context.tsx +19 -0
- package/src/custom-pages.test.tsx +177 -0
- package/src/errors.test.tsx +95 -0
- package/src/fixtures.ts +20 -0
- package/src/index.ts +29 -0
- package/src/kit/api.ts +305 -0
- package/src/kit/auth.tsx +109 -0
- package/src/kit/bridge.tsx +142 -0
- package/src/kit/button.tsx +396 -0
- package/src/kit/cell.tsx +260 -0
- package/src/kit/charts.tsx +638 -0
- package/src/kit/create.tsx +166 -0
- package/src/kit/dashboard.tsx +335 -0
- package/src/kit/dates.tsx +62 -0
- package/src/kit/entry.tsx +87 -0
- package/src/kit/field.tsx +449 -0
- package/src/kit/fields/CodeEditor.tsx +157 -0
- package/src/kit/fields/KeyValue.tsx +215 -0
- package/src/kit/fields/MarkdownEditor.tsx +380 -0
- package/src/kit/fields/OneTimeCode.tsx +192 -0
- package/src/kit/fields/Repeater.tsx +350 -0
- package/src/kit/fields/Slider.tsx +185 -0
- package/src/kit/fields/TagsInput.tsx +219 -0
- package/src/kit/filters.tsx +214 -0
- package/src/kit/form-shared.tsx +62 -0
- package/src/kit/form.tsx +253 -0
- package/src/kit/hooks.ts +119 -0
- package/src/kit/i18n.tsx +67 -0
- package/src/kit/icons.tsx +190 -0
- package/src/kit/infolist.tsx +452 -0
- package/src/kit/kit.test.tsx +2200 -0
- package/src/kit/layout.tsx +236 -0
- package/src/kit/loading.tsx +120 -0
- package/src/kit/notice.tsx +72 -0
- package/src/kit/page.tsx +103 -0
- package/src/kit/pageform.tsx +75 -0
- package/src/kit/picker.tsx +110 -0
- package/src/kit/plugins.tsx +206 -0
- package/src/kit/profile.tsx +86 -0
- package/src/kit/relations.tsx +146 -0
- package/src/kit/rich.tsx +276 -0
- package/src/kit/search.tsx +92 -0
- package/src/kit/select.tsx +244 -0
- package/src/kit/shell.tsx +401 -0
- package/src/kit/table-extras.tsx +457 -0
- package/src/kit/table.tsx +734 -0
- package/src/kit/theme.ts +57 -0
- package/src/layout-parts.test.tsx +159 -0
- package/src/pages/index.ts +7 -0
- package/src/part.test.tsx +56 -0
- package/src/plugins.test.tsx +159 -0
- package/src/plugins.tsx +240 -0
- package/src/preview.test.tsx +81 -0
- package/src/rail.test.tsx +56 -0
- package/src/repeater.test.tsx +48 -0
- package/src/resource-pages.test.tsx +119 -0
- package/src/screens.test.tsx +121 -0
- package/src/ssr.test.tsx +86 -0
- package/src/tenant.test.tsx +94 -0
- package/src/ui.tsx +100 -0
- package/theme.css +467 -0
- package/README.md +0 -3
|
@@ -0,0 +1,2200 @@
|
|
|
1
|
+
import { createElement, StrictMode } from 'react';
|
|
2
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
|
|
4
|
+
import {
|
|
5
|
+
api,
|
|
6
|
+
changed,
|
|
7
|
+
type Action,
|
|
8
|
+
type Field,
|
|
9
|
+
type Page,
|
|
10
|
+
type Panel,
|
|
11
|
+
type Place,
|
|
12
|
+
type RecordData,
|
|
13
|
+
type RelationResource,
|
|
14
|
+
type Resource,
|
|
15
|
+
} from './api';
|
|
16
|
+
import { ProfilePage } from './profile';
|
|
17
|
+
import tr from '../../../../locales/tr.json';
|
|
18
|
+
import { buttonAttrs } from './button';
|
|
19
|
+
import { Cell } from './cell';
|
|
20
|
+
import { Area, Pie, ticks } from './charts';
|
|
21
|
+
import { Ask } from './create';
|
|
22
|
+
import { Dashboard } from './dashboard';
|
|
23
|
+
import { FieldView, holds, shape, toForm, toSend, type Vals } from './field';
|
|
24
|
+
import { CodeEditor } from './fields/CodeEditor';
|
|
25
|
+
import { KeyValue } from './fields/KeyValue';
|
|
26
|
+
import { MarkdownEditor, markdownToHtml } from './fields/MarkdownEditor';
|
|
27
|
+
import { OneTimeCode } from './fields/OneTimeCode';
|
|
28
|
+
import { Slider } from './fields/Slider';
|
|
29
|
+
import { TagsInput } from './fields/TagsInput';
|
|
30
|
+
import { Filters, FiltersAbove } from './filters';
|
|
31
|
+
import { Form } from './form';
|
|
32
|
+
import { filterVisiblePlaces } from './form-shared';
|
|
33
|
+
import { I18nProvider, useT } from './i18n';
|
|
34
|
+
import { InfolistView } from './infolist';
|
|
35
|
+
import { Fields } from './layout';
|
|
36
|
+
import { Notices, sure } from './notice';
|
|
37
|
+
import { HOOKS, PanelHookContext, replaces, type HookProps } from './plugins';
|
|
38
|
+
import { RelationManagers } from './relations';
|
|
39
|
+
import { RichEditor, sanitizeHtml } from './rich';
|
|
40
|
+
import { RecordPage, Rows, Table } from './table';
|
|
41
|
+
import { Shell } from './shell';
|
|
42
|
+
import { Select } from './select';
|
|
43
|
+
import { PageForm } from './pageform';
|
|
44
|
+
|
|
45
|
+
// The server sends the messages with the panel. Without a panel here, the pieces speak English.
|
|
46
|
+
vi.mock('./i18n', async (original) => {
|
|
47
|
+
const i18n = await original<typeof import('./i18n')>();
|
|
48
|
+
const { default: en } = await import('../../../../locales/en.json');
|
|
49
|
+
i18n.setI18n('en', 'ltr', en);
|
|
50
|
+
return {
|
|
51
|
+
...i18n,
|
|
52
|
+
useT: () => {
|
|
53
|
+
const { messages } = i18n.useI18n();
|
|
54
|
+
return (key: string, vars?: Record<string, string | number>) => i18n.formatMessage(Object.keys(messages).length ? messages : en, key, vars);
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
afterEach(() => {
|
|
60
|
+
cleanup();
|
|
61
|
+
vi.restoreAllMocks();
|
|
62
|
+
vi.unstubAllGlobals();
|
|
63
|
+
localStorage.clear();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('field value helpers', () => {
|
|
67
|
+
it('converts values with toForm and toSend across field types', () => {
|
|
68
|
+
const toggle: Field = { key: 'active', label: 'Active', type: 'toggle' };
|
|
69
|
+
expect(toForm(toggle, true, () => {})).toBe('true');
|
|
70
|
+
expect(toForm(toggle, 1, () => {})).toBe('true');
|
|
71
|
+
expect(toForm(toggle, false, () => {})).toBe('false');
|
|
72
|
+
expect(toSend(toggle, 'true')).toBe(true);
|
|
73
|
+
expect(toSend(toggle, 'false')).toBe(false);
|
|
74
|
+
|
|
75
|
+
const known: Record<string, unknown> = {};
|
|
76
|
+
const status: Field = {
|
|
77
|
+
key: 'status',
|
|
78
|
+
label: 'Status',
|
|
79
|
+
type: 'select',
|
|
80
|
+
options: [
|
|
81
|
+
{ value: 1, label: 'Open' },
|
|
82
|
+
{ value: 2, label: 'Closed' },
|
|
83
|
+
],
|
|
84
|
+
};
|
|
85
|
+
expect(toForm(status, 2, (s, o) => { known[s] = o; })).toBe('2');
|
|
86
|
+
expect(toSend(status, '2', known)).toBe(2);
|
|
87
|
+
|
|
88
|
+
const tags: Field = {
|
|
89
|
+
key: 'tags',
|
|
90
|
+
label: 'Tags',
|
|
91
|
+
type: 'select',
|
|
92
|
+
select: { multiple: true },
|
|
93
|
+
};
|
|
94
|
+
const tagKnown: Record<string, unknown> = {};
|
|
95
|
+
expect(toForm(tags, [10, 20], (s, o) => { tagKnown[s] = o; })).toEqual(['10', '20']);
|
|
96
|
+
expect(toSend(tags, ['10', '20'], tagKnown)).toEqual([10, 20]);
|
|
97
|
+
|
|
98
|
+
const num: Field = { key: 'qty', label: 'Qty', type: 'text', look: { input: 'number' } };
|
|
99
|
+
expect(toSend(num, '42')).toBe(42);
|
|
100
|
+
expect(toSend(num, '')).toBeNull();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('evaluates holds() conditions including negation and empty values', () => {
|
|
104
|
+
expect(holds(undefined, {})).toBe(true);
|
|
105
|
+
expect(holds([{ field: 'published' }], { published: 'true' })).toBe(true);
|
|
106
|
+
expect(holds([{ field: 'published' }], { published: 'false' })).toBe(false);
|
|
107
|
+
expect(holds([{ field: 'role', values: ['admin', 'editor'] }], { role: 'admin' })).toBe(true);
|
|
108
|
+
expect(holds([{ field: 'role', values: ['admin'], not: true }], { role: 'viewer' })).toBe(true);
|
|
109
|
+
expect(holds([{ field: 'role', values: [''] }], { role: '' })).toBe(true);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('formats masked text with shape()', () => {
|
|
113
|
+
expect(shape('(999) 999-9999', '123abc4567890')).toBe('(123) 456-7890');
|
|
114
|
+
expect(shape('aa-99', 'ab12')).toBe('ab-12');
|
|
115
|
+
expect(shape('**-99', 'A123')).toBe('A1-23');
|
|
116
|
+
expect(shape('99/99', '1a2')).toBe('12');
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe('ticks()', () => {
|
|
121
|
+
it('produces nice rounded axis ticks enclosing values and zero', () => {
|
|
122
|
+
expect(ticks([12, 87])).toEqual([0, 50, 100]);
|
|
123
|
+
expect(ticks([12, 80])).toEqual([0, 20, 40, 60, 80]);
|
|
124
|
+
expect(ticks([-15, 35])).toEqual([-20, 0, 20, 40]);
|
|
125
|
+
expect(ticks([0, 0])).toEqual([0, 1]);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe('buttonAttrs()', () => {
|
|
130
|
+
it('infers soft variant for colored and danger buttons without overriding explicit props', () => {
|
|
131
|
+
expect(buttonAttrs({})).toEqual({ color: undefined, variant: undefined, size: 'md' });
|
|
132
|
+
expect(buttonAttrs({ danger: true })).toEqual({ color: 'danger', variant: 'soft', size: 'md' });
|
|
133
|
+
expect(buttonAttrs({ color: 'primary', variant: 'solid', size: 'sm', danger: true })).toEqual({
|
|
134
|
+
color: 'primary',
|
|
135
|
+
variant: 'solid',
|
|
136
|
+
size: 'sm',
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe('RichEditor & XSS sanitization', () => {
|
|
142
|
+
it('strips dangerous tags and unsafe href protocols', () => {
|
|
143
|
+
const dirty =
|
|
144
|
+
'<p>Hello</p><script>alert(1)</script><iframe src="https://evil.com"></iframe>' +
|
|
145
|
+
'<a href="javascript:alert(1)" onclick="hack()">bad</a>' +
|
|
146
|
+
'<a href="https://example.com">good</a>';
|
|
147
|
+
const clean = sanitizeHtml(dirty);
|
|
148
|
+
expect(clean).not.toContain('<script');
|
|
149
|
+
expect(clean).not.toContain('<iframe');
|
|
150
|
+
expect(clean).not.toContain('javascript:');
|
|
151
|
+
expect(clean).not.toContain('onclick');
|
|
152
|
+
expect(clean).toContain('<p>Hello</p>');
|
|
153
|
+
expect(clean).toContain('<a>bad</a>');
|
|
154
|
+
expect(clean).toContain('href="https://example.com"');
|
|
155
|
+
expect(clean).toContain('rel="noopener noreferrer"');
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('renders sanitized content, supports minHeight/maxHeight, inline link popover, and external sync', () => {
|
|
159
|
+
const onChange = (v: string) => v;
|
|
160
|
+
const { rerender } = render(
|
|
161
|
+
createElement(
|
|
162
|
+
I18nProvider,
|
|
163
|
+
{ locale: 'en', children: createElement(RichEditor, {
|
|
164
|
+
id: 'bio',
|
|
165
|
+
value: '<p>Initial</p><script>xss()</script>',
|
|
166
|
+
set: onChange,
|
|
167
|
+
invalid: false,
|
|
168
|
+
minHeight: 180,
|
|
169
|
+
maxHeight: 420,
|
|
170
|
+
}) },
|
|
171
|
+
),
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
const toolbar = screen.getByRole('toolbar', { name: 'Formatting toolbar' });
|
|
175
|
+
expect(toolbar).toBeTruthy();
|
|
176
|
+
|
|
177
|
+
const editor = screen.getByRole('textbox');
|
|
178
|
+
expect(editor.innerHTML).toBe('<p>Initial</p>');
|
|
179
|
+
expect(editor.style.minHeight).toBe('180px');
|
|
180
|
+
expect(editor.style.maxHeight).toBe('420px');
|
|
181
|
+
|
|
182
|
+
// Focus the editor, then trigger an external value update (e.g. 409 "Use theirs" or reset)
|
|
183
|
+
fireEvent.focus(editor);
|
|
184
|
+
rerender(
|
|
185
|
+
createElement(
|
|
186
|
+
I18nProvider,
|
|
187
|
+
{ locale: 'en', children: createElement(RichEditor, {
|
|
188
|
+
id: 'bio',
|
|
189
|
+
value: '<p>Updated externally</p>',
|
|
190
|
+
set: onChange,
|
|
191
|
+
invalid: false,
|
|
192
|
+
minHeight: 180,
|
|
193
|
+
maxHeight: 420,
|
|
194
|
+
}) },
|
|
195
|
+
),
|
|
196
|
+
);
|
|
197
|
+
expect(editor.innerHTML).toBe('<p>Updated externally</p>');
|
|
198
|
+
|
|
199
|
+
// Open inline link popover without window.prompt
|
|
200
|
+
fireEvent.click(screen.getByRole('button', { name: 'Link' }));
|
|
201
|
+
expect(screen.getByLabelText('Enter URL')).toBeTruthy();
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
describe('filterVisiblePlaces()', () => {
|
|
206
|
+
it('filters hidden fields, respects group visibleWhen, and keeps conflict fields', () => {
|
|
207
|
+
const layout: Place[] = [
|
|
208
|
+
{ field: 'title' },
|
|
209
|
+
{
|
|
210
|
+
type: 'section',
|
|
211
|
+
heading: 'Shipping',
|
|
212
|
+
visibleWhen: [{ field: 'physical' }],
|
|
213
|
+
items: [{ field: 'weight' }, { field: 'hidden_field' }],
|
|
214
|
+
},
|
|
215
|
+
];
|
|
216
|
+
|
|
217
|
+
expect(filterVisiblePlaces(layout, ['title', 'weight'], { physical: 'false' })).toEqual([{ field: 'title' }]);
|
|
218
|
+
|
|
219
|
+
expect(filterVisiblePlaces(layout, ['title', 'weight'], { physical: 'true' })).toEqual([
|
|
220
|
+
{ field: 'title' },
|
|
221
|
+
{
|
|
222
|
+
type: 'section',
|
|
223
|
+
heading: 'Shipping',
|
|
224
|
+
visibleWhen: [{ field: 'physical' }],
|
|
225
|
+
items: [{ field: 'weight' }],
|
|
226
|
+
},
|
|
227
|
+
]);
|
|
228
|
+
|
|
229
|
+
expect(filterVisiblePlaces(layout, ['title', 'weight'], { physical: 'false' }, { weight: '5kg' })).toEqual([
|
|
230
|
+
{ field: 'title' },
|
|
231
|
+
{
|
|
232
|
+
type: 'section',
|
|
233
|
+
heading: 'Shipping',
|
|
234
|
+
visibleWhen: [{ field: 'physical' }],
|
|
235
|
+
items: [{ field: 'weight' }],
|
|
236
|
+
},
|
|
237
|
+
]);
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
describe('I18nProvider StrictMode purity', () => {
|
|
242
|
+
it('updates document attributes in effect and translates via context', () => {
|
|
243
|
+
function Sample() {
|
|
244
|
+
const t = useT();
|
|
245
|
+
return createElement('span', { 'data-testid': 'label' }, t('nav.main'));
|
|
246
|
+
}
|
|
247
|
+
render(
|
|
248
|
+
createElement(
|
|
249
|
+
StrictMode,
|
|
250
|
+
null,
|
|
251
|
+
createElement(I18nProvider, {
|
|
252
|
+
locale: 'ar',
|
|
253
|
+
messages: { 'nav.main': 'الرئيسية' },
|
|
254
|
+
children: createElement(Sample),
|
|
255
|
+
}),
|
|
256
|
+
),
|
|
257
|
+
);
|
|
258
|
+
expect(screen.getByTestId('label').textContent).toBe('الرئيسية');
|
|
259
|
+
expect(document.documentElement.lang).toBe('ar');
|
|
260
|
+
expect(document.documentElement.dir).toBe('rtl');
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
describe('Phase 1 field value conversions', () => {
|
|
265
|
+
it('converts tags, keyvalue, repeater, and slider (single & range) with toForm and toSend', () => {
|
|
266
|
+
const tagsField: Field = { key: 'tags', label: 'Tags', type: 'tags' };
|
|
267
|
+
expect(toForm(tagsField, ['php', 'ts'], () => {})).toEqual(['php', 'ts']);
|
|
268
|
+
expect(toForm(tagsField, null, () => {})).toEqual([]);
|
|
269
|
+
expect(toSend(tagsField, ['php', 'ts'])).toEqual(['php', 'ts']);
|
|
270
|
+
|
|
271
|
+
const kvField: Field = { key: 'meta', label: 'Meta', type: 'keyvalue' };
|
|
272
|
+
expect(toForm(kvField, { env: 'prod', port: 8080 }, () => {})).toEqual({ env: 'prod', port: '8080' });
|
|
273
|
+
expect(toForm(kvField, undefined, () => {})).toEqual({});
|
|
274
|
+
expect(toSend(kvField, { env: 'prod', '': 'ignored' })).toEqual({ env: 'prod' });
|
|
275
|
+
|
|
276
|
+
const repField: Field = {
|
|
277
|
+
key: 'items',
|
|
278
|
+
label: 'Items',
|
|
279
|
+
type: 'repeater',
|
|
280
|
+
look: {
|
|
281
|
+
fields: [
|
|
282
|
+
{ key: 'title', label: 'Title', type: 'text' },
|
|
283
|
+
{ key: 'qty', label: 'Qty', type: 'text', look: { input: 'number' } },
|
|
284
|
+
{ key: 'done', label: 'Done', type: 'toggle' },
|
|
285
|
+
],
|
|
286
|
+
},
|
|
287
|
+
};
|
|
288
|
+
const formed = toForm(repField, [{ id: 9, title: 'Desk', qty: 2, done: true }], () => {});
|
|
289
|
+
expect(formed).toEqual([{ id: 9, title: 'Desk', qty: '2', done: 'true' }]);
|
|
290
|
+
expect(toSend(repField, formed)).toEqual([{ id: 9, title: 'Desk', qty: 2, done: true }]);
|
|
291
|
+
|
|
292
|
+
const singleSlider: Field = { key: 'volume', label: 'Volume', type: 'slider', look: { min: 0, max: 100 } };
|
|
293
|
+
expect(toForm(singleSlider, 45, () => {})).toBe('45');
|
|
294
|
+
expect(toSend(singleSlider, '45')).toBe(45);
|
|
295
|
+
|
|
296
|
+
const rangeSlider: Field = { key: 'price', label: 'Price', type: 'slider', look: { min: 10, max: 200, range: true } };
|
|
297
|
+
expect(toForm(rangeSlider, [20, 150], () => {})).toEqual(['20', '150']);
|
|
298
|
+
expect(toSend(rangeSlider, ['20', '150'])).toEqual([20, 150]);
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
describe('TagsInput', () => {
|
|
303
|
+
it('supports Enter, Tab, Backspace, comma split, suggestions, duplicate prevention, and maxItems', () => {
|
|
304
|
+
const onChange = vi.fn();
|
|
305
|
+
const { rerender } = render(
|
|
306
|
+
createElement(I18nProvider, {
|
|
307
|
+
locale: 'en',
|
|
308
|
+
children: createElement(TagsInput, {
|
|
309
|
+
id: 'tags',
|
|
310
|
+
value: ['alpha'],
|
|
311
|
+
set: onChange,
|
|
312
|
+
separator: ',',
|
|
313
|
+
suggestions: ['alpha', 'beta', 'gamma'],
|
|
314
|
+
maxItems: 3,
|
|
315
|
+
}),
|
|
316
|
+
}),
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
const input = screen.getByRole('textbox');
|
|
320
|
+
// Duplicate is ignored
|
|
321
|
+
fireEvent.change(input, { target: { value: 'alpha' } });
|
|
322
|
+
fireEvent.keyDown(input, { key: 'Enter' });
|
|
323
|
+
expect(onChange).not.toHaveBeenCalled();
|
|
324
|
+
|
|
325
|
+
// Comma separator splits multiple tags
|
|
326
|
+
fireEvent.change(input, { target: { value: 'beta, gamma, delta' } });
|
|
327
|
+
expect(onChange).toHaveBeenLastCalledWith(['alpha', 'beta', 'gamma']);
|
|
328
|
+
|
|
329
|
+
// Tab adds pending tag
|
|
330
|
+
fireEvent.change(input, { target: { value: 'beta' } });
|
|
331
|
+
fireEvent.keyDown(input, { key: 'Tab' });
|
|
332
|
+
expect(onChange).toHaveBeenLastCalledWith(['alpha', 'beta']);
|
|
333
|
+
|
|
334
|
+
// Backspace on empty input removes last tag
|
|
335
|
+
rerender(
|
|
336
|
+
createElement(I18nProvider, {
|
|
337
|
+
locale: 'en',
|
|
338
|
+
children: createElement(TagsInput, {
|
|
339
|
+
id: 'tags',
|
|
340
|
+
value: ['alpha', 'beta'],
|
|
341
|
+
set: onChange,
|
|
342
|
+
suggestions: ['gamma'],
|
|
343
|
+
}),
|
|
344
|
+
}),
|
|
345
|
+
);
|
|
346
|
+
const input2 = screen.getByRole('textbox');
|
|
347
|
+
fireEvent.keyDown(input2, { key: 'Backspace' });
|
|
348
|
+
expect(onChange).toHaveBeenLastCalledWith(['alpha']);
|
|
349
|
+
|
|
350
|
+
// Remove button works
|
|
351
|
+
fireEvent.click(screen.getByRole('button', { name: 'Remove alpha' }));
|
|
352
|
+
expect(onChange).toHaveBeenLastCalledWith(['beta']);
|
|
353
|
+
});
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
describe('Select chip ordering', () => {
|
|
357
|
+
it('reorders selected values by dragging one chip onto another', () => {
|
|
358
|
+
const onChange = vi.fn();
|
|
359
|
+
const transfer = { effectAllowed: 'none', dropEffect: 'none', setData: vi.fn(), getData: vi.fn(() => 'washed') };
|
|
360
|
+
render(createElement(I18nProvider, { locale: 'en', children: createElement(Select, {
|
|
361
|
+
value: ['single-origin', 'washed', 'micro-lot'], onChange, options: [], settings: { multiple: true }, chosen: (v: string) => v,
|
|
362
|
+
}) }));
|
|
363
|
+
const chips = document.querySelectorAll('[data-lk="chip"]');
|
|
364
|
+
fireEvent.dragStart(chips[0], { dataTransfer: transfer });
|
|
365
|
+
fireEvent.dragOver(chips[2], { dataTransfer: transfer });
|
|
366
|
+
fireEvent.drop(chips[2], { dataTransfer: transfer });
|
|
367
|
+
expect(onChange).toHaveBeenCalledWith(['washed', 'micro-lot', 'single-origin']);
|
|
368
|
+
});
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
describe('KeyValue', () => {
|
|
372
|
+
it('supports adding, editing, reordering, deleting, and respects editableKeys/editableValues', () => {
|
|
373
|
+
const onChange = vi.fn();
|
|
374
|
+
render(
|
|
375
|
+
createElement(I18nProvider, {
|
|
376
|
+
locale: 'en',
|
|
377
|
+
children: createElement(KeyValue, {
|
|
378
|
+
id: 'kv',
|
|
379
|
+
value: { host: 'localhost', port: '5432' },
|
|
380
|
+
set: onChange,
|
|
381
|
+
keyLabel: 'Header',
|
|
382
|
+
valueLabel: 'Value',
|
|
383
|
+
reorderable: true,
|
|
384
|
+
}),
|
|
385
|
+
}),
|
|
386
|
+
);
|
|
387
|
+
|
|
388
|
+
expect(screen.getByText('Header')).toBeTruthy();
|
|
389
|
+
// Move first row down
|
|
390
|
+
const downButtons = screen.getAllByRole('button', { name: 'Move down' });
|
|
391
|
+
fireEvent.click(downButtons[0]);
|
|
392
|
+
expect(Object.keys(onChange.mock.calls.at(-1)![0])).toEqual(['port', 'host']);
|
|
393
|
+
|
|
394
|
+
// Add a row and type key
|
|
395
|
+
fireEvent.click(screen.getByRole('button', { name: 'Add row' }));
|
|
396
|
+
const newKey = screen.getByLabelText('Header 3');
|
|
397
|
+
fireEvent.change(newKey, { target: { value: 'ssl' } });
|
|
398
|
+
expect(onChange.mock.calls.at(-1)![0]).toEqual({ port: '5432', host: 'localhost', ssl: '' });
|
|
399
|
+
|
|
400
|
+
// Respects editableKeys=false & editableValues=false
|
|
401
|
+
cleanup();
|
|
402
|
+
render(
|
|
403
|
+
createElement(I18nProvider, {
|
|
404
|
+
locale: 'en',
|
|
405
|
+
children: createElement(KeyValue, {
|
|
406
|
+
id: 'kv2',
|
|
407
|
+
value: { token: 'secret' },
|
|
408
|
+
set: vi.fn(),
|
|
409
|
+
editableKeys: false,
|
|
410
|
+
editableValues: false,
|
|
411
|
+
}),
|
|
412
|
+
}),
|
|
413
|
+
);
|
|
414
|
+
expect((screen.getByLabelText('Key 1') as HTMLInputElement).readOnly).toBe(true);
|
|
415
|
+
expect((screen.getByLabelText('Value 1') as HTMLInputElement).readOnly).toBe(true);
|
|
416
|
+
expect(screen.queryByRole('button', { name: 'Add row' })).toBeNull();
|
|
417
|
+
});
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
describe('Repeater', () => {
|
|
421
|
+
it('renders card mode (collapse, clone, reorder, add/remove) and table mode', () => {
|
|
422
|
+
const onChange = vi.fn();
|
|
423
|
+
const repeaterField: Field = {
|
|
424
|
+
key: 'contacts',
|
|
425
|
+
label: 'Contacts',
|
|
426
|
+
type: 'repeater',
|
|
427
|
+
look: {
|
|
428
|
+
itemLabel: 'name',
|
|
429
|
+
collapsible: true,
|
|
430
|
+
cloneable: true,
|
|
431
|
+
reorderable: true,
|
|
432
|
+
minItems: 1,
|
|
433
|
+
maxItems: 3,
|
|
434
|
+
addLabel: 'Add contact',
|
|
435
|
+
fields: [
|
|
436
|
+
{ key: 'name', label: 'Name', type: 'text' },
|
|
437
|
+
{ key: 'email', label: 'Email', type: 'text' },
|
|
438
|
+
],
|
|
439
|
+
},
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
const { rerender } = render(
|
|
443
|
+
createElement(I18nProvider, {
|
|
444
|
+
locale: 'en',
|
|
445
|
+
children: createElement(FieldView, {
|
|
446
|
+
id: 'f-contacts',
|
|
447
|
+
errorId: 'e-contacts',
|
|
448
|
+
url: '',
|
|
449
|
+
field: repeaterField,
|
|
450
|
+
value: [{ name: 'Ada', email: 'ada@example.com' }],
|
|
451
|
+
values: {} as Vals,
|
|
452
|
+
onKnown: () => {},
|
|
453
|
+
set: onChange,
|
|
454
|
+
}),
|
|
455
|
+
}),
|
|
456
|
+
);
|
|
457
|
+
|
|
458
|
+
expect(screen.getByText('Ada')).toBeTruthy();
|
|
459
|
+
// Min items = 1 prevents removing the only item
|
|
460
|
+
expect(screen.queryByRole('button', { name: 'Remove Ada' })).toBeNull();
|
|
461
|
+
|
|
462
|
+
// Clone item
|
|
463
|
+
fireEvent.click(screen.getByRole('button', { name: 'Duplicate' }));
|
|
464
|
+
expect(onChange).toHaveBeenLastCalledWith([
|
|
465
|
+
{ name: 'Ada', email: 'ada@example.com' },
|
|
466
|
+
{ name: 'Ada', email: 'ada@example.com' },
|
|
467
|
+
]);
|
|
468
|
+
|
|
469
|
+
// Collapse item
|
|
470
|
+
fireEvent.click(screen.getByRole('button', { name: 'Ada' }));
|
|
471
|
+
expect(screen.queryByLabelText('Email')).toBeNull();
|
|
472
|
+
|
|
473
|
+
// Table mode
|
|
474
|
+
const tableField: Field = {
|
|
475
|
+
...repeaterField,
|
|
476
|
+
look: { ...repeaterField.look, table: true },
|
|
477
|
+
};
|
|
478
|
+
rerender(
|
|
479
|
+
createElement(I18nProvider, {
|
|
480
|
+
locale: 'en',
|
|
481
|
+
children: createElement(FieldView, {
|
|
482
|
+
id: 'f-contacts',
|
|
483
|
+
errorId: 'e-contacts',
|
|
484
|
+
url: '',
|
|
485
|
+
field: tableField,
|
|
486
|
+
value: [{ name: 'Grace', email: 'grace@example.com' }],
|
|
487
|
+
values: {} as Vals,
|
|
488
|
+
onKnown: () => {},
|
|
489
|
+
set: onChange,
|
|
490
|
+
}),
|
|
491
|
+
}),
|
|
492
|
+
);
|
|
493
|
+
expect(document.querySelector('[data-lk="repeater"][data-mode="table"]')).toBeTruthy();
|
|
494
|
+
expect(screen.getByRole('columnheader', { name: 'Name' })).toBeTruthy();
|
|
495
|
+
expect(screen.getByRole('columnheader', { name: 'Email' })).toBeTruthy();
|
|
496
|
+
});
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
describe('Slider', () => {
|
|
500
|
+
it('keeps the active handle mounted as values change in a grouped page form', async () => {
|
|
501
|
+
mockFetchJson(() => ({ body: { values: { price: [20, 70] } } }));
|
|
502
|
+
render(createElement(PageForm, {
|
|
503
|
+
page: 'settings',
|
|
504
|
+
fields: [{ key: 'price', label: 'Price', type: 'slider', look: { min: 10, max: 100, step: 10, range: true } }],
|
|
505
|
+
items: [{ type: 'section', heading: 'Prices', items: [{ field: 'price' }] }],
|
|
506
|
+
canSave: true,
|
|
507
|
+
}));
|
|
508
|
+
const high = await screen.findByRole('slider', { name: 'Price (Maximum)' });
|
|
509
|
+
high.focus();
|
|
510
|
+
|
|
511
|
+
for (const value of ['80', '90', '100']) {
|
|
512
|
+
fireEvent.change(high, { target: { value } });
|
|
513
|
+
expect(screen.getByRole('slider', { name: 'Price (Maximum)' })).toBe(high);
|
|
514
|
+
expect(document.activeElement).toBe(high);
|
|
515
|
+
expect(screen.getByText(`20 – ${value}`)).toBeTruthy();
|
|
516
|
+
}
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
it('supports single handle and dual-handle range mode with marks', () => {
|
|
520
|
+
const onSingle = vi.fn();
|
|
521
|
+
render(
|
|
522
|
+
createElement(I18nProvider, {
|
|
523
|
+
locale: 'en',
|
|
524
|
+
children: createElement(Slider, {
|
|
525
|
+
id: 'vol',
|
|
526
|
+
label: 'Volume',
|
|
527
|
+
value: '30',
|
|
528
|
+
set: onSingle,
|
|
529
|
+
min: 0,
|
|
530
|
+
max: 100,
|
|
531
|
+
step: 10,
|
|
532
|
+
marks: true,
|
|
533
|
+
suffix: '%',
|
|
534
|
+
}),
|
|
535
|
+
}),
|
|
536
|
+
);
|
|
537
|
+
const singleInput = screen.getByRole('slider') as HTMLInputElement;
|
|
538
|
+
expect(singleInput.value).toBe('30');
|
|
539
|
+
expect(screen.getByText('30%')).toBeTruthy();
|
|
540
|
+
const mark20 = Array.from(document.querySelectorAll('.slider-marks span')).find((el) => el.textContent === '20') as HTMLElement;
|
|
541
|
+
const mark50 = Array.from(document.querySelectorAll('.slider-marks span')).find((el) => el.textContent === '50') as HTMLElement;
|
|
542
|
+
expect(mark20).toBeTruthy();
|
|
543
|
+
expect(mark50).toBeTruthy();
|
|
544
|
+
expect(mark20.style.insetInlineStart).not.toBe('');
|
|
545
|
+
expect(mark50.style.insetInlineStart).toContain('50%');
|
|
546
|
+
fireEvent.change(singleInput, { target: { value: '60' } });
|
|
547
|
+
expect(onSingle).toHaveBeenCalledWith('60');
|
|
548
|
+
|
|
549
|
+
cleanup();
|
|
550
|
+
const onRange = vi.fn();
|
|
551
|
+
render(
|
|
552
|
+
createElement(I18nProvider, {
|
|
553
|
+
locale: 'en',
|
|
554
|
+
children: createElement(Slider, {
|
|
555
|
+
id: 'price',
|
|
556
|
+
label: 'Price',
|
|
557
|
+
value: ['20', '80'],
|
|
558
|
+
set: onRange,
|
|
559
|
+
min: 0,
|
|
560
|
+
max: 100,
|
|
561
|
+
range: true,
|
|
562
|
+
prefix: '$',
|
|
563
|
+
}),
|
|
564
|
+
}),
|
|
565
|
+
);
|
|
566
|
+
const lo = screen.getByLabelText('Price (Minimum)') as HTMLInputElement;
|
|
567
|
+
const hi = screen.getByLabelText('Price (Maximum)') as HTMLInputElement;
|
|
568
|
+
expect(screen.getByText('$20 – $80')).toBeTruthy();
|
|
569
|
+
fireEvent.change(lo, { target: { value: '90' } });
|
|
570
|
+
// Clamped to high (80)
|
|
571
|
+
expect(onRange).toHaveBeenLastCalledWith(['80', '80']);
|
|
572
|
+
fireEvent.change(hi, { target: { value: '50' } });
|
|
573
|
+
expect(onRange).toHaveBeenLastCalledWith(['20', '50']);
|
|
574
|
+
});
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
describe('MarkdownEditor & XSS neutralization', () => {
|
|
578
|
+
it('escapes raw HTML, neutralizes unsafe link schemes, and toggles Write/Preview', () => {
|
|
579
|
+
const dirty = [
|
|
580
|
+
'# Title',
|
|
581
|
+
'<script>alert(1)</script>',
|
|
582
|
+
'<img src=x onerror=alert(1)>',
|
|
583
|
+
'[bad](javascript:alert(1))',
|
|
584
|
+
'[break](https://example.com" onclick="alert(1))',
|
|
585
|
+
'[good](https://example.com)',
|
|
586
|
+
'[local](/docs)',
|
|
587
|
+
].join('\n\n');
|
|
588
|
+
|
|
589
|
+
const html = markdownToHtml(dirty);
|
|
590
|
+
expect(html).not.toContain('<script');
|
|
591
|
+
expect(html).not.toContain('<img');
|
|
592
|
+
expect(html).not.toContain('javascript:');
|
|
593
|
+
expect(html).not.toContain('onclick=');
|
|
594
|
+
expect(html).toContain('<script>alert(1)</script>');
|
|
595
|
+
expect(html).toContain('<h2>Title</h2>');
|
|
596
|
+
expect(html).toContain('<a href="https://example.com" target="_blank" rel="noopener noreferrer">good</a>');
|
|
597
|
+
expect(html).toContain('<a href="/docs">local</a>');
|
|
598
|
+
|
|
599
|
+
const onChange = vi.fn();
|
|
600
|
+
render(
|
|
601
|
+
createElement(I18nProvider, {
|
|
602
|
+
locale: 'en',
|
|
603
|
+
children: createElement(MarkdownEditor, {
|
|
604
|
+
id: 'md',
|
|
605
|
+
value: '**Hello** <script>xss()</script>',
|
|
606
|
+
set: onChange,
|
|
607
|
+
minHeight: 200,
|
|
608
|
+
}),
|
|
609
|
+
}),
|
|
610
|
+
);
|
|
611
|
+
|
|
612
|
+
fireEvent.click(screen.getByRole('tab', { name: 'Preview' }));
|
|
613
|
+
const preview = document.querySelector('[data-lk="markdown-preview"]')!;
|
|
614
|
+
expect(preview.innerHTML).toContain('<strong>Hello</strong>');
|
|
615
|
+
expect(preview.innerHTML).not.toContain('<script>');
|
|
616
|
+
});
|
|
617
|
+
});
|
|
618
|
+
|
|
619
|
+
describe('CodeEditor & OneTimeCode', () => {
|
|
620
|
+
it('CodeEditor indents on Tab and toggles line wrap', () => {
|
|
621
|
+
const onChange = vi.fn();
|
|
622
|
+
render(
|
|
623
|
+
createElement(I18nProvider, {
|
|
624
|
+
locale: 'en',
|
|
625
|
+
children: createElement(CodeEditor, {
|
|
626
|
+
id: 'code',
|
|
627
|
+
value: 'const x = 1;',
|
|
628
|
+
set: onChange,
|
|
629
|
+
language: 'typescript',
|
|
630
|
+
}),
|
|
631
|
+
}),
|
|
632
|
+
);
|
|
633
|
+
|
|
634
|
+
expect(screen.getByText('typescript')).toBeTruthy();
|
|
635
|
+
const textarea = screen.getByRole('textbox') as HTMLTextAreaElement;
|
|
636
|
+
textarea.selectionStart = 0;
|
|
637
|
+
textarea.selectionEnd = 0;
|
|
638
|
+
fireEvent.keyDown(textarea, { key: 'Tab' });
|
|
639
|
+
expect(onChange).toHaveBeenCalledWith(' const x = 1;');
|
|
640
|
+
|
|
641
|
+
const wrapBtn = screen.getByRole('button', { name: 'Wrap lines' });
|
|
642
|
+
expect(wrapBtn.getAttribute('aria-pressed')).toBe('false');
|
|
643
|
+
fireEvent.click(wrapBtn);
|
|
644
|
+
expect(wrapBtn.getAttribute('aria-pressed')).toBe('true');
|
|
645
|
+
});
|
|
646
|
+
|
|
647
|
+
it('OneTimeCode handles paste, arrow navigation, backspace jump, numeric mode, and submitOnCompletion', () => {
|
|
648
|
+
const onChange = vi.fn();
|
|
649
|
+
const onSubmit = vi.fn((e: Event) => e.preventDefault());
|
|
650
|
+
render(
|
|
651
|
+
createElement(I18nProvider, {
|
|
652
|
+
locale: 'en',
|
|
653
|
+
children: createElement(
|
|
654
|
+
'form',
|
|
655
|
+
{ onSubmit },
|
|
656
|
+
createElement(OneTimeCode, {
|
|
657
|
+
id: 'otp',
|
|
658
|
+
value: '',
|
|
659
|
+
set: onChange,
|
|
660
|
+
length: 6,
|
|
661
|
+
numeric: true,
|
|
662
|
+
submitOnCompletion: true,
|
|
663
|
+
}),
|
|
664
|
+
),
|
|
665
|
+
}),
|
|
666
|
+
);
|
|
667
|
+
|
|
668
|
+
const d1 = screen.getByLabelText('Digit 1 of 6') as HTMLInputElement;
|
|
669
|
+
const d2 = screen.getByLabelText('Digit 2 of 6') as HTMLInputElement;
|
|
670
|
+
expect(d1.inputMode).toBe('numeric');
|
|
671
|
+
|
|
672
|
+
// Paste full code with non-digits filtered out
|
|
673
|
+
fireEvent.paste(d1, { clipboardData: { getData: () => '12a34b56' } });
|
|
674
|
+
expect(onChange).toHaveBeenLastCalledWith('123456');
|
|
675
|
+
expect(onSubmit).toHaveBeenCalledTimes(1);
|
|
676
|
+
|
|
677
|
+
// Arrow navigation and backspace jump back
|
|
678
|
+
d2.focus();
|
|
679
|
+
fireEvent.keyDown(d2, { key: 'ArrowLeft' });
|
|
680
|
+
expect(document.activeElement).toBe(d1);
|
|
681
|
+
fireEvent.keyDown(d2, { key: 'Backspace' });
|
|
682
|
+
expect(onChange).toHaveBeenLastCalledWith('13456');
|
|
683
|
+
});
|
|
684
|
+
});
|
|
685
|
+
|
|
686
|
+
describe('Schema layout groups (Callout, Wizard, Section/Tab icons & badges) and ToggleButtons', () => {
|
|
687
|
+
it('renders Callout, Wizard with error auto-jump, Section/Tab badges, and fullWidth ToggleButtons', () => {
|
|
688
|
+
const fields: Record<string, Field> = {
|
|
689
|
+
step1_name: { key: 'step1_name', label: 'Name', type: 'text' },
|
|
690
|
+
step2_code: { key: 'step2_code', label: 'Code', type: 'text' },
|
|
691
|
+
mode: {
|
|
692
|
+
key: 'mode',
|
|
693
|
+
label: 'Mode',
|
|
694
|
+
type: 'buttons',
|
|
695
|
+
look: { fullWidth: true },
|
|
696
|
+
options: [
|
|
697
|
+
{ value: 'fast', label: 'Fast', icon: 'check' },
|
|
698
|
+
{ value: 'safe', label: 'Safe', icon: 'shield' },
|
|
699
|
+
],
|
|
700
|
+
},
|
|
701
|
+
};
|
|
702
|
+
const vals: Vals = { step1_name: 'Ada', step2_code: '', mode: 'fast' };
|
|
703
|
+
|
|
704
|
+
const layoutItems: Place[] = [
|
|
705
|
+
{
|
|
706
|
+
type: 'callout',
|
|
707
|
+
heading: 'Notice',
|
|
708
|
+
description: 'Check settings carefully.',
|
|
709
|
+
icon: 'info',
|
|
710
|
+
color: 'warning',
|
|
711
|
+
items: [],
|
|
712
|
+
},
|
|
713
|
+
{
|
|
714
|
+
type: 'section',
|
|
715
|
+
heading: 'Options',
|
|
716
|
+
icon: 'gear',
|
|
717
|
+
badge: 'Beta',
|
|
718
|
+
badgeColor: 'info',
|
|
719
|
+
items: [{ field: 'mode' }],
|
|
720
|
+
},
|
|
721
|
+
{
|
|
722
|
+
type: 'wizard',
|
|
723
|
+
skippable: false,
|
|
724
|
+
items: [
|
|
725
|
+
{ type: 'step', heading: 'Account', badge: '1', items: [{ field: 'step1_name' }] },
|
|
726
|
+
{ type: 'step', heading: 'Security', items: [{ field: 'step2_code' }] },
|
|
727
|
+
],
|
|
728
|
+
},
|
|
729
|
+
];
|
|
730
|
+
|
|
731
|
+
const Draw = ({ name }: { name: string }) =>
|
|
732
|
+
createElement(FieldView, {
|
|
733
|
+
id: 'f-' + name,
|
|
734
|
+
errorId: 'e-' + name,
|
|
735
|
+
url: '',
|
|
736
|
+
field: fields[name],
|
|
737
|
+
value: vals[name],
|
|
738
|
+
values: vals,
|
|
739
|
+
onKnown: () => {},
|
|
740
|
+
set: () => {},
|
|
741
|
+
});
|
|
742
|
+
|
|
743
|
+
const { rerender } = render(
|
|
744
|
+
createElement(I18nProvider, {
|
|
745
|
+
locale: 'en',
|
|
746
|
+
children: createElement(Fields, {
|
|
747
|
+
resource: 'users',
|
|
748
|
+
id: null,
|
|
749
|
+
fields: Object.values(fields).map(({ key, label, type }) => ({ key, label, type })),
|
|
750
|
+
Field: Draw,
|
|
751
|
+
layout: { columns: 1, items: layoutItems, spans: {}, wrong: [] },
|
|
752
|
+
}),
|
|
753
|
+
}),
|
|
754
|
+
);
|
|
755
|
+
|
|
756
|
+
const callout = document.querySelector('[data-lk="callout"]')!;
|
|
757
|
+
expect(callout.getAttribute('data-color')).toBe('warning');
|
|
758
|
+
expect(screen.getByText('Check settings carefully.')).toBeTruthy();
|
|
759
|
+
expect(screen.getByText('Beta')).toBeTruthy();
|
|
760
|
+
|
|
761
|
+
// Full width button has flex-1
|
|
762
|
+
const fastBtn = screen.getByText('Fast').closest('label')!;
|
|
763
|
+
expect(fastBtn.className).toContain('flex-1');
|
|
764
|
+
|
|
765
|
+
// Wizard starts on step 1; step 2 is disabled when not skippable
|
|
766
|
+
const stepButtons = document.querySelectorAll('[data-lk="wizard-step"]');
|
|
767
|
+
expect(stepButtons[0].getAttribute('aria-current')).toBe('step');
|
|
768
|
+
expect((stepButtons[1] as HTMLButtonElement).disabled).toBe(true);
|
|
769
|
+
|
|
770
|
+
// Validation error on step 2 auto-jumps wizard to step 2
|
|
771
|
+
rerender(
|
|
772
|
+
createElement(I18nProvider, {
|
|
773
|
+
locale: 'en',
|
|
774
|
+
children: createElement(Fields, {
|
|
775
|
+
resource: 'users',
|
|
776
|
+
id: null,
|
|
777
|
+
fields: Object.values(fields).map(({ key, label, type }) => ({ key, label, type })),
|
|
778
|
+
Field: Draw,
|
|
779
|
+
layout: { columns: 1, items: layoutItems, spans: {}, wrong: ['step2_code'] },
|
|
780
|
+
}),
|
|
781
|
+
}),
|
|
782
|
+
);
|
|
783
|
+
expect(document.querySelectorAll('[data-lk="wizard-step"]')[1].getAttribute('aria-current')).toBe('step');
|
|
784
|
+
});
|
|
785
|
+
});
|
|
786
|
+
|
|
787
|
+
function ensureDialogPolyfill() {
|
|
788
|
+
if (typeof HTMLDialogElement !== 'undefined' && !HTMLDialogElement.prototype.showModal) {
|
|
789
|
+
HTMLDialogElement.prototype.showModal = function showModal() {
|
|
790
|
+
this.setAttribute('open', '');
|
|
791
|
+
};
|
|
792
|
+
HTMLDialogElement.prototype.close = function close() {
|
|
793
|
+
this.removeAttribute('open');
|
|
794
|
+
this.dispatchEvent(new Event('close'));
|
|
795
|
+
};
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
function mockFetchJson(handler: (url: string, init?: RequestInit) => { ok?: boolean; status?: number; body: unknown } | Promise<{ ok?: boolean; status?: number; body: unknown }>) {
|
|
800
|
+
return vi.spyOn(globalThis, 'fetch').mockImplementation(async (input, init) => {
|
|
801
|
+
const url = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url;
|
|
802
|
+
const res = await handler(url, init);
|
|
803
|
+
const status = res.status ?? (res.ok === false ? 500 : 200);
|
|
804
|
+
const ok = res.ok ?? (status >= 200 && status < 300);
|
|
805
|
+
return {
|
|
806
|
+
ok,
|
|
807
|
+
status,
|
|
808
|
+
redirected: false,
|
|
809
|
+
headers: new Headers({ 'content-type': 'application/json' }),
|
|
810
|
+
json: async () => res.body,
|
|
811
|
+
} as Response;
|
|
812
|
+
});
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
describe('Phase 2 table power features', () => {
|
|
816
|
+
it('renders Table Tabs with icons, colors, and live counts (including 0) and resets page on switch', async () => {
|
|
817
|
+
const urls: string[] = [];
|
|
818
|
+
mockFetchJson((url) => {
|
|
819
|
+
urls.push(url);
|
|
820
|
+
const u = new URL(url, 'http://localhost');
|
|
821
|
+
const tab = u.searchParams.get('tab') ?? 'all';
|
|
822
|
+
const pageNum = Number(u.searchParams.get('page') ?? '1');
|
|
823
|
+
const body: Page = {
|
|
824
|
+
data: [{ id: tab === 'pending' ? '2' : '1', cells: { name: tab === 'pending' ? 'Pending Row' : 'All Row' }, can: { update: false, actions: [] } }],
|
|
825
|
+
total: tab === 'pending' ? 0 : 30,
|
|
826
|
+
page: pageNum,
|
|
827
|
+
lastPage: tab === 'pending' ? 1 : 2,
|
|
828
|
+
tabCounts: { all: 30, pending: 0 },
|
|
829
|
+
};
|
|
830
|
+
return { body };
|
|
831
|
+
});
|
|
832
|
+
|
|
833
|
+
const resource: Resource = {
|
|
834
|
+
name: 'orders',
|
|
835
|
+
label: 'Orders',
|
|
836
|
+
columns: [{ key: 'name', label: 'Name', type: 'text', sortable: true, searchable: false }],
|
|
837
|
+
fields: [],
|
|
838
|
+
actions: [],
|
|
839
|
+
canCreate: false,
|
|
840
|
+
perPage: 15,
|
|
841
|
+
perPages: [15],
|
|
842
|
+
tabs: [
|
|
843
|
+
{ name: 'all', label: 'All', icon: 'list', color: 'primary' },
|
|
844
|
+
{ name: 'pending', label: 'Pending', icon: 'clock', color: 'warning' },
|
|
845
|
+
],
|
|
846
|
+
};
|
|
847
|
+
|
|
848
|
+
render(createElement(I18nProvider, { locale: 'en', children: createElement(Table, { resource, form: 'dialog' }) }));
|
|
849
|
+
|
|
850
|
+
await waitFor(() => expect(screen.getByText('All Row')).toBeTruthy());
|
|
851
|
+
const tablist = document.querySelector('[data-lk="table-tabs"]')!;
|
|
852
|
+
expect(tablist).toBeTruthy();
|
|
853
|
+
const tabs = document.querySelectorAll('[data-lk="table-tab"]');
|
|
854
|
+
expect(tabs.length).toBe(2);
|
|
855
|
+
expect(tabs[0].getAttribute('aria-selected')).toBe('true');
|
|
856
|
+
expect(tabs[0].getAttribute('data-color')).toBe('primary');
|
|
857
|
+
expect(tabs[0].textContent).toContain('30');
|
|
858
|
+
expect(tabs[1].textContent).toContain('0');
|
|
859
|
+
|
|
860
|
+
// Go to page 2 first
|
|
861
|
+
fireEvent.click(document.querySelector('[data-lk="next"]')!);
|
|
862
|
+
await waitFor(() => expect(urls.some((u) => u.includes('page=2'))).toBe(true));
|
|
863
|
+
|
|
864
|
+
// Switch tab -> resets page to 1 and queries ?tab=pending
|
|
865
|
+
fireEvent.click(tabs[1]);
|
|
866
|
+
await waitFor(() => expect(screen.getByText('Pending Row')).toBeTruthy());
|
|
867
|
+
const lastUrl = urls[urls.length - 1];
|
|
868
|
+
expect(lastUrl).toContain('tab=pending');
|
|
869
|
+
expect(lastUrl).toContain('page=1');
|
|
870
|
+
expect(tabs[1].getAttribute('aria-selected')).toBe('true');
|
|
871
|
+
expect(tabs[1].getAttribute('data-color')).toBe('warning');
|
|
872
|
+
});
|
|
873
|
+
|
|
874
|
+
it('renders Summaries <tfoot> formatted via shown() and marks hiddenLabel as sr-only', () => {
|
|
875
|
+
render(
|
|
876
|
+
createElement(I18nProvider, {
|
|
877
|
+
locale: 'en',
|
|
878
|
+
children: createElement(Rows, {
|
|
879
|
+
resource: 'orders',
|
|
880
|
+
columns: [
|
|
881
|
+
{ key: 'title', label: 'Title', type: 'text', sortable: false },
|
|
882
|
+
{
|
|
883
|
+
key: 'total',
|
|
884
|
+
label: 'Total',
|
|
885
|
+
type: 'text',
|
|
886
|
+
sortable: false,
|
|
887
|
+
align: 'end',
|
|
888
|
+
look: { format: 'money', currency: 'USD', cents: true },
|
|
889
|
+
summaries: [
|
|
890
|
+
{ type: 'sum', label: 'Sum' },
|
|
891
|
+
{ type: 'avg', label: 'Average', hiddenLabel: true },
|
|
892
|
+
],
|
|
893
|
+
},
|
|
894
|
+
],
|
|
895
|
+
rows: [{ id: '1', cells: { title: 'Order #1', total: 12550 }, can: { update: false, actions: [] } }],
|
|
896
|
+
loading: false,
|
|
897
|
+
actions: [],
|
|
898
|
+
sort: '',
|
|
899
|
+
onSort: () => {},
|
|
900
|
+
onEdit: () => {},
|
|
901
|
+
onAction: () => {},
|
|
902
|
+
Cell: ({ column, row }) => createElement('span', null, String(row.cells[column] ?? '')),
|
|
903
|
+
selected: [],
|
|
904
|
+
onSelect: () => {},
|
|
905
|
+
empty: { heading: 'Empty', description: '' },
|
|
906
|
+
striped: false,
|
|
907
|
+
summaries: {
|
|
908
|
+
total: [
|
|
909
|
+
{ value: 12550, label: 'Sum' },
|
|
910
|
+
{ value: 6275, label: 'Average', hiddenLabel: true },
|
|
911
|
+
],
|
|
912
|
+
},
|
|
913
|
+
}),
|
|
914
|
+
}),
|
|
915
|
+
);
|
|
916
|
+
|
|
917
|
+
const tfoot = document.querySelector('tfoot[data-lk="table-summaries"]')!;
|
|
918
|
+
expect(tfoot).toBeTruthy();
|
|
919
|
+
const rows = tfoot.querySelectorAll('tr');
|
|
920
|
+
expect(rows.length).toBe(2);
|
|
921
|
+
expect(rows[0].textContent).toContain('Sum');
|
|
922
|
+
expect(rows[0].textContent).toContain('$125.50');
|
|
923
|
+
expect(rows[1].textContent).toContain('$62.75');
|
|
924
|
+
const avgLabel = rows[1].querySelector('[data-lk="summary-label"]')!;
|
|
925
|
+
expect(avgLabel.className).toContain('sr-only');
|
|
926
|
+
});
|
|
927
|
+
|
|
928
|
+
it('renders protocol-shape object Summaries ({ column: { kind: value } }) with flat SummarySpec formatting', () => {
|
|
929
|
+
render(
|
|
930
|
+
createElement(I18nProvider, {
|
|
931
|
+
locale: 'en',
|
|
932
|
+
children: createElement(Rows, {
|
|
933
|
+
resource: 'orders',
|
|
934
|
+
columns: [
|
|
935
|
+
{
|
|
936
|
+
key: 'number',
|
|
937
|
+
label: 'Order #',
|
|
938
|
+
type: 'text',
|
|
939
|
+
sortable: false,
|
|
940
|
+
summaries: [{ kind: 'count', label: 'Orders' }],
|
|
941
|
+
},
|
|
942
|
+
{
|
|
943
|
+
key: 'total',
|
|
944
|
+
label: 'Total',
|
|
945
|
+
type: 'text',
|
|
946
|
+
sortable: false,
|
|
947
|
+
align: 'end',
|
|
948
|
+
look: { format: 'money', currency: 'USD' },
|
|
949
|
+
summaries: [
|
|
950
|
+
{ kind: 'sum', label: 'Total', format: 'money', currency: 'USD' },
|
|
951
|
+
{ kind: 'avg', label: 'Average', format: 'money', currency: 'USD' },
|
|
952
|
+
],
|
|
953
|
+
},
|
|
954
|
+
],
|
|
955
|
+
rows: [{ id: '1', cells: { number: 'ORD-1', total: 134.33 }, can: { update: false, actions: [] } }],
|
|
956
|
+
loading: false,
|
|
957
|
+
actions: [],
|
|
958
|
+
sort: '',
|
|
959
|
+
onSort: () => {},
|
|
960
|
+
onEdit: () => {},
|
|
961
|
+
onAction: () => {},
|
|
962
|
+
Cell: ({ column, row }) => createElement('span', null, String(row.cells[column] ?? '')),
|
|
963
|
+
selected: [],
|
|
964
|
+
onSelect: () => {},
|
|
965
|
+
empty: { heading: 'Empty', description: '' },
|
|
966
|
+
striped: false,
|
|
967
|
+
summaries: {
|
|
968
|
+
number: { count: 1578 },
|
|
969
|
+
total: { sum: 211970.3, avg: 134.33 },
|
|
970
|
+
},
|
|
971
|
+
}),
|
|
972
|
+
}),
|
|
973
|
+
);
|
|
974
|
+
|
|
975
|
+
const tfoot = document.querySelector('tfoot[data-lk="table-summaries"]')!;
|
|
976
|
+
expect(tfoot).toBeTruthy();
|
|
977
|
+
const rows = tfoot.querySelectorAll('tr');
|
|
978
|
+
expect(rows.length).toBe(2);
|
|
979
|
+
expect(rows[0].textContent).toContain('Orders');
|
|
980
|
+
expect(rows[0].textContent).toContain('1,578');
|
|
981
|
+
expect(rows[0].textContent).toContain('$211,970.30');
|
|
982
|
+
expect(rows[1].textContent).toContain('Average');
|
|
983
|
+
expect(rows[1].textContent).toContain('$134.33');
|
|
984
|
+
});
|
|
985
|
+
|
|
986
|
+
it('supports inline editable Toggle PATCH with optimistic update, propagation stop, editColumns guard, and error rollback', async () => {
|
|
987
|
+
let shouldFail = false;
|
|
988
|
+
const patchCalls: { url: string; body: unknown }[] = [];
|
|
989
|
+
mockFetchJson((url, init) => {
|
|
990
|
+
if (init?.method === 'PATCH') {
|
|
991
|
+
patchCalls.push({ url, body: JSON.parse(String(init.body)) });
|
|
992
|
+
// A refusal shows the server's words (a 5xx only says that the server failed).
|
|
993
|
+
if (shouldFail) return { ok: false, status: 422, body: { message: 'Toggle denied' } };
|
|
994
|
+
return { body: { ok: true } };
|
|
995
|
+
}
|
|
996
|
+
return {
|
|
997
|
+
body: {
|
|
998
|
+
data: [
|
|
999
|
+
{ id: '1', cells: { title: 'First', active: true }, can: { update: true, actions: [], editColumns: ['active'] } },
|
|
1000
|
+
{ id: '2', cells: { title: 'Second', active: false }, can: { update: false, actions: [] } },
|
|
1001
|
+
],
|
|
1002
|
+
total: 2,
|
|
1003
|
+
page: 1,
|
|
1004
|
+
lastPage: 1,
|
|
1005
|
+
} satisfies Page,
|
|
1006
|
+
};
|
|
1007
|
+
});
|
|
1008
|
+
|
|
1009
|
+
const resource: Resource = {
|
|
1010
|
+
name: 'users',
|
|
1011
|
+
label: 'Users',
|
|
1012
|
+
view: true,
|
|
1013
|
+
columns: [
|
|
1014
|
+
{ key: 'title', label: 'Title', type: 'text', sortable: false, searchable: false, editable: { type: 'text' } },
|
|
1015
|
+
{ key: 'active', label: 'Active', type: 'boolean', sortable: false, searchable: false, editable: { type: 'toggle' } },
|
|
1016
|
+
],
|
|
1017
|
+
fields: [],
|
|
1018
|
+
actions: [],
|
|
1019
|
+
canCreate: false,
|
|
1020
|
+
};
|
|
1021
|
+
|
|
1022
|
+
render(
|
|
1023
|
+
createElement(
|
|
1024
|
+
I18nProvider,
|
|
1025
|
+
{
|
|
1026
|
+
locale: 'en',
|
|
1027
|
+
children: createElement('div', null, createElement(Table, { resource, form: 'dialog' }), createElement(Notices)),
|
|
1028
|
+
},
|
|
1029
|
+
),
|
|
1030
|
+
);
|
|
1031
|
+
|
|
1032
|
+
await waitFor(() => expect(screen.getByText('First')).toBeTruthy());
|
|
1033
|
+
|
|
1034
|
+
// Row 1 title is NOT in editColumns so it renders read-only (no input)
|
|
1035
|
+
expect(document.querySelectorAll('[data-lk="cell-input"]').length).toBe(0);
|
|
1036
|
+
// Row 1 active IS editable; Row 2 can.update is false so only 1 switch renders
|
|
1037
|
+
const switches = document.querySelectorAll('[data-lk="cell-toggle"]') as NodeListOf<HTMLInputElement>;
|
|
1038
|
+
expect(switches.length).toBe(1);
|
|
1039
|
+
expect(switches[0].checked).toBe(true);
|
|
1040
|
+
|
|
1041
|
+
// Clicking toggle triggers PATCH and stops row propagation (no view dialog opened)
|
|
1042
|
+
fireEvent.click(switches[0]);
|
|
1043
|
+
expect(switches[0].checked).toBe(false);
|
|
1044
|
+
await waitFor(() => expect(patchCalls.length).toBe(1));
|
|
1045
|
+
expect(patchCalls[0].url).toContain('/users/1/cell');
|
|
1046
|
+
expect(patchCalls[0].body).toEqual({ column: 'active', value: false });
|
|
1047
|
+
expect(document.querySelector('dialog[data-lk="form"]')).toBeNull();
|
|
1048
|
+
|
|
1049
|
+
// Failing PATCH rolls back optimistic value and shows complain() toast
|
|
1050
|
+
shouldFail = true;
|
|
1051
|
+
fireEvent.click(switches[0]);
|
|
1052
|
+
await waitFor(() => expect(screen.getByText('Toggle denied')).toBeTruthy());
|
|
1053
|
+
expect(switches[0].checked).toBe(false);
|
|
1054
|
+
});
|
|
1055
|
+
|
|
1056
|
+
it('groups visible rows under collapsible headers, toggles Asc/Desc, and persists in localStorage', async () => {
|
|
1057
|
+
mockFetchJson(() => ({
|
|
1058
|
+
body: {
|
|
1059
|
+
data: [
|
|
1060
|
+
{ id: '1', cells: { title: 'Order A', status: 'pending' }, can: { update: false, actions: [] } },
|
|
1061
|
+
{ id: '2', cells: { title: 'Order B', status: 'active' }, can: { update: false, actions: [] } },
|
|
1062
|
+
{ id: '3', cells: { title: 'Order C', status: 'active' }, can: { update: false, actions: [] } },
|
|
1063
|
+
],
|
|
1064
|
+
total: 3,
|
|
1065
|
+
page: 1,
|
|
1066
|
+
lastPage: 1,
|
|
1067
|
+
} satisfies Page,
|
|
1068
|
+
}));
|
|
1069
|
+
|
|
1070
|
+
const resource: Resource = {
|
|
1071
|
+
name: 'orders',
|
|
1072
|
+
label: 'Orders',
|
|
1073
|
+
columns: [
|
|
1074
|
+
{ key: 'title', label: 'Title', type: 'text', sortable: false, searchable: false },
|
|
1075
|
+
{ key: 'status', label: 'Status', type: 'text', sortable: false, searchable: false },
|
|
1076
|
+
],
|
|
1077
|
+
fields: [],
|
|
1078
|
+
actions: [],
|
|
1079
|
+
canCreate: false,
|
|
1080
|
+
groups: [{ key: 'status', label: 'Status', collapsible: true }],
|
|
1081
|
+
table: { defaultGroup: 'status', defaultGroupDir: 'asc' },
|
|
1082
|
+
};
|
|
1083
|
+
|
|
1084
|
+
render(createElement(I18nProvider, { locale: 'en', children: createElement(Table, { resource, form: 'dialog' }) }));
|
|
1085
|
+
|
|
1086
|
+
await waitFor(() => expect(screen.getByText('Order A')).toBeTruthy());
|
|
1087
|
+
|
|
1088
|
+
const headers = document.querySelectorAll('[data-lk="group-header"]');
|
|
1089
|
+
expect(headers.length).toBe(2);
|
|
1090
|
+
expect(headers[0].textContent).toContain('Status: active');
|
|
1091
|
+
expect(headers[0].textContent).toContain('2');
|
|
1092
|
+
expect(headers[1].textContent).toContain('Status: pending');
|
|
1093
|
+
expect(headers[1].textContent).toContain('1');
|
|
1094
|
+
|
|
1095
|
+
// Collapse 'active' group
|
|
1096
|
+
const activeToggle = headers[0].querySelector('[data-lk="group-toggle"]')!;
|
|
1097
|
+
fireEvent.click(activeToggle);
|
|
1098
|
+
expect(activeToggle.getAttribute('aria-expanded')).toBe('false');
|
|
1099
|
+
expect(screen.queryByText('Order B')).toBeNull();
|
|
1100
|
+
expect(screen.queryByText('Order C')).toBeNull();
|
|
1101
|
+
expect(screen.getByText('Order A')).toBeTruthy();
|
|
1102
|
+
|
|
1103
|
+
// Flip direction to desc and verify localStorage persistence
|
|
1104
|
+
fireEvent.click(document.querySelector('[data-lk="group-dir"]')!);
|
|
1105
|
+
const flipped = document.querySelectorAll('[data-lk="group-header"]');
|
|
1106
|
+
expect(flipped[0].textContent).toContain('Status: pending');
|
|
1107
|
+
expect(localStorage.getItem('larakit::group:orders')).toBe('{"key":"status","dir":"desc"}');
|
|
1108
|
+
});
|
|
1109
|
+
|
|
1110
|
+
it('supports deferred filters Apply vs immediate and Reset', () => {
|
|
1111
|
+
const resource: Resource = {
|
|
1112
|
+
name: 'posts',
|
|
1113
|
+
label: 'Posts',
|
|
1114
|
+
columns: [],
|
|
1115
|
+
fields: [],
|
|
1116
|
+
actions: [],
|
|
1117
|
+
canCreate: false,
|
|
1118
|
+
filters: [{ key: 'published', label: 'Published', type: 'toggle' }],
|
|
1119
|
+
};
|
|
1120
|
+
|
|
1121
|
+
const onDeferred = vi.fn();
|
|
1122
|
+
const { rerender } = render(
|
|
1123
|
+
createElement(I18nProvider, {
|
|
1124
|
+
locale: 'en',
|
|
1125
|
+
children: createElement(FiltersAbove, { resource, chosen: {}, defer: true, onChange: onDeferred }),
|
|
1126
|
+
}),
|
|
1127
|
+
);
|
|
1128
|
+
|
|
1129
|
+
const toggle = screen.getByLabelText('Published') as HTMLInputElement;
|
|
1130
|
+
fireEvent.click(toggle);
|
|
1131
|
+
expect(onDeferred).not.toHaveBeenCalled();
|
|
1132
|
+
|
|
1133
|
+
fireEvent.click(document.querySelector('[data-lk="filters-apply"]')!);
|
|
1134
|
+
expect(onDeferred).toHaveBeenCalledWith({ published: ['on'] });
|
|
1135
|
+
|
|
1136
|
+
// Immediate mode calls onChange right away and hides Apply button
|
|
1137
|
+
const onImmediate = vi.fn();
|
|
1138
|
+
rerender(
|
|
1139
|
+
createElement(I18nProvider, {
|
|
1140
|
+
locale: 'en',
|
|
1141
|
+
children: createElement(FiltersAbove, { resource, chosen: {}, defer: false, onChange: onImmediate }),
|
|
1142
|
+
}),
|
|
1143
|
+
);
|
|
1144
|
+
expect(document.querySelector('[data-lk="filters-apply"]')).toBeNull();
|
|
1145
|
+
fireEvent.click(screen.getByLabelText('Published'));
|
|
1146
|
+
expect(onImmediate).toHaveBeenCalledWith({ published: ['on'] });
|
|
1147
|
+
});
|
|
1148
|
+
|
|
1149
|
+
it('renders Column Icons before or after value with iconColors tones', () => {
|
|
1150
|
+
const { rerender } = render(
|
|
1151
|
+
createElement(I18nProvider, {
|
|
1152
|
+
locale: 'en',
|
|
1153
|
+
children: createElement(Cell, {
|
|
1154
|
+
column: {
|
|
1155
|
+
key: 'tier',
|
|
1156
|
+
type: 'text',
|
|
1157
|
+
look: { icon: 'star', iconColors: { vip: 'warning' } },
|
|
1158
|
+
},
|
|
1159
|
+
row: { tier: 'vip' },
|
|
1160
|
+
}),
|
|
1161
|
+
}),
|
|
1162
|
+
);
|
|
1163
|
+
|
|
1164
|
+
const wrap1 = document.querySelector('[data-lk="cell-with-icon"]')!;
|
|
1165
|
+
expect(wrap1).toBeTruthy();
|
|
1166
|
+
const icon1 = wrap1.querySelector('[data-lk="icon"]')!;
|
|
1167
|
+
expect(icon1.getAttribute('class')).toContain('tone-text-warning');
|
|
1168
|
+
expect(wrap1.firstElementChild).toBe(icon1);
|
|
1169
|
+
|
|
1170
|
+
rerender(
|
|
1171
|
+
createElement(I18nProvider, {
|
|
1172
|
+
locale: 'en',
|
|
1173
|
+
children: createElement(Cell, {
|
|
1174
|
+
column: {
|
|
1175
|
+
key: 'state',
|
|
1176
|
+
type: 'text',
|
|
1177
|
+
look: { icon: 'check-circle', iconAfter: true, iconColors: { done: 'success' } },
|
|
1178
|
+
},
|
|
1179
|
+
row: { state: 'done' },
|
|
1180
|
+
}),
|
|
1181
|
+
}),
|
|
1182
|
+
);
|
|
1183
|
+
|
|
1184
|
+
const wrap2 = document.querySelector('[data-lk="cell-with-icon"]')!;
|
|
1185
|
+
const icon2 = wrap2.querySelector('[data-lk="icon"]')!;
|
|
1186
|
+
expect(icon2.getAttribute('class')).toContain('tone-text-success');
|
|
1187
|
+
expect(wrap2.lastElementChild).toBe(icon2);
|
|
1188
|
+
});
|
|
1189
|
+
|
|
1190
|
+
it('supports reordering, import modal expected columns, and sort loading spinner', async () => {
|
|
1191
|
+
ensureDialogPolyfill();
|
|
1192
|
+
const calls: { url: string; method?: string; body?: unknown }[] = [];
|
|
1193
|
+
mockFetchJson((url, init) => {
|
|
1194
|
+
calls.push({ url, method: init?.method, body: init?.body ? JSON.parse(String(init.body)) : undefined });
|
|
1195
|
+
return {
|
|
1196
|
+
body: {
|
|
1197
|
+
data: [
|
|
1198
|
+
{ id: 'a', cells: { name: 'Alpha' }, can: { update: false, actions: [] } },
|
|
1199
|
+
{ id: 'b', cells: { name: 'Beta' }, can: { update: false, actions: [] } },
|
|
1200
|
+
],
|
|
1201
|
+
total: 2,
|
|
1202
|
+
page: 1,
|
|
1203
|
+
lastPage: 1,
|
|
1204
|
+
} satisfies Page,
|
|
1205
|
+
};
|
|
1206
|
+
});
|
|
1207
|
+
|
|
1208
|
+
const resource: Resource = {
|
|
1209
|
+
name: 'items',
|
|
1210
|
+
label: 'Items',
|
|
1211
|
+
columns: [{ key: 'name', label: 'Name', type: 'text', sortable: true, searchable: false }],
|
|
1212
|
+
fields: [],
|
|
1213
|
+
actions: [],
|
|
1214
|
+
canCreate: false,
|
|
1215
|
+
table: {
|
|
1216
|
+
reorderable: true,
|
|
1217
|
+
exportable: true,
|
|
1218
|
+
importable: { columns: [{ key: 'name', label: 'Item Name', required: true }] },
|
|
1219
|
+
},
|
|
1220
|
+
};
|
|
1221
|
+
|
|
1222
|
+
render(createElement(I18nProvider, { locale: 'en', children: createElement(Table, { resource, form: 'dialog' }) }));
|
|
1223
|
+
await waitFor(() => expect(screen.getByText('Alpha')).toBeTruthy());
|
|
1224
|
+
|
|
1225
|
+
// Enter reorder mode and move first row down -> POST /items/reorder { ids: ['b', 'a'] }
|
|
1226
|
+
fireEvent.click(document.querySelector('[data-lk="reorder"]')!);
|
|
1227
|
+
const downButtons = document.querySelectorAll('[data-lk="move-down"]');
|
|
1228
|
+
expect(downButtons.length).toBe(2);
|
|
1229
|
+
fireEvent.click(downButtons[0]);
|
|
1230
|
+
await waitFor(() => expect(calls.some((c) => c.url.endsWith('/items/reorder') && c.method === 'POST')).toBe(true));
|
|
1231
|
+
const reorderCall = calls.find((c) => c.url.endsWith('/items/reorder'))!;
|
|
1232
|
+
expect(reorderCall.body).toEqual({ ids: ['b', 'a'] });
|
|
1233
|
+
|
|
1234
|
+
// Open Import modal and verify expected column + required badge
|
|
1235
|
+
fireEvent.click(document.querySelector('[data-lk="import"]')!);
|
|
1236
|
+
const modal = document.querySelector('[data-lk="import-modal"]')!;
|
|
1237
|
+
expect(modal).toBeTruthy();
|
|
1238
|
+
expect(modal.textContent).toContain('Item Name');
|
|
1239
|
+
expect(modal.textContent).toContain('Required');
|
|
1240
|
+
fireEvent.click(document.querySelector('[data-lk="import-close"]')!);
|
|
1241
|
+
|
|
1242
|
+
// Sort click shows animate-spin loader icon while request is pending
|
|
1243
|
+
let resolveSort!: (v: { ok?: boolean; status?: number; body: unknown }) => void;
|
|
1244
|
+
mockFetchJson(() => new Promise<{ ok?: boolean; status?: number; body: unknown }>((resolve) => { resolveSort = resolve; }));
|
|
1245
|
+
const sortBtn = document.querySelector('[data-lk="column"][data-name="name"] button')!;
|
|
1246
|
+
fireEvent.click(sortBtn);
|
|
1247
|
+
expect(sortBtn.querySelector('.animate-spin')).toBeTruthy();
|
|
1248
|
+
resolveSort({ body: { data: [], total: 0, page: 1, lastPage: 1 } });
|
|
1249
|
+
await waitFor(() => expect(sortBtn.querySelector('.animate-spin')).toBeNull());
|
|
1250
|
+
});
|
|
1251
|
+
});
|
|
1252
|
+
|
|
1253
|
+
describe('Phase 3 standalone infolists, relation managers, and action modals', () => {
|
|
1254
|
+
it('renders standalone infolist schema with all entry types, layout groups, visibleWhen, and form-backed fallback', async () => {
|
|
1255
|
+
ensureDialogPolyfill();
|
|
1256
|
+
mockFetchJson((url) => {
|
|
1257
|
+
if (url.endsWith('/orders/42')) {
|
|
1258
|
+
return {
|
|
1259
|
+
body: {
|
|
1260
|
+
id: '42',
|
|
1261
|
+
version: 'v1',
|
|
1262
|
+
values: { code: 'ORD-42' },
|
|
1263
|
+
infolist: {
|
|
1264
|
+
code: 'ORD-42',
|
|
1265
|
+
total: 12500,
|
|
1266
|
+
status: 'paid',
|
|
1267
|
+
paid: true,
|
|
1268
|
+
tier_icon: 'shield',
|
|
1269
|
+
avatar: '/img/avatar.png',
|
|
1270
|
+
accent: '#0e7a4d',
|
|
1271
|
+
meta: { Region: 'EU', Channel: 'Web' },
|
|
1272
|
+
line_items: [{ sku: 'SKU-1', qty: 2 }],
|
|
1273
|
+
tags: ['priority', 'express'],
|
|
1274
|
+
notes_rich: '<p>Safe <strong>note</strong></p><script>alert(1)</script>',
|
|
1275
|
+
notes_md: '**Markdown** [bad](javascript:alert(1))',
|
|
1276
|
+
payload: '{"ok":true}',
|
|
1277
|
+
empty_note: '',
|
|
1278
|
+
show_shipping: false,
|
|
1279
|
+
tracking: 'TRK-999',
|
|
1280
|
+
},
|
|
1281
|
+
} satisfies RecordData,
|
|
1282
|
+
};
|
|
1283
|
+
}
|
|
1284
|
+
return { body: {} };
|
|
1285
|
+
});
|
|
1286
|
+
|
|
1287
|
+
const resourceWithInfolist: Resource = {
|
|
1288
|
+
name: 'orders',
|
|
1289
|
+
label: 'Orders',
|
|
1290
|
+
view: true,
|
|
1291
|
+
canCreate: false,
|
|
1292
|
+
columns: [{ key: 'code', label: 'Code', type: 'text', sortable: false, searchable: false }],
|
|
1293
|
+
fields: [{ key: 'code', label: 'Code', type: 'text' }],
|
|
1294
|
+
actions: [],
|
|
1295
|
+
infolist: {
|
|
1296
|
+
columns: 2,
|
|
1297
|
+
width: 'xl',
|
|
1298
|
+
entries: [
|
|
1299
|
+
{ key: 'code', label: 'Order Code', type: 'text', look: { copyable: true, icon: 'tag' } },
|
|
1300
|
+
{ key: 'total', label: 'Total', type: 'text', look: { format: 'money', currency: 'USD', cents: true } },
|
|
1301
|
+
{ key: 'status', label: 'Status', type: 'badge', look: { colors: { paid: 'success' }, labels: { paid: 'Paid' } } },
|
|
1302
|
+
{ key: 'paid', label: 'Is Paid', type: 'boolean' },
|
|
1303
|
+
{ key: 'tier_icon', label: 'Tier', type: 'icon', look: { labels: { shield: 'Protected' }, colors: { shield: 'info' } } },
|
|
1304
|
+
{ key: 'avatar', label: 'Avatar', type: 'image', look: { size: 48, round: true } },
|
|
1305
|
+
{ key: 'accent', label: 'Accent', type: 'color' },
|
|
1306
|
+
{ key: 'meta', label: 'Metadata', type: 'keyvalue' },
|
|
1307
|
+
{ key: 'line_items', label: 'Items', type: 'repeatable', look: { fields: [{ key: 'sku', label: 'SKU', type: 'text' }, { key: 'qty', label: 'Qty', type: 'text' }], columns: 2 } },
|
|
1308
|
+
{ key: 'tags', label: 'Tags', type: 'text', look: { bulleted: true } },
|
|
1309
|
+
{ key: 'notes_rich', label: 'Rich Notes', type: 'rich' },
|
|
1310
|
+
{ key: 'notes_md', label: 'Markdown Notes', type: 'markdown' },
|
|
1311
|
+
{ key: 'payload', label: 'Payload', type: 'code', look: { wrap: true } },
|
|
1312
|
+
{ key: 'empty_note', label: 'Empty Note', type: 'text', look: { placeholder: 'No notes' } },
|
|
1313
|
+
{ key: 'tracking', label: 'Tracking', type: 'text', visibleWhen: [{ field: 'show_shipping' }] },
|
|
1314
|
+
],
|
|
1315
|
+
items: [
|
|
1316
|
+
{
|
|
1317
|
+
type: 'section',
|
|
1318
|
+
heading: 'Overview',
|
|
1319
|
+
columns: 2,
|
|
1320
|
+
items: [{ entry: 'code' }, { entry: 'total' }, { entry: 'status' }, { entry: 'paid' }],
|
|
1321
|
+
},
|
|
1322
|
+
{
|
|
1323
|
+
type: 'callout',
|
|
1324
|
+
heading: 'Info',
|
|
1325
|
+
color: 'info',
|
|
1326
|
+
items: [{ entry: 'tier_icon' }, { entry: 'empty_note' }],
|
|
1327
|
+
},
|
|
1328
|
+
{
|
|
1329
|
+
type: 'tabs',
|
|
1330
|
+
items: [
|
|
1331
|
+
{
|
|
1332
|
+
type: 'tab',
|
|
1333
|
+
heading: 'Details',
|
|
1334
|
+
items: [{ entry: 'avatar' }, { entry: 'accent' }, { entry: 'meta' }, { entry: 'line_items' }, { entry: 'tags' }],
|
|
1335
|
+
},
|
|
1336
|
+
{
|
|
1337
|
+
type: 'tab',
|
|
1338
|
+
heading: 'Content',
|
|
1339
|
+
items: [{ entry: 'notes_rich' }, { entry: 'notes_md' }, { entry: 'payload' }],
|
|
1340
|
+
},
|
|
1341
|
+
],
|
|
1342
|
+
},
|
|
1343
|
+
{
|
|
1344
|
+
type: 'fieldset',
|
|
1345
|
+
heading: 'Shipping',
|
|
1346
|
+
visibleWhen: [{ field: 'show_shipping' }],
|
|
1347
|
+
items: [{ entry: 'tracking' }],
|
|
1348
|
+
},
|
|
1349
|
+
],
|
|
1350
|
+
},
|
|
1351
|
+
};
|
|
1352
|
+
|
|
1353
|
+
render(
|
|
1354
|
+
createElement(I18nProvider, {
|
|
1355
|
+
locale: 'en',
|
|
1356
|
+
children: createElement(Form, {
|
|
1357
|
+
resource: resourceWithInfolist,
|
|
1358
|
+
layout: 'page',
|
|
1359
|
+
id: '42',
|
|
1360
|
+
view: true,
|
|
1361
|
+
close: () => {},
|
|
1362
|
+
}),
|
|
1363
|
+
}),
|
|
1364
|
+
);
|
|
1365
|
+
|
|
1366
|
+
await waitFor(() => expect(document.querySelector('[data-lk="infolist"]')).toBeTruthy());
|
|
1367
|
+
|
|
1368
|
+
expect(screen.getByText('Overview')).toBeTruthy();
|
|
1369
|
+
expect(document.querySelector('[data-lk="infolist"] [data-name="code"] [data-lk="value"]')?.textContent).toContain('ORD-42');
|
|
1370
|
+
expect(document.querySelector('[data-lk="copy"]')).toBeTruthy();
|
|
1371
|
+
expect(screen.getByText('$125.00')).toBeTruthy();
|
|
1372
|
+
expect(screen.getByText('Paid').getAttribute('data-color')).toBe('success');
|
|
1373
|
+
expect(screen.getByText('Yes')).toBeTruthy();
|
|
1374
|
+
expect(screen.getByText('Protected')).toBeTruthy();
|
|
1375
|
+
expect(document.querySelector('[data-lk="entry-images"] img')?.getAttribute('src')).toBe('/img/avatar.png');
|
|
1376
|
+
expect(screen.getByText('#0e7a4d')).toBeTruthy();
|
|
1377
|
+
expect(screen.getByText('Region')).toBeTruthy();
|
|
1378
|
+
expect(screen.getByText('SKU-1')).toBeTruthy();
|
|
1379
|
+
expect(document.querySelector('[data-lk="entry-list"] li')?.textContent).toBe('priority');
|
|
1380
|
+
expect(document.querySelector('[data-lk="infolist"] [data-name="notes_rich"] strong')?.textContent).toBe('note');
|
|
1381
|
+
expect(document.querySelector('[data-lk="infolist"] [data-name="notes_md"] strong')?.textContent).toBe('Markdown');
|
|
1382
|
+
expect(document.querySelector('[data-lk="infolist"] [data-name="notes_md"] a')).toBeNull();
|
|
1383
|
+
expect(document.querySelector('[data-lk="entry-code"]')?.textContent).toBe('{"ok":true}');
|
|
1384
|
+
expect(document.querySelector('script')).toBeNull();
|
|
1385
|
+
expect(screen.getByText('No notes')).toBeTruthy();
|
|
1386
|
+
// Hidden by visibleWhen
|
|
1387
|
+
expect(screen.queryByText('Shipping')).toBeNull();
|
|
1388
|
+
expect(screen.queryByText('TRK-999')).toBeNull();
|
|
1389
|
+
|
|
1390
|
+
// Verify fallback to Entry when resource.infolist is NOT defined
|
|
1391
|
+
cleanup();
|
|
1392
|
+
const fallbackResource: Resource = {
|
|
1393
|
+
name: 'orders',
|
|
1394
|
+
label: 'Orders',
|
|
1395
|
+
view: true,
|
|
1396
|
+
canCreate: false,
|
|
1397
|
+
columns: [{ key: 'code', label: 'Code', type: 'text', sortable: false, searchable: false }],
|
|
1398
|
+
fields: [{ key: 'code', label: 'Legacy Field Code', type: 'text' }],
|
|
1399
|
+
actions: [],
|
|
1400
|
+
};
|
|
1401
|
+
render(
|
|
1402
|
+
createElement(I18nProvider, {
|
|
1403
|
+
locale: 'en',
|
|
1404
|
+
children: createElement(Form, {
|
|
1405
|
+
resource: fallbackResource,
|
|
1406
|
+
layout: 'dialog',
|
|
1407
|
+
id: '42',
|
|
1408
|
+
view: true,
|
|
1409
|
+
close: () => {},
|
|
1410
|
+
}),
|
|
1411
|
+
}),
|
|
1412
|
+
);
|
|
1413
|
+
await waitFor(() => expect(screen.getByText('Legacy Field Code')).toBeTruthy());
|
|
1414
|
+
expect(document.querySelector('[data-lk="infolist"]')).toBeNull();
|
|
1415
|
+
});
|
|
1416
|
+
|
|
1417
|
+
it('renders RelationManagers as titled card or tabs, routes API via /parent/id/relations/rel, and preserves parent location.hash', async () => {
|
|
1418
|
+
ensureDialogPolyfill();
|
|
1419
|
+
location.hash = '#/orders/42';
|
|
1420
|
+
const calls: { url: string; method?: string; body?: unknown }[] = [];
|
|
1421
|
+
mockFetchJson((url, init) => {
|
|
1422
|
+
calls.push({ url, method: init?.method ?? 'GET', body: init?.body ? JSON.parse(String(init.body)) : undefined });
|
|
1423
|
+
if (url.endsWith('/orders/42')) {
|
|
1424
|
+
return {
|
|
1425
|
+
body: {
|
|
1426
|
+
id: '42',
|
|
1427
|
+
version: '1',
|
|
1428
|
+
values: { title: 'Order 42' },
|
|
1429
|
+
cells: { title: 'Order 42' },
|
|
1430
|
+
can: { update: true, actions: [] },
|
|
1431
|
+
},
|
|
1432
|
+
};
|
|
1433
|
+
}
|
|
1434
|
+
if (url.includes('/orders/42/relations/items') && (init?.method ?? 'GET') === 'GET') {
|
|
1435
|
+
return {
|
|
1436
|
+
body: {
|
|
1437
|
+
data: [{ id: '10', cells: { sku: 'PEN-1' }, can: { update: true, actions: [] } }],
|
|
1438
|
+
total: 1,
|
|
1439
|
+
page: 1,
|
|
1440
|
+
lastPage: 1,
|
|
1441
|
+
} satisfies Page,
|
|
1442
|
+
};
|
|
1443
|
+
}
|
|
1444
|
+
if (url.includes('/orders/42/relations/payments') && (init?.method ?? 'GET') === 'GET') {
|
|
1445
|
+
return {
|
|
1446
|
+
body: {
|
|
1447
|
+
data: [{ id: '20', cells: { amount: '50.00' }, can: { update: false, actions: [] } }],
|
|
1448
|
+
total: 1,
|
|
1449
|
+
page: 1,
|
|
1450
|
+
lastPage: 1,
|
|
1451
|
+
} satisfies Page,
|
|
1452
|
+
};
|
|
1453
|
+
}
|
|
1454
|
+
if (url.endsWith('/orders/42/relations/items') && init?.method === 'POST') {
|
|
1455
|
+
return { body: { id: '11' } };
|
|
1456
|
+
}
|
|
1457
|
+
return { body: {} };
|
|
1458
|
+
});
|
|
1459
|
+
|
|
1460
|
+
const itemsRel: RelationResource = {
|
|
1461
|
+
name: 'items',
|
|
1462
|
+
label: 'Line Items',
|
|
1463
|
+
icon: 'package',
|
|
1464
|
+
canCreate: true,
|
|
1465
|
+
columns: [{ key: 'sku', label: 'SKU', type: 'text', sortable: false, searchable: false }],
|
|
1466
|
+
fields: [{ key: 'sku', label: 'SKU', type: 'text' }],
|
|
1467
|
+
actions: [],
|
|
1468
|
+
};
|
|
1469
|
+
const paymentsRel: RelationResource = {
|
|
1470
|
+
name: 'payments',
|
|
1471
|
+
label: 'Payments',
|
|
1472
|
+
icon: 'credit-card',
|
|
1473
|
+
canCreate: false,
|
|
1474
|
+
columns: [{ key: 'amount', label: 'Amount', type: 'text', sortable: false, searchable: false }],
|
|
1475
|
+
fields: [{ key: 'amount', label: 'Amount', type: 'text' }],
|
|
1476
|
+
actions: [],
|
|
1477
|
+
};
|
|
1478
|
+
|
|
1479
|
+
// 1. Single relation renders as titled card inside RecordPage and opens dialog without touching hash
|
|
1480
|
+
const singleParent: Resource = {
|
|
1481
|
+
name: 'orders',
|
|
1482
|
+
label: 'Orders',
|
|
1483
|
+
view: true,
|
|
1484
|
+
canCreate: true,
|
|
1485
|
+
columns: [{ key: 'title', label: 'Title', type: 'text', sortable: false, searchable: false }],
|
|
1486
|
+
fields: [{ key: 'title', label: 'Title', type: 'text' }],
|
|
1487
|
+
actions: [],
|
|
1488
|
+
relations: [itemsRel],
|
|
1489
|
+
};
|
|
1490
|
+
|
|
1491
|
+
render(
|
|
1492
|
+
createElement(I18nProvider, {
|
|
1493
|
+
locale: 'en',
|
|
1494
|
+
children: createElement(RecordPage, { resource: singleParent, path: ['42'] }),
|
|
1495
|
+
}),
|
|
1496
|
+
);
|
|
1497
|
+
|
|
1498
|
+
await waitFor(() => expect(screen.getByText('PEN-1')).toBeTruthy());
|
|
1499
|
+
expect(document.querySelector('[data-lk="relation-panel"][data-name="items"]')).toBeTruthy();
|
|
1500
|
+
expect(document.querySelector('[data-lk="relation-title"]')?.textContent).toContain('Line Items');
|
|
1501
|
+
|
|
1502
|
+
// Click "New" inside relation -> opens dialog (not page route!) and preserves #/orders/42
|
|
1503
|
+
fireEvent.click(document.querySelector('[data-lk="relation-panel"] [data-lk="new"]')!);
|
|
1504
|
+
const modal = document.querySelector('dialog[data-lk="form"][open]')!;
|
|
1505
|
+
expect(modal).toBeTruthy();
|
|
1506
|
+
expect(location.hash).toBe('#/orders/42');
|
|
1507
|
+
|
|
1508
|
+
const skuInput = modal.querySelector('input')!;
|
|
1509
|
+
fireEvent.change(skuInput, { target: { value: 'BOOK-9' } });
|
|
1510
|
+
fireEvent.click(modal.querySelector('[data-lk="save"]')!);
|
|
1511
|
+
|
|
1512
|
+
await waitFor(() => expect(calls.some((c) => c.url.endsWith('/orders/42/relations/items') && c.method === 'POST')).toBe(true));
|
|
1513
|
+
const postCall = calls.find((c) => c.url.endsWith('/orders/42/relations/items') && c.method === 'POST')!;
|
|
1514
|
+
expect(postCall.body).toEqual({ sku: 'BOOK-9' });
|
|
1515
|
+
expect(location.hash).toBe('#/orders/42');
|
|
1516
|
+
|
|
1517
|
+
// 2. Multiple relations render tab switcher
|
|
1518
|
+
cleanup();
|
|
1519
|
+
const multiParent: Resource = { ...singleParent, relations: [itemsRel, paymentsRel] };
|
|
1520
|
+
render(
|
|
1521
|
+
createElement(I18nProvider, {
|
|
1522
|
+
locale: 'en',
|
|
1523
|
+
children: createElement(RelationManagers, { parentResource: multiParent, parentId: '42' }),
|
|
1524
|
+
}),
|
|
1525
|
+
);
|
|
1526
|
+
await waitFor(() => expect(screen.getByText('PEN-1')).toBeTruthy());
|
|
1527
|
+
const tabs = document.querySelectorAll('[data-lk="relations-tab"]');
|
|
1528
|
+
expect(tabs.length).toBe(2);
|
|
1529
|
+
expect(tabs[0].getAttribute('aria-selected')).toBe('true');
|
|
1530
|
+
|
|
1531
|
+
fireEvent.click(tabs[1]);
|
|
1532
|
+
await waitFor(() => expect(screen.getByText('50.00')).toBeTruthy());
|
|
1533
|
+
expect(tabs[1].getAttribute('aria-selected')).toBe('true');
|
|
1534
|
+
|
|
1535
|
+
// 3. A record opened in a dialog or drawer has its relation managers too, below the form, but a new one doesn't.
|
|
1536
|
+
for (const layout of ['dialog', 'drawer'] as const) {
|
|
1537
|
+
cleanup();
|
|
1538
|
+
render(createElement(I18nProvider, { locale: 'en', children: createElement(Form, { resource: singleParent, layout, id: '42', view: true, close: () => {} }) }));
|
|
1539
|
+
await waitFor(() => expect(screen.getByText('PEN-1')).toBeTruthy());
|
|
1540
|
+
expect(document.querySelector('[data-lk="form"] [data-lk="relation-panel"][data-name="items"]')).toBeTruthy();
|
|
1541
|
+
}
|
|
1542
|
+
cleanup();
|
|
1543
|
+
render(createElement(I18nProvider, { locale: 'en', children: createElement(Form, { resource: singleParent, layout: 'dialog', id: null, close: () => {} }) }));
|
|
1544
|
+
await waitFor(() => expect(document.querySelector('[data-lk="form"] input')).toBeTruthy());
|
|
1545
|
+
expect(document.querySelector('[data-lk="relation-panel"]')).toBeNull();
|
|
1546
|
+
});
|
|
1547
|
+
|
|
1548
|
+
it('supports Ask modal upgrades (eyebrow, description, slideOver, modalWidth, submitLabel, cancelLabel) and stacked Escape handling', async () => {
|
|
1549
|
+
ensureDialogPolyfill();
|
|
1550
|
+
const calls: { url: string; body?: unknown }[] = [];
|
|
1551
|
+
mockFetchJson((url, init) => {
|
|
1552
|
+
calls.push({ url, body: init?.body ? JSON.parse(String(init.body)) : undefined });
|
|
1553
|
+
if (url.includes('/items?')) {
|
|
1554
|
+
return {
|
|
1555
|
+
body: {
|
|
1556
|
+
data: [{ id: '7', cells: { name: 'Widget' }, can: { update: true, actions: ['refund'] } }],
|
|
1557
|
+
total: 1,
|
|
1558
|
+
page: 1,
|
|
1559
|
+
lastPage: 1,
|
|
1560
|
+
} satisfies Page,
|
|
1561
|
+
};
|
|
1562
|
+
}
|
|
1563
|
+
if (url.endsWith('/items/7')) {
|
|
1564
|
+
return { body: { id: '7', version: '1', values: { name: 'Widget' }, can: { update: true, actions: ['refund'] } } };
|
|
1565
|
+
}
|
|
1566
|
+
return { body: { message: 'Refunded' } };
|
|
1567
|
+
});
|
|
1568
|
+
|
|
1569
|
+
const refundAction: Action = {
|
|
1570
|
+
name: 'refund',
|
|
1571
|
+
label: 'Refund',
|
|
1572
|
+
confirm: '',
|
|
1573
|
+
danger: true,
|
|
1574
|
+
eyebrow: 'Billing Action',
|
|
1575
|
+
description: 'Issue a partial or full refund to the customer.',
|
|
1576
|
+
slideOver: true,
|
|
1577
|
+
modalWidth: 'lg',
|
|
1578
|
+
submitLabel: 'Send Refund',
|
|
1579
|
+
cancelLabel: 'Keep Order',
|
|
1580
|
+
form: [{ key: 'reason', label: 'Reason', type: 'text' }],
|
|
1581
|
+
};
|
|
1582
|
+
|
|
1583
|
+
const resource: Resource = {
|
|
1584
|
+
name: 'items',
|
|
1585
|
+
label: 'Items',
|
|
1586
|
+
canCreate: false,
|
|
1587
|
+
columns: [{ key: 'name', label: 'Name', type: 'text', sortable: false, searchable: false }],
|
|
1588
|
+
fields: [{ key: 'name', label: 'Name', type: 'text' }],
|
|
1589
|
+
headerActions: [refundAction],
|
|
1590
|
+
actions: [refundAction],
|
|
1591
|
+
};
|
|
1592
|
+
|
|
1593
|
+
render(
|
|
1594
|
+
createElement(I18nProvider, {
|
|
1595
|
+
locale: 'en',
|
|
1596
|
+
children: createElement('div', null, createElement(Table, { resource, form: 'dialog' }), createElement(Notices)),
|
|
1597
|
+
}),
|
|
1598
|
+
);
|
|
1599
|
+
|
|
1600
|
+
await waitFor(() => expect(screen.getByText('Widget')).toBeTruthy());
|
|
1601
|
+
// Trigger HeaderAsk
|
|
1602
|
+
fireEvent.click(document.querySelector('[data-lk="header-refund"]')!);
|
|
1603
|
+
const askDialog = document.querySelector('dialog[data-lk="create-form"][open]')!;
|
|
1604
|
+
expect(askDialog).toBeTruthy();
|
|
1605
|
+
expect(askDialog.getAttribute('data-layout')).toBe('drawer');
|
|
1606
|
+
expect(askDialog.className).toContain('max-w-3xl');
|
|
1607
|
+
expect(askDialog.querySelector('[data-lk="eyebrow"]')?.textContent).toBe('Billing Action');
|
|
1608
|
+
expect(askDialog.querySelector('[data-lk="description"]')?.textContent).toBe('Issue a partial or full refund to the customer.');
|
|
1609
|
+
expect(askDialog.querySelector('[data-lk="create"]')?.textContent).toBe('Send Refund');
|
|
1610
|
+
expect(askDialog.querySelector('[data-lk="cancel"]')?.textContent).toBe('Keep Order');
|
|
1611
|
+
fireEvent.click(askDialog.querySelector('[data-lk="cancel"]')!);
|
|
1612
|
+
await waitFor(() => expect(document.querySelector('dialog[data-lk="create-form"][open]')).toBeNull());
|
|
1613
|
+
|
|
1614
|
+
// Test stacked modals: open parent Form dialog, then stack Ask modal & Notices confirm modal on top;
|
|
1615
|
+
// Escape closes ONLY the topmost modal without closing parent modals underneath.
|
|
1616
|
+
fireEvent.click(document.querySelector('[data-lk="edit"]')!);
|
|
1617
|
+
await waitFor(() => expect(document.querySelector('dialog[data-lk="form"][open] input')).toBeTruthy());
|
|
1618
|
+
const parentModal = document.querySelector('dialog[data-lk="form"][open]')!;
|
|
1619
|
+
|
|
1620
|
+
// Stack an Ask drawer modal over the open Form dialog
|
|
1621
|
+
const stackHolder = document.createElement('div');
|
|
1622
|
+
document.body.appendChild(stackHolder);
|
|
1623
|
+
const onAskClose = vi.fn();
|
|
1624
|
+
render(
|
|
1625
|
+
createElement(I18nProvider, {
|
|
1626
|
+
locale: 'en',
|
|
1627
|
+
children: createElement(Ask, {
|
|
1628
|
+
url: '/items/7/actions/refund',
|
|
1629
|
+
fields: refundAction.form!,
|
|
1630
|
+
title: 'Stacked Refund',
|
|
1631
|
+
button: 'Refund',
|
|
1632
|
+
eyebrow: 'Stacked',
|
|
1633
|
+
slideOver: true,
|
|
1634
|
+
modalWidth: 'xl',
|
|
1635
|
+
submitLabel: 'Confirm',
|
|
1636
|
+
cancelLabel: 'Back',
|
|
1637
|
+
close: onAskClose,
|
|
1638
|
+
}),
|
|
1639
|
+
}),
|
|
1640
|
+
{ container: stackHolder },
|
|
1641
|
+
);
|
|
1642
|
+
|
|
1643
|
+
const stackedAsk = document.querySelector('dialog[data-lk="create-form"][open]')!;
|
|
1644
|
+
expect(stackedAsk).toBeTruthy();
|
|
1645
|
+
expect(stackedAsk.className).toContain('max-w-5xl');
|
|
1646
|
+
|
|
1647
|
+
// Open confirm dialog via sure() on top of BOTH modals
|
|
1648
|
+
const confirmPromise = sure('Are you really sure?', 'Proceed', true);
|
|
1649
|
+
await waitFor(() => expect(document.querySelector('dialog[data-lk="confirm"][open]')).toBeTruthy());
|
|
1650
|
+
const confirmDialog = document.querySelector('dialog[data-lk="confirm"][open]')!;
|
|
1651
|
+
|
|
1652
|
+
// Pressing Escape closes ONLY confirmDialog; stackedAsk and parentModal stay open
|
|
1653
|
+
fireEvent.keyDown(confirmDialog, { key: 'Escape' });
|
|
1654
|
+
await expect(confirmPromise).resolves.toBe(false);
|
|
1655
|
+
expect(document.querySelector('dialog[data-lk="confirm"][open]')).toBeNull();
|
|
1656
|
+
expect(stackedAsk.hasAttribute('open')).toBe(true);
|
|
1657
|
+
expect(parentModal.hasAttribute('open')).toBe(true);
|
|
1658
|
+
|
|
1659
|
+
// Pressing Escape on stackedAsk closes ONLY stackedAsk; parentModal stays open
|
|
1660
|
+
fireEvent.keyDown(stackedAsk, { key: 'Escape' });
|
|
1661
|
+
expect(onAskClose).toHaveBeenCalledTimes(1);
|
|
1662
|
+
expect(parentModal.hasAttribute('open')).toBe(true);
|
|
1663
|
+
stackHolder.remove();
|
|
1664
|
+
});
|
|
1665
|
+
});
|
|
1666
|
+
|
|
1667
|
+
describe('Phase 4 multi-tenancy and dashboard widget upgrades', () => {
|
|
1668
|
+
it('renders Pie and Area charts with data, hover/keyboard tooltips, custom empty message, and sr-only assistive description', () => {
|
|
1669
|
+
const { rerender } = render(
|
|
1670
|
+
createElement(I18nProvider, {
|
|
1671
|
+
locale: 'en',
|
|
1672
|
+
children: createElement(Pie, {
|
|
1673
|
+
label: 'Revenue Share',
|
|
1674
|
+
labels: ['Direct', 'Partner', 'Organic'],
|
|
1675
|
+
series: [{ label: 'Revenue', values: [60, 25, 15] }],
|
|
1676
|
+
assistive: 'Direct accounts for 60 percent of revenue.',
|
|
1677
|
+
}),
|
|
1678
|
+
}),
|
|
1679
|
+
);
|
|
1680
|
+
|
|
1681
|
+
const pie = document.querySelector('[data-lk="pie"]')!;
|
|
1682
|
+
expect(pie).toBeTruthy();
|
|
1683
|
+
const sectors = pie.querySelectorAll('[data-lk="slice"]');
|
|
1684
|
+
expect(sectors.length).toBe(3);
|
|
1685
|
+
const pieAssistive = pie.querySelector('[data-lk="assistive"]')!;
|
|
1686
|
+
expect(pieAssistive.className).toContain('sr-only');
|
|
1687
|
+
expect(pieAssistive.textContent).toBe('Direct accounts for 60 percent of revenue.');
|
|
1688
|
+
expect(pie.querySelector('svg[role="img"]')?.getAttribute('aria-describedby')).toBe(pieAssistive.id);
|
|
1689
|
+
|
|
1690
|
+
// Hovering a sector displays tooltip
|
|
1691
|
+
fireEvent.pointerEnter(sectors[1]);
|
|
1692
|
+
expect(pie.querySelector('[data-lk="tooltip"]')?.textContent).toContain('Partner');
|
|
1693
|
+
|
|
1694
|
+
// Custom empty message when pie has no data or zero sum
|
|
1695
|
+
rerender(
|
|
1696
|
+
createElement(I18nProvider, {
|
|
1697
|
+
locale: 'en',
|
|
1698
|
+
children: createElement(Pie, {
|
|
1699
|
+
label: 'Revenue Share',
|
|
1700
|
+
labels: ['Direct'],
|
|
1701
|
+
series: [{ label: 'Revenue', values: [0] }],
|
|
1702
|
+
empty: 'No revenue recorded yet.',
|
|
1703
|
+
}),
|
|
1704
|
+
}),
|
|
1705
|
+
);
|
|
1706
|
+
expect(document.querySelector('[data-lk="empty"]')?.textContent).toBe('No revenue recorded yet.');
|
|
1707
|
+
|
|
1708
|
+
// Multi-series Area chart with gradient fills, gridlines, scrubber, assistive, and custom empty
|
|
1709
|
+
rerender(
|
|
1710
|
+
createElement(I18nProvider, {
|
|
1711
|
+
locale: 'en',
|
|
1712
|
+
children: createElement(Area, {
|
|
1713
|
+
label: 'Traffic',
|
|
1714
|
+
labels: ['Mon', 'Tue', 'Wed'],
|
|
1715
|
+
series: [
|
|
1716
|
+
{ label: 'Desktop', values: [120, 180, 150] },
|
|
1717
|
+
{ label: 'Mobile', values: [80, 110, 140] },
|
|
1718
|
+
],
|
|
1719
|
+
assistive: 'Daily visits split across desktop and mobile.',
|
|
1720
|
+
}),
|
|
1721
|
+
}),
|
|
1722
|
+
);
|
|
1723
|
+
|
|
1724
|
+
const area = document.querySelector('[data-lk="area"]')!;
|
|
1725
|
+
expect(area).toBeTruthy();
|
|
1726
|
+
expect(area.querySelectorAll('[data-lk="area-fill"]').length).toBe(2);
|
|
1727
|
+
expect(area.querySelectorAll('linearGradient').length).toBe(2);
|
|
1728
|
+
const slider = area.querySelector('[role="slider"]')!;
|
|
1729
|
+
const areaAssistive = area.querySelector('[data-lk="assistive"]')!;
|
|
1730
|
+
expect(areaAssistive.className).toContain('sr-only');
|
|
1731
|
+
expect(slider.getAttribute('aria-describedby')).toBe(areaAssistive.id);
|
|
1732
|
+
|
|
1733
|
+
// Keyboard navigation shows tooltip for selected index
|
|
1734
|
+
fireEvent.focus(slider);
|
|
1735
|
+
fireEvent.keyDown(slider, { key: 'ArrowLeft' });
|
|
1736
|
+
const tooltip = area.querySelector('[data-lk="tooltip"]')!;
|
|
1737
|
+
expect(tooltip.textContent).toContain('Tue');
|
|
1738
|
+
expect(tooltip.textContent).toContain('Desktop');
|
|
1739
|
+
expect(tooltip.textContent).toContain('180');
|
|
1740
|
+
|
|
1741
|
+
// Custom empty message on Area chart
|
|
1742
|
+
rerender(
|
|
1743
|
+
createElement(I18nProvider, {
|
|
1744
|
+
locale: 'en',
|
|
1745
|
+
children: createElement(Area, {
|
|
1746
|
+
label: 'Traffic',
|
|
1747
|
+
labels: [],
|
|
1748
|
+
series: [],
|
|
1749
|
+
empty: 'No traffic data for range.',
|
|
1750
|
+
}),
|
|
1751
|
+
}),
|
|
1752
|
+
);
|
|
1753
|
+
expect(document.querySelector('[data-lk="empty"]')?.textContent).toBe('No traffic data for range.');
|
|
1754
|
+
});
|
|
1755
|
+
|
|
1756
|
+
it('opens a widget\'s and a stat\'s link to another site in a new tab', async () => {
|
|
1757
|
+
mockFetchJson(() => ({ body: { stats: [{ label: 'Guide', value: 'Start', href: 'https://larakit.com/docs' }, { label: 'Open', value: 3, href: '/admin/orders' }] } }));
|
|
1758
|
+
const panel = {
|
|
1759
|
+
protocol: 1, name: 'Admin', dates: 0,
|
|
1760
|
+
theme: { preset: 'ink', mode: 'light', tokens: {}, light: {}, dark: {} },
|
|
1761
|
+
layout: { nav: 'side', width: 'full', form: 'dialog' },
|
|
1762
|
+
pages: [], plugins: [], resources: [],
|
|
1763
|
+
dashboard: [{ name: 'docs', label: 'Docs', type: 'stats', wide: false, poll: 0,
|
|
1764
|
+
links: [{ label: 'Read the docs', href: 'https://larakit.com/docs' }, { label: 'Orders', href: 'orders' }] }],
|
|
1765
|
+
} as unknown as Panel;
|
|
1766
|
+
render(createElement(I18nProvider, { locale: 'en', children: createElement(Dashboard, { panel }) }));
|
|
1767
|
+
const link = (text: string) => [...document.querySelectorAll('a')].find((a) => a.textContent?.includes(text));
|
|
1768
|
+
await waitFor(() => expect(link('Start')).toBeTruthy());
|
|
1769
|
+
expect([link('Read the docs')!.target, link('Read the docs')!.rel]).toEqual(['_blank', 'noreferrer']);
|
|
1770
|
+
expect(link('Orders')!.target).toBe('');
|
|
1771
|
+
expect(link('Start')!.target).toBe('_blank');
|
|
1772
|
+
expect(link('3')!.target).toBe('');
|
|
1773
|
+
});
|
|
1774
|
+
|
|
1775
|
+
it('supports widget-level filter bar in instant and deferred (Apply/Reset) modes', async () => {
|
|
1776
|
+
const requestedUrls: string[] = [];
|
|
1777
|
+
mockFetchJson((url) => {
|
|
1778
|
+
requestedUrls.push(url);
|
|
1779
|
+
if (url.includes('instant_chart')) {
|
|
1780
|
+
return {
|
|
1781
|
+
body: {
|
|
1782
|
+
labels: ['Jan', 'Feb'],
|
|
1783
|
+
series: [{ label: 'Orders', values: [10, 20] }],
|
|
1784
|
+
},
|
|
1785
|
+
};
|
|
1786
|
+
}
|
|
1787
|
+
return {
|
|
1788
|
+
body: {
|
|
1789
|
+
labels: ['A', 'B'],
|
|
1790
|
+
series: [{ label: 'Share', values: [40, 60] }],
|
|
1791
|
+
},
|
|
1792
|
+
};
|
|
1793
|
+
});
|
|
1794
|
+
|
|
1795
|
+
const panel: Panel = {
|
|
1796
|
+
protocol: 1,
|
|
1797
|
+
name: 'Admin',
|
|
1798
|
+
dates: 0,
|
|
1799
|
+
theme: { preset: 'ink', mode: 'light', tokens: {}, light: {}, dark: {} },
|
|
1800
|
+
layout: { nav: 'side', width: 'full', form: 'dialog' },
|
|
1801
|
+
pages: [],
|
|
1802
|
+
plugins: [],
|
|
1803
|
+
resources: [],
|
|
1804
|
+
dashboard: [
|
|
1805
|
+
{
|
|
1806
|
+
name: 'instant_chart',
|
|
1807
|
+
label: 'Instant Orders',
|
|
1808
|
+
type: 'area',
|
|
1809
|
+
wide: false,
|
|
1810
|
+
poll: 0,
|
|
1811
|
+
filters: [
|
|
1812
|
+
{
|
|
1813
|
+
name: 'status',
|
|
1814
|
+
label: 'Status',
|
|
1815
|
+
options: [
|
|
1816
|
+
{ value: 'paid', label: 'Paid' },
|
|
1817
|
+
{ value: 'pending', label: 'Pending' },
|
|
1818
|
+
],
|
|
1819
|
+
},
|
|
1820
|
+
],
|
|
1821
|
+
},
|
|
1822
|
+
{
|
|
1823
|
+
name: 'deferred_pie',
|
|
1824
|
+
label: 'Deferred Mix',
|
|
1825
|
+
type: 'pie',
|
|
1826
|
+
wide: false,
|
|
1827
|
+
poll: 0,
|
|
1828
|
+
deferFilters: true,
|
|
1829
|
+
filters: [
|
|
1830
|
+
{
|
|
1831
|
+
name: 'region',
|
|
1832
|
+
label: 'Region',
|
|
1833
|
+
options: [
|
|
1834
|
+
{ value: 'eu', label: 'Europe' },
|
|
1835
|
+
{ value: 'us', label: 'North America' },
|
|
1836
|
+
],
|
|
1837
|
+
},
|
|
1838
|
+
],
|
|
1839
|
+
},
|
|
1840
|
+
],
|
|
1841
|
+
};
|
|
1842
|
+
|
|
1843
|
+
render(
|
|
1844
|
+
createElement(I18nProvider, {
|
|
1845
|
+
locale: 'en',
|
|
1846
|
+
children: createElement(Dashboard, { panel }),
|
|
1847
|
+
}),
|
|
1848
|
+
);
|
|
1849
|
+
|
|
1850
|
+
await waitFor(() => expect(document.querySelectorAll('[data-lk="widget"]').length).toBe(2));
|
|
1851
|
+
await waitFor(() => expect(requestedUrls.length).toBe(2));
|
|
1852
|
+
|
|
1853
|
+
// Instant mode: changing select immediately triggers reload with filter_status=paid
|
|
1854
|
+
const instantSelect = document.querySelector('[data-lk="widget"][data-name="instant_chart"] select[data-name="status"]') as HTMLSelectElement;
|
|
1855
|
+
fireEvent.change(instantSelect, { target: { value: 'paid' } });
|
|
1856
|
+
await waitFor(() => expect(requestedUrls.some((u) => u.includes('/dashboard/instant_chart?filter_status=paid'))).toBe(true));
|
|
1857
|
+
|
|
1858
|
+
// Deferred mode: changing select does NOT trigger fetch until Apply is clicked
|
|
1859
|
+
const countBeforeDefer = requestedUrls.length;
|
|
1860
|
+
const deferredWidget = document.querySelector('[data-lk="widget"][data-name="deferred_pie"]')!;
|
|
1861
|
+
const deferredSelect = deferredWidget.querySelector('select[data-name="region"]') as HTMLSelectElement;
|
|
1862
|
+
fireEvent.change(deferredSelect, { target: { value: 'eu' } });
|
|
1863
|
+
expect(requestedUrls.length).toBe(countBeforeDefer);
|
|
1864
|
+
|
|
1865
|
+
// Clicking Apply triggers fetch with filter_region=eu
|
|
1866
|
+
fireEvent.click(deferredWidget.querySelector('[data-lk="widget-filters-apply"]')!);
|
|
1867
|
+
await waitFor(() => expect(requestedUrls.some((u) => u.includes('/dashboard/deferred_pie?filter_region=eu'))).toBe(true));
|
|
1868
|
+
|
|
1869
|
+
// Clicking Reset clears filter and reloads without filter_region
|
|
1870
|
+
const countBeforeReset = requestedUrls.length;
|
|
1871
|
+
fireEvent.click(deferredWidget.querySelector('[data-lk="widget-filters-reset"]')!);
|
|
1872
|
+
await waitFor(() => expect(requestedUrls.length).toBeGreaterThan(countBeforeReset));
|
|
1873
|
+
expect(requestedUrls[requestedUrls.length - 1]).toMatch(/\/dashboard\/deferred_pie$/);
|
|
1874
|
+
});
|
|
1875
|
+
});
|
|
1876
|
+
|
|
1877
|
+
describe('Phase A: Topbar links, topbar actions, and 22 render hooks', () => {
|
|
1878
|
+
const basePanel = (overrides: Partial<Panel> = {}): Panel => ({
|
|
1879
|
+
protocol: 1,
|
|
1880
|
+
name: 'Admin',
|
|
1881
|
+
dates: 0,
|
|
1882
|
+
theme: { preset: 'ink', mode: 'light', tokens: {}, light: {}, dark: {} },
|
|
1883
|
+
layout: { nav: 'side', width: 'full', form: 'dialog' },
|
|
1884
|
+
dashboard: [],
|
|
1885
|
+
pages: [],
|
|
1886
|
+
plugins: [],
|
|
1887
|
+
resources: [],
|
|
1888
|
+
...overrides,
|
|
1889
|
+
});
|
|
1890
|
+
const mockTheme: ['light', (c: 'light' | 'dark' | 'system') => void, 'light'] = ['light', vi.fn(), 'light'];
|
|
1891
|
+
const navItems = [{ name: 'dashboard', label: 'Dashboard', href: '#/', current: true, icon: 'home', group: '' }];
|
|
1892
|
+
|
|
1893
|
+
it('renders styled topbar Links with .btn/tone/data-variant/badge vs unstyled .tool Links', async () => {
|
|
1894
|
+
const fetchCalls: { url: string; method?: string }[] = [];
|
|
1895
|
+
mockFetchJson((url, init) => {
|
|
1896
|
+
fetchCalls.push({ url, method: init?.method });
|
|
1897
|
+
return { body: { message: 'Cache cleared' } };
|
|
1898
|
+
});
|
|
1899
|
+
|
|
1900
|
+
const panel = basePanel({
|
|
1901
|
+
links: [
|
|
1902
|
+
{ label: 'Classic', href: '#/classic', icon: 'home', post: false },
|
|
1903
|
+
{ label: 'IconOnlyColored', href: '#/icon-only', icon: 'bell', color: 'primary', iconOnly: true },
|
|
1904
|
+
{
|
|
1905
|
+
label: 'Docs',
|
|
1906
|
+
href: 'https://docs.example.com',
|
|
1907
|
+
icon: 'book',
|
|
1908
|
+
color: 'primary',
|
|
1909
|
+
variant: 'soft',
|
|
1910
|
+
size: 'sm',
|
|
1911
|
+
badge: 'New',
|
|
1912
|
+
badgeColor: 'success',
|
|
1913
|
+
},
|
|
1914
|
+
{
|
|
1915
|
+
label: 'Purge',
|
|
1916
|
+
href: '/api/purge',
|
|
1917
|
+
color: 'danger',
|
|
1918
|
+
variant: 'outline',
|
|
1919
|
+
post: true,
|
|
1920
|
+
},
|
|
1921
|
+
],
|
|
1922
|
+
});
|
|
1923
|
+
|
|
1924
|
+
render(
|
|
1925
|
+
createElement(I18nProvider, {
|
|
1926
|
+
locale: 'en',
|
|
1927
|
+
children: createElement(
|
|
1928
|
+
'div',
|
|
1929
|
+
null,
|
|
1930
|
+
createElement(Shell, { panel, theme: mockTheme, items: navItems, name: 'dashboard', children: createElement('div', null, 'Body') }),
|
|
1931
|
+
createElement(Notices),
|
|
1932
|
+
),
|
|
1933
|
+
}),
|
|
1934
|
+
);
|
|
1935
|
+
|
|
1936
|
+
const tools = document.querySelector('[data-lk="tools"]')!;
|
|
1937
|
+
// Classic link keeps .tool and sr-only label
|
|
1938
|
+
const classic = tools.querySelector('a[href="#/classic"]')!;
|
|
1939
|
+
expect(classic.className).toContain('tool');
|
|
1940
|
+
expect(classic.className).not.toContain('btn');
|
|
1941
|
+
expect(classic.querySelector('.sr-only')?.textContent).toBe('Classic');
|
|
1942
|
+
|
|
1943
|
+
// iconOnly link also falls back to .tool
|
|
1944
|
+
const iconOnlyLink = tools.querySelector('a[href="#/icon-only"]')!;
|
|
1945
|
+
expect(iconOnlyLink.className).toContain('tool');
|
|
1946
|
+
expect(iconOnlyLink.className).not.toContain('btn');
|
|
1947
|
+
|
|
1948
|
+
// Rich styled external link renders .btn, tone-primary, data-variant, data-size, target="_blank", and Badge pill
|
|
1949
|
+
const docs = tools.querySelector('a[href="https://docs.example.com"]')!;
|
|
1950
|
+
expect(docs.className).toContain('btn');
|
|
1951
|
+
expect(docs.className).toContain('tone-primary');
|
|
1952
|
+
expect(docs.getAttribute('data-variant')).toBe('soft');
|
|
1953
|
+
expect(docs.getAttribute('data-size')).toBe('sm');
|
|
1954
|
+
expect(docs.getAttribute('target')).toBe('_blank');
|
|
1955
|
+
const badge = docs.querySelector('[data-lk="badge"]')!;
|
|
1956
|
+
expect(badge.textContent).toBe('New');
|
|
1957
|
+
expect(badge.getAttribute('data-color')).toBe('success');
|
|
1958
|
+
|
|
1959
|
+
// Rich styled POST link renders <button type="button"> with .btn + tone-danger and fires POST
|
|
1960
|
+
const purgeBtn = screen.getByRole('button', { name: 'Purge' });
|
|
1961
|
+
expect(purgeBtn.className).toContain('btn');
|
|
1962
|
+
expect(purgeBtn.className).toContain('tone-danger');
|
|
1963
|
+
expect(purgeBtn.getAttribute('data-variant')).toBe('outline');
|
|
1964
|
+
fireEvent.click(purgeBtn);
|
|
1965
|
+
await waitFor(() => expect(fetchCalls.some((c) => c.url === '/api/purge' && c.method === 'POST')).toBe(true));
|
|
1966
|
+
await waitFor(() => expect(screen.getByText('Cache cleared')).toBeTruthy());
|
|
1967
|
+
});
|
|
1968
|
+
|
|
1969
|
+
it('renders panel.topbarActions with direct POST and Ask modal/confirm calling POST /topbar/actions/{name}', async () => {
|
|
1970
|
+
ensureDialogPolyfill();
|
|
1971
|
+
const calls: { url: string; method?: string; body?: unknown }[] = [];
|
|
1972
|
+
mockFetchJson((url, init) => {
|
|
1973
|
+
calls.push({ url, method: init?.method, body: init?.body ? JSON.parse(String(init.body)) : undefined });
|
|
1974
|
+
return { body: { message: 'Action completed' } };
|
|
1975
|
+
});
|
|
1976
|
+
|
|
1977
|
+
const onChanged = vi.fn();
|
|
1978
|
+
addEventListener(changed, onChanged);
|
|
1979
|
+
|
|
1980
|
+
const panel = basePanel({
|
|
1981
|
+
topbarActions: [
|
|
1982
|
+
{ name: 'sync', label: 'Sync Now', confirm: '', danger: false, color: 'primary', variant: 'soft' },
|
|
1983
|
+
{
|
|
1984
|
+
name: 'deploy',
|
|
1985
|
+
label: 'Deploy',
|
|
1986
|
+
confirm: 'Deploy current release to production?',
|
|
1987
|
+
danger: true,
|
|
1988
|
+
eyebrow: 'Release',
|
|
1989
|
+
submitLabel: 'Ship It',
|
|
1990
|
+
form: [{ key: 'env', label: 'Environment', type: 'text', default: 'prod' }],
|
|
1991
|
+
},
|
|
1992
|
+
],
|
|
1993
|
+
});
|
|
1994
|
+
|
|
1995
|
+
try {
|
|
1996
|
+
render(
|
|
1997
|
+
createElement(I18nProvider, {
|
|
1998
|
+
locale: 'en',
|
|
1999
|
+
children: createElement(
|
|
2000
|
+
'div',
|
|
2001
|
+
null,
|
|
2002
|
+
createElement(Shell, { panel, theme: mockTheme, items: navItems, name: 'dashboard', children: createElement('div', null, 'Body') }),
|
|
2003
|
+
createElement(Notices),
|
|
2004
|
+
),
|
|
2005
|
+
}),
|
|
2006
|
+
);
|
|
2007
|
+
|
|
2008
|
+
// Direct topbarAction calls POST /topbar/actions/sync and fires changed + toast
|
|
2009
|
+
fireEvent.click(document.querySelector('[data-lk="topbar-action-sync"]')!);
|
|
2010
|
+
await waitFor(() => expect(calls.some((c) => c.url.endsWith('/topbar/actions/sync') && c.method === 'POST')).toBe(true));
|
|
2011
|
+
expect(onChanged).toHaveBeenCalled();
|
|
2012
|
+
await waitFor(() => expect(screen.getByText('Action completed')).toBeTruthy());
|
|
2013
|
+
|
|
2014
|
+
// Action with confirm + form opens Ask modal and submits to POST /topbar/actions/deploy
|
|
2015
|
+
fireEvent.click(document.querySelector('[data-lk="topbar-action-deploy"]')!);
|
|
2016
|
+
const dialog = document.querySelector('dialog[data-lk="create-form"][open]')!;
|
|
2017
|
+
expect(dialog).toBeTruthy();
|
|
2018
|
+
expect(dialog.querySelector('[data-lk="eyebrow"]')?.textContent).toBe('Release');
|
|
2019
|
+
expect(dialog.querySelector('[data-lk="description"]')?.textContent).toBe('Deploy current release to production?');
|
|
2020
|
+
fireEvent.click(dialog.querySelector('[data-lk="create"]')!);
|
|
2021
|
+
await waitFor(() =>
|
|
2022
|
+
expect(calls.some((c) => c.url.endsWith('/topbar/actions/deploy') && (c.body as Record<string, unknown>)?.env === 'prod')).toBe(true),
|
|
2023
|
+
);
|
|
2024
|
+
await waitFor(() => expect(document.querySelector('dialog[data-lk="create-form"][open]')).toBeNull());
|
|
2025
|
+
} finally {
|
|
2026
|
+
removeEventListener(changed, onChanged);
|
|
2027
|
+
}
|
|
2028
|
+
|
|
2029
|
+
// Also renders inside sidebar Tools when topbar === 'off'
|
|
2030
|
+
cleanup();
|
|
2031
|
+
const offTopbarPanel = basePanel({
|
|
2032
|
+
layout: { nav: 'side', topbar: 'off', width: 'full', form: 'dialog' },
|
|
2033
|
+
topbarActions: [{ name: 'sync', label: 'Side Sync', confirm: '', danger: false }],
|
|
2034
|
+
});
|
|
2035
|
+
render(
|
|
2036
|
+
createElement(I18nProvider, {
|
|
2037
|
+
locale: 'en',
|
|
2038
|
+
children: createElement(Shell, { panel: offTopbarPanel, theme: mockTheme, items: navItems, name: 'dashboard', children: createElement('div', null, 'Body') }),
|
|
2039
|
+
}),
|
|
2040
|
+
);
|
|
2041
|
+
expect(document.querySelector('[data-lk="side"] [data-lk="topbar-action-sync"]')).toBeTruthy();
|
|
2042
|
+
});
|
|
2043
|
+
|
|
2044
|
+
it('renders all 5 server-side HookItem types across shell, page header, table, form, and infolist hook locations', async () => {
|
|
2045
|
+
ensureDialogPolyfill();
|
|
2046
|
+
expect(HOOKS).toHaveLength(22);
|
|
2047
|
+
|
|
2048
|
+
const calls: { url: string; method?: string; body?: unknown }[] = [];
|
|
2049
|
+
mockFetchJson((url, init) => {
|
|
2050
|
+
calls.push({ url, method: init?.method, body: init?.body ? JSON.parse(String(init.body)) : undefined });
|
|
2051
|
+
if (url.includes('/posts?')) {
|
|
2052
|
+
return { body: { data: [], total: 0, page: 1, lastPage: 1 } satisfies Page };
|
|
2053
|
+
}
|
|
2054
|
+
if (url.endsWith('/posts/1')) {
|
|
2055
|
+
return { body: { id: '1', version: '1', values: { title: 'Hello' }, can: { update: true, actions: [] } } };
|
|
2056
|
+
}
|
|
2057
|
+
return { body: { message: 'Hook executed' } };
|
|
2058
|
+
});
|
|
2059
|
+
|
|
2060
|
+
const panel = basePanel({
|
|
2061
|
+
hooks: {
|
|
2062
|
+
'body.start': [{ type: 'banner', title: 'Maintenance Notice', description: 'Window at 02:00 UTC', color: 'warning', icon: 'warning' }],
|
|
2063
|
+
'topbar.start': [{ type: 'badge', label: 'Staging', color: 'info', href: '#/env' }],
|
|
2064
|
+
'topbar.before-search': [{ type: 'text', text: 'v2.0', color: 'success' }],
|
|
2065
|
+
'topbar.after-search': [{ type: 'button', label: 'Help', href: 'https://help.example.com', color: 'primary', variant: 'ghost', size: 'sm' }],
|
|
2066
|
+
'topbar.end': [{ type: 'action', action: { name: 'refresh-stats', label: 'Refresh', confirm: '', danger: false } }],
|
|
2067
|
+
'sidebar.header': [{ type: 'text', text: 'Workspace' }],
|
|
2068
|
+
'sidebar.nav.start': [{ type: 'text', text: 'NavStart' }],
|
|
2069
|
+
'sidebar.nav.end': [{ type: 'text', text: 'NavEnd' }],
|
|
2070
|
+
'sidebar.footer': [{ type: 'text', text: 'FooterNote' }],
|
|
2071
|
+
'content.start': [{ type: 'text', text: 'ContentStart' }],
|
|
2072
|
+
'content.end': [{ type: 'text', text: 'ContentEnd' }],
|
|
2073
|
+
'body.end': [{ type: 'text', text: 'BodyEnd' }],
|
|
2074
|
+
'page.header.actions.before': [{ type: 'button', label: 'BeforeHeader', href: '#/before', variant: 'outline' }],
|
|
2075
|
+
'page.header.actions.after': [
|
|
2076
|
+
{
|
|
2077
|
+
type: 'action',
|
|
2078
|
+
action: {
|
|
2079
|
+
name: 'bulk-notify',
|
|
2080
|
+
label: 'Notify',
|
|
2081
|
+
confirm: 'Send notification?',
|
|
2082
|
+
danger: false,
|
|
2083
|
+
slideOver: true,
|
|
2084
|
+
description: 'Broadcast to subscribers.',
|
|
2085
|
+
},
|
|
2086
|
+
},
|
|
2087
|
+
],
|
|
2088
|
+
'table.before': [{ type: 'banner', title: 'Table Banner', color: 'info' }],
|
|
2089
|
+
'table.toolbar.start': [{ type: 'text', text: 'ToolbarStart' }],
|
|
2090
|
+
'table.toolbar.end': [{ type: 'badge', label: 'Live', color: 'success' }],
|
|
2091
|
+
'table.after': [{ type: 'text', text: 'TableAfter' }],
|
|
2092
|
+
'form.before': [{ type: 'banner', title: 'Form Notice', color: 'primary' }],
|
|
2093
|
+
'form.after': [{ type: 'text', text: 'FormAfter' }],
|
|
2094
|
+
'infolist.before': [{ type: 'text', text: 'InfoBefore' }],
|
|
2095
|
+
'infolist.after': [{ type: 'text', text: 'InfoAfter' }],
|
|
2096
|
+
},
|
|
2097
|
+
});
|
|
2098
|
+
|
|
2099
|
+
const resource: Resource = {
|
|
2100
|
+
name: 'posts',
|
|
2101
|
+
label: 'Posts',
|
|
2102
|
+
canCreate: false,
|
|
2103
|
+
actions: [],
|
|
2104
|
+
columns: [{ key: 'title', label: 'Title', type: 'text', sortable: false, searchable: false }],
|
|
2105
|
+
fields: [{ key: 'title', label: 'Title', type: 'text' }],
|
|
2106
|
+
infolist: { entries: [{ key: 'title', label: 'Title', type: 'text' }] },
|
|
2107
|
+
};
|
|
2108
|
+
|
|
2109
|
+
const reload = vi.fn();
|
|
2110
|
+
|
|
2111
|
+
render(
|
|
2112
|
+
createElement(I18nProvider, {
|
|
2113
|
+
locale: 'en',
|
|
2114
|
+
children: createElement(
|
|
2115
|
+
PanelHookContext,
|
|
2116
|
+
{
|
|
2117
|
+
value: { panel, screen: 'posts', reload },
|
|
2118
|
+
children: createElement(
|
|
2119
|
+
'div',
|
|
2120
|
+
null,
|
|
2121
|
+
createElement(
|
|
2122
|
+
Shell,
|
|
2123
|
+
{
|
|
2124
|
+
panel,
|
|
2125
|
+
theme: mockTheme,
|
|
2126
|
+
items: navItems,
|
|
2127
|
+
name: 'posts',
|
|
2128
|
+
children: createElement(
|
|
2129
|
+
'div',
|
|
2130
|
+
null,
|
|
2131
|
+
createElement(Table, { resource, form: 'dialog' }),
|
|
2132
|
+
createElement(Form, { resource, layout: 'page', id: null, close: vi.fn() }),
|
|
2133
|
+
createElement(InfolistView, { resource, infolist: resource.infolist!, data: { title: 'Hello' } }),
|
|
2134
|
+
),
|
|
2135
|
+
},
|
|
2136
|
+
),
|
|
2137
|
+
createElement(Notices),
|
|
2138
|
+
),
|
|
2139
|
+
},
|
|
2140
|
+
),
|
|
2141
|
+
}),
|
|
2142
|
+
);
|
|
2143
|
+
|
|
2144
|
+
await waitFor(() => expect(document.querySelector('[data-lk="table"]')).toBeTruthy());
|
|
2145
|
+
|
|
2146
|
+
// All 22 hook wrappers rendered with data-hook
|
|
2147
|
+
for (const h of HOOKS) {
|
|
2148
|
+
expect(document.querySelector(`[data-lk="hook"][data-hook="${h}"]`)).toBeTruthy();
|
|
2149
|
+
}
|
|
2150
|
+
|
|
2151
|
+
// Banner, badge (with href), button, text rendered cleanly
|
|
2152
|
+
const maintenanceBanner = document.querySelector('[data-hook="body.start"] [data-lk="hook-banner"]')!;
|
|
2153
|
+
expect(maintenanceBanner.getAttribute('role')).toBe('alert');
|
|
2154
|
+
expect(maintenanceBanner.textContent).toContain('Maintenance Notice');
|
|
2155
|
+
expect(document.querySelector('[data-hook="topbar.start"] a[data-lk="hook-badge"]')?.getAttribute('href')).toBe('#/env');
|
|
2156
|
+
expect(document.querySelector('[data-hook="topbar.after-search"] a[data-lk="hook-button"]')?.getAttribute('target')).toBe('_blank');
|
|
2157
|
+
|
|
2158
|
+
// Direct hook action runs POST /hooks/actions/refresh-stats + toast + reload
|
|
2159
|
+
fireEvent.click(document.querySelector('[data-lk="hook-action-refresh-stats"]')!);
|
|
2160
|
+
await waitFor(() => expect(calls.some((c) => c.url.endsWith('/hooks/actions/refresh-stats') && c.method === 'POST')).toBe(true));
|
|
2161
|
+
expect(reload).toHaveBeenCalledTimes(1);
|
|
2162
|
+
await waitFor(() => expect(screen.getByText('Hook executed')).toBeTruthy());
|
|
2163
|
+
|
|
2164
|
+
// Hook action with slideOver/description/confirm opens Ask drawer and submits POST /hooks/actions/bulk-notify
|
|
2165
|
+
fireEvent.click(document.querySelector('[data-lk="hook-action-bulk-notify"]')!);
|
|
2166
|
+
const drawer = document.querySelector('dialog[data-lk="create-form"][open]')!;
|
|
2167
|
+
expect(drawer.getAttribute('data-layout')).toBe('drawer');
|
|
2168
|
+
expect(drawer.querySelector('[data-lk="description"]')?.textContent).toBe('Broadcast to subscribers.');
|
|
2169
|
+
fireEvent.click(drawer.querySelector('[data-lk="create"]')!);
|
|
2170
|
+
await waitFor(() => expect(calls.some((c) => c.url.endsWith('/hooks/actions/bulk-notify') && c.method === 'POST')).toBe(true));
|
|
2171
|
+
expect(reload).toHaveBeenCalledTimes(2);
|
|
2172
|
+
});
|
|
2173
|
+
|
|
2174
|
+
});
|
|
2175
|
+
|
|
2176
|
+
describe('the profile', () => {
|
|
2177
|
+
afterEach(() => { cleanup(); vi.restoreAllMocks(); });
|
|
2178
|
+
|
|
2179
|
+
it('shows who is signed in, saves a change and shows the errors of a password', async () => {
|
|
2180
|
+
const sent: { url: string; body: unknown }[] = [];
|
|
2181
|
+
mockFetchJson((url, init) => {
|
|
2182
|
+
if (init?.method === 'POST') sent.push({ url, body: JSON.parse(String(init.body)) });
|
|
2183
|
+
if (url.endsWith('/profile/password')) return { status: 422, body: { message: 'no', errors: { current_password: ['This is not your current password.'] } } };
|
|
2184
|
+
if (init?.method === 'POST') return { body: { message: 'Your profile is saved.', name: 'Ada Lovelace', email: 'ada@example.org' } };
|
|
2185
|
+
return { body: { name: 'Ada', email: 'ada@example.com' } };
|
|
2186
|
+
});
|
|
2187
|
+
const saved = vi.fn();
|
|
2188
|
+
render(createElement(ProfilePage, { onSaved: saved }));
|
|
2189
|
+
const name = await screen.findByLabelText('Name') as HTMLInputElement;
|
|
2190
|
+
expect(name.value).toBe('Ada');
|
|
2191
|
+
fireEvent.change(name, { target: { value: 'Ada Lovelace' } });
|
|
2192
|
+
fireEvent.click(screen.getByRole('button', { name: 'Save' }));
|
|
2193
|
+
await waitFor(() => expect(saved).toHaveBeenCalledWith('Ada Lovelace', 'ada@example.org'));
|
|
2194
|
+
expect(sent[0]).toEqual({ url: '/api/profile', body: { name: 'Ada Lovelace', email: 'ada@example.com' } });
|
|
2195
|
+
fireEvent.change(screen.getByLabelText('Current password'), { target: { value: 'wrong' } });
|
|
2196
|
+
fireEvent.click(screen.getAllByRole('button', { name: 'Change password' })[0]);
|
|
2197
|
+
expect(await screen.findByText('This is not your current password.')).toBeTruthy();
|
|
2198
|
+
expect(screen.getByLabelText('Current password').getAttribute('aria-invalid')).toBe('true');
|
|
2199
|
+
});
|
|
2200
|
+
});
|