|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#27524, Target 7 — a page a list opens counts and steps through |
| 5 | + * the rows that list showed. The list hands them along: in the page's `from` |
| 6 | + * state whenever the record is one of them (a row click on an object whose |
| 7 | + * rows open as pages, a record link), and in the ↗'s way back to the pane as |
| 8 | + * well (`recordDrawerReturnState`). A record the list does not show gets the |
| 9 | + * origin alone, so its page draws no counter. |
| 10 | + */ |
| 11 | + |
| 12 | +import * as React from 'react'; |
| 13 | +import { describe, it, expect, vi } from 'vitest'; |
| 14 | +import { act, renderHook } from '@testing-library/react'; |
| 15 | +import { MemoryRouter, type Location } from 'react-router-dom'; |
| 16 | + |
| 17 | +import { useObjectViewRecordNavigation } from './objectViewRecordNavigation'; |
| 18 | + |
| 19 | +const ROWS = [{ id: 'r-1' }, { id: 'r-2' }, { id: 'r-3' }]; |
| 20 | +const LIST = { pathname: '/apps/crm/crm_lead', search: '?view=all', state: null, hash: '', key: 'k' } as Location; |
| 21 | + |
| 22 | +function mount(navigation?: { mode: 'page' | 'drawer' }) { |
| 23 | + const navigate = vi.fn(); |
| 24 | + const { result } = renderHook(() => |
| 25 | + useObjectViewRecordNavigation({ |
| 26 | + activeView: { id: 'all', name: 'all', label: 'All leads' } as never, |
| 27 | + objectDef: { name: 'crm_lead', label: 'Lead', fields: {}, ...(navigation ? { navigation } : {}) } as never, |
| 28 | + objects: [{ name: 'crm_lead', label: 'Lead' }] as never, |
| 29 | + appName: 'crm', |
| 30 | + viewId: undefined, |
| 31 | + searchParams: new URLSearchParams('view=all'), |
| 32 | + setSearchParams: vi.fn(), |
| 33 | + location: LIST, |
| 34 | + navigate, |
| 35 | + routerRootHref: '/', |
| 36 | + servedViews: { views: [], objects: [] } as never, |
| 37 | + viewLabel: (_object: string, _view: string, fallback: string) => fallback, |
| 38 | + unlabelledViewText: 'Untitled view', |
| 39 | + language: 'en', |
| 40 | + recordOpenPreference: 'object' as never, |
| 41 | + }), |
| 42 | + { wrapper: ({ children }: { children: React.ReactNode }) => <MemoryRouter initialEntries={['/apps/crm/crm_lead?view=all']}>{children}</MemoryRouter> }, |
| 43 | + ); |
| 44 | + act(() => result.current.handleListRowsChange(ROWS)); |
| 45 | + return { result, navigate }; |
| 46 | +} |
| 47 | + |
| 48 | +const lastState = (navigate: ReturnType<typeof vi.fn>) => navigate.mock.calls.at(-1)![1].state; |
| 49 | + |
| 50 | +describe('a page the list opens carries the list\'s rows (objectstack#27524, Target 7)', () => { |
| 51 | + it('a row that opens as a page: the origin, its view\'s name and the rows shown', () => { |
| 52 | + const { result, navigate } = mount({ mode: 'page' }); |
| 53 | + act(() => result.current.handleRowClick({ id: 'r-2' })); |
| 54 | + expect(navigate).toHaveBeenCalledTimes(1); |
| 55 | + expect(navigate.mock.calls[0][0]).toBe('record/r-2'); |
| 56 | + expect(lastState(navigate)).toEqual({ |
| 57 | + from: { pathname: '/apps/crm/crm_lead?view=all', label: 'All leads', siblings: ['r-1', 'r-2', 'r-3'] }, |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('control: a record the list does not show gets the origin alone', () => { |
| 62 | + const { result, navigate } = mount({ mode: 'page' }); |
| 63 | + act(() => result.current.handleRowClick({ id: 'r-9' })); |
| 64 | + expect(lastState(navigate)).toEqual({ from: { pathname: '/apps/crm/crm_lead?view=all', label: 'All leads' } }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('the row\'s ↗: the rows go in the way back to the pane, and in the origin', () => { |
| 68 | + const { result, navigate } = mount(); |
| 69 | + act(() => result.current.handleOpenRowAsPage('r-3')); |
| 70 | + const state = lastState(navigate); |
| 71 | + expect(state.from.siblings).toEqual(['r-1', 'r-2', 'r-3']); |
| 72 | + expect(state.recordDrawerReturn.path).toEqual([{ objectName: 'crm_lead', recordId: 'r-3', siblings: ['r-1', 'r-2', 'r-3'] }]); |
| 73 | + expect(state.recordDrawerReturn.search).toBe('?view=all&recordId=r-3'); |
| 74 | + }); |
| 75 | +}); |
0 commit comments