Skip to content

Commit 36063d5

Browse files
committed
test(console): pin who may share a view, and the share acts (#36371)
Claude-Session: https://claude.ai/code/session_01QsR2jTPMb2q2wRssyREqhQ Co-authored-by: Claude <noreply@anthropic.com>
1 parent 227a2fb commit 36063d5

2 files changed

Lines changed: 249 additions & 0 deletions

File tree

‎ui/packages/app-shell/src/views/objectViewTitleSwitcher.test.tsx‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,44 @@ describe('objectstack#30046 — a host with no reader of its own hands no pin an
235235
expect(onViewChange).toHaveBeenCalledWith('board');
236236
});
237237
});
238+
239+
describe('objectstack#36371 — 「Make shared」 and 「Unshare」 on the dropdown rows', () => {
240+
const SHARING_VIEWS: TitleSwitcherView[] = [
241+
{ id: 'all', label: 'All Leads', type: 'grid', authored: true },
242+
{ id: 'crm_lead.team', label: 'Team', type: 'grid', authored: false, unsharable: true },
243+
{ id: 'crm_lead.packaged', label: 'Packaged', type: 'grid', authored: false },
244+
{ id: 'pv_1', label: 'Mine', type: 'grid', authored: false, personal: true },
245+
];
246+
247+
it('a reader who may share gets Make shared on their own views and Unshare on an unsharable shared view only', () => {
248+
const onMakeSharedView = vi.fn();
249+
const onUnshareView = vi.fn();
250+
const { menu } = mount({ views: SHARING_VIEWS, onMakeSharedView, onUnshareView });
251+
const row = (id: string) => within(menu).getByTestId(`view-switcher-row-${id}`);
252+
expect(within(row('pv_1')).getByTestId('view-switcher-make-shared')).toBeInTheDocument();
253+
expect(within(row('pv_1')).queryByTestId('view-switcher-unshare')).toBeNull();
254+
expect(within(row('crm_lead.team')).getByTestId('view-switcher-unshare')).toBeInTheDocument();
255+
expect(within(row('crm_lead.team')).queryByTestId('view-switcher-make-shared')).toBeNull();
256+
// A view the object authors, and a shared view not marked unsharable (a
257+
// package's), offer neither.
258+
for (const id of ['all', 'crm_lead.packaged']) {
259+
expect(within(row(id)).queryByTestId('view-switcher-unshare')).toBeNull();
260+
expect(within(row(id)).queryByTestId('view-switcher-make-shared')).toBeNull();
261+
}
262+
fireEvent.click(within(row('pv_1')).getByTestId('view-switcher-make-shared'));
263+
expect(onMakeSharedView).toHaveBeenCalledWith('pv_1');
264+
});
265+
266+
it('Unshare hands the view id to the host', () => {
267+
const onUnshareView = vi.fn();
268+
const { menu } = mount({ views: SHARING_VIEWS, onUnshareView });
269+
fireEvent.click(within(within(menu).getByTestId('view-switcher-row-crm_lead.team')).getByTestId('view-switcher-unshare'));
270+
expect(onUnshareView).toHaveBeenCalledWith('crm_lead.team');
271+
});
272+
273+
it('a reader who may not share (no handlers) is offered neither, whatever the rows say', () => {
274+
const { menu } = mount({ views: SHARING_VIEWS });
275+
expect(within(menu).queryByTestId('view-switcher-make-shared')).toBeNull();
276+
expect(within(menu).queryByTestId('view-switcher-unshare')).toBeNull();
277+
});
278+
});
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* objectstack#36371 — who may share a list's views, and the three acts that
5+
* share or unshare one (`./objectViewViewSharing.ts`).
6+
*
7+
* WHAT IS PINNED.
8+
* - Who: an administrator whose session holds `manage_metadata`; an
9+
* administrator without it (an organization administrator) is not offered
10+
* a control the server refuses.
11+
* - Create, shared: saved and published in one act, never left as a draft.
12+
* - Make shared: the reader's row becomes a published view under a key no
13+
* served view holds, then the row is deleted and the pin follows it.
14+
* - Unshare: the view becomes one of the reader's own, then is deleted; a
15+
* delete the server refuses takes the copy back.
16+
* - Which shared views may be unshared: one saved here, never a view the
17+
* object authors, a read-only package ships or a package's customization.
18+
*/
19+
20+
import { describe, it, expect, vi, beforeEach } from 'vitest';
21+
import { renderHook, act } from '@testing-library/react';
22+
23+
const toast = vi.hoisted(() => ({ success: vi.fn(), error: vi.fn(), warning: vi.fn(), info: vi.fn() }));
24+
vi.mock('sonner', () => ({ toast }));
25+
26+
import { freeViewKey, isUnsharableView, mayShareViews, useViewSharing } from './objectViewViewSharing';
27+
import type { PersonalView } from './personalViews';
28+
29+
const t = ((key: string, options?: { defaultValue?: string; name?: string }) =>
30+
(options?.defaultValue ?? key).replace('{{name}}', options?.name ?? '')) as never;
31+
32+
const OBJECT = 'crm_lead';
33+
const OBJECT_DEF = { name: OBJECT, fields: { name: { type: 'text' } } };
34+
35+
function rig(over: { personal?: PersonalView[]; savedViews?: unknown[]; deleteView?: (...a: unknown[]) => Promise<unknown>; pinned?: string | null; activeViewId?: string } = {}) {
36+
const metadataClient = {
37+
save: vi.fn(async () => ({})),
38+
publish: vi.fn(async () => ({})),
39+
reset: vi.fn(async () => ({})),
40+
};
41+
const dataSource = { deleteView: vi.fn(over.deleteView ?? (async () => ({ deleted: true }))) };
42+
const personalViews = {
43+
views: over.personal ?? [],
44+
save: vi.fn(async () => 'pv_copy'),
45+
remove: vi.fn(async () => {}),
46+
};
47+
const navigate = vi.fn();
48+
const setPinnedViewId = vi.fn();
49+
const refresh = vi.fn();
50+
const confirmHandler = vi.fn(async () => true);
51+
const viewIds = ['all', `${OBJECT}.team`, ...(over.personal ?? []).map((v) => v.id)];
52+
const { result } = renderHook(() => useViewSharing({
53+
objectName: OBJECT,
54+
objectDef: OBJECT_DEF,
55+
metadataClient,
56+
dataSource,
57+
app: { packageId: null },
58+
personalViews,
59+
savedViews: over.savedViews ?? [],
60+
viewIds,
61+
labelOf: (id) => (id === `${OBJECT}.team` ? 'Team' : id),
62+
activeViewId: over.activeViewId,
63+
routeViewId: undefined,
64+
navigate,
65+
pinnedViewId: over.pinned ?? null,
66+
setPinnedViewId,
67+
refresh,
68+
confirmHandler: confirmHandler as never,
69+
t,
70+
}));
71+
return { result, metadataClient, dataSource, personalViews, navigate, setPinnedViewId, refresh, confirmHandler };
72+
}
73+
74+
beforeEach(() => {
75+
for (const fn of Object.values(toast)) fn.mockClear();
76+
});
77+
78+
describe('objectstack#36371 — who may share views', () => {
79+
it('an administrator holding manage_metadata; neither half alone', () => {
80+
expect(mayShareViews(true, true)).toBe(true);
81+
expect(mayShareViews(true, false)).toBe(false); // an organization administrator
82+
expect(mayShareViews(false, true)).toBe(false);
83+
expect(mayShareViews(false, false)).toBe(false);
84+
});
85+
});
86+
87+
describe('objectstack#36371 — which shared views may be unshared', () => {
88+
it('a saved, published view of the list, and no other', () => {
89+
expect(isUnsharableView({}, { name: 'team' } as never)).toBe(true);
90+
expect(isUnsharableView({ readonly: true }, undefined)).toBe(false); // authored by the object
91+
expect(isUnsharableView({}, undefined)).toBe(false); // not a saved view
92+
expect(isUnsharableView({ definitionReadonly: true }, {})).toBe(false); // a read-only package ships it
93+
expect(isUnsharableView({ customized: true }, {})).toBe(false); // a package view's customization
94+
expect(isUnsharableView({}, { _draft: true })).toBe(false); // an unpublished draft
95+
});
96+
});
97+
98+
describe('objectstack#36371 — the key a view made shared is saved under', () => {
99+
it('the key its name gives, numbered past every served view', () => {
100+
expect(freeViewKey(OBJECT, 'Team', 'grid', ['all'])).toBe('team');
101+
expect(freeViewKey(OBJECT, 'Team', 'grid', [`${OBJECT}.team`])).toBe('team_2');
102+
expect(freeViewKey(OBJECT, 'Team', 'grid', ['team', `${OBJECT}.team_2`])).toBe('team_3');
103+
});
104+
});
105+
106+
describe('objectstack#36371 — Create, shared', () => {
107+
it('saves and publishes the new view in one act, and opens it', async () => {
108+
const r = rig();
109+
let created = false;
110+
await act(async () => {
111+
created = await r.result.current.createShared({ type: 'grid', label: 'Hot leads', name: 'hot_leads', columns: ['name'] });
112+
});
113+
expect(created).toBe(true);
114+
expect(r.metadataClient.save).toHaveBeenCalledTimes(1);
115+
const [type, name, , options] = r.metadataClient.save.mock.calls[0] as unknown as [string, string, unknown, Record<string, unknown>];
116+
expect([type, name]).toEqual(['view', `${OBJECT}.hot_leads`]);
117+
expect(options).toEqual({ mode: 'draft', ifNoneMatch: '*' });
118+
expect(r.metadataClient.publish).toHaveBeenCalledWith('view', `${OBJECT}.hot_leads`);
119+
expect(r.navigate).toHaveBeenCalledWith(`view/${OBJECT}.hot_leads`);
120+
});
121+
122+
it('a refused save is said and reported unsaved; nothing is published', async () => {
123+
const r = rig();
124+
r.metadataClient.save.mockRejectedValueOnce(Object.assign(new Error('Saving metadata requires the `manage_metadata` capability.'), { status: 403 }));
125+
let created = true;
126+
await act(async () => {
127+
created = await r.result.current.createShared({ type: 'grid', label: 'Hot leads', name: 'hot_leads' });
128+
});
129+
expect(created).toBe(false);
130+
expect(r.metadataClient.publish).not.toHaveBeenCalled();
131+
expect(toast.error).toHaveBeenCalled();
132+
});
133+
});
134+
135+
describe('objectstack#36371 — Make shared', () => {
136+
const MINE: PersonalView = { id: 'pv_1', label: 'Team', config: { type: 'kanban', columns: ['name'], kanban: { groupByField: 'name' } } };
137+
138+
it('publishes the row\'s body as a new view under a free key, then deletes the row and moves the pin', async () => {
139+
const r = rig({ personal: [MINE], pinned: 'pv_1', activeViewId: 'pv_1' });
140+
await act(async () => { await r.result.current.makeShared('pv_1'); });
141+
expect(r.confirmHandler).toHaveBeenCalledTimes(1);
142+
const [, name, body] = r.metadataClient.save.mock.calls[0] as unknown as [string, string, Record<string, any>];
143+
// `team` is served already, so the next free key.
144+
expect(name).toBe(`${OBJECT}.team_2`);
145+
expect(body).toMatchObject({ name: `${OBJECT}.team_2`, label: 'Team', config: { type: 'kanban', kanban: { groupByField: 'name' }, data: { object: OBJECT } } });
146+
expect(r.metadataClient.publish).toHaveBeenCalledWith('view', `${OBJECT}.team_2`);
147+
expect(r.personalViews.remove).toHaveBeenCalledWith('pv_1');
148+
expect(r.setPinnedViewId).toHaveBeenCalledWith(`${OBJECT}.team_2`);
149+
expect(r.navigate).toHaveBeenCalledWith(`view/${OBJECT}.team_2`);
150+
expect(toast.success).toHaveBeenCalled();
151+
});
152+
153+
it('a declined confirm writes nothing', async () => {
154+
const r = rig({ personal: [MINE] });
155+
r.confirmHandler.mockResolvedValueOnce(false);
156+
await act(async () => { await r.result.current.makeShared('pv_1'); });
157+
expect(r.metadataClient.save).not.toHaveBeenCalled();
158+
expect(r.personalViews.remove).not.toHaveBeenCalled();
159+
});
160+
161+
it('a refused publish keeps the reader\'s row: nothing changed', async () => {
162+
const r = rig({ personal: [MINE] });
163+
r.metadataClient.publish.mockRejectedValueOnce(new Error('refused'));
164+
await act(async () => { await r.result.current.makeShared('pv_1'); });
165+
expect(r.personalViews.remove).not.toHaveBeenCalled();
166+
expect(toast.error).toHaveBeenCalled();
167+
});
168+
});
169+
170+
describe('objectstack#36371 — Unshare', () => {
171+
const SAVED = { id: `${OBJECT}.team`, name: `${OBJECT}.team`, label: 'Team', type: 'grid', columns: ['name'], filter: [['name', '=', 'x']] };
172+
173+
it('copies the view into the reader\'s own views, then deletes the shared view and opens the copy', async () => {
174+
const r = rig({ savedViews: [SAVED], pinned: `${OBJECT}.team`, activeViewId: `${OBJECT}.team` });
175+
await act(async () => { await r.result.current.unshare(`${OBJECT}.team`); });
176+
expect(r.personalViews.save).toHaveBeenCalledTimes(1);
177+
const [label, config] = r.personalViews.save.mock.calls[0] as unknown as [string, Record<string, any>];
178+
expect(label).toBe('Team');
179+
expect(config).toMatchObject({ type: 'grid', columns: ['name'], filter: [['name', '=', 'x']], data: { object: OBJECT } });
180+
expect(r.dataSource.deleteView).toHaveBeenCalledWith(OBJECT, `${OBJECT}.team`);
181+
expect(r.personalViews.remove).not.toHaveBeenCalled();
182+
expect(r.setPinnedViewId).toHaveBeenCalledWith('pv_copy');
183+
expect(r.navigate).toHaveBeenCalledWith('view/pv_copy');
184+
});
185+
186+
it('a delete the server refuses takes the copy back, and says so', async () => {
187+
const r = rig({ savedViews: [SAVED], deleteView: async () => { throw new Error('Deleting metadata requires the `manage_metadata` capability.'); } });
188+
await act(async () => { await r.result.current.unshare(`${OBJECT}.team`); });
189+
expect(r.personalViews.remove).toHaveBeenCalledWith('pv_copy');
190+
expect(r.refresh).not.toHaveBeenCalled();
191+
expect(toast.error).toHaveBeenCalled();
192+
});
193+
194+
it('a delete that answers deleted: false is a refusal too', async () => {
195+
const r = rig({ savedViews: [SAVED], deleteView: async () => ({ deleted: false }) });
196+
await act(async () => { await r.result.current.unshare(`${OBJECT}.team`); });
197+
expect(r.personalViews.remove).toHaveBeenCalledWith('pv_copy');
198+
expect(r.navigate).not.toHaveBeenCalled();
199+
});
200+
201+
it('a view that is not a saved view of the list is left alone', async () => {
202+
const r = rig({ savedViews: [] });
203+
await act(async () => { await r.result.current.unshare('all'); });
204+
expect(r.confirmHandler).not.toHaveBeenCalled();
205+
expect(r.personalViews.save).not.toHaveBeenCalled();
206+
expect(r.dataSource.deleteView).not.toHaveBeenCalled();
207+
});
208+
});

0 commit comments

Comments
 (0)