Skip to content

Commit 2fe51af

Browse files
committed
feat: updated cursor bot and test case file
1 parent 76f2913 commit 2fe51af

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed

frontend/src/components/cmdKPalette/__test__/cmdkPalette.test.tsx

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,30 @@ import '@testing-library/jest-dom/extend-expect';
66
// ---- Mocks (must run BEFORE importing the component) ----
77
import ROUTES from 'constants/routes';
88
import history from 'lib/history';
9-
// ---- Now import test helpers & component ----
109
import { render, screen, userEvent } from 'tests/test-utils';
1110

1211
import { CmdKPalette } from '../cmdKPalette';
1312

1413
const HOME_LABEL = 'Go to Home';
14+
1515
beforeAll(() => {
1616
Object.defineProperty(HTMLElement.prototype, 'scrollIntoView', {
1717
configurable: true,
1818
value: jest.fn(),
1919
});
2020
});
21+
2122
afterAll(() => {
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
3130
jest.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
9286
class 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
});

frontend/src/components/cmdKPalette/cmdKPalette.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
/* Overlay stays below content */
2+
[data-slot='dialog-overlay'] {
3+
z-index: 50;
4+
}
5+
6+
/* Dialog content always above overlay */
7+
[data-slot='dialog-content'] {
8+
position: fixed;
9+
z-index: 60;
10+
}
11+
12+
113
.cmdk-section-heading [cmdk-group-heading] {
214
text-transform: uppercase;
315
color: var(--bg-slate-100);

frontend/src/components/cmdKPalette/cmdKPalette.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function CmdKPalette({
7878
): void {
7979
if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 'k') {
8080
e.preventDefault();
81-
setOpen((s) => !s);
81+
setOpen(true);
8282
}
8383
}
8484

@@ -91,6 +91,7 @@ export function CmdKPalette({
9191

9292
return (): void => {
9393
window.removeEventListener('keydown', listener);
94+
setOpen(false);
9495
};
9596
};
9697

0 commit comments

Comments
 (0)