Nuxt/test/unit/spa.test.js

66 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-04-18 15:12:27 +00:00
import consola from 'consola'
import mockLog from '../utils/mock-log'
import { loadFixture, getPort, Nuxt } from '../utils'
2017-10-07 09:07:52 +00:00
let nuxt = null
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
2017-10-07 09:07:52 +00:00
const renderRoute = async (_url) => {
2018-03-18 23:41:14 +00:00
const window = await nuxt.renderAndGetWindow(url(_url))
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', () => {
mockLog(['log'], consola)
2018-03-18 19:31:32 +00:00
beforeAll(async () => {
2018-03-18 23:41:14 +00:00
const config = loadFixture('spa')
nuxt = new Nuxt(config)
2018-03-18 23:41:14 +00:00
port = await getPort()
await nuxt.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-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-03-18 19:31:32 +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
})
test('/error-handler', async () => {
await renderRoute('/error-handler')
const { html } = await renderRoute('/error-handler')
expect(html).toMatch('error handler triggered')
})
2018-03-18 19:31:32 +00:00
test('/_nuxt/ (access publicPath in spa mode)', async () => {
await expect(renderRoute('/_nuxt/')).rejects.toMatchObject({
response: {
statusCode: 404,
statusMessage: 'ResourceNotFound'
}
})
})
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
})