diff --git a/package.json b/package.json index d01615e1dd..16a747e1e1 100644 --- a/package.json +++ b/package.json @@ -96,6 +96,8 @@ "jsdom": "^9.8.3", "json-loader": "^0.5.4", "nyc": "^10.0.0", + "request": "^2.79.0", + "request-promise-native": "^1.0.3", "webpack-node-externals": "^1.5.4" } } diff --git a/test/basic.generate.test.js b/test/basic.generate.test.js index 1c492ef87e..923deeb725 100644 --- a/test/basic.generate.test.js +++ b/test/basic.generate.test.js @@ -1,10 +1,13 @@ import test from 'ava' import { resolve } from 'path' -import pify from 'pify' -import fs from 'fs' -const readFile = pify(fs.readFile) +import http from 'http' +import serveStatic from 'serve-static' +import rp from 'request-promise-native' +const port = 4003 +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 => { @@ -15,9 +18,84 @@ test.before('Init Nuxt.js', async t => { } nuxt = new Nuxt(options) await nuxt.generate() + const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist')) + server = http.createServer(serve) + server.listen(port) }) test('/stateless', async t => { - const html = await readFile(resolve(__dirname, 'fixtures/basic/dist/stateless/index.html'), 'utf8') + const window = await nuxt.renderAndGetWindow(url('/stateless')) + const html = window.document.body.innerHTML t.true(html.includes('

My component!

')) }) + +test('/css', async t => { + const window = await nuxt.renderAndGetWindow(url('/css')) + const element = window.document.querySelector('.red') + t.not(element, null) + t.is(element.textContent, 'This is red') + t.is(element.className, 'red') + t.is(window.getComputedStyle(element).color, 'red') +}) + +test('/stateful', async t => { + const window = await nuxt.renderAndGetWindow(url('/stateful')) + const html = window.document.body.innerHTML + t.true(html.includes('

The answer is 42

')) +}) + +test('/head', async t => { + const window = await nuxt.renderAndGetWindow(url('/head')) + const html = window.document.body.innerHTML + const metas = window.document.getElementsByTagName('meta') + t.is(window.document.title, 'My title') + t.is(metas[0].getAttribute('content'), 'my meta') + t.true(html.includes('

I can haz meta tags

')) +}) + +test('/async-data', async t => { + const window = await nuxt.renderAndGetWindow(url('/async-data')) + const html = window.document.body.innerHTML + t.true(html.includes('

Nuxt.js

')) +}) + +test('/validate should not be server-rendered', async t => { + const html = await rp(url('/validate')) + t.true(html.includes('
')) + t.true(html.includes('serverRendered:!1')) +}) + +test('/validate -> should display a 404', async t => { + const window = await nuxt.renderAndGetWindow(url('/validate')) + const html = window.document.body.innerHTML + t.true(html.includes('This page could not be found')) +}) + +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

')) +}) + +test('/redirect should not be server-rendered', async t => { + const html = await rp(url('/redirect')) + t.true(html.includes('
')) + t.true(html.includes('serverRendered:!1')) +}) + +test('/redirect -> check redirected source', async t => { + const window = await nuxt.renderAndGetWindow(url('/redirect')) + const html = window.document.body.innerHTML + t.true(html.includes('

Index page

')) +}) + +test('/error', async t => { + const window = await nuxt.renderAndGetWindow(url('/error')) + const html = window.document.body.innerHTML + t.true(html.includes('Error mouahahah')) +}) + +// Close server and ask nuxt to stop listening to file changes +test.after('Closing server', t => { + server.close() +}) diff --git a/test/basic.test.js b/test/basic.test.js index 5d5b28a00b..a7a4a836e6 100755 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -1,5 +1,6 @@ import test from 'ava' import { resolve } from 'path' +import rp from 'request-promise-native' const port = 4000 const url = (route) => 'http://localhost:' + port + route @@ -66,8 +67,10 @@ test('/validate?valid=true', async t => { }) test('/redirect', async t => { - const { html } = await nuxt.renderRoute('/redirect') + const { html, redirected } = await nuxt.renderRoute('/redirect') t.true(html.includes('
')) + t.true(redirected.path === '/') + t.true(redirected.status === 302) }) test('/redirect -> check redirected source', async t => { @@ -80,6 +83,32 @@ test('/error', async t => { const { html, error } = await nuxt.renderRoute('/error') t.true(html.includes('Error mouahahah')) t.true(error.message.includes('Error mouahahah')) + t.true(error.statusCode === 500) +}) + +test('/error status code', async t => { + try { + await rp(url('/error')) + } catch (err) { + t.true(err.statusCode === 500) + t.true(err.response.body.includes('Error mouahahah')) + } +}) + +test('/error2', async t => { + const { html, error } = await nuxt.renderRoute('/error2') + t.true(html.includes('Custom error')) + t.true(error.message.includes('Custom error')) + t.true(error.statusCode === undefined) +}) + +test('/error2 status code', async t => { + try { + await rp(url('/error2')) + } catch (err) { + t.true(err.statusCode === 500) + t.true(err.response.body.includes('Custom error')) + } }) // Close server and ask nuxt to stop listening to file changes diff --git a/test/fixtures/basic/pages/error2.vue b/test/fixtures/basic/pages/error2.vue new file mode 100644 index 0000000000..13eff4acb3 --- /dev/null +++ b/test/fixtures/basic/pages/error2.vue @@ -0,0 +1,11 @@ + + + diff --git a/test/fixtures/with-config/nuxt.config.js b/test/fixtures/with-config/nuxt.config.js new file mode 100644 index 0000000000..426afb8d18 --- /dev/null +++ b/test/fixtures/with-config/nuxt.config.js @@ -0,0 +1,5 @@ +module.exports = { + router: { + base: '/test/' + } +} diff --git a/test/fixtures/with-config/pages/about.vue b/test/fixtures/with-config/pages/about.vue new file mode 100644 index 0000000000..93009e1b3e --- /dev/null +++ b/test/fixtures/with-config/pages/about.vue @@ -0,0 +1,6 @@ +