2017-10-07 09:07:52 +00:00
|
|
|
import test from 'ava'
|
2017-12-17 19:30:26 +00:00
|
|
|
import { resolve } from 'path'
|
2017-12-12 09:42:29 +00:00
|
|
|
import { Nuxt, Builder } from '..'
|
2017-12-13 05:38:46 +00:00
|
|
|
import { interceptLog, release } from './helpers/console'
|
2017-10-07 09:07:52 +00:00
|
|
|
|
|
|
|
let nuxt = null
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
const port = 4012
|
2017-10-07 09:07:52 +00:00
|
|
|
const url = (route) => 'http://localhost:' + port + route
|
|
|
|
|
|
|
|
const renderRoute = async _url => {
|
|
|
|
const window = await nuxt.renderAndGetWindow(url(_url))
|
2017-11-14 08:53:01 +00:00
|
|
|
const head = window.document.head.innerHTML
|
2017-10-07 09:07:52 +00:00
|
|
|
const html = window.document.body.innerHTML
|
2017-11-14 08:53:01 +00:00
|
|
|
return { window, head, html }
|
2017-10-07 09:07:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init nuxt.js and create server listening on localhost:4000
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('Init Nuxt.js', async t => {
|
|
|
|
const rootDir = resolve(__dirname, 'fixtures/spa')
|
|
|
|
const config = require(resolve(rootDir, 'nuxt.config.js'))
|
|
|
|
config.rootDir = rootDir
|
|
|
|
|
|
|
|
const logSpy = await interceptLog(async () => {
|
|
|
|
nuxt = new Nuxt(config)
|
|
|
|
await new Builder(nuxt).build()
|
|
|
|
await nuxt.listen(port, 'localhost')
|
|
|
|
})
|
|
|
|
|
|
|
|
t.true(logSpy.calledWithMatch('DONE'))
|
|
|
|
t.true(logSpy.calledWithMatch('OPEN'))
|
2017-10-07 09:07:52 +00:00
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/ (basic spa)', async t => {
|
|
|
|
const logSpy = await interceptLog()
|
2017-10-07 09:07:52 +00:00
|
|
|
const { html } = await renderRoute('/')
|
|
|
|
t.true(html.includes('Hello SPA!'))
|
2017-12-17 19:30:26 +00:00
|
|
|
release()
|
|
|
|
t.true(logSpy.withArgs('created').notCalled)
|
|
|
|
t.true(logSpy.withArgs('mounted').calledOnce)
|
2017-10-07 09:07:52 +00:00
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/custom (custom layout)', async t => {
|
|
|
|
const logSpy = await interceptLog()
|
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'))
|
2017-12-17 19:30:26 +00:00
|
|
|
release()
|
|
|
|
t.true(logSpy.withArgs('created').calledOnce)
|
|
|
|
t.true(logSpy.withArgs('mounted').calledOnce)
|
2017-10-07 09:07:52 +00:00
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/custom (not default layout)', async t => {
|
|
|
|
const logSpy = await interceptLog()
|
2017-11-14 08:53:01 +00:00
|
|
|
const { head } = await renderRoute('/custom')
|
|
|
|
t.false(head.includes('src="/_nuxt/layouts/default.'))
|
2017-12-17 19:30:26 +00:00
|
|
|
release()
|
|
|
|
t.true(logSpy.withArgs('created').calledOnce)
|
|
|
|
t.true(logSpy.withArgs('mounted').calledOnce)
|
2017-11-14 08:53:01 +00:00
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/custom (call mounted and created once)', async t => {
|
2017-12-13 05:38:46 +00:00
|
|
|
const logSpy = await interceptLog()
|
2017-11-17 08:27:34 +00:00
|
|
|
await renderRoute('/custom')
|
2017-12-13 05:38:46 +00:00
|
|
|
release()
|
|
|
|
t.true(logSpy.withArgs('created').calledOnce)
|
|
|
|
t.true(logSpy.withArgs('mounted').calledOnce)
|
2017-11-17 08:27:34 +00:00
|
|
|
})
|
|
|
|
|
2017-12-07 09:10:06 +00:00
|
|
|
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
|
2017-12-17 19:30:26 +00:00
|
|
|
test.after.always('Closing server and nuxt.js', async t => {
|
|
|
|
await nuxt.close()
|
2017-10-07 09:07:52 +00:00
|
|
|
})
|