This repository was archived by the owner on Nov 4, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathObjectView.refreshSignal.test.tsx
More file actions
163 lines (147 loc) · 5.93 KB
/
Copy pathObjectView.refreshSignal.test.tsx
File metadata and controls
163 lines (147 loc) · 5.93 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
/**
* 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.
*/
/**
* ObjectView's refresh signal has exactly the entry points that are reachable
* (objectui#4568).
*
* ## Why this file exists
*
* `ObjectView` kept a `handleRefresh` callback that bumped `refreshKey` and was
* referenced by nothing — not passed to the toolbar, not exposed on a handle,
* not wired to any control. It read as "ObjectView has a refresh entry point"
* to anyone scanning for one, which cost objectui#4549 a detour to rule out.
* Triage ruled deletion rather than wiring it to a new toolbar button: the
* button is a feature with no recorded pull, and the affordance already exists
* downstream (see below). objectui#4568 removed the callback.
*
* A dead-code deletion is only safe once the surviving paths are pinned, so
* this file asserts the ones that remain rather than asserting the absence of
* the one that went. `refreshKey` itself stays — it is read by the non-grid
* fetch effect, by the child-view remount key, and is forwarded to a host list
* view as `refreshKey` / `refreshTrigger`.
*
* ## Where the real refresh button lives
*
* Not here. `packages/plugin-list/src/ListView.tsx` renders a toolbar Refresh
* button gated on `toolbarFlags.showRefresh` (derived from
* `userActions.refresh`) driving ListView's OWN refresh counter. ObjectView
* reaches it by delegating through `renderListView` — and deliberately does not
* auto-subscribe in that mode, which is the third test below. That delegation
* is why the deleted callback had no toolbar to attach to in the first place.
*/
import { describe, it, expect, vi } from 'vitest';
import { render, waitFor, act } from '@testing-library/react';
import { ObjectView } from '../ObjectView';
import type { ObjectViewSchema, DataSourceMutationEvent } from '@object-ui/types';
vi.mock('@object-ui/react', async (importOriginal) => {
const React = await import('react');
return {
...(await importOriginal<Record<string, unknown>>()),
SchemaRenderer: ({ schema }: any) => (
<div data-testid="schema-renderer">{schema?.type}</div>
),
SchemaRendererContext: React.createContext(null),
subscribeDataChanges: () => () => {},
notifyDataChanged: () => {},
};
});
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) => ({
...(await importOriginal<Record<string, unknown>>()),
ObjectForm: () => <div data-testid="object-form" />,
}));
/**
* A DataSource whose `onMutation` hands the registered callback back to the
* test, so a mutation can be emitted the way a real adapter would.
*/
function makeDataSource() {
const subscribers: ((event: DataSourceMutationEvent) => void)[] = [];
const unsubscribe = vi.fn();
const onMutation = vi.fn((cb: (event: DataSourceMutationEvent) => void) => {
subscribers.push(cb);
return unsubscribe;
});
const find = vi.fn().mockResolvedValue({ data: [], total: 0 });
const ds: any = {
find,
findOne: vi.fn(),
create: vi.fn(),
update: vi.fn(),
delete: vi.fn(),
getObjectSchema: vi.fn().mockResolvedValue({ name: 'task', fields: {} }),
onMutation,
};
return {
ds,
find,
onMutation,
unsubscribe,
emit: async (event: DataSourceMutationEvent) => {
await act(async () => {
subscribers.forEach(cb => cb(event));
});
},
};
}
/** A non-grid view type, because the grid branch fetches its own data. */
const KANBAN_SCHEMA = {
type: 'object-view',
objectName: 'task',
defaultViewType: 'kanban',
} as unknown as ObjectViewSchema;
describe('ObjectView refresh signal (objectui#4568)', () => {
it('re-fetches when the DataSource reports a mutation on the same object', async () => {
const { ds, find, emit } = makeDataSource();
render(<ObjectView schema={KANBAN_SCHEMA} dataSource={ds} />);
await waitFor(() => expect(find).toHaveBeenCalledTimes(1));
await emit({ type: 'create', resource: 'task', id: '1' });
await waitFor(
() => expect(find).toHaveBeenCalledTimes(2),
{ timeout: 2000 },
);
});
it('ignores a mutation reported for a different object', async () => {
const { ds, find, emit } = makeDataSource();
render(<ObjectView schema={KANBAN_SCHEMA} dataSource={ds} />);
await waitFor(() => expect(find).toHaveBeenCalledTimes(1));
await emit({ type: 'create', resource: 'not_task', id: '1' });
// Nothing should re-run the fetch. Give the effect a turn to misbehave
// before asserting it did not.
await act(async () => { await Promise.resolve(); });
expect(
find,
'A mutation on an unrelated object re-queried this view. The auto-subscribe\n'
+ 'is gated on `event.resource === schema.objectName`; losing that gate turns\n'
+ 'every mutation anywhere into a refetch storm.',
).toHaveBeenCalledTimes(1);
});
it('does not auto-subscribe when a host list view is supplied', async () => {
const { ds, onMutation } = makeDataSource();
render(
<ObjectView
schema={KANBAN_SCHEMA}
dataSource={ds}
renderListView={({ refreshKey }) => (
<div data-testid="host-list" data-refresh-key={refreshKey} />
)}
/>,
);
await waitFor(() => expect(ds.getObjectSchema).toHaveBeenCalled());
expect(
onMutation,
'ObjectView auto-subscribed to mutations while a host list view was supplied.\n'
+ 'The host (e.g. plugin-list ListView) owns its own fetching AND its own\n'
+ 'toolbar Refresh button; subscribing here double-fetches. This delegation is\n'
+ 'also why ObjectView never had a refresh control of its own for the\n'
+ 'objectui#4568 callback to attach to.',
).not.toHaveBeenCalled();
});
});