|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#35115 — a navigation write reads and saves a packaged app at its |
| 5 | + * package. |
| 6 | + * |
| 7 | + * `useNavigationSync` read the stored app (`getAuthoredItem('app', name)`) and |
| 8 | + * saved it (`client.meta.saveItem('app', name, body)`) with no package, so a |
| 9 | + * navigation write into an app a package owns went to the package-less |
| 10 | + * address (no `?package=`). The read, the save and the Undo now name the |
| 11 | + * served app's package (`{ packageId }`, sent as `?package=`); a package-less |
| 12 | + * app, and an app served with the server's `sys_metadata` sentinel, keep the |
| 13 | + * package-less address. |
| 14 | + * |
| 15 | + * Direction, written before the run: with the hook left package-less (the |
| 16 | + * unmodified tree) the packaged case goes RED on the read's and the save's |
| 17 | + * package while the package-less case stays GREEN. |
| 18 | + */ |
| 19 | + |
| 20 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 21 | +import React, { useEffect } from 'react'; |
| 22 | +import { render, waitFor, cleanup } from '@testing-library/react'; |
| 23 | +import { MetadataCtx, AdapterCtx, type MetadataContextValue } from '@object-ui/react'; |
| 24 | +import { toast } from 'sonner'; |
| 25 | +import { useNavigationSync } from '../useNavigationSync'; |
| 26 | + |
| 27 | +vi.mock('sonner', () => ({ |
| 28 | + toast: Object.assign(vi.fn(), { success: vi.fn(), error: vi.fn(), info: vi.fn() }), |
| 29 | +})); |
| 30 | + |
| 31 | +/** The app as its author stored it. */ |
| 32 | +const STORED = { |
| 33 | + name: 'qa_scope_app', |
| 34 | + label: 'QA Scope App', |
| 35 | + navigation: [{ id: 'nav_home', type: 'page', label: 'Home', pageName: 'qa_scope_home' }], |
| 36 | +}; |
| 37 | + |
| 38 | +function metaValue(apps: Record<string, unknown>[]): MetadataContextValue { |
| 39 | + return { |
| 40 | + apps, |
| 41 | + objects: [], |
| 42 | + dashboards: [], |
| 43 | + reports: [], |
| 44 | + pages: [], |
| 45 | + loading: false, |
| 46 | + error: null, |
| 47 | + refresh: async () => {}, |
| 48 | + invalidate: () => {}, |
| 49 | + ensureType: async () => [], |
| 50 | + getItem: async () => null, |
| 51 | + getItemsByType: () => [], |
| 52 | + getTypeStatus: () => 'ready', |
| 53 | + } as unknown as MetadataContextValue; |
| 54 | +} |
| 55 | + |
| 56 | +/** Add a page to the navigation of every app, the way a page publish does. */ |
| 57 | +async function addPage(served: Record<string, unknown>) { |
| 58 | + const saveItem = vi.fn().mockResolvedValue({}); |
| 59 | + const getAuthoredItem = vi.fn(async () => STORED); |
| 60 | + let done = false; |
| 61 | + function Site() { |
| 62 | + const { syncPageCreatedAllApps } = useNavigationSync(); |
| 63 | + useEffect(() => { |
| 64 | + void syncPageCreatedAllApps('qa_scope_new_page').then(() => { done = true; }); |
| 65 | + // Once, on mount: the write is the case. |
| 66 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 67 | + }, []); |
| 68 | + return null; |
| 69 | + } |
| 70 | + const adapter = { getClient: () => ({ meta: { saveItem } }), getAuthoredItem } as unknown as React.ComponentProps< |
| 71 | + typeof AdapterCtx.Provider |
| 72 | + >['value']; |
| 73 | + render( |
| 74 | + <AdapterCtx.Provider value={adapter}> |
| 75 | + <MetadataCtx.Provider value={metaValue([served])}> |
| 76 | + <Site /> |
| 77 | + </MetadataCtx.Provider> |
| 78 | + </AdapterCtx.Provider>, |
| 79 | + ); |
| 80 | + await waitFor(() => expect(done).toBe(true)); |
| 81 | + return { saveItem, getAuthoredItem }; |
| 82 | +} |
| 83 | + |
| 84 | +/** Press the success toast's Undo, and wait for its save. */ |
| 85 | +async function undo(saveItem: ReturnType<typeof vi.fn>) { |
| 86 | + const options = vi.mocked(toast.success).mock.calls[0]?.[1] as unknown as { action: { onClick: () => Promise<void> } }; |
| 87 | + await options.action.onClick(); |
| 88 | + await waitFor(() => expect(saveItem).toHaveBeenCalledTimes(2)); |
| 89 | +} |
| 90 | + |
| 91 | +beforeEach(() => vi.clearAllMocks()); |
| 92 | +afterEach(() => cleanup()); |
| 93 | + |
| 94 | +describe('a navigation write addresses the app at its package (objectstack#35115)', () => { |
| 95 | + it('a packaged app is read, saved and undone at its package', async () => { |
| 96 | + const { saveItem, getAuthoredItem } = await addPage({ ...STORED, _packageId: 'com.qa.scope' }); |
| 97 | + |
| 98 | + expect(getAuthoredItem).toHaveBeenCalledWith('app', 'qa_scope_app', { packageId: 'com.qa.scope' }); |
| 99 | + expect(saveItem).toHaveBeenCalledTimes(1); |
| 100 | + const [type, name, body, options] = saveItem.mock.calls[0] as [string, string, any, unknown]; |
| 101 | + expect([type, name, options]).toEqual(['app', 'qa_scope_app', { packageId: 'com.qa.scope' }]); |
| 102 | + // Control: the write's own change is what it carries. |
| 103 | + expect(body.navigation).toHaveLength(2); |
| 104 | + |
| 105 | + await undo(saveItem); |
| 106 | + expect(saveItem.mock.calls[1]?.[3]).toEqual({ packageId: 'com.qa.scope' }); |
| 107 | + expect((saveItem.mock.calls[1]?.[2] as any).navigation).toHaveLength(1); |
| 108 | + }); |
| 109 | + |
| 110 | + it('a package-less app is read, saved and undone with no package', async () => { |
| 111 | + const { saveItem, getAuthoredItem } = await addPage({ ...STORED }); |
| 112 | + |
| 113 | + expect(getAuthoredItem).toHaveBeenCalledWith('app', 'qa_scope_app'); |
| 114 | + expect(saveItem).toHaveBeenCalledTimes(1); |
| 115 | + expect(saveItem.mock.calls[0]).toHaveLength(3); |
| 116 | + expect(saveItem.mock.calls[0]?.slice(0, 2)).toEqual(['app', 'qa_scope_app']); |
| 117 | + |
| 118 | + await undo(saveItem); |
| 119 | + expect(saveItem.mock.calls[1]).toHaveLength(3); |
| 120 | + }); |
| 121 | + |
| 122 | + it('an app served with the sys_metadata sentinel names no package on the read or the save', async () => { |
| 123 | + const { saveItem, getAuthoredItem } = await addPage({ ...STORED, _packageId: 'sys_metadata' }); |
| 124 | + |
| 125 | + expect(getAuthoredItem).toHaveBeenCalledWith('app', 'qa_scope_app'); |
| 126 | + expect(saveItem).toHaveBeenCalledTimes(1); |
| 127 | + expect(saveItem.mock.calls[0]).toHaveLength(3); |
| 128 | + }); |
| 129 | +}); |
0 commit comments