diff --git a/test/basic.dev.test.js b/test/basic.dev.test.js new file mode 100644 index 0000000000..b8f8ed2b3a --- /dev/null +++ b/test/basic.dev.test.js @@ -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('

My component!

')) +}) + +// Close server and ask nuxt to stop listening to file changes +test.after('Closing server and nuxt.js', t => { + server.close() + nuxt.close() +}) diff --git a/test/basic.fail.generate.test.js b/test/basic.fail.generate.test.js new file mode 100644 index 0000000000..034a6f3365 --- /dev/null +++ b/test/basic.fail.generate.test.js @@ -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!') + }) + }) +}) diff --git a/test/basic.generate.test.js b/test/basic.generate.test.js index 923deeb725..1c285d4a21 100644 --- a/test/basic.generate.test.js +++ b/test/basic.generate.test.js @@ -2,6 +2,7 @@ import test from 'ava' import { resolve } from 'path' import http from 'http' import serveStatic from 'serve-static' +import finalhandler from 'finalhandler' import rp from 'request-promise-native' const port = 4003 const url = (route) => 'http://localhost:' + port + route @@ -12,14 +13,16 @@ 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: false - } - nuxt = new Nuxt(options) + const rootDir = resolve(__dirname, 'fixtures/basic') + let config = require(resolve(rootDir, 'nuxt.config.js')) + config.rootDir = rootDir + config.dev = false + nuxt = new Nuxt(config) await nuxt.generate() 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) }) @@ -59,6 +62,30 @@ test('/async-data', async t => { t.true(html.includes('

Nuxt.js

')) }) +test('/users/1', async t => { + const html = await rp(url('/users/1')) + t.true(html.includes('

User: 1

')) +}) + +test('/users/2', async t => { + const html = await rp(url('/users/2')) + t.true(html.includes('

User: 2

')) +}) + +test('/users/3', async t => { + const html = await rp(url('/users/3')) + t.true(html.includes('

User: 3

')) +}) + +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 => { const html = await rp(url('/validate')) t.true(html.includes('
')) diff --git a/test/basic.test.js b/test/basic.test.js index a7a4a836e6..b720fd3b82 100755 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -56,6 +56,11 @@ test('/async-data', async t => { t.true(html.includes('

Nuxt.js

')) }) +test('/users/1', async t => { + const { html } = await nuxt.renderRoute('/users/1') + t.true(html.includes('

User: 1

')) +}) + test('/validate should display a 404', async t => { const { html } = await nuxt.renderRoute('/validate') t.true(html.includes('This page could not be found')) diff --git a/test/fixtures/basic/nuxt.config.js b/test/fixtures/basic/nuxt.config.js new file mode 100644 index 0000000000..1793846e1f --- /dev/null +++ b/test/fixtures/basic/nuxt.config.js @@ -0,0 +1,11 @@ +module.exports = { + generate: { + routeParams: { + '/users/:id': [ + { id: 1 }, + { id: 2 }, + { id: 3 } + ] + } + } +} diff --git a/test/fixtures/basic/pages/users/_id.vue b/test/fixtures/basic/pages/users/_id.vue new file mode 100644 index 0000000000..768a6924d8 --- /dev/null +++ b/test/fixtures/basic/pages/users/_id.vue @@ -0,0 +1,11 @@ + + + diff --git a/test/fixtures/with-config/nuxt.config.js b/test/fixtures/with-config/nuxt.config.js index 040ef75068..862863d8d1 100644 --- a/test/fixtures/with-config/nuxt.config.js +++ b/test/fixtures/with-config/nuxt.config.js @@ -3,5 +3,11 @@ module.exports = { base: '/test/' }, cache: true, - loading: '~components/loading' + plugins: ['~plugins/test.js'], + loading: '~components/loading', + env: { + bool: true, + num: 23, + string: 'Nuxt.js' + } } diff --git a/test/fixtures/with-config/package.json b/test/fixtures/with-config/package.json new file mode 100644 index 0000000000..60cfd8e3ad --- /dev/null +++ b/test/fixtures/with-config/package.json @@ -0,0 +1,5 @@ +{ + "name": "with-config", + "version": "1.0.0", + "dependencies": {} +} diff --git a/test/fixtures/with-config/pages/env.vue b/test/fixtures/with-config/pages/env.vue new file mode 100644 index 0000000000..08ab5daca5 --- /dev/null +++ b/test/fixtures/with-config/pages/env.vue @@ -0,0 +1,11 @@ + + + diff --git a/test/with-config.test.js b/test/with-config.test.js index ad8a677b2d..fa6cf141fc 100644 --- a/test/with-config.test.js +++ b/test/with-config.test.js @@ -30,8 +30,25 @@ test('/test/ (router base)', async t => { t.true(html.includes('

I have custom configurations

')) }) +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 test.after('Closing server and nuxt.js', t => { server.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) +})