Repository navigation
Expand file tree
/
Copy pathObjectView.formLoadsOnOpen-37444.test.tsx
More file actions
76 lines (68 loc) · 3.18 KB
/
Copy pathObjectView.formLoadsOnOpen-37444.test.tsx
File metadata and controls
76 lines (68 loc) · 3.18 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
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* objectstack#37444 — an object view loads `@object-ui/plugin-form` the first
* time its create / edit form opens, not with the list.
*
* `ObjectView` imported `ObjectForm` statically, so every list's first screen
* fetched the whole form package, though the form is drawn only inside the
* drawer or modal that mounts its content when it opens. The bundle half (which
* chunks a list load fetches) is measured on the built console and recorded on
* the PR. What a unit test can pin is that the form module is not evaluated
* until a form opens.
*
* The instrument: `vi.mock(path, factory)` runs `factory` the first time the
* module is imported, and not before. A STATIC import of the form package in
* `ObjectView`'s graph runs it while this file imports `ObjectView`, before any
* case starts, and the first case reds. The module registry is per FILE, so the
* case that needs it unloaded runs first, and the later case asserts the count
* it starts from.
*/
import { describe, it, expect, vi } from 'vitest';
import { render, screen, waitFor, act, fireEvent } from '@testing-library/react';
import type { DataSource, ObjectViewSchema } from '@object-ui/types';
const loads = vi.hoisted(() => ({ form: 0 }));
vi.mock('@object-ui/plugin-grid', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
ObjectGrid: () => <div data-testid="object-grid" />,
}));
vi.mock('@object-ui/plugin-form', async (importOriginal) => {
loads.form += 1;
return {
...(await importOriginal<Record<string, unknown>>()),
ObjectForm: ({ schema }: { schema: { mode?: string } }) => <div data-testid="object-form" data-mode={schema.mode} />,
};
});
import { ObjectView } from '../ObjectView';
function makeDataSource() {
const state = { settled: false };
const ds = {
find: vi.fn().mockResolvedValue({ data: [], total: 0 }),
findOne: vi.fn(),
create: vi.fn(),
update: vi.fn(),
delete: vi.fn(),
getObjectSchema: vi.fn(async () => {
state.settled = true;
return { name: 'task', label: 'Tasks', fields: { name: { type: 'text' } } };
}),
};
return { ds: ds as unknown as DataSource, state };
}
describe('objectstack#37444 — the object view loads the form package when a form opens', () => {
it('importing and drawing the list evaluates no form module', async () => {
expect(loads.form).toBe(0);
const { ds, state } = makeDataSource();
const schema = { type: 'object-view', objectName: 'task', layout: 'drawer' } as unknown as ObjectViewSchema;
render(<ObjectView schema={schema} dataSource={ds} />);
await screen.findByTestId('object-grid');
await waitFor(() => expect(state.settled, 'the object schema was never read').toBe(true));
await act(async () => {});
expect(screen.queryByTestId('object-form')).toBeNull();
expect(loads.form).toBe(0);
// Opening the create form loads it, and the form is drawn for that verb.
fireEvent.click(screen.getByRole('button', { name: 'New' }));
const form = await screen.findByTestId('object-form');
expect(form.getAttribute('data-mode')).toBe('create');
expect(loads.form).toBe(1);
});
});