1212import * as React from 'react' ;
1313import { describe , it , expect , vi } from 'vitest' ;
1414import { act , renderHook } from '@testing-library/react' ;
15- import { MemoryRouter , type Location } from 'react-router-dom' ;
15+ import { MemoryRouter , useLocation , useNavigate , useSearchParams , type Location } from 'react-router-dom' ;
1616
1717import { useObjectViewRecordNavigation } from './objectViewRecordNavigation' ;
1818
@@ -111,7 +111,7 @@ describe('a lookup chip opens the record it names in the list\'s drawer (objects
111111 ) ;
112112 act ( ( ) => result . current . handleListRowsChange ( ROWS ) ) ;
113113 const lastParams = ( ) => String ( setSearchParams . mock . calls . at ( - 1 ) ! [ 0 ] ) ;
114- return { result, navigate, lastParams } ;
114+ return { result, navigate, lastParams, setSearchParams } ;
115115 }
116116
117117 it ( 'another object\'s record: its id and its object in the URL, beside the list\'s own params' , ( ) => {
@@ -120,6 +120,54 @@ describe('a lookup chip opens the record it names in the list\'s drawer (objects
120120 expect ( lastParams ( ) ) . toBe ( 'view=all&recordId=a7&recordObject=crm_account' ) ;
121121 } ) ;
122122
123+ it ( 'the record the drawer already shows is not pushed again, so one Back still closes it' , ( ) => {
124+ const foreign = mountDrawer ( 'view=all&recordId=a7&recordObject=crm_account' ) ;
125+ act ( ( ) => foreign . result . current . handleOpenReferencedRecord ( 'crm_account' , 'a7' ) ) ;
126+ const own = mountDrawer ( 'view=all&recordId=r-2' ) ;
127+ act ( ( ) => own . result . current . handleOpenReferencedRecord ( 'crm_lead' , 'r-2' ) ) ;
128+ expect ( foreign . setSearchParams ) . not . toHaveBeenCalled ( ) ;
129+ expect ( own . setSearchParams ) . not . toHaveBeenCalled ( ) ;
130+ // The same id under another object is another record.
131+ act ( ( ) => foreign . result . current . handleOpenReferencedRecord ( 'crm_lead' , 'a7' ) ) ;
132+ expect ( foreign . lastParams ( ) ) . toBe ( 'view=all&recordId=a7' ) ;
133+ } ) ;
134+
135+ it ( 'in a router: a chip opened twice (a double-click) leaves one history entry, and one Back closes the drawer' , ( ) => {
136+ // The console's declarative router pushes on every navigate, the same URL included.
137+ function useRouted ( ) {
138+ const [ searchParams , setSearchParams ] = useSearchParams ( ) ;
139+ const location = useLocation ( ) ;
140+ const navigate = useNavigate ( ) ;
141+ const nav = useObjectViewRecordNavigation ( {
142+ activeView : { id : 'all' , name : 'all' , label : 'All leads' } as never ,
143+ objectDef : { name : 'crm_lead' , label : 'Lead' , fields : { } } as never ,
144+ objects : OBJECTS as never ,
145+ appName : 'crm' ,
146+ viewId : undefined ,
147+ searchParams,
148+ setSearchParams,
149+ location,
150+ navigate,
151+ routerRootHref : '/' ,
152+ servedViews : { views : [ ] , objects : [ ] } as never ,
153+ viewLabel : ( _object : string , _view : string , fallback : string ) => fallback ,
154+ unlabelledViewText : 'Untitled view' ,
155+ language : 'en' ,
156+ phone : false ,
157+ } ) ;
158+ return { nav, search : location . search , back : ( ) => navigate ( - 1 ) } ;
159+ }
160+ const { result } = renderHook ( useRouted , {
161+ wrapper : ( { children } : { children : React . ReactNode } ) => < MemoryRouter initialEntries = { [ '/apps/crm/crm_lead?view=all' ] } > { children } </ MemoryRouter > ,
162+ } ) ;
163+ act ( ( ) => result . current . nav . handleListRowsChange ( ROWS ) ) ;
164+ act ( ( ) => result . current . nav . handleOpenReferencedRecord ( 'crm_account' , 'a7' ) ) ;
165+ act ( ( ) => result . current . nav . handleOpenReferencedRecord ( 'crm_account' , 'a7' ) ) ;
166+ expect ( result . current . search ) . toBe ( '?view=all&recordId=a7&recordObject=crm_account' ) ;
167+ act ( ( ) => result . current . back ( ) ) ;
168+ expect ( result . current . search ) . toBe ( '?view=all' ) ;
169+ } ) ;
170+
123171 it ( 'a record of the listed object opens as the name cell\'s icon opens it, with no object param' , ( ) => {
124172 const { result, lastParams } = mountDrawer ( 'view=all&recordId=a7&recordObject=crm_account' ) ;
125173 act ( ( ) => result . current . handleOpenReferencedRecord ( 'crm_lead' , 'r-2' ) ) ;
0 commit comments