test 100%

This commit is contained in:
Sébastien Chopin 2016-12-21 20:51:43 +01:00
parent 25f9f1a345
commit c559740b2f
10 changed files with 193 additions and 7 deletions

32
test/basic.dev.test.js Normal file
View File

@ -0,0 +1,32 @@
import test from 'ava'
import { resolve } from 'path'
const port = 4005
const url = (route) => 'http://localhost:' + port + route
let nuxt = null
let server = null
// Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => {
const Nuxt = require('../')
const options = {
rootDir: resolve(__dirname, 'fixtures/basic'),
dev: true
}
nuxt = new Nuxt(options)
await nuxt.build()
server = new nuxt.Server(nuxt)
server.listen(port, 'localhost')
})
test('/stateless', async t => {
const window = await nuxt.renderAndGetWindow(url('/stateless'))
const html = window.document.body.innerHTML
t.true(html.includes('<h1>My component!</h1>'))
})
// Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => {
server.close()
nuxt.close()
})

View File

@ -0,0 +1,61 @@
import test from 'ava'
import { resolve } from 'path'
test('Fail to generate without routeParams', t => {
const Nuxt = require('../')
const options = {
rootDir: resolve(__dirname, 'fixtures/basic'),
dev: false
// no generate.routeParams
}
const nuxt = new Nuxt(options)
return new Promise((resolve) => {
var oldExit = process.exit
var oldCE = console.error // eslint-disable-line no-console
var _log = ''
console.error = (s) => { _log += s } // eslint-disable-line no-console
process.exit = (code) => {
process.exit = oldExit
console.error = oldCE // eslint-disable-line no-console
t.is(code, 1)
t.true(_log.includes('Could not generate the dynamic route /users/:id'))
resolve()
}
nuxt.generate()
})
})
test('Fail with routeParams which throw an error', t => {
const Nuxt = require('../')
const options = {
rootDir: resolve(__dirname, 'fixtures/basic'),
dev: false,
generate: {
routeParams: {
'/users/:id': function () {
return new Promise((resolve, reject) => {
reject('Not today!')
})
}
}
}
}
const nuxt = new Nuxt(options)
return new Promise((resolve) => {
var oldExit = process.exit
var oldCE = console.error // eslint-disable-line no-console
var _log = ''
console.error = (s) => { _log += s } // eslint-disable-line no-console
process.exit = (code) => {
process.exit = oldExit
console.error = oldCE // eslint-disable-line no-console
t.is(code, 1)
t.true(_log.includes('Could not resolve routeParams[/users/:id]'))
resolve()
}
nuxt.generate()
.catch((e) => {
t.true(e === 'Not today!')
})
})
})

View File

@ -2,6 +2,7 @@ import test from 'ava'
import { resolve } from 'path' import { resolve } from 'path'
import http from 'http' import http from 'http'
import serveStatic from 'serve-static' import serveStatic from 'serve-static'
import finalhandler from 'finalhandler'
import rp from 'request-promise-native' import rp from 'request-promise-native'
const port = 4003 const port = 4003
const url = (route) => 'http://localhost:' + port + route const url = (route) => 'http://localhost:' + port + route
@ -12,14 +13,16 @@ let server = null
// Init nuxt.js and create server listening on localhost:4000 // Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => { test.before('Init Nuxt.js', async t => {
const Nuxt = require('../') const Nuxt = require('../')
const options = { const rootDir = resolve(__dirname, 'fixtures/basic')
rootDir: resolve(__dirname, 'fixtures/basic'), let config = require(resolve(rootDir, 'nuxt.config.js'))
dev: false config.rootDir = rootDir
} config.dev = false
nuxt = new Nuxt(options) nuxt = new Nuxt(config)
await nuxt.generate() await nuxt.generate()
const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist')) const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist'))
server = http.createServer(serve) server = http.createServer((req, res) => {
serve(req, res, finalhandler(req, res))
})
server.listen(port) server.listen(port)
}) })
@ -59,6 +62,30 @@ test('/async-data', async t => {
t.true(html.includes('<p>Nuxt.js</p>')) t.true(html.includes('<p>Nuxt.js</p>'))
}) })
test('/users/1', async t => {
const html = await rp(url('/users/1'))
t.true(html.includes('<h1>User: 1</h1>'))
})
test('/users/2', async t => {
const html = await rp(url('/users/2'))
t.true(html.includes('<h1>User: 2</h1>'))
})
test('/users/3', async t => {
const html = await rp(url('/users/3'))
t.true(html.includes('<h1>User: 3</h1>'))
})
test('/users/4 -> Not found', async t => {
try {
await rp(url('/users/4'))
} catch (error) {
t.true(error.statusCode === 404)
t.true(error.response.body.includes('Cannot GET /users/4'))
}
})
test('/validate should not be server-rendered', async t => { test('/validate should not be server-rendered', async t => {
const html = await rp(url('/validate')) const html = await rp(url('/validate'))
t.true(html.includes('<div id="__nuxt"></div>')) t.true(html.includes('<div id="__nuxt"></div>'))

View File

@ -56,6 +56,11 @@ test('/async-data', async t => {
t.true(html.includes('<p>Nuxt.js</p>')) t.true(html.includes('<p>Nuxt.js</p>'))
}) })
test('/users/1', async t => {
const { html } = await nuxt.renderRoute('/users/1')
t.true(html.includes('<h1>User: 1</h1>'))
})
test('/validate should display a 404', async t => { test('/validate should display a 404', async t => {
const { html } = await nuxt.renderRoute('/validate') const { html } = await nuxt.renderRoute('/validate')
t.true(html.includes('This page could not be found')) t.true(html.includes('This page could not be found'))

11
test/fixtures/basic/nuxt.config.js vendored Normal file
View File

@ -0,0 +1,11 @@
module.exports = {
generate: {
routeParams: {
'/users/:id': [
{ id: 1 },
{ id: 2 },
{ id: 3 }
]
}
}
}

11
test/fixtures/basic/pages/users/_id.vue vendored Normal file
View File

@ -0,0 +1,11 @@
<template>
<h1>User: {{ id }}</h1>
</template>
<script>
export default {
data ({ params }) {
return { id: params.id }
}
}
</script>

View File

@ -3,5 +3,11 @@ module.exports = {
base: '/test/' base: '/test/'
}, },
cache: true, cache: true,
loading: '~components/loading' plugins: ['~plugins/test.js'],
loading: '~components/loading',
env: {
bool: true,
num: 23,
string: 'Nuxt.js'
}
} }

View File

@ -0,0 +1,5 @@
{
"name": "with-config",
"version": "1.0.0",
"dependencies": {}
}

11
test/fixtures/with-config/pages/env.vue vendored Normal file
View File

@ -0,0 +1,11 @@
<template>
<pre>{{ env }}</pre>
</template>
<script>
export default {
data ({ env }) {
return { env }
}
}
</script>

View File

@ -30,8 +30,25 @@ test('/test/ (router base)', async t => {
t.true(html.includes('<h1>I have custom configurations</h1>')) t.true(html.includes('<h1>I have custom configurations</h1>'))
}) })
test('/test/env', async t => {
const window = await nuxt.renderAndGetWindow(url('/test/env'))
const html = window.document.body.innerHTML
t.true(html.includes('"bool": true'))
t.true(html.includes('"num": 23'))
t.true(html.includes('"string": "Nuxt.js"'))
})
// 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 => {
server.close() server.close()
nuxt.close() nuxt.close()
}) })
test.after('Should be able to start Nuxt with build done', t => {
const Nuxt = require('../')
const rootDir = resolve(__dirname, 'fixtures/with-config')
let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir
config.dev = false
nuxt = new Nuxt(config)
})