2018-04-18 15:12:27 +00:00
|
|
|
import consola from 'consola'
|
2018-03-27 22:28:17 +00:00
|
|
|
import { loadFixture, getPort, Nuxt } from '../utils'
|
2017-10-07 09:07:52 +00:00
|
|
|
|
2018-08-16 15:46:42 +00:00
|
|
|
let nuxt, port
|
2018-01-13 05:22:11 +00:00
|
|
|
const url = route => 'http://localhost:' + port + route
|
2017-10-07 09:07:52 +00:00
|
|
|
|
2018-08-06 00:12:44 +00:00
|
|
|
const renderRoute = async (_url) => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const window = await nuxt.server.renderAndGetWindow(url(_url))
|
2018-03-18 23:41:14 +00:00
|
|
|
const head = window.document.head.innerHTML
|
|
|
|
const html = window.document.body.innerHTML
|
|
|
|
return { window, head, html }
|
|
|
|
}
|
2017-10-07 09:07:52 +00:00
|
|
|
|
2018-04-18 15:12:27 +00:00
|
|
|
describe('spa', () => {
|
2018-03-18 19:31:32 +00:00
|
|
|
beforeAll(async () => {
|
2018-08-17 20:25:23 +00:00
|
|
|
const config = await loadFixture('spa')
|
2017-12-17 19:30:26 +00:00
|
|
|
nuxt = new Nuxt(config)
|
2018-03-18 23:41:14 +00:00
|
|
|
port = await getPort()
|
2018-10-30 20:42:53 +00:00
|
|
|
await nuxt.server.listen(port, 'localhost')
|
2018-03-18 23:41:14 +00:00
|
|
|
})
|
2017-10-07 09:07:52 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/ (basic spa)', async () => {
|
|
|
|
const { html } = await renderRoute('/')
|
2018-04-18 15:12:27 +00:00
|
|
|
expect(html).toMatch('Hello SPA!')
|
|
|
|
expect(consola.log).not.toHaveBeenCalledWith('created')
|
|
|
|
expect(consola.log).toHaveBeenCalledWith('mounted')
|
2018-08-16 15:46:42 +00:00
|
|
|
consola.log.mockClear()
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-10-07 09:07:52 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/custom (custom layout)', async () => {
|
|
|
|
const { html } = await renderRoute('/custom')
|
2018-04-18 15:12:27 +00:00
|
|
|
expect(html).toMatch('Custom layout')
|
|
|
|
expect(consola.log).toHaveBeenCalledWith('created')
|
|
|
|
expect(consola.log).toHaveBeenCalledWith('mounted')
|
2018-08-16 15:46:42 +00:00
|
|
|
consola.log.mockClear()
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-11-17 08:27:34 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/mounted', async () => {
|
|
|
|
const { html } = await renderRoute('/mounted')
|
2018-04-18 15:12:27 +00:00
|
|
|
expect(html).toMatch('<h1>Test: updated</h1>')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2018-01-04 15:40:34 +00:00
|
|
|
|
2018-08-08 10:11:08 +00:00
|
|
|
test('/error-handler', async () => {
|
|
|
|
const { html } = await renderRoute('/error-handler')
|
2018-08-22 13:10:43 +00:00
|
|
|
expect(html).toMatch('error handler triggered: fetch error!')
|
|
|
|
})
|
|
|
|
|
2018-11-14 19:17:44 +00:00
|
|
|
test('/error-handler-object', async () => {
|
2018-11-19 16:06:48 +00:00
|
|
|
const { html } = await renderRoute('/error-handler-object')
|
2018-11-14 19:17:44 +00:00
|
|
|
expect(html).toMatch('error handler triggered: fetch error!')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('/error-handler-string', async () => {
|
|
|
|
const { html } = await renderRoute('/error-handler-string')
|
|
|
|
expect(html).toMatch('error handler triggered: fetch error!')
|
|
|
|
})
|
|
|
|
|
2018-08-22 13:10:43 +00:00
|
|
|
test('/error-handler-async', async () => {
|
|
|
|
const { html } = await renderRoute('/error-handler-async')
|
|
|
|
expect(html).toMatch('error handler triggered: asyncData error!')
|
2018-08-08 10:11:08 +00:00
|
|
|
})
|
|
|
|
|
2018-11-25 14:52:37 +00:00
|
|
|
test('/тест雨 (test non ascii route)', async () => {
|
|
|
|
const { html } = await renderRoute('/тест雨')
|
|
|
|
expect(html).toMatch('Hello unicode SPA!')
|
|
|
|
expect(consola.log).not.toHaveBeenCalledWith('created')
|
|
|
|
expect(consola.log).toHaveBeenCalledWith('mounted')
|
|
|
|
consola.log.mockClear()
|
|
|
|
})
|
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()
|
|
|
|
})
|
2017-10-07 09:07:52 +00:00
|
|
|
})
|