Repository navigation
Expand file tree
/
Copy pathrendering.js
More file actions
142 lines (123 loc) · 5.59 KB
/
Copy pathrendering.js
File metadata and controls
142 lines (123 loc) · 5.59 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
/**
* To be able to run these tests you need to index the fixtures!
* And you need to have an Elasticsearch URL to connect to for the server.
*
* To index the fixtures, run:
*
* ELASTICSEARCH_URL=http://localhost:9200 npm run index-test-fixtures
*
* This will replace any "real" Elasticsearch indexes you might have so
* once you're done working on jest tests you need to index real
* content again.
*/
import { jest, test, expect } from '@jest/globals'
import { describeIfElasticsearchURL } from '../../../tests/helpers/conditional-runs.js'
import { get, getDOM } from '../../../tests/helpers/e2etest.js'
import { SURROGATE_ENUMS } from '../../../middleware/set-fastly-surrogate-key.js'
if (!process.env.ELASTICSEARCH_URL) {
console.warn(
'None of the API search middleware tests are run because ' +
"the environment variable 'ELASTICSEARCH_URL' is currently not set.",
)
}
// This suite only runs if $ELASTICSEARCH_URL is set.
describeIfElasticsearchURL('search rendering page', () => {
jest.setTimeout(60 * 1000)
test('happy path', async () => {
// To see why this will work,
// see src/search/tests/fixtures/search-indexes/github-docs-dotcom-en-records.json
// which clearly has a record with the title "Foo"
const $ = await getDOM('/en/search?query=foo')
expect($('h1').text()).toMatch(/\d+ Search results for "foo"/)
// Note it testid being 'search-result', not 'search-results'
const results = $('[data-testid="search-result"]')
expect(results.length).toBeGreaterThan(0)
const result = results.first()
expect($('h2', result).text()).toBe('Foo')
const paragraph = $('p', result)
expect(paragraph.text()).toMatch('fooing')
const link = $('a', result)
expect(link.html()).toMatch('<mark>Foo</mark>')
const content = $('div', result)
expect(content.html()).toMatch('<mark>foo</mark>')
})
test('response headers', async () => {
const res = await get('/en/search?query=foo')
// Check that it can be cached at the CDN
expect(res.headers['set-cookie']).toBeUndefined()
expect(res.headers['cache-control']).toContain('public')
expect(res.headers['cache-control']).toMatch(/max-age=[1-9]/)
expect(res.headers['surrogate-control']).toContain('public')
expect(res.headers['surrogate-control']).toMatch(/max-age=[1-9]/)
expect(res.headers['surrogate-key']).toMatch(SURROGATE_ENUMS.DEFAULT)
expect(res.headers['surrogate-key']).toMatch('language:en')
})
test('debug search', async () => {
const $ = await getDOM('/en/search?query=foo&debug=1')
expect($('h1').text()).toMatch(/\d+ Search results for "foo"/)
// Note it testid being 'search-result', not 'search-results'
const results = $('[data-testid="search-result"]')
expect(results.length).toBeGreaterThan(0)
const result = results.first()
expect($('p', result).text()).toMatch(/score:\s+\d/)
expect($('p', result).text()).toMatch(/popularity:\s+\d/)
})
test('no query', async () => {
const $ = await getDOM('/en/search')
const results = $('[data-testid="search-result"]')
expect(results.length).toBe(0)
expect($('[data-testid="search-results"]').text()).toMatch('Enter a search term')
})
test('empty query', async () => {
const $ = await getDOM('/en/search?query=')
const results = $('[data-testid="search-result"]')
expect(results.length).toBe(0)
expect($('[data-testid="search-results"]').text()).toMatch('Enter a search term')
})
test('find nothing', async () => {
const $ = await getDOM('/en/search?query=xojixjoiwejhfoiuwehjfioweufhj')
const results = $('[data-testid="search-result"]')
expect(results.length).toBe(0)
expect($('[data-testid="search-results"]').text()).toMatch('0 Search results')
})
test('unrecognized version', async () => {
const res = await get('/en/never-heard@of/search?query=foo')
expect(res.statusCode).toBe(404)
})
test('links per version in pathname', async () => {
const $ = await getDOM('/en/github-ae@latest/search?query=foo')
expect($('[data-testid="search-results"]').text()).toMatch('Exclusively for GHAE')
// Note it testid being 'search-result', not 'search-results'
const results = $('[data-testid="search-result"]')
expect(results.length).toBeGreaterThan(0)
// Each link should have github-ae@latest in the pathname
const links = $('[data-testid="search-result"] a')
const hrefs = links.map((i, el) => $(el).attr('href')).get()
for (const href of hrefs) {
expect(href).toMatch('/en/github-ae@latest/')
}
expect.assertions(results.length + 2)
})
test('invalid parameters (page)', async () => {
const $ = await getDOM('/en/search?query=foo&page=999')
expect($('[data-testid="search-results"]').text()).toMatch('Not a valid value (999)')
const results = $('[data-testid="search-result"]')
expect(results.length).toBe(0)
})
test('invalid parameters (size)', async () => {
const $ = await getDOM('/en/search?query=foo&size=888')
expect($('[data-testid="search-results"]').text()).toMatch('Not a valid value (888)')
const results = $('[data-testid="search-result"]')
expect(results.length).toBe(0)
})
test("don't convert q= to query= if query= already present", async () => {
const res = await get('/en/search?q=pulls&query=pushes')
expect(res.statusCode).toBe(200)
})
test('more that one search query', async () => {
const $ = await getDOM('/en/search?query=foo&query=bar')
expect($('[data-testid="search-results"]').text()).toMatch('Can not have multiple values')
const results = $('[data-testid="search-result"]')
expect(results.length).toBe(0)
})
})