|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#27525 — metadata-admin's nav inspector reaches the report index |
| 5 | + * the way the Studio inspector does: by REMOVING `reportName`, never by |
| 6 | + * writing `''`. |
| 7 | + * |
| 8 | + * A `report` item with no `reportName` opens the app's report index. The |
| 9 | + * target combo offered only reports, so an author could not get back to the |
| 10 | + * index once a report was picked, and the one empty value the console reads |
| 11 | + * as the index (`''`) is a value `defineStack` refuses as an unknown report. |
| 12 | + * The combo now leads with a 「Report index」 row for a `report` item; picking |
| 13 | + * it drops the key. |
| 14 | + * |
| 15 | + * - picking the index row writes the item with no `reportName` key; |
| 16 | + * - the trigger names the index while the item has no `reportName`; |
| 17 | + * - CONTROL: picking a report still writes its name; |
| 18 | + * - CONTROL: another type's target combo offers no empty row. |
| 19 | + */ |
| 20 | + |
| 21 | +import * as React from 'react'; |
| 22 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 23 | +import { render, screen, cleanup } from '@testing-library/react'; |
| 24 | +import userEvent from '@testing-library/user-event'; |
| 25 | +import { MetadataCtx } from '@object-ui/react'; |
| 26 | + |
| 27 | +// ONE client for the whole file (see AppNavInspector.labelInherits-11196). |
| 28 | +const client = vi.hoisted(() => ({ |
| 29 | + list: async (type: string) => |
| 30 | + type === 'report' ? [{ name: 'pipeline', label: 'Pipeline' }] : type === 'page' ? [{ name: 'home', label: 'Home' }] : [], |
| 31 | +})); |
| 32 | + |
| 33 | +vi.mock('../useMetadata', async (importOriginal) => ({ |
| 34 | + ...(await importOriginal<Record<string, unknown>>()), |
| 35 | + useMetadataClient: () => client, |
| 36 | +})); |
| 37 | + |
| 38 | +import { AppNavInspector } from './AppNavInspector'; |
| 39 | + |
| 40 | +afterEach(cleanup); |
| 41 | + |
| 42 | +const METADATA = { |
| 43 | + apps: [], |
| 44 | + objects: [], |
| 45 | + dashboards: [], |
| 46 | + reports: [{ name: 'pipeline', label: 'Pipeline' }], |
| 47 | + pages: [], |
| 48 | + loading: false, |
| 49 | + error: null, |
| 50 | + refresh: async () => {}, |
| 51 | + invalidate: () => {}, |
| 52 | + ensureType: async () => [], |
| 53 | + getItem: async () => null, |
| 54 | + getItemsByType: () => [], |
| 55 | + getTypeStatus: () => 'ready' as const, |
| 56 | +}; |
| 57 | + |
| 58 | +function inspect(item: Record<string, unknown>) { |
| 59 | + const onPatch = vi.fn(); |
| 60 | + render( |
| 61 | + <MetadataCtx.Provider value={METADATA as never}> |
| 62 | + <AppNavInspector |
| 63 | + type="app" |
| 64 | + name="crm" |
| 65 | + selection={{ kind: 'nav', id: 'navigation[0]' }} |
| 66 | + draft={{ name: 'crm', navigation: [item] }} |
| 67 | + onPatch={onPatch} |
| 68 | + onClearSelection={() => {}} |
| 69 | + locale="en-US" |
| 70 | + readOnly={false} |
| 71 | + /> |
| 72 | + </MetadataCtx.Provider>, |
| 73 | + ); |
| 74 | + return onPatch; |
| 75 | +} |
| 76 | + |
| 77 | +/** The single nav item an `onPatch` call wrote. */ |
| 78 | +function written(onPatch: ReturnType<typeof vi.fn>): Record<string, unknown> { |
| 79 | + expect(onPatch).toHaveBeenCalledTimes(1); |
| 80 | + return (onPatch.mock.calls[0][0] as { navigation: Array<Record<string, unknown>> }).navigation[0]; |
| 81 | +} |
| 82 | + |
| 83 | +const NAMED = { id: 'nav_pipeline', type: 'report', label: 'Pipeline', reportName: 'pipeline' }; |
| 84 | +const INDEX = { id: 'nav_reports', type: 'report', label: 'Reports' }; |
| 85 | + |
| 86 | +describe('objectstack#27525 — AppNavInspector clears a report target by removing reportName', () => { |
| 87 | + it("picking the Report index row removes the key, never writes ''", async () => { |
| 88 | + const user = userEvent.setup(); |
| 89 | + const onPatch = inspect(NAMED); |
| 90 | + await user.click(screen.getByRole('combobox', { name: 'Report' })); |
| 91 | + await user.click(await screen.findByRole('option', { name: /Report index/ })); |
| 92 | + const item = written(onPatch); |
| 93 | + expect(Object.prototype.hasOwnProperty.call(item, 'reportName')).toBe(false); |
| 94 | + expect(item).toEqual({ id: 'nav_pipeline', type: 'report', label: 'Pipeline' }); |
| 95 | + }); |
| 96 | + |
| 97 | + it('the trigger names the index while the item has no reportName', () => { |
| 98 | + inspect(INDEX); |
| 99 | + expect(screen.getByRole('combobox', { name: 'Report' }).textContent).toContain('Report index'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('CONTROL: picking a report writes its name', async () => { |
| 103 | + const user = userEvent.setup(); |
| 104 | + const onPatch = inspect(INDEX); |
| 105 | + await user.click(screen.getByRole('combobox', { name: 'Report' })); |
| 106 | + await user.click(await screen.findByRole('option', { name: /pipeline/ })); |
| 107 | + expect(written(onPatch).reportName).toBe('pipeline'); |
| 108 | + }); |
| 109 | + |
| 110 | + it("CONTROL: a page item's target combo offers no empty row", async () => { |
| 111 | + const user = userEvent.setup(); |
| 112 | + inspect({ id: 'nav_home', type: 'page', label: 'Home', pageName: 'home' }); |
| 113 | + await user.click(screen.getByRole('combobox', { name: 'Page' })); |
| 114 | + await screen.findByRole('option', { name: /home/ }); |
| 115 | + expect(screen.queryByRole('option', { name: /Report index/ })).toBeNull(); |
| 116 | + }); |
| 117 | +}); |
0 commit comments