fix: Fix tests in generate mode with redirect

This commit is contained in:
Sébastien Chopin 2017-10-28 22:42:51 +02:00
parent 41b775a365
commit e02e8df224
7 changed files with 28 additions and 29 deletions

View File

@ -330,6 +330,20 @@ function normalizeComponents (to, ___) {
})
}
function showNextPage(to) {
// Hide error component if no error
if (this._hadError && this._dateLastError === this.$options.nuxt.dateErr) {
this.error()
}
// Set layout
let layout = this.$options.nuxt.err ? NuxtError.layout : to.matched[0].components.default.options.layout
if (typeof layout === 'function') {
layout = layout(app.context)
}
this.setLayout(layout)
}
// When navigating on a different route but the same component is used, Vue.js
// Will not update the instance data, so we have to update $data ourselves
function fixPrepatch (to, ___) {
@ -351,17 +365,7 @@ function fixPrepatch (to, ___) {
return instance.constructor.options.__file
})
// Hide error component if no error
if (this._hadError && this._dateLastError === this.$options.nuxt.dateErr) {
this.error()
}
// Set layout
let layout = this.$options.nuxt.err ? NuxtError.layout : to.matched[0].components.default.options.layout
if (typeof layout === 'function') {
layout = layout(app.context)
}
this.setLayout(layout)
showNextPage.call(this, to)
<% if (isDev) { %>
// Hot reloading
setTimeout(() => hotReloadAPI(this), 100)
@ -541,18 +545,13 @@ async function mountApp(__app) {
// If not redirected
if (!path) {
normalizeComponents(router.currentRoute, router.currentRoute)
showNextPage.call(_app, router.currentRoute)
// Dont call fixPrepatch.call(_app, router.currentRoute, router.currentRoute) since it's first render
mountApp()
return
}
// Push the path and then mount app
let mounted = false
router.afterEach(() => {
if (mounted) return
mounted = true
mountApp()
})
router.push(path)
router.push(path, () => mountApp(), (err) => console.error(err))
})
}

View File

@ -143,6 +143,7 @@ export async function setContext(app, context) {
}
// Dynamic keys
app.context.next = context.next
app.context._redirected = false
app.context.isHMR = !!context.isHMR
if (context.route) app.context.route = await getRouteData(context.route)
app.context.params = app.context.route.params || {}

View File

@ -104,12 +104,11 @@ test('/validate -> should display a 404', async t => {
t.true(html.includes('This page could not be found'))
})
test.todo('/validate?valid=true (#1705)')
// test('/validate?valid=true', async t => {
// const window = await nuxt.renderAndGetWindow(url('/validate?valid=true'))
// const html = window.document.body.innerHTML
// t.true(html.includes('I am valid</h1>'))
// })
test('/validate?valid=true', async t => {
const window = await nuxt.renderAndGetWindow(url('/validate?valid=true'))
const html = window.document.body.innerHTML
t.true(html.includes('I am valid</h1>'))
})
test('/redirect should not be server-rendered', async t => {
const html = await rp(url('/redirect'))

View File

@ -9,7 +9,7 @@ const url = (route) => 'http://localhost:' + port + route
let nuxt = null
// Init nuxt.js and create server listening on localhost:4000
// Init nuxt.js and create server listening on localhost:4003
test.before('Init Nuxt.js', async t => {
const options = {
rootDir: resolve(__dirname, 'fixtures/basic'),

View File

@ -1,5 +1,5 @@
<template>
<div></div>
<div>Redirecting...</div>
</template>
<script>

View File

@ -4,8 +4,8 @@
<script>
export default {
fetch ({ isServer, beforeNuxtRender }) {
if (isServer) {
fetch ({ beforeNuxtRender }) {
if (process.server) {
beforeNuxtRender(({ nuxtState }) => {
nuxtState.test = true
})

View File

@ -1,3 +1,3 @@
export default function (context) {
context.userAgent = context.isServer ? context.req.headers['user-agent'] : navigator.userAgent
context.userAgent = process.server ? context.req.headers['user-agent'] : navigator.userAgent
}