-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathObjectView.test.tsx
More file actions
665 lines (539 loc) · 20.8 KB
/
Copy pathObjectView.test.tsx
File metadata and controls
665 lines (539 loc) · 20.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { ObjectView } from '../ObjectView';
import type { ObjectViewSchema, DataSource } from '@object-ui/types';
// Mock @object-ui/react to avoid circular dependency issues
vi.mock('@object-ui/react', () => ({
SchemaRenderer: ({ schema }: any) => (
<div data-testid="schema-renderer" data-schema-type={schema?.type}>
{schema?.type}
</div>
),
SchemaRendererContext: null,
}));
// Mock @object-ui/plugin-grid
vi.mock('@object-ui/plugin-grid', () => ({
ObjectGrid: ({ schema, onRowClick }: any) => (
<div data-testid="object-grid" data-object={schema?.objectName}>
<button data-testid="grid-row" onClick={() => onRowClick?.({ _id: '1', name: 'Test' })}>
Row 1
</button>
</div>
),
}));
// Mock @object-ui/plugin-form
vi.mock('@object-ui/plugin-form', () => ({
ObjectForm: ({ schema }: any) => (
<div data-testid="object-form" data-mode={schema?.mode}>
Form ({schema?.mode})
</div>
),
}));
const createMockDataSource = (overrides: Partial<DataSource> = {}): DataSource => ({
find: vi.fn().mockResolvedValue([]),
findOne: vi.fn().mockResolvedValue(null),
create: vi.fn().mockResolvedValue({}),
update: vi.fn().mockResolvedValue({}),
delete: vi.fn().mockResolvedValue({}),
getObjectSchema: vi.fn().mockResolvedValue({
label: 'Contacts',
fields: {
name: { label: 'Name', type: 'text' },
email: { label: 'Email', type: 'text' },
status: {
label: 'Status',
type: 'select',
options: [
{ label: 'Active', value: 'active' },
{ label: 'Inactive', value: 'inactive' },
],
},
created_at: { label: 'Created', type: 'date' },
},
}),
...overrides,
} as DataSource);
describe('ObjectView', () => {
let mockDataSource: DataSource;
beforeEach(() => {
vi.clearAllMocks();
mockDataSource = createMockDataSource();
});
// ============================
// Basic Rendering
// ============================
describe('Basic Rendering', () => {
it('should render with minimal schema', () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
};
render(<ObjectView schema={schema} dataSource={mockDataSource} />);
// Should render the grid by default
expect(screen.getByTestId('object-grid')).toBeDefined();
});
it('should render title and description', () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
title: 'Contact List',
description: 'Manage your contacts',
};
render(<ObjectView schema={schema} dataSource={mockDataSource} />);
expect(screen.getByText('Contact List')).toBeDefined();
expect(screen.getByText('Manage your contacts')).toBeDefined();
});
it('should not render search box (delegated to ListView toolbar)', () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
};
render(<ObjectView schema={schema} dataSource={mockDataSource} />);
expect(screen.queryByPlaceholderText(/search/i)).toBeNull();
});
it('should not render search box when showSearch is false', () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
showSearch: false,
};
render(<ObjectView schema={schema} dataSource={mockDataSource} />);
expect(screen.queryByPlaceholderText(/search/i)).toBeNull();
});
it('should render create button by default', () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
};
render(<ObjectView schema={schema} dataSource={mockDataSource} />);
expect(screen.getByText('Create')).toBeDefined();
});
it('should hide create button when showCreate is false', () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
showCreate: false,
};
render(<ObjectView schema={schema} dataSource={mockDataSource} />);
expect(screen.queryByText('Create')).toBeNull();
});
});
// ============================
// Named List Views
// ============================
describe('Named List Views', () => {
it('should render named view tabs when listViews has multiple entries', () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
listViews: {
all: { label: 'All Contacts', type: 'grid' },
active: { label: 'Active', type: 'grid', filter: [['status', '=', 'active']] },
},
defaultListView: 'all',
};
render(<ObjectView schema={schema} dataSource={mockDataSource} />);
expect(screen.getByText('All Contacts')).toBeDefined();
expect(screen.getByText('Active')).toBeDefined();
});
it('should not render tabs when only one named view exists', () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
listViews: {
all: { label: 'All Contacts', type: 'grid' },
},
};
render(<ObjectView schema={schema} dataSource={mockDataSource} />);
// Should not show tabs for a single view
expect(screen.queryByRole('tablist')).toBeNull();
});
it('should default to first named view when defaultListView is not set', () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
listViews: {
all: { label: 'All Contacts', type: 'grid' },
active: { label: 'Active', type: 'grid' },
},
};
render(<ObjectView schema={schema} dataSource={mockDataSource} />);
// The grid should be rendered (first view is grid type)
expect(screen.getByTestId('object-grid')).toBeDefined();
});
});
// ============================
// Default View Type
// ============================
describe('Default View Type', () => {
it('should render grid by default when no defaultViewType set', () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
};
render(<ObjectView schema={schema} dataSource={mockDataSource} />);
expect(screen.getByTestId('object-grid')).toBeDefined();
});
});
// ============================
// Navigation Config
// ============================
describe('Navigation Config', () => {
it('should not navigate when mode is none', () => {
const onRowClick = vi.fn();
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
navigation: { mode: 'none' },
};
render(<ObjectView schema={schema} dataSource={mockDataSource} />);
// Click a grid row
fireEvent.click(screen.getByTestId('grid-row'));
// onRowClick should not be called (mode is none)
expect(onRowClick).not.toHaveBeenCalled();
});
it('should not navigate when preventNavigation is true', () => {
const onRowClick = vi.fn();
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
navigation: { mode: 'page', preventNavigation: true },
};
render(<ObjectView schema={schema} dataSource={mockDataSource} />);
fireEvent.click(screen.getByTestId('grid-row'));
expect(onRowClick).not.toHaveBeenCalled();
});
it('should open in new window when mode is new_window', () => {
const openSpy = vi.spyOn(window, 'open').mockImplementation(() => null);
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
navigation: { mode: 'new_window' },
};
render(<ObjectView schema={schema} dataSource={mockDataSource} />);
fireEvent.click(screen.getByTestId('grid-row'));
expect(openSpy).toHaveBeenCalledWith('/contacts/1', '_blank');
openSpy.mockRestore();
});
it('should call onNavigate for page mode', () => {
const onNavigate = vi.fn();
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
navigation: { mode: 'page' },
onNavigate,
};
render(<ObjectView schema={schema} dataSource={mockDataSource} />);
fireEvent.click(screen.getByTestId('grid-row'));
expect(onNavigate).toHaveBeenCalledWith('1', 'view');
});
it('should open form in view mode when split navigation mode is clicked', () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
navigation: { mode: 'split' },
};
render(<ObjectView schema={schema} dataSource={mockDataSource} />);
fireEvent.click(screen.getByTestId('grid-row'));
// Split mode renders NavigationOverlay with split panels including a close button
expect(screen.getByTestId('object-form')).toBeDefined();
expect(screen.getByLabelText('Close panel')).toBeDefined();
});
it('should open form in view mode when popover navigation mode is clicked', () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
navigation: { mode: 'popover' },
};
render(<ObjectView schema={schema} dataSource={mockDataSource} />);
fireEvent.click(screen.getByTestId('grid-row'));
// Popover mode renders NavigationOverlay Dialog fallback (no popoverTrigger)
expect(screen.getByTestId('object-form')).toBeDefined();
expect(screen.getByRole('dialog')).toBeDefined();
});
it('should close split panel and return to normal view', () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
navigation: { mode: 'split' },
};
render(<ObjectView schema={schema} dataSource={mockDataSource} />);
// Open split panel
fireEvent.click(screen.getByTestId('grid-row'));
expect(screen.getByLabelText('Close panel')).toBeDefined();
// Close split panel
fireEvent.click(screen.getByLabelText('Close panel'));
// Form should be gone, grid should remain
expect(screen.queryByLabelText('Close panel')).toBeNull();
expect(screen.getByTestId('object-grid')).toBeDefined();
});
});
// ============================
// CRUD Operations
// ============================
describe('CRUD Operations', () => {
it('should hide create button when operations.create is false', () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
operations: { create: false, read: true, update: true, delete: true },
};
render(<ObjectView schema={schema} dataSource={mockDataSource} />);
expect(screen.queryByText('Create')).toBeNull();
});
});
// ============================
// Data Source Integration
// ============================
describe('Data Source Integration', () => {
it('should fetch object schema on mount', async () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
};
render(<ObjectView schema={schema} dataSource={mockDataSource} />);
await waitFor(() => {
expect(mockDataSource.getObjectSchema).toHaveBeenCalledWith('contacts');
});
});
});
// ============================
// View Switcher (prop-based views)
// ============================
describe('View Switcher (prop-based)', () => {
it('should not render view switcher with single view prop', () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
};
const views = [
{ id: 'grid', label: 'Grid', type: 'grid' as const },
];
render(
<ObjectView schema={schema} dataSource={mockDataSource} views={views} />,
);
// Only one view, no switcher needed
expect(screen.getByTestId('object-grid')).toBeDefined();
});
});
// ============================
// showViewSwitcher config
// ============================
describe('showViewSwitcher', () => {
it('should hide view switcher when showViewSwitcher is false', () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
showViewSwitcher: false,
};
const views = [
{ id: 'grid', label: 'Grid', type: 'grid' as const },
{ id: 'kanban', label: 'Kanban', type: 'kanban' as const },
];
render(
<ObjectView schema={schema} dataSource={mockDataSource} views={views} />,
);
// Switcher should be hidden
expect(screen.queryByText('Kanban')).toBeNull();
});
});
// ============================
// Live Preview — viewConfig sync
// ============================
describe('Live Preview', () => {
it('should re-render grid when views prop updates with new columns', async () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
};
const initialViews = [
{ id: 'all', label: 'All', type: 'grid' as const, columns: ['name', 'email'] },
];
const { rerender } = render(
<ObjectView schema={schema} dataSource={mockDataSource} views={initialViews} activeViewId="all" />,
);
expect(screen.getByTestId('object-grid')).toBeInTheDocument();
// Simulate live preview: update views prop with new columns (as parent would after viewDraft change)
const updatedViews = [
{ id: 'all', label: 'All', type: 'grid' as const, columns: ['name', 'email', 'status'] },
];
rerender(
<ObjectView schema={schema} dataSource={mockDataSource} views={updatedViews} activeViewId="all" />,
);
// Grid should still render (component did not crash on prop update)
expect(screen.getByTestId('object-grid')).toBeInTheDocument();
});
it('should re-render when views prop updates with new sort config', async () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
};
const initialViews = [
{ id: 'all', label: 'All', type: 'grid' as const, columns: ['name'] },
];
const { rerender } = render(
<ObjectView schema={schema} dataSource={mockDataSource} views={initialViews} activeViewId="all" />,
);
// Update with sort config — simulates live preview of sort changes
const updatedViews = [
{ id: 'all', label: 'All', type: 'grid' as const, columns: ['name'], sort: [{ field: 'name', direction: 'desc' as const }] },
];
rerender(
<ObjectView schema={schema} dataSource={mockDataSource} views={updatedViews} activeViewId="all" />,
);
expect(screen.getByTestId('object-grid')).toBeInTheDocument();
});
it('should re-render when views prop updates with new filter', async () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
};
const initialViews = [
{ id: 'all', label: 'All', type: 'grid' as const, columns: ['name'] },
];
const { rerender } = render(
<ObjectView schema={schema} dataSource={mockDataSource} views={initialViews} activeViewId="all" />,
);
// Update with filter — simulates live preview of filter changes
const updatedViews = [
{ id: 'all', label: 'All', type: 'grid' as const, columns: ['name'], filter: [['status', '=', 'active']] },
];
rerender(
<ObjectView schema={schema} dataSource={mockDataSource} views={updatedViews} activeViewId="all" />,
);
expect(screen.getByTestId('object-grid')).toBeInTheDocument();
});
it('should re-render when views prop updates with appearance properties', async () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
};
const initialViews = [
{ id: 'all', label: 'All', type: 'grid' as const, columns: ['name'] },
];
const { rerender } = render(
<ObjectView schema={schema} dataSource={mockDataSource} views={initialViews} activeViewId="all" />,
);
// Update with appearance changes — simulates live preview of rowHeight/striped/bordered
const updatedViews = [
{ id: 'all', label: 'All', type: 'grid' as const, columns: ['name'], striped: true, bordered: true },
];
rerender(
<ObjectView schema={schema} dataSource={mockDataSource} views={updatedViews} activeViewId="all" />,
);
expect(screen.getByTestId('object-grid')).toBeInTheDocument();
});
it('should pass renderListView with updated schema when views change', async () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
};
const renderListViewSpy = vi.fn(({ schema: listSchema }: any) => (
<div data-testid="custom-list" data-fields={JSON.stringify(listSchema.fields)}>
Custom ListView
</div>
));
const initialViews = [
{ id: 'all', label: 'All', type: 'grid' as const, columns: ['name'] },
];
const { rerender } = render(
<ObjectView
schema={schema}
dataSource={mockDataSource}
views={initialViews}
activeViewId="all"
renderListView={renderListViewSpy}
/>,
);
expect(screen.getByTestId('custom-list')).toBeInTheDocument();
const firstCallSchema = renderListViewSpy.mock.calls[0]?.[0]?.schema;
expect(firstCallSchema?.fields).toEqual(['name']);
// Update views — simulate live preview change
const updatedViews = [
{ id: 'all', label: 'All', type: 'grid' as const, columns: ['name', 'email', 'status'] },
];
rerender(
<ObjectView
schema={schema}
dataSource={mockDataSource}
views={updatedViews}
activeViewId="all"
renderListView={renderListViewSpy}
/>,
);
// renderListView should have been called again with the updated columns
const lastCallIndex = renderListViewSpy.mock.calls.length - 1;
const lastCallSchema = renderListViewSpy.mock.calls[lastCallIndex]?.[0]?.schema;
expect(lastCallSchema?.fields).toEqual(['name', 'email', 'status']);
});
it('should pass showSort=false through schema to suppress sort UI', async () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
showSort: false,
};
render(
<ObjectView schema={schema} dataSource={mockDataSource} />,
);
// Component renders without crash — showSort is respected
expect(screen.getByTestId('object-grid')).toBeInTheDocument();
});
it('should include showSearch/showFilters/showSort in renderListView schema', async () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
showSearch: false,
showFilters: false,
showSort: false,
};
const renderListViewSpy = vi.fn(({ schema: listSchema }: any) => (
<div data-testid="custom-list">Custom ListView</div>
));
render(
<ObjectView
schema={schema}
dataSource={mockDataSource}
renderListView={renderListViewSpy}
/>,
);
expect(renderListViewSpy).toHaveBeenCalled();
const callSchema = renderListViewSpy.mock.calls[0]?.[0]?.schema;
expect(callSchema?.showSearch).toBe(false);
expect(callSchema?.showFilters).toBe(false);
expect(callSchema?.showSort).toBe(false);
});
it('should propagate showSearch/showFilters/showSort from activeView in renderListView', async () => {
const schema: ObjectViewSchema = {
type: 'object-view',
objectName: 'contacts',
};
const renderListViewSpy = vi.fn(({ schema: listSchema }: any) => (
<div data-testid="custom-list">Custom ListView</div>
));
const views = [
{ id: 'v1', label: 'View 1', type: 'grid' as const, showSearch: false, showFilters: false, showSort: false },
];
render(
<ObjectView
schema={schema}
dataSource={mockDataSource}
views={views}
activeViewId="v1"
renderListView={renderListViewSpy}
/>,
);
expect(renderListViewSpy).toHaveBeenCalled();
const callSchema = renderListViewSpy.mock.calls[0]?.[0]?.schema;
expect(callSchema?.showSearch).toBe(false);
expect(callSchema?.showFilters).toBe(false);
expect(callSchema?.showSort).toBe(false);
});
});
});