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 // 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 // Will not update the instance data, so we have to update $data ourselves
function fixPrepatch (to, ___) { function fixPrepatch (to, ___) {
@ -351,17 +365,7 @@ function fixPrepatch (to, ___) {
return instance.constructor.options.__file return instance.constructor.options.__file
}) })
// Hide error component if no error showNextPage.call(this, to)
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)
<% if (isDev) { %> <% if (isDev) { %>
// Hot reloading // Hot reloading
setTimeout(() => hotReloadAPI(this), 100) setTimeout(() => hotReloadAPI(this), 100)
@ -541,18 +545,13 @@ async function mountApp(__app) {
// If not redirected // If not redirected
if (!path) { if (!path) {
normalizeComponents(router.currentRoute, router.currentRoute) normalizeComponents(router.currentRoute, router.currentRoute)
showNextPage.call(_app, router.currentRoute)
// Dont call fixPrepatch.call(_app, router.currentRoute, router.currentRoute) since it's first render // Dont call fixPrepatch.call(_app, router.currentRoute, router.currentRoute) since it's first render
mountApp() mountApp()
return return
} }
// Push the path and then mount app // Push the path and then mount app
let mounted = false router.push(path, () => mountApp(), (err) => console.error(err))
router.afterEach(() => {
if (mounted) return
mounted = true
mountApp()
})
router.push(path)
}) })
} }

View File

@ -143,6 +143,7 @@ export async function setContext(app, context) {
} }
// Dynamic keys // Dynamic keys
app.context.next = context.next app.context.next = context.next
app.context._redirected = false
app.context.isHMR = !!context.isHMR app.context.isHMR = !!context.isHMR
if (context.route) app.context.route = await getRouteData(context.route) if (context.route) app.context.route = await getRouteData(context.route)
app.context.params = app.context.route.params || {} 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')) t.true(html.includes('This page could not be found'))
}) })
test.todo('/validate?valid=true (#1705)') test('/validate?valid=true', async t => {
// test('/validate?valid=true', async t => { const window = await nuxt.renderAndGetWindow(url('/validate?valid=true'))
// const window = await nuxt.renderAndGetWindow(url('/validate?valid=true')) const html = window.document.body.innerHTML
// const html = window.document.body.innerHTML t.true(html.includes('I am valid</h1>'))
// t.true(html.includes('I am valid</h1>')) })
// })
test('/redirect should not be server-rendered', async t => { test('/redirect should not be server-rendered', async t => {
const html = await rp(url('/redirect')) const html = await rp(url('/redirect'))

View File

@ -9,7 +9,7 @@ const url = (route) => 'http://localhost:' + port + route
let nuxt = null 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 => { test.before('Init Nuxt.js', async t => {
const options = { const options = {
rootDir: resolve(__dirname, 'fixtures/basic'), rootDir: resolve(__dirname, 'fixtures/basic'),

View File

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

View File

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

View File

@ -1,3 +1,3 @@
export default function (context) { 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
} }