forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathintegration.test.js
122 lines (101 loc) · 3.73 KB
/
integration.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
/* global expect, jasmine, describe, test, beforeAll, afterAll */
'use strict'
import { join } from 'path'
import next from '../dist/server/next'
import pkg from '../package.json'
const dir = join(__dirname, 'fixtures', 'basic')
const app = next({
dir,
dev: true,
staticMarkup: true,
quiet: true
})
jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000
describe('integration tests', () => {
beforeAll(() => app.prepare())
afterAll(() => app.close())
test('renders a stateless component', async () => {
const html = await render('/stateless')
expect(html.includes('<meta charset="utf-8" class="next-head"/>')).toBeTruthy()
expect(html.includes('<h1>My component!</h1>')).toBeTruthy()
})
test('renders a stateful component', async () => {
const html = await render('/stateful')
expect(html.includes('<div><p>The answer is 42</p></div>')).toBeTruthy()
})
test('header helper renders header information', async () => {
const html = await (render('/head'))
expect(html.includes('<meta charset="iso-8859-5" class="next-head"/>')).toBeTruthy()
expect(html.includes('<meta content="my meta" class="next-head"/>')).toBeTruthy()
expect(html.includes('<div><h1>I can haz meta tags</h1></div>')).toBeTruthy()
})
test('css helper renders styles', async () => {
const html = await render('/css')
expect(/\.css-\w+/.test(html)).toBeTruthy()
expect(/<div class="css-\w+">This is red<\/div>/.test(html)).toBeTruthy()
})
test('renders styled jsx', async () => {
const html = await render('/styled-jsx')
expect(html).toMatch(/<style id="__jsx-style-1401785258">p\[data-jsx="1401785258"] {color: blue }[^]+<\/style>/)
expect(html.includes('<div data-jsx="1401785258"><p data-jsx="1401785258">This is blue</p></div>')).toBeTruthy()
})
test('renders properties populated asynchronously', async () => {
const html = await render('/async-props')
expect(html.includes('<p>Diego Milito</p>')).toBeTruthy()
})
test('renders a link component', async () => {
const html = await render('/link')
expect(html.includes('<a href="/about">About</a>')).toBeTruthy()
})
test('error', async () => {
const html = await render('/error')
expect(html).toMatch(/<pre class=".+">Error: This is an expected error\n[^]+<\/pre>/)
})
test('error 404', async () => {
const html = await render('/non-existent')
expect(html).toMatch(/<h1 data-jsx=".+">404<\/h1>/)
expect(html).toMatch(/<h2 data-jsx=".+">This page could not be found\.<\/h2>/)
})
test('finishes response', async () => {
const res = {
finished: false,
end () {
this.finished = true
}
}
const html = await app.renderToHTML({}, res, '/finish-response', {})
expect(html).toBeFalsy()
})
describe('X-Powered-By header', () => {
test('set it by default', async () => {
const req = { url: '/stateless' }
const headers = {}
const res = {
setHeader (key, value) {
headers[key] = value
},
end () {}
}
await app.render(req, res, req.url)
expect(headers['X-Powered-By']).toEqual(`Next.js ${pkg.version}`)
})
test('do not set it when poweredByHeader==false', async () => {
const req = { url: '/stateless' }
const originalConfigValue = app.config.poweredByHeader
app.config.poweredByHeader = false
const res = {
setHeader (key, value) {
if (key === 'X-Powered-By') {
throw new Error('Should not set the X-Powered-By header')
}
},
end () {}
}
await app.render(req, res, req.url)
app.config.poweredByHeader = originalConfigValue
})
})
})
function render (pathname, query = {}) {
return app.renderToHTML({}, {}, pathname, query)
}