Skip to content

Commit 2fb74b2

Browse files
committed
tests: add tests around keyboard shortcuts
1 parent ca5f9e9 commit 2fb74b2

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import { afterEach, beforeEach, describe, expect, test, vi, vitest } from 'vitest'
2+
import { render, waitFor } from '@testing-library/react'
3+
import { MemoryRouter } from 'react-router'
4+
import { KBarProvider } from 'kbar'
5+
import * as router from 'react-router'
6+
import userEvent from '@testing-library/user-event'
7+
8+
import OmniBar from '~/components/OmniBar'
9+
10+
import { settingStore } from '~/core/setting/SettingStore'
11+
import { chatStore } from '~/core/chat/ChatStore'
12+
import { focusStore } from '~/core/FocusStore'
13+
import { connectionStore } from '../../core/connection/ConnectionStore'
14+
import { setServerResponse } from '../msw'
15+
16+
describe('OmniBar', () => {
17+
const navigate = vi.fn()
18+
19+
beforeEach(() => {
20+
vi.spyOn(router, 'useNavigate').mockImplementation(() => navigate)
21+
vi.clearAllMocks()
22+
})
23+
24+
afterEach(() => {
25+
vi.restoreAllMocks()
26+
})
27+
28+
const renderOmniBar = () => {
29+
return render(
30+
<MemoryRouter initialEntries={['/']}>
31+
<KBarProvider>
32+
<OmniBar />
33+
</KBarProvider>
34+
</MemoryRouter>,
35+
)
36+
}
37+
38+
describe('keyboard shortcuts', () => {
39+
let unmountOmnibar: () => void
40+
41+
beforeEach(() => {
42+
const { unmount } = renderOmniBar()
43+
44+
unmountOmnibar = unmount
45+
46+
expect(navigate).not.toHaveBeenCalled()
47+
})
48+
49+
afterEach(() => {
50+
unmountOmnibar()
51+
})
52+
53+
test('registers Ctrl+K to navigate to search', async () => {
54+
await userEvent.keyboard('{Control>}{K}')
55+
56+
await waitFor(() => {
57+
expect(navigate).toHaveBeenCalledWith('/search')
58+
})
59+
})
60+
61+
test('registers Ctrl+/ to navigate to initial settings', async () => {
62+
await userEvent.keyboard('{Control>}/')
63+
64+
await waitFor(() => {
65+
expect(navigate).toHaveBeenCalledWith('/initial')
66+
})
67+
})
68+
69+
test('registers Ctrl+. to navigate to model panel', async () => {
70+
expect(navigate).not.toHaveBeenCalled()
71+
72+
await userEvent.keyboard('{Control>}{.}')
73+
74+
await waitFor(() => {
75+
expect(navigate).toHaveBeenCalledWith('/models')
76+
})
77+
})
78+
79+
test('registers Ctrl+. to navigate to selected model panel', async () => {
80+
setServerResponse('https://api.openai.com/v1/models', {
81+
data: [],
82+
})
83+
84+
const selectedConnection = await connectionStore.addConnection('OpenAi')
85+
86+
await userEvent.keyboard('{Control>}{.}')
87+
88+
await waitFor(() => {
89+
expect(navigate).toHaveBeenCalledWith('/models/' + selectedConnection.id)
90+
})
91+
})
92+
93+
test('registers Ctrl+; to open persona menu', async () => {
94+
await userEvent.keyboard('{Control>}{;}')
95+
96+
await waitFor(() => {
97+
expect(navigate).toHaveBeenCalledWith('/personas')
98+
})
99+
})
100+
101+
test('registers Ctrl+Shift+M to toggle sidebar', async () => {
102+
expect(settingStore.setting.isSidebarOpen).toBe(true)
103+
104+
await userEvent.keyboard('{Control>}{M}')
105+
106+
expect(settingStore.setting.isSidebarOpen).toBe(false)
107+
})
108+
109+
test('registers Cmd+Shift+O to create a new chat', async () => {
110+
vitest.spyOn(chatStore, 'createChat')
111+
112+
expect(chatStore.createChat).not.toHaveBeenCalled()
113+
114+
await userEvent.keyboard('{Control>}{Shift>}{O}')
115+
116+
expect(chatStore.createChat).toHaveBeenCalled()
117+
})
118+
119+
test('registers Ctrl+J to focus chat input', async () => {
120+
vitest.spyOn(focusStore, 'focusChatInput')
121+
122+
expect(focusStore.focusChatInput).not.toHaveBeenCalled()
123+
124+
await userEvent.keyboard('{Control>}{J}')
125+
126+
expect(focusStore.focusChatInput).toHaveBeenCalled()
127+
})
128+
})
129+
})

0 commit comments

Comments
 (0)