2018-04-18 15:50:49 +00:00
|
|
|
import consola from 'consola'
|
2018-03-27 22:28:17 +00:00
|
|
|
import { loadFixture, getPort, Nuxt, rp } from '../utils'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
2018-03-18 23:41:14 +00:00
|
|
|
let port
|
2018-01-13 05:22:11 +00:00
|
|
|
const url = route => 'http://localhost:' + port + route
|
2016-12-09 17:51:19 +00:00
|
|
|
|
|
|
|
let nuxt = null
|
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
describe('basic ssr', () => {
|
|
|
|
beforeAll(async () => {
|
2018-08-17 20:25:23 +00:00
|
|
|
const options = await loadFixture('basic')
|
2017-11-27 22:35:42 +00:00
|
|
|
nuxt = new Nuxt(options)
|
2018-03-18 23:41:14 +00:00
|
|
|
port = await getPort()
|
2018-10-30 20:42:53 +00:00
|
|
|
await nuxt.server.listen(port, '0.0.0.0')
|
2018-03-18 23:41:14 +00:00
|
|
|
})
|
2017-12-29 08:33:13 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/stateless', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/stateless')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<h1>My component!</h1>')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2016-12-09 17:51:19 +00:00
|
|
|
|
2018-09-18 15:21:25 +00:00
|
|
|
test('/store-module', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/store-module')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<h1>mutated</h1>')
|
2018-09-18 15:21:25 +00:00
|
|
|
})
|
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
/*
|
|
|
|
** Example of testing via dom checking
|
|
|
|
*/
|
|
|
|
test('/css', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const window = await nuxt.server.renderAndGetWindow(url('/css'))
|
2017-12-29 08:33:13 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
const headHtml = window.document.head.innerHTML
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(headHtml).toContain('color:red')
|
2017-12-29 08:33:13 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
const element = window.document.querySelector('.red')
|
|
|
|
expect(element).not.toBe(null)
|
|
|
|
expect(element.textContent).toBe('This is red')
|
|
|
|
expect(element.className).toBe('red')
|
|
|
|
// t.is(window.getComputedStyle(element).color, 'red')
|
|
|
|
})
|
2017-11-21 07:38:12 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/postcss', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const window = await nuxt.server.renderAndGetWindow(url('/css'))
|
2016-12-09 17:51:19 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
const headHtml = window.document.head.innerHTML
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(headHtml).toContain('background-color:#00f')
|
2017-03-25 23:53:50 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
// const element = window.document.querySelector('div.red')
|
|
|
|
// t.is(window.getComputedStyle(element)['background-color'], 'blue')
|
2018-01-13 05:22:11 +00:00
|
|
|
})
|
2017-11-27 22:35:42 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/stateful', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/stateful')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<div><p>The answer is 42</p></div>')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-12-17 19:30:26 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/store', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/store')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<h1>Vuex Nested Modules</h1>')
|
|
|
|
expect(html).toContain('<p>1</p>')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-11-27 22:35:42 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/head', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const window = await nuxt.server.renderAndGetWindow(url('/head'))
|
2018-03-18 19:31:32 +00:00
|
|
|
expect(window.document.title).toBe('My title - Nuxt.js')
|
|
|
|
|
|
|
|
const html = window.document.body.innerHTML
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<div><h1>I can haz meta tags</h1></div>')
|
2018-03-18 19:31:32 +00:00
|
|
|
expect(
|
|
|
|
html.includes('<script data-n-head="true" src="/body.js" data-body="true">')
|
|
|
|
).toBe(true)
|
|
|
|
|
|
|
|
const metas = window.document.getElementsByTagName('meta')
|
|
|
|
expect(metas[0].getAttribute('content')).toBe('my meta')
|
2018-04-18 15:50:49 +00:00
|
|
|
expect(consola.log).toHaveBeenCalledWith('Body script!')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2016-12-09 17:51:19 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/async-data', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/async-data')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<p>Nuxt.js</p>')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2016-12-09 17:51:19 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/await-async-data', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/await-async-data')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<p>Await Nuxt.js</p>')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2016-12-27 15:31:25 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/callback-async-data', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/callback-async-data')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<p>Callback Nuxt.js</p>')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2016-12-27 15:31:25 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/users/1', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/users/1')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<h1>User: 1</h1>')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2016-12-21 19:51:43 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/validate should display a 404', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/validate')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('This page could not be found')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2016-12-20 17:26:46 +00:00
|
|
|
|
2018-08-25 09:42:00 +00:00
|
|
|
test('/validate-async should display a 404', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/validate-async')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('This page could not be found')
|
2018-08-25 09:42:00 +00:00
|
|
|
})
|
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/validate?valid=true', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/validate?valid=true')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<h1>I am valid</h1>')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2016-12-20 17:26:46 +00:00
|
|
|
|
2018-08-25 09:42:00 +00:00
|
|
|
test('/validate-async?valid=true', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/validate-async?valid=true')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<h1>I am valid</h1>')
|
2018-08-25 09:42:00 +00:00
|
|
|
})
|
|
|
|
|
2018-08-25 17:54:16 +00:00
|
|
|
test('/validate?error=403', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html, error } = await nuxt.server.renderRoute('/validate?error=403')
|
2018-08-25 17:54:16 +00:00
|
|
|
expect(error).toMatchObject({ statusCode: 403, message: 'Custom Error' })
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('Custom Error')
|
2018-08-25 17:54:16 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('/validate-async?error=503', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html, error } = await nuxt.server.renderRoute('/validate-async?error=503')
|
2018-08-25 17:54:16 +00:00
|
|
|
expect(error).toMatchObject({ statusCode: 503, message: 'Custom Error' })
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('Custom Error')
|
2018-08-25 17:54:16 +00:00
|
|
|
})
|
|
|
|
|
2018-08-18 16:15:37 +00:00
|
|
|
test('/before-enter', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/before-enter')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<h1>Index page</h1>')
|
2018-08-18 16:15:37 +00:00
|
|
|
})
|
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/redirect', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html, redirected } = await nuxt.server.renderRoute('/redirect')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<div id="__nuxt"></div>')
|
2018-03-18 19:31:32 +00:00
|
|
|
expect(redirected.path === '/').toBe(true)
|
|
|
|
expect(redirected.status === 302).toBe(true)
|
|
|
|
})
|
2016-12-20 18:25:51 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/redirect -> check redirected source', async () => {
|
|
|
|
// there are no transition properties in jsdom, ignore the error log
|
2018-10-30 20:42:53 +00:00
|
|
|
const window = await nuxt.server.renderAndGetWindow(url('/redirect'))
|
2018-03-18 19:31:32 +00:00
|
|
|
const html = window.document.body.innerHTML
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<h1>Index page</h1>')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2016-12-20 18:25:51 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/redirect -> external link', async () => {
|
|
|
|
let _headers, _status
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/redirect-external', {
|
2018-03-18 19:31:32 +00:00
|
|
|
res: {
|
|
|
|
writeHead(status, headers) {
|
|
|
|
_status = status
|
|
|
|
_headers = headers
|
|
|
|
},
|
|
|
|
end() { }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
expect(_status).toBe(302)
|
|
|
|
expect(_headers.Location).toBe('https://nuxtjs.org')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<div data-server-rendered="true"></div>')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-11-28 09:10:44 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/special-state -> check window.__NUXT__.test = true', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const window = await nuxt.server.renderAndGetWindow(url('/special-state'))
|
2018-03-18 19:31:32 +00:00
|
|
|
expect(window.document.title).toBe('Nuxt.js')
|
|
|
|
expect(window.__NUXT__.test).toBe(true)
|
|
|
|
})
|
2017-07-27 14:50:24 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/error', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
await expect(nuxt.server.renderRoute('/error', { req: {}, res: {} }))
|
2018-03-18 19:31:32 +00:00
|
|
|
.rejects.toThrow('Error mouahahah')
|
|
|
|
})
|
2016-12-21 14:03:37 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/error status code', async () => {
|
|
|
|
await expect(rp(url('/error'))).rejects.toMatchObject({
|
|
|
|
statusCode: 500
|
|
|
|
})
|
|
|
|
})
|
2016-12-21 14:03:37 +00:00
|
|
|
|
2018-04-15 16:40:42 +00:00
|
|
|
test('/error json format error', async () => {
|
|
|
|
const opts = {
|
|
|
|
headers: {
|
|
|
|
accept: 'application/json'
|
|
|
|
},
|
|
|
|
resolveWithFullResponse: true
|
|
|
|
}
|
|
|
|
await expect(rp(url('/error'), opts)).rejects.toMatchObject({
|
|
|
|
statusCode: 500,
|
|
|
|
response: {
|
|
|
|
headers: {
|
|
|
|
'content-type': 'text/json; charset=utf-8'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/error2', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html, error } = await nuxt.server.renderRoute('/error2')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('Custom error')
|
|
|
|
expect(error.message).toContain('Custom error')
|
2018-03-18 19:31:32 +00:00
|
|
|
expect(error.statusCode === undefined).toBe(true)
|
|
|
|
})
|
2016-12-21 14:03:37 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/error2 status code', async () => {
|
|
|
|
await expect(rp(url('/error2'))).rejects.toMatchObject({
|
|
|
|
statusCode: 500,
|
|
|
|
message: expect.stringContaining('Custom error')
|
|
|
|
})
|
|
|
|
})
|
2016-12-20 18:25:51 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/error-midd', async () => {
|
2018-04-18 15:50:49 +00:00
|
|
|
await expect(rp(url('/error-midd'))).rejects.toMatchObject({ statusCode: 505 })
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-11-02 16:48:20 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/redirect-middleware', async () => {
|
2018-04-18 15:50:49 +00:00
|
|
|
await expect(rp(url('/redirect-middleware'))).resolves.toBeTruthy()
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-05-16 13:12:30 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/redirect-name', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html, redirected } = await nuxt.server.renderRoute('/redirect-name')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<div id="__nuxt"></div>')
|
2018-03-18 19:31:32 +00:00
|
|
|
expect(redirected.path === '/stateless').toBe(true)
|
|
|
|
expect(redirected.status === 302).toBe(true)
|
|
|
|
})
|
2017-12-21 04:55:32 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/no-ssr', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/no-ssr')
|
2018-03-18 19:31:32 +00:00
|
|
|
expect(html.includes(
|
2018-09-13 09:11:29 +00:00
|
|
|
'<p class="no-ssr-placeholder">Loading...</p>'
|
2018-03-18 19:31:32 +00:00
|
|
|
)).toBe(true)
|
|
|
|
})
|
2017-08-24 10:46:30 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/no-ssr (client-side)', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const window = await nuxt.server.renderAndGetWindow(url('/no-ssr'))
|
2018-03-18 19:31:32 +00:00
|
|
|
const html = window.document.body.innerHTML
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('Displayed only on client-side</h1>')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-08-24 10:46:30 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('ETag Header', async () => {
|
|
|
|
const { headers: { etag } } = await rp(url('/stateless'), {
|
|
|
|
resolveWithFullResponse: true
|
|
|
|
})
|
|
|
|
// Verify functionality
|
|
|
|
await expect(rp(url('/stateless'), { headers: { 'If-None-Match': etag } }))
|
|
|
|
.rejects.toMatchObject({ statusCode: 304 })
|
2018-01-13 05:22:11 +00:00
|
|
|
})
|
2017-05-21 16:42:38 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/_nuxt/server-bundle.json should return 404', async () => {
|
|
|
|
await expect(rp(url('/_nuxt/server-bundle.json')))
|
|
|
|
.rejects.toMatchObject({ statusCode: 404 })
|
|
|
|
})
|
2017-06-20 14:12:55 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/_nuxt/ should return 404', async () => {
|
|
|
|
await expect(rp(url('/_nuxt/')))
|
|
|
|
.rejects.toMatchObject({ statusCode: 404 })
|
|
|
|
})
|
2017-06-20 14:12:55 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/meta', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/meta')
|
2018-09-25 08:51:07 +00:00
|
|
|
expect(/<pre>.*"works": true.*<\/pre>/s.test(html)).toBe(true)
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-11-02 17:07:33 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/fn-midd', async () => {
|
|
|
|
await expect(rp(url('/fn-midd')))
|
|
|
|
.rejects.toMatchObject({ statusCode: 403 })
|
|
|
|
})
|
2017-11-03 16:14:05 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/fn-midd?please=true', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/fn-midd?please=true')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<h1>Date:')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-11-03 16:14:05 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/router-guard', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/router-guard')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<p>Nuxt.js</p>')
|
2018-03-18 19:31:32 +00:00
|
|
|
expect(html.includes('Router Guard')).toBe(false)
|
|
|
|
})
|
2017-12-01 09:25:21 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/jsx', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/jsx')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<h1>JSX Page</h1>')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-12-11 08:18:28 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/jsx-link', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/jsx-link')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<h1>JSX Link Page</h1>')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2018-01-18 12:10:23 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/js-link', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/js-link')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<h1>vue file is first-class</h1>')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-12-12 13:32:45 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
// Close server and ask nuxt to stop listening to file changes
|
2018-03-30 09:20:16 +00:00
|
|
|
afterAll(async () => {
|
2018-03-18 19:31:32 +00:00
|
|
|
await nuxt.close()
|
|
|
|
})
|
2016-12-09 17:51:19 +00:00
|
|
|
})
|