-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathNavigationSearchTelemetry.test.tsx
More file actions
319 lines (276 loc) · 10.5 KB
/
Copy pathNavigationSearchTelemetry.test.tsx
File metadata and controls
319 lines (276 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import * as logging from '../../telemetry/logging'
import { cooldownStore } from '../shared/cooldown.store'
import { NavigationSearch } from './NavigationSearch'
import { SearchResultsList } from './SearchResultsList'
import { navigationSearchStore } from './navigationSearch.store'
import * as queryHook from './useNavigationSearchQuery'
import { EuiProvider } from '@elastic/eui'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import * as React from 'react'
// Mock htmx - it uses XPath which jsdom doesn't support properly
jest.mock('htmx.org', () => ({
on: jest.fn(),
off: jest.fn(),
process: jest.fn(),
ajax: jest.fn(),
}))
// Mock the telemetry logging functions
jest.mock('../../telemetry/logging', () => ({
logInfo: jest.fn(),
logWarn: jest.fn(),
}))
// Mock search results for result click tests
const mockSearchResults = {
results: [
{
url: '/docs/elasticsearch/guide',
title: 'Elasticsearch Guide',
description: 'Learn about Elasticsearch',
score: 0.95,
type: 'docs' as const,
parents: [{ title: 'Docs' }, { title: 'Elasticsearch' }],
},
{
url: '/docs/kibana/dashboard',
title: 'Kibana Dashboard',
description: 'Create dashboards',
score: 0.85,
type: 'docs' as const,
parents: [{ title: 'Docs' }, { title: 'Kibana' }],
},
],
totalResults: 2,
pageCount: 1,
}
// Create a fresh QueryClient for each test
const createTestQueryClient = () =>
new QueryClient({
defaultOptions: {
queries: { retry: false },
mutations: { retry: false },
},
})
// Wrapper component for tests
const renderWithProviders = (ui: React.ReactElement) => {
const testQueryClient = createTestQueryClient()
return render(
<EuiProvider
colorMode="light"
globalStyles={false}
utilityClasses={false}
>
<QueryClientProvider client={testQueryClient}>
{ui}
</QueryClientProvider>
</EuiProvider>
)
}
// Helper to reset all stores
const resetStores = () => {
navigationSearchStore.getState().actions.clearSearchTerm()
cooldownStore.setState({
cooldowns: {
search: { cooldown: null, awaitingNewInput: false },
askAi: { cooldown: null, awaitingNewInput: false },
},
})
}
describe('Navigation Search Telemetry Integration', () => {
beforeEach(() => {
jest.clearAllMocks()
resetStores()
})
afterEach(() => {
jest.restoreAllMocks()
})
describe('Opening Navigation Search', () => {
it('should track navigation_search_opened when input is focused', async () => {
// Arrange
renderWithProviders(<NavigationSearch />)
const input = screen.getByPlaceholderText(/jump to page/i)
// Act
await userEvent.click(input)
// Assert
expect(logging.logInfo).toHaveBeenCalledWith(
'navigation_search_opened',
{
'navigation_search.trigger': 'focus',
}
)
})
it('should track keyboard_shortcut trigger when opened via Cmd+K', async () => {
// Arrange
renderWithProviders(<NavigationSearch />)
// Act - simulate Cmd+K
await userEvent.keyboard('{Meta>}k{/Meta}')
// Assert
expect(logging.logInfo).toHaveBeenCalledWith(
'navigation_search_opened',
{
'navigation_search.trigger': 'keyboard_shortcut',
}
)
})
})
describe('Closing Navigation Search', () => {
it('should track navigation_search_closed with escape reason when pressing Escape', async () => {
// Arrange
renderWithProviders(<NavigationSearch />)
const input = screen.getByPlaceholderText(/jump to page/i)
// Act - focus and type, then escape
await userEvent.click(input)
await userEvent.type(input, 'elasticsearch')
jest.clearAllMocks() // Clear the opened event
await userEvent.keyboard('{Escape}')
// Assert
expect(logging.logInfo).toHaveBeenCalledWith(
'navigation_search_closed',
expect.objectContaining({
'navigation_search.close_reason': 'escape',
'navigation_search.query': 'elasticsearch',
})
)
})
it('should track navigation_search_closed with blur reason when clicking outside', async () => {
// Arrange
renderWithProviders(
<div>
<NavigationSearch />
<button data-testid="outside">Outside</button>
</div>
)
const input = screen.getByPlaceholderText(/jump to page/i)
// Act - focus, type, then click outside
await userEvent.click(input)
await userEvent.type(input, 'test')
jest.clearAllMocks()
await userEvent.click(screen.getByTestId('outside'))
// Assert
expect(logging.logInfo).toHaveBeenCalledWith(
'navigation_search_closed',
expect.objectContaining({
'navigation_search.close_reason': 'blur',
})
)
})
it('should include hadResults and hadSelection in close event', async () => {
// Arrange
renderWithProviders(<NavigationSearch />)
const input = screen.getByPlaceholderText(/jump to page/i)
// Act - focus, type, then escape without results
await userEvent.click(input)
await userEvent.type(input, 'test')
jest.clearAllMocks()
await userEvent.keyboard('{Escape}')
// Assert - should have hadResults and hadSelection fields
expect(logging.logInfo).toHaveBeenCalledWith(
'navigation_search_closed',
expect.objectContaining({
'navigation_search.had_results': expect.any(Boolean),
'navigation_search.had_selection': expect.any(Boolean),
})
)
})
})
describe('Error Tracking', () => {
it('should track navigation_search_rate_limited on 429 response', async () => {
// Arrange - mock 429 response
global.fetch = jest.fn().mockResolvedValue({
ok: false,
status: 429,
headers: {
get: (name: string) =>
name === 'Retry-After' ? '30' : null,
},
json: () => Promise.resolve({ error: 'Rate limited' }),
})
renderWithProviders(<NavigationSearch />)
const input = screen.getByPlaceholderText(/jump to page/i)
// Act
await userEvent.click(input)
await userEvent.type(input, 'test query')
// Assert - wait for rate limit warning
await waitFor(() => {
expect(logging.logWarn).toHaveBeenCalledWith(
'navigation_search_rate_limited',
expect.objectContaining({
'navigation_search.query': expect.any(String),
})
)
})
})
})
})
describe('Navigation Search Result Click Tracking', () => {
// Shared props for SearchResultsList - reduces duplication
const createResultsListProps = () => ({
isKeyboardNavigating: { current: false },
onMouseMove: jest.fn(),
onResultClick: jest.fn(),
})
beforeEach(() => {
jest.clearAllMocks()
resetStores()
// Set up the store with a search term
navigationSearchStore.getState().actions.setSearchTerm('elasticsearch')
// Mock the query hook to return results
jest.spyOn(queryHook, 'useNavigationSearchQuery').mockReturnValue({
isLoading: false,
isFetching: false,
data: mockSearchResults,
error: null,
} as ReturnType<typeof queryHook.useNavigationSearchQuery>)
})
afterEach(() => {
jest.restoreAllMocks()
})
it('should track result click with query, position, url, and score', async () => {
// Arrange
const props = createResultsListProps()
renderWithProviders(<SearchResultsList {...props} />)
// Act
await userEvent.click(screen.getByText('Elasticsearch Guide'))
// Assert - verify all required telemetry fields
expect(logging.logInfo).toHaveBeenCalledWith(
'navigation_search_result_clicked',
{
'navigation_search.query': 'elasticsearch',
'navigation_search.result.position': 0,
'navigation_search.result.url': '/docs/elasticsearch/guide',
'navigation_search.result.score': 0.95,
}
)
expect(props.onResultClick).toHaveBeenCalledTimes(1)
})
it('should track correct position for each result (0-indexed)', async () => {
// Arrange
const props = createResultsListProps()
renderWithProviders(<SearchResultsList {...props} />)
// Act - click second result
await userEvent.click(screen.getByText('Kibana Dashboard'))
// Assert
expect(logging.logInfo).toHaveBeenCalledWith(
'navigation_search_result_clicked',
expect.objectContaining({
'navigation_search.result.position': 1,
})
)
})
it('should use current search term from store in telemetry', async () => {
// Arrange - change the search term after initial setup
navigationSearchStore.getState().actions.setSearchTerm('updated query')
const props = createResultsListProps()
renderWithProviders(<SearchResultsList {...props} />)
// Act
await userEvent.click(screen.getByText('Elasticsearch Guide'))
// Assert - query should reflect the updated store value
expect(logging.logInfo).toHaveBeenCalledWith(
'navigation_search_result_clicked',
expect.objectContaining({
'navigation_search.query': 'updated query',
})
)
})
})