|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#36011 — Manage views restores the factory version of a view a |
| 5 | + * read-only package ships, after a pin, a rename or a set-default wrote the |
| 6 | + * view into the package's customization pair (ruling record 7058654416 keeps |
| 7 | + * those three on such a view). |
| 8 | + * |
| 9 | + * A row the list serves with the override declaration naming the package it |
| 10 | + * is served under (`overrides`, ADR-0146 D2) is marked "customized" and |
| 11 | + * offers "Restore factory version". One confirm says every user of the view |
| 12 | + * sees the factory version again; on confirm the host calls the adapter's |
| 13 | + * `restoreFactoryView`, reads the views again and shows the package's own |
| 14 | + * view. A view without a customization offers no restore. A refused restore |
| 15 | + * says why, with the door's message. |
| 16 | + * |
| 17 | + * What runs: the real `ObjectView` and the real `ManageViewsDialog` (held |
| 18 | + * open), under the real English `I18nProvider`, on a stub data source whose |
| 19 | + * list answers what the server stores: the pin writes the override, the |
| 20 | + * restore removes it. `ListView` and the view panel are stand-ins. |
| 21 | + */ |
| 22 | + |
| 23 | +import * as React from 'react'; |
| 24 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 25 | +import { render, cleanup, act, waitFor, screen, fireEvent, within } from '@testing-library/react'; |
| 26 | +import { toast } from 'sonner'; |
| 27 | +import { I18nProvider } from '@object-ui/i18n'; |
| 28 | +import { MemoryRouter, Routes, Route } from 'react-router-dom'; |
| 29 | +// `ObjectView` reaches a dynamic `import('@objectstack/lint')`: pay its load at |
| 30 | +// collection, never inside a case's clocked window (`check:test-source-alias`). |
| 31 | +import '@objectstack/lint'; |
| 32 | + |
| 33 | +vi.mock('@object-ui/permissions', async (importOriginal) => { |
| 34 | + const actual = await importOriginal<typeof import('@object-ui/permissions')>(); |
| 35 | + // Stable identities: `ListView` names `perms` in its fetch dependencies. |
| 36 | + const perms = { |
| 37 | + check: () => ({ allowed: true }), |
| 38 | + checkField: () => true, |
| 39 | + getFieldPermissions: () => [], |
| 40 | + getRowFilter: () => undefined, |
| 41 | + getObjectApiOperations: () => undefined, |
| 42 | + roles: [], |
| 43 | + isLoaded: false, |
| 44 | + hasCapabilities: () => true, |
| 45 | + can: () => true, |
| 46 | + cannot: () => false, |
| 47 | + }; |
| 48 | + const fieldPerms = { canRead: () => true, canWrite: () => true, permissions: [] }; |
| 49 | + return { ...actual, usePermissions: () => perms, useFieldPermissions: () => fieldPerms }; |
| 50 | +}); |
| 51 | + |
| 52 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 53 | + ...(await importOriginal<Record<string, unknown>>()), |
| 54 | + useAuth: () => ({ user: { id: 'u1', name: 'Ada' }, activeOrganization: { id: 'org1', name: 'Acme' } }), |
| 55 | + // Manage views and the view panel are an administrator's. |
| 56 | + useWorkspaceAdminStatus: () => ({ isAdmin: true, isResolved: true }), |
| 57 | + createAuthenticatedFetch: () => vi.fn(), |
| 58 | +})); |
| 59 | + |
| 60 | +vi.mock('@object-ui/collaboration', async (importOriginal) => ({ |
| 61 | + ...(await importOriginal<Record<string, unknown>>()), |
| 62 | + useRealtimeSubscription: () => ({ lastMessage: null }), |
| 63 | + useConflictResolution: () => ({ hasConflicts: false, resolveAllConflicts: () => {} }), |
| 64 | +})); |
| 65 | + |
| 66 | +vi.mock('sonner', () => ({ |
| 67 | + toast: Object.assign(vi.fn(), { |
| 68 | + success: vi.fn(), error: vi.fn(), info: vi.fn(), |
| 69 | + warning: vi.fn(), loading: vi.fn(), dismiss: vi.fn(), |
| 70 | + }), |
| 71 | +})); |
| 72 | + |
| 73 | +vi.mock('./MetadataInspector', () => ({ |
| 74 | + MetadataPanel: () => null, |
| 75 | + useMetadataInspector: () => ({ showDebug: false, toggle: () => {} }), |
| 76 | +})); |
| 77 | +vi.mock('./RecordDetailView', () => ({ RecordDetailView: () => null })); |
| 78 | + |
| 79 | +vi.mock('@object-ui/plugin-list', async (importOriginal) => ({ |
| 80 | + ...(await importOriginal<typeof import('@object-ui/plugin-list')>()), |
| 81 | + ListView: () => null, |
| 82 | +})); |
| 83 | + |
| 84 | +// The real dialog, held open: the host opens it from the title switcher. |
| 85 | +vi.mock('@object-ui/plugin-view', async (importOriginal) => { |
| 86 | + const actual = await importOriginal<typeof import('@object-ui/plugin-view')>(); |
| 87 | + const Dialog = actual.ManageViewsDialog; |
| 88 | + return { |
| 89 | + ...actual, |
| 90 | + ManageViewsDialog: (props: React.ComponentProps<typeof Dialog>) => ( |
| 91 | + <Dialog {...props} open onOpenChange={() => {}} /> |
| 92 | + ), |
| 93 | + }; |
| 94 | +}); |
| 95 | + |
| 96 | +vi.mock('./ViewConfigPanel', () => ({ ViewConfigPanel: () => null })); |
| 97 | + |
| 98 | +/** The session-held package list, as `GET /api/v1/packages` answers it (parsed). */ |
| 99 | +const packageList = vi.hoisted(() => ({ answer: (): Promise<any[]> => Promise.resolve([]) })); |
| 100 | +vi.mock('./studio-design/packages-io', async (importOriginal) => ({ |
| 101 | + ...(await importOriginal<Record<string, unknown>>()), |
| 102 | + fetchSessionPackages: vi.fn(() => packageList.answer()), |
| 103 | +})); |
| 104 | + |
| 105 | +import { ObjectView } from './ObjectView'; |
| 106 | +import { ExpressionProvider } from '../providers/ExpressionProvider'; |
| 107 | + |
| 108 | +const OBJECT = 'showcase_task'; |
| 109 | +const BASE = 'com.example.showcase'; |
| 110 | +const PAIR = 'com.example.showcase.custom'; |
| 111 | +const VIEW = `${OBJECT}.in_progress`; |
| 112 | +const OTHER = `${OBJECT}.done`; |
| 113 | + |
| 114 | +/** The package's own view, as `listViews()` serves it. */ |
| 115 | +const factory = (name: string, label: string) => ({ |
| 116 | + name, object: OBJECT, label, type: 'grid', columns: ['name', 'amount'], _packageId: BASE, |
| 117 | +}); |
| 118 | + |
| 119 | +const PACKAGES = [ |
| 120 | + { id: BASE, name: 'Showcase', writable: false, namespace: 'showcase' }, |
| 121 | + { id: PAIR, name: 'Showcase (custom)', writable: true, namespace: 'showcase' }, |
| 122 | +]; |
| 123 | + |
| 124 | +/** |
| 125 | + * The stored state behind the stub: the pair's override of VIEW, or none. |
| 126 | + * `updateView` writes the override (served under the base, declaring it); |
| 127 | + * `restoreFactoryView` removes it, or throws `refusal` when one is set. |
| 128 | + */ |
| 129 | +function makeDataSource(opts: { refusal?: Error } = {}) { |
| 130 | + let override: Record<string, unknown> | null = null; |
| 131 | + const dataSource = { |
| 132 | + find: vi.fn(async () => ({ data: [], total: 0 })), |
| 133 | + findOne: vi.fn(async () => null), |
| 134 | + create: vi.fn(async () => ({})), |
| 135 | + update: vi.fn(async () => ({})), |
| 136 | + delete: vi.fn(async () => ({})), |
| 137 | + getObjectSchema: vi.fn(async () => ({ name: OBJECT, fields: {} })), |
| 138 | + listViews: vi.fn(async () => [override ?? factory(VIEW, 'In progress'), factory(OTHER, 'Done')]), |
| 139 | + updateView: vi.fn(async (_object: string, name: string, patch: Record<string, unknown>) => { |
| 140 | + if (name !== VIEW) throw new Error('only VIEW is customized in this test'); |
| 141 | + override = { ...factory(VIEW, 'In progress'), ...(override ?? {}), ...patch, overrides: { package: BASE, basedOn: '0.1.0' } }; |
| 142 | + return override; |
| 143 | + }), |
| 144 | + restoreFactoryView: vi.fn(async () => { |
| 145 | + if (opts.refusal) throw opts.refusal; |
| 146 | + override = null; |
| 147 | + return { deleted: true }; |
| 148 | + }), |
| 149 | + }; |
| 150 | + return dataSource; |
| 151 | +} |
| 152 | + |
| 153 | +function mount(dataSource: ReturnType<typeof makeDataSource>) { |
| 154 | + render( |
| 155 | + <I18nProvider config={{ defaultLanguage: 'en', detectBrowserLanguage: false }}> |
| 156 | + <ExpressionProvider user={{ id: 'u1', name: 'Ada', profile: 'admin' }}> |
| 157 | + <MemoryRouter initialEntries={[`/apps/demo/${OBJECT}/view/${encodeURIComponent(OTHER)}`]}> |
| 158 | + <Routes> |
| 159 | + <Route |
| 160 | + path="/apps/:appName/:objectName/view/:viewId" |
| 161 | + element={ |
| 162 | + <ObjectView |
| 163 | + dataSource={dataSource as never} |
| 164 | + objects={[{ |
| 165 | + name: OBJECT, |
| 166 | + label: 'Task', |
| 167 | + fields: { name: { type: 'text', label: 'Name' }, amount: { type: 'number', label: 'Amount' } }, |
| 168 | + }]} |
| 169 | + onEdit={() => {}} |
| 170 | + /> |
| 171 | + } |
| 172 | + /> |
| 173 | + </Routes> |
| 174 | + </MemoryRouter> |
| 175 | + </ExpressionProvider> |
| 176 | + </I18nProvider>, |
| 177 | + ); |
| 178 | +} |
| 179 | + |
| 180 | +const settle = () => act(() => new Promise<void>((resolve) => setTimeout(resolve, 300))); |
| 181 | + |
| 182 | +async function openMenu(id: string) { |
| 183 | + const trigger = await waitFor(() => screen.getByTestId(`manage-views-actions-${id}`)); |
| 184 | + fireEvent.pointerDown(trigger, { button: 0, ctrlKey: false }); |
| 185 | + fireEvent.click(trigger); |
| 186 | + await waitFor(() => expect(screen.getByRole('menu')).toBeTruthy()); |
| 187 | +} |
| 188 | +async function closeMenu() { |
| 189 | + fireEvent.keyDown(screen.getByRole('menu'), { key: 'Escape' }); |
| 190 | + await waitFor(() => expect(screen.queryByRole('menu')).toBeNull()); |
| 191 | +} |
| 192 | +/** Whether the row offers a restore, read by opening its menu. */ |
| 193 | +async function offersRestore(id: string): Promise<boolean> { |
| 194 | + await openMenu(id); |
| 195 | + const offered = screen.queryByTestId(`manage-views-action-restore-${id}`) !== null; |
| 196 | + await closeMenu(); |
| 197 | + return offered; |
| 198 | +} |
| 199 | +const marked = (id: string) => screen.queryByTestId(`manage-views-customized-${id}`) !== null; |
| 200 | + |
| 201 | +/** Pin VIEW from its row: the write that creates the override. */ |
| 202 | +async function pin() { |
| 203 | + await waitFor(async () => expect((await rowReady(VIEW))).toBe(true)); |
| 204 | + fireEvent.click(screen.getByTestId(`manage-views-pin-${VIEW}`)); |
| 205 | +} |
| 206 | +async function rowReady(id: string) { |
| 207 | + // The row is a read-only package's once its configuration reads "View config". |
| 208 | + await openMenu(id); |
| 209 | + const label = screen.queryByTestId(`manage-views-action-config-${id}`)?.textContent?.trim(); |
| 210 | + await closeMenu(); |
| 211 | + return label === 'View config'; |
| 212 | +} |
| 213 | + |
| 214 | +/** Click the row's restore, and answer the confirm (`true` confirms). */ |
| 215 | +async function restore(confirm: boolean) { |
| 216 | + await openMenu(VIEW); |
| 217 | + fireEvent.click(screen.getByTestId(`manage-views-action-restore-${VIEW}`)); |
| 218 | + const dialog = await screen.findByRole('alertdialog'); |
| 219 | + const text = dialog.textContent ?? ''; |
| 220 | + const button = within(dialog).getByRole('button', { name: confirm ? 'Restore' : 'Cancel' }); |
| 221 | + await act(async () => { fireEvent.click(button); }); |
| 222 | + return text; |
| 223 | +} |
| 224 | + |
| 225 | +beforeEach(() => { |
| 226 | + packageList.answer = () => Promise.resolve(PACKAGES); |
| 227 | + localStorage.clear(); |
| 228 | + vi.mocked(toast.error).mockClear(); |
| 229 | + vi.mocked(toast.success).mockClear(); |
| 230 | +}); |
| 231 | + |
| 232 | +afterEach(() => { |
| 233 | + cleanup(); |
| 234 | +}); |
| 235 | + |
| 236 | +describe('objectstack#36011 — restore the factory version of a customized package view', () => { |
| 237 | + it('pin a factory view, then restore it: the override is removed and the list shows the package\'s own view', async () => { |
| 238 | + const dataSource = makeDataSource(); |
| 239 | + mount(dataSource); |
| 240 | + await settle(); |
| 241 | + // Before: the package's own view, unmarked, no restore. |
| 242 | + await waitFor(async () => expect(await rowReady(VIEW)).toBe(true)); |
| 243 | + expect(marked(VIEW)).toBe(false); |
| 244 | + expect(await offersRestore(VIEW)).toBe(false); |
| 245 | + |
| 246 | + await pin(); |
| 247 | + expect(dataSource.updateView).toHaveBeenCalledWith(OBJECT, VIEW, { isPinned: true }); |
| 248 | + await waitFor(() => expect(marked(VIEW)).toBe(true)); |
| 249 | + expect(await offersRestore(VIEW)).toBe(true); |
| 250 | + // The uncustomized view beside it offers none. |
| 251 | + expect(marked(OTHER)).toBe(false); |
| 252 | + expect(await offersRestore(OTHER)).toBe(false); |
| 253 | + |
| 254 | + const said = await restore(true); |
| 255 | + expect(said).toContain('Everyone who uses this view will see the factory version again'); |
| 256 | + expect(said).toContain('In progress'); |
| 257 | + await waitFor(() => expect(dataSource.restoreFactoryView).toHaveBeenCalledWith(OBJECT, VIEW)); |
| 258 | + // The views are read again: the factory view, unmarked and unpinned. |
| 259 | + await waitFor(() => expect(marked(VIEW)).toBe(false)); |
| 260 | + expect(screen.getByTestId(`manage-views-pin-${VIEW}`).getAttribute('aria-label')).toBe('Pin View'); |
| 261 | + expect(await offersRestore(VIEW)).toBe(false); |
| 262 | + expect(toast.success).toHaveBeenCalledTimes(1); |
| 263 | + expect(toast.error).not.toHaveBeenCalled(); |
| 264 | + }); |
| 265 | + |
| 266 | + it('a cancelled confirm removes nothing', async () => { |
| 267 | + const dataSource = makeDataSource(); |
| 268 | + mount(dataSource); |
| 269 | + await settle(); |
| 270 | + await pin(); |
| 271 | + await waitFor(() => expect(marked(VIEW)).toBe(true)); |
| 272 | + |
| 273 | + await restore(false); |
| 274 | + await settle(); |
| 275 | + expect(dataSource.restoreFactoryView).not.toHaveBeenCalled(); |
| 276 | + expect(marked(VIEW)).toBe(true); |
| 277 | + }); |
| 278 | + |
| 279 | + it('a refused restore says why, with the door\'s message, and the view stays customized', async () => { |
| 280 | + const reason = 'Deleting metadata requires the Manage Metadata permission'; |
| 281 | + const dataSource = makeDataSource({ refusal: Object.assign(new Error(reason), { status: 403, code: 'PERMISSION_DENIED' }) }); |
| 282 | + mount(dataSource); |
| 283 | + await settle(); |
| 284 | + await pin(); |
| 285 | + await waitFor(() => expect(marked(VIEW)).toBe(true)); |
| 286 | + |
| 287 | + await restore(true); |
| 288 | + await waitFor(() => expect(toast.error).toHaveBeenCalledTimes(1)); |
| 289 | + const [title, options] = vi.mocked(toast.error).mock.calls[0] as [string, { description?: string }]; |
| 290 | + expect(title).toBe('Failed to restore the factory version'); |
| 291 | + expect(options.description).toContain(reason); |
| 292 | + expect(toast.success).not.toHaveBeenCalled(); |
| 293 | + expect(marked(VIEW)).toBe(true); |
| 294 | + }); |
| 295 | +}); |
0 commit comments