test: call created and mounted only once with spa and custom layout

This commit is contained in:
Clark Du 2017-11-17 16:27:34 +08:00 committed by Pooya Parsa
parent 68680ee65a
commit 7f8671f1de
3 changed files with 22 additions and 1 deletions

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

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

View File

@ -5,8 +5,10 @@
<script> <script>
export default { export default {
layout: 'custom', layout: 'custom',
created() {
console.log('created') // eslint-disable-line no-console
},
mounted() { mounted() {
window.customMounted = (+window.customMounted) + 1
console.log('mounted') // eslint-disable-line no-console console.log('mounted') // eslint-disable-line no-console
} }
} }

View File

@ -1,4 +1,5 @@
import test from 'ava' import test from 'ava'
import stdMocks from 'std-mocks'
import { Nuxt, Builder } from '../index.js' import { Nuxt, Builder } from '../index.js'
let nuxt = null let nuxt = null
@ -35,6 +36,17 @@ test('/custom (not default layout)', async t => {
t.false(head.includes('src="/_nuxt/layouts/default.')) 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)
})
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => { test.after('Closing server and nuxt.js', t => {
nuxt.close() nuxt.close()