Nuxt/test/spa.test.js

60 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-10-07 09:07:52 +00:00
import test from 'ava'
import stdMocks from 'std-mocks'
import { Nuxt, Builder } from '..'
2017-10-07 09:07:52 +00:00
let nuxt = null
const port = 4004
const url = (route) => 'http://localhost:' + port + route
const renderRoute = async _url => {
const window = await nuxt.renderAndGetWindow(url(_url))
const head = window.document.head.innerHTML
2017-10-07 09:07:52 +00:00
const html = window.document.body.innerHTML
return { window, head, html }
2017-10-07 09:07:52 +00:00
}
// Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => {
nuxt = new Nuxt(require('./fixtures/spa/nuxt.config'))
await new Builder(nuxt).build()
await nuxt.listen(port, 'localhost')
})
test('/ (basic spa)', async t => {
const { html } = await renderRoute('/')
t.true(html.includes('Hello SPA!'))
})
2017-10-28 21:01:46 +00:00
test('/custom (custom layout)', async t => {
2017-10-07 15:24:15 +00:00
const { html } = await renderRoute('/custom')
2017-10-07 09:07:52 +00:00
t.true(html.includes('Custom layout'))
})
test('/custom (not default layout)', async t => {
const { head } = await renderRoute('/custom')
t.false(head.includes('src="/_nuxt/layouts/default.'))
})
test('/custom (call mounted and created once)', async t => {
stdMocks.use()
await renderRoute('/custom')
stdMocks.restore()
const output = stdMocks.flush()
const creates = output.stdout.filter(value => value === 'created\n')
t.true(creates.length === 1)
const mounts = output.stdout.filter(value => value === 'mounted\n')
t.true(mounts.length === 1)
})
test('/_nuxt/ (access publicPath in spa mode)', async t => {
const { response: { statusCode, statusMessage } } = await t.throws(renderRoute('/_nuxt/'))
t.is(statusCode, 404)
t.is(statusMessage, 'ResourceNotFound')
})
2017-10-07 09:07:52 +00:00
// Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => {
nuxt.close()
})