@@ -6,33 +6,30 @@ import '@testing-library/jest-dom/extend-expect';
66// ---- Mocks (must run BEFORE importing the component) ----
77import ROUTES from 'constants/routes' ;
88import history from 'lib/history' ;
9- // ---- Now import test helpers & component ----
109import { render , screen , userEvent } from 'tests/test-utils' ;
1110
1211import { CmdKPalette } from '../cmdKPalette' ;
1312
1413const HOME_LABEL = 'Go to Home' ;
14+
1515beforeAll ( ( ) => {
1616 Object . defineProperty ( HTMLElement . prototype , 'scrollIntoView' , {
1717 configurable : true ,
1818 value : jest . fn ( ) ,
1919 } ) ;
2020} ) ;
21+
2122afterAll ( ( ) => {
2223 // restore
2324 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2425 // @ts -ignore
2526 delete ( HTMLElement . prototype as any ) . scrollIntoView ;
2627} ) ;
2728
28- // mock history.push
29- // at top of your test file (replace existing history mock)
30- // put at top of test file (before other imports)
29+ // mock history.push / replace / go / location
3130jest . mock ( 'lib/history' , ( ) => {
32- // shared location object (used by code that reads history.location.search)
3331 const location = { pathname : '/' , search : '' , hash : '' } ;
3432
35- // simple internal stack to show different semantics for push vs replace
3633 const stack : { pathname : string ; search : string } [ ] = [
3734 { pathname : '/' , search : '' } ,
3835 ] ;
@@ -42,11 +39,9 @@ jest.mock('lib/history', () => {
4239 const pathname = rawPath || '/' ;
4340 const search = path . includes ( '?' ) ? `?${ rawQuery || '' } ` : '' ;
4441
45- // update public location
4642 location . pathname = pathname ;
4743 location . search = search ;
4844
49- // append new history entry
5045 stack . push ( { pathname, search } ) ;
5146 return undefined ;
5247 } ) ;
@@ -56,11 +51,9 @@ jest.mock('lib/history', () => {
5651 const pathname = rawPath || '/' ;
5752 const search = path . includes ( '?' ) ? `?${ rawQuery || '' } ` : '' ;
5853
59- // update public location
6054 location . pathname = pathname ;
6155 location . search = search ;
6256
63- // overwrite the current entry instead of appending
6457 if ( stack . length > 0 ) {
6558 stack [ stack . length - 1 ] = { pathname, search } ;
6659 } else {
@@ -88,6 +81,7 @@ jest.mock('lib/history', () => {
8881 __stack : stack ,
8982 } ;
9083} ) ;
84+
9185// Mock ResizeObserver for Jest/jsdom
9286class ResizeObserver {
9387 // eslint-disable-next-line @typescript-eslint/explicit-function-return-type, class-methods-use-this
@@ -157,15 +151,8 @@ describe('CmdKPalette', () => {
157151 jest . clearAllMocks ( ) ;
158152 } ) ;
159153
160- test ( 'returns null when not logged in' , ( ) => {
161- const { container } = render (
162- < CmdKPalette userRole = "ADMIN" isLoggedInState = { false } /> ,
163- ) ;
164- expect ( container ) . toBeEmptyDOMElement ( ) ;
165- } ) ;
166-
167- test ( 'renders navigation and settings groups and items when logged in' , ( ) => {
168- render ( < CmdKPalette userRole = "ADMIN" isLoggedInState /> ) ;
154+ test ( 'renders navigation and settings groups and items' , ( ) => {
155+ render ( < CmdKPalette userRole = "ADMIN" /> ) ;
169156
170157 expect ( screen . getByText ( 'Navigation' ) ) . toBeInTheDocument ( ) ;
171158 expect ( screen . getByText ( 'Settings' ) ) . toBeInTheDocument ( ) ;
@@ -177,7 +164,7 @@ describe('CmdKPalette', () => {
177164 } ) ;
178165
179166 test ( 'clicking a navigation item calls history.push with correct route' , async ( ) => {
180- render ( < CmdKPalette userRole = "ADMIN" isLoggedInState /> ) ;
167+ render ( < CmdKPalette userRole = "ADMIN" /> ) ;
181168
182169 const homeItem = screen . getByText ( HOME_LABEL ) ;
183170 await userEvent . click ( homeItem ) ;
@@ -186,30 +173,36 @@ describe('CmdKPalette', () => {
186173 } ) ;
187174
188175 test ( 'role-based filtering (basic smoke)' , ( ) => {
189- render ( < CmdKPalette userRole = "VIEWER" isLoggedInState /> ) ;
176+ render ( < CmdKPalette userRole = "VIEWER" /> ) ;
177+
178+ // VIEWER still sees basic navigation items
190179 expect ( screen . getByText ( HOME_LABEL ) ) . toBeInTheDocument ( ) ;
191180 } ) ;
192181
193- test ( 'keyboard shortcut toggles open via setOpen' , ( ) => {
194- render ( < CmdKPalette userRole = "ADMIN" isLoggedInState /> ) ;
182+ test ( 'keyboard shortcut opens palette via setOpen' , ( ) => {
183+ render ( < CmdKPalette userRole = "ADMIN" /> ) ;
195184
196185 const event = new KeyboardEvent ( 'keydown' , { key : 'k' , ctrlKey : true } ) ;
197186 window . dispatchEvent ( event ) ;
198187
199- expect ( mockSetOpen ) . toHaveBeenCalled ( ) ;
188+ expect ( mockSetOpen ) . toHaveBeenCalledWith ( true ) ;
200189 } ) ;
201190
202191 test ( 'items render with icons when provided' , ( ) => {
203- render ( < CmdKPalette userRole = "ADMIN" isLoggedInState /> ) ;
192+ render ( < CmdKPalette userRole = "ADMIN" /> ) ;
193+
204194 const iconHolders = document . querySelectorAll ( '.cmd-item-icon' ) ;
205195 expect ( iconHolders . length ) . toBeGreaterThan ( 0 ) ;
206196 expect ( screen . getByText ( HOME_LABEL ) ) . toBeInTheDocument ( ) ;
207197 } ) ;
208198
209199 test ( 'closing the palette via handleInvoke sets open to false' , async ( ) => {
210- render ( < CmdKPalette userRole = "ADMIN" isLoggedInState /> ) ;
200+ render ( < CmdKPalette userRole = "ADMIN" /> ) ;
201+
211202 const dashItem = screen . getByText ( 'Go to Dashboards' ) ;
212203 await userEvent . click ( dashItem ) ;
213- expect ( mockSetOpen ) . toHaveBeenCalled ( ) ;
204+
205+ // last call from handleInvoke should set open to false
206+ expect ( mockSetOpen ) . toHaveBeenCalledWith ( false ) ;
214207 } ) ;
215208} ) ;
0 commit comments