-
Notifications
You must be signed in to change notification settings - Fork 226
/
search.test.js
298 lines (258 loc) · 11 KB
/
search.test.js
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
const { setupPage } = require('../lib/jest-utilities.js')
const configPaths = require('../lib/paths.js')
const PORT = configPaths.testPort
// Regex that can be used to match on fingerprinted search index files
const isSearchIndex = /.*\/search-index-[0-9a-f]{32}.json$/
let page
const baseUrl = 'http://localhost:' + PORT
beforeEach(async () => {
page = await setupPage()
})
afterEach(async () => {
await page.close()
})
describe('Site search', () => {
it('does not return any results when searching for something that does not exist', async () => {
await page.goto(baseUrl, { waitUntil: 'load' })
await page.waitForSelector('.app-site-search__input')
await page.type('.app-site-search__input', 'lorem ipsum')
const optionResult = await page.$eval('.app-site-search__option', option => option.textContent)
expect(optionResult).toBe('No results found')
})
it('returns results where a word in the title begins with the letter "d"', async () => {
await page.goto(baseUrl, { waitUntil: 'load' })
await page.waitForSelector('.app-site-search__input')
await page.type('.app-site-search__input', 'd')
const resultsArray = await page.evaluate(
() => [...document.querySelectorAll('.app-site-search__option')]
// ignore any results where a match was found in the alias
.filter(elem => !elem.querySelector('.app-site-search__aliases'))
// only get text, ignore child nodes
.map(elem => elem.firstChild.textContent.toLowerCase())
)
// regex with word boundary, in our case words that begin with 'd'
expect(resultsArray.every(item => (/\b[d]\w*/).test(item))).toBeTruthy()
})
it('returns results that contain aliases that start with the letter "d"', async () => {
await page.goto(baseUrl, { waitUntil: 'load' })
await page.waitForSelector('.app-site-search__input')
await page.type('.app-site-search__input', 'd')
const resultsArray = await page.evaluate(
() => [...document.querySelectorAll('.app-site-search__option')]
// only get results where a match was found in the alias
.filter(elem => elem.querySelector('.app-site-search__aliases'))
.map(elem => elem.querySelector('.app-site-search__aliases').textContent)
)
// regex with word boundary, in our case words that begin with 'd'
expect(resultsArray.every(item => (/\b[d]\w*/).test(item))).toBeTruthy()
})
it('doesn\'t show any aliases if it finds any matches in the title', async () => {
await page.goto(baseUrl, { waitUntil: 'load' })
await page.waitForSelector('.app-site-search__input')
await page.type('.app-site-search__input', 'det')
const resultsArray = await page.evaluate(
() => [...document.querySelectorAll('.app-site-search__aliases')]
.map(elem => elem.querySelector('.app-site-search__aliases'))
)
expect(resultsArray).toHaveLength(0)
})
it('selecting "details" as the result takes you to the the "details" page', async () => {
await page.goto(baseUrl, { waitUntil: 'load' })
await page.waitForSelector('.app-site-search__input')
await page.click('.app-site-search__input')
await page.type('.app-site-search__input', 'details')
await Promise.all([
page.waitForNavigation(),
page.keyboard.press('Enter')
])
const url = await page.url()
expect(url).toBe(baseUrl + '/components/details/')
})
it('shows user a message that the index has failed to download', async () => {
await page.setRequestInterception(true)
page.on('request', interceptedRequest => {
if (isSearchIndex.test(interceptedRequest.url())) {
interceptedRequest.abort()
} else {
interceptedRequest.continue()
}
})
await page.goto(baseUrl, { waitUntil: 'load' })
await page.waitForSelector('.app-site-search__input')
await page.click('.app-site-search__input')
await page.type('.app-site-search__input', 'lorem')
const optionResult = await page.$eval('.app-site-search__option', option => option.textContent)
expect(optionResult).toBe('Failed to load the search index')
})
it('shows user a message that the search index is loading', async () => {
await page.setRequestInterception(true)
page.on('request', interceptedRequest => {
// Intentionally make the search-index request hang
if (!isSearchIndex.test(interceptedRequest.url())) {
interceptedRequest.continue()
}
})
await page.goto(baseUrl, { waitUntil: 'load' })
await page.waitForSelector('.app-site-search__input')
await page.click('.app-site-search__input')
await page.type('.app-site-search__input', 'd')
const optionResult = await page.$eval('.app-site-search__option', option => option.textContent)
expect(optionResult).toBe('Loading search index')
})
it('should focus the input when clicking the button', async () => {
await page.goto(baseUrl, { waitUntil: 'load' })
await page.waitForSelector('.app-site-search__input')
// The button is actually a background on the left most part of the wrapper element.
const wrapperCoordinates = await page.$eval('.app-site-search__wrapper', $wrapper => {
const { top, left } = $wrapper.getBoundingClientRect()
return { top, left }
})
// Click the top left side of the element.
await page.mouse.click(wrapperCoordinates.left, wrapperCoordinates.top)
// Get the active focused element to compare with the actual expected input.
const $activeElement = await page.evaluate(() => document.activeElement)
const $input = await page.evaluate(() => document.querySelector('.app-site-search__input'))
expect($activeElement).toEqual($input)
})
describe('tracking', () => {
it('should track if there are no results', async () => {
await page.goto(baseUrl, { waitUntil: 'load' })
await page.evaluate(() => { window.__SITE_SEARCH_TRACKING_TIMEOUT = 0 })
await page.waitForSelector('.app-site-search__input')
await page.focus('.app-site-search__input')
await page.type('.app-site-search__input', 'lorem ipsum')
const GoogleTagManagerDataLayer = await page.evaluate(() => window.dataLayer)
expect(GoogleTagManagerDataLayer).toEqual(
expect.arrayContaining([
expect.objectContaining({
ecommerce: {
impressions: []
},
event: 'site-search',
eventDetails: {
action: 'no result',
category: 'site search',
label: 'lorem ipsum'
}
})
])
)
})
it('should track if there are results', async () => {
await page.goto(baseUrl, { waitUntil: 'load' })
await page.evaluate(() => { window.__SITE_SEARCH_TRACKING_TIMEOUT = 0 })
await page.waitForSelector('.app-site-search__input')
await page.focus('.app-site-search__input')
await page.type('.app-site-search__input', 'g')
const optionResults = await page.$$('.app-site-search__option')
const GoogleTagManagerDataLayer = await page.evaluate(() => window.dataLayer)
// Find layer that has the impressions to test.
const impressions =
GoogleTagManagerDataLayer
.filter(layer => layer.ecommerce)
.map(layer => layer.ecommerce.impressions)[0]
expect(impressions.length).toEqual(optionResults.length)
expect(GoogleTagManagerDataLayer).toEqual(
expect.arrayContaining([
expect.objectContaining({
ecommerce: {
impressions: expect.arrayContaining([
expect.objectContaining({
name: expect.any(String),
category: expect.any(String),
list: 'g',
position: expect.any(Number)
})
])
},
event: 'site-search',
eventDetails: {
action: 'results',
category: 'site search',
label: 'g'
}
})
])
)
})
it('should track if a result is clicked', async () => {
await page.goto(baseUrl, { waitUntil: 'load' })
// Prevent page from unloading so we can check what was tracked.
// By setting onbeforeunload it forces a dialog to appear that allows a user
// to cancel leaving the page, so we detect the dialog opening and dismiss it to stop the navigation.
await page.evaluate(() => {
window.onbeforeunload = () => true
})
page.on('dialog', async dialog => {
await dialog.dismiss()
})
await page.waitForSelector('.app-site-search__input')
await page.focus('.app-site-search__input')
await page.type('.app-site-search__input', 'g')
await page.keyboard.press('ArrowDown')
await page.keyboard.press('Enter')
const GoogleTagManagerDataLayer = await page.evaluate(() => window.dataLayer)
expect(GoogleTagManagerDataLayer).toEqual(
expect.arrayContaining([
expect.objectContaining({
ecommerce: {
click: {
actionField: {
list: 'g'
},
products: expect.arrayContaining([
expect.objectContaining({
name: expect.any(String),
category: expect.any(String),
list: 'g',
position: 2
})
])
}
},
event: 'site-search',
eventDetails: {
action: 'click',
category: 'site search',
label: expect.stringContaining('g |')
}
})
])
)
})
it('should block personally identifable information emails', async () => {
await page.goto(baseUrl, { waitUntil: 'load' })
await page.evaluate(() => { window.__SITE_SEARCH_TRACKING_TIMEOUT = 0 })
await page.waitForSelector('.app-site-search__input')
await page.focus('.app-site-search__input')
await page.type('.app-site-search__input', 'user@example.com')
const GoogleTagManagerDataLayer = await page.evaluate(() => window.dataLayer)
expect(GoogleTagManagerDataLayer).toEqual(
expect.arrayContaining([
expect.objectContaining({
eventDetails: expect.objectContaining({
label: '[REDACTED EMAIL]'
})
})
])
)
})
it('should block personally identifable information numbers', async () => {
await page.goto(baseUrl, { waitUntil: 'load' })
await page.evaluate(() => { window.__SITE_SEARCH_TRACKING_TIMEOUT = 0 })
await page.waitForSelector('.app-site-search__input')
await page.focus('.app-site-search__input')
await page.type('.app-site-search__input', '079460999')
const GoogleTagManagerDataLayer = await page.evaluate(() => window.dataLayer)
expect(GoogleTagManagerDataLayer).toEqual(
expect.arrayContaining([
expect.objectContaining({
eventDetails: expect.objectContaining({
label: '[REDACTED NUMBER]'
})
})
])
)
})
})
})