test: add tests for spa

This commit is contained in:
Pooya Parsa 2017-10-07 12:37:52 +03:30
parent f6d09642d7
commit 75350cdd78
4 changed files with 63 additions and 0 deletions

7
test/fixtures/spa/layouts/custom.vue vendored Normal file
View File

@ -0,0 +1,7 @@
<template>
<div>
Custom layout
<br>
<nuxt/>
</div>
</template>

5
test/fixtures/spa/nuxt.config.js vendored Normal file
View File

@ -0,0 +1,5 @@
module.exports = {
rootDir: __dirname,
mode: 'spa',
dev: false
}

15
test/fixtures/spa/pages/index.vue vendored Normal file
View File

@ -0,0 +1,15 @@
<template>
<div>
Hello SPA!
</div>
</template>
<script>
export default {
layout: 'custom',
mounted () {
window.indexMounted = (+window.indexMounted) + 1
console.log('mounted')
}
}
</script>

36
test/spa.test.js Executable file
View File

@ -0,0 +1,36 @@
import test from 'ava'
import { resolve } from 'path'
import { Nuxt, Builder } from '../index.js'
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 html = window.document.body.innerHTML
return { window, html }
}
// 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!'))
})
test('/ (custom layout)', async t => {
const { html } = await renderRoute('/')
t.true(html.includes('Custom layout'))
})
// Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => {
nuxt.close()
})