test: add tests for validate with error

This commit is contained in:
Pooya Parsa 2018-08-25 22:24:16 +04:30
parent 87d303845f
commit 83d9dc4105
3 changed files with 26 additions and 2 deletions

View File

@ -5,9 +5,15 @@
<script>
export default {
validate({ query }) {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (query.error) {
const err = new Error('Custom Error')
err.statusCode = parseInt(query.error) || 500
reject(err)
} else {
resolve(Boolean(query.valid))
}
}, 500)
})
}

View File

@ -5,6 +5,12 @@
<script>
export default {
validate({ query }) {
if (query.error) {
const err = new Error('Custom Error')
err.statusCode = parseInt(query.error) || 500
throw err
}
return Boolean(query.valid)
}
}

View File

@ -111,6 +111,18 @@ describe('basic ssr', () => {
expect(html.includes('<h1>I am valid</h1>')).toBe(true)
})
test('/validate?error=403', async () => {
const { html, error } = await nuxt.renderRoute('/validate?error=403')
expect(error).toMatchObject({ statusCode: 403, message: 'Custom Error' })
expect(html.includes('Custom Error')).toBe(true)
})
test('/validate-async?error=503', async () => {
const { html, error } = await nuxt.renderRoute('/validate-async?error=503')
expect(error).toMatchObject({ statusCode: 503, message: 'Custom Error' })
expect(html.includes('Custom Error')).toBe(true)
})
test('/before-enter', async () => {
const { html } = await nuxt.renderRoute('/before-enter')
expect(html.includes('<h1>Index page</h1>')).toBe(true)