2017-11-17 00:27:30 +00:00
|
|
|
import { existsSync } from 'fs'
|
2016-12-21 14:03:37 +00:00
|
|
|
import http from 'http'
|
2018-03-16 19:52:17 +00:00
|
|
|
import { resolve } from 'path'
|
|
|
|
|
|
|
|
import test from 'ava'
|
|
|
|
import { remove } from 'fs-extra'
|
2016-12-21 14:03:37 +00:00
|
|
|
import serveStatic from 'serve-static'
|
2016-12-21 19:51:43 +00:00
|
|
|
import finalhandler from 'finalhandler'
|
2016-12-21 14:03:37 +00:00
|
|
|
import rp from 'request-promise-native'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
2017-12-12 09:42:29 +00:00
|
|
|
import { Nuxt, Builder, Generator } from '..'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
|
|
|
import { interceptLog, release } from './helpers/console'
|
2018-03-16 16:12:06 +00:00
|
|
|
import { loadConfig } from './helpers/config'
|
2017-06-14 18:51:14 +00:00
|
|
|
|
2017-05-21 19:00:01 +00:00
|
|
|
const port = 4002
|
2018-01-13 05:22:11 +00:00
|
|
|
const url = route => 'http://localhost:' + port + route
|
2017-12-17 19:30:26 +00:00
|
|
|
const rootDir = resolve(__dirname, 'fixtures/basic')
|
2016-12-20 19:44:42 +00:00
|
|
|
|
|
|
|
let nuxt = null
|
2016-12-21 14:03:37 +00:00
|
|
|
let server = null
|
2017-12-17 19:30:26 +00:00
|
|
|
let generator = null
|
2016-12-20 19:44:42 +00:00
|
|
|
|
|
|
|
// Init nuxt.js and create server listening on localhost:4000
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('Init Nuxt.js', async t => {
|
2018-03-16 16:12:06 +00:00
|
|
|
const config = loadConfig('basic', {
|
|
|
|
buildDir: '.nuxt-generate',
|
|
|
|
dev: false
|
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
config.build.stats = false
|
|
|
|
|
|
|
|
const logSpy = await interceptLog(async () => {
|
|
|
|
nuxt = new Nuxt(config)
|
|
|
|
const builder = new Builder(nuxt)
|
|
|
|
generator = new Generator(nuxt, builder)
|
|
|
|
|
|
|
|
await generator.generate()
|
|
|
|
})
|
|
|
|
t.true(logSpy.calledWithMatch('DONE'))
|
|
|
|
|
2016-12-21 14:03:37 +00:00
|
|
|
const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist'))
|
2016-12-21 19:51:43 +00:00
|
|
|
server = http.createServer((req, res) => {
|
|
|
|
serve(req, res, finalhandler(req, res))
|
|
|
|
})
|
2017-06-20 12:55:52 +00:00
|
|
|
server.listen(port)
|
2016-12-20 19:44:42 +00:00
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('Check ready hook called', async t => {
|
2017-10-31 11:33:15 +00:00
|
|
|
t.true(nuxt.__hook_called__)
|
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/stateless', async t => {
|
2016-12-21 14:03:37 +00:00
|
|
|
const window = await nuxt.renderAndGetWindow(url('/stateless'))
|
|
|
|
const html = window.document.body.innerHTML
|
2016-12-20 19:44:42 +00:00
|
|
|
t.true(html.includes('<h1>My component!</h1>'))
|
|
|
|
})
|
2016-12-21 14:03:37 +00:00
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/css', async t => {
|
2016-12-21 14:03:37 +00:00
|
|
|
const window = await nuxt.renderAndGetWindow(url('/css'))
|
2017-12-29 08:33:13 +00:00
|
|
|
|
|
|
|
const headHtml = window.document.head.innerHTML
|
|
|
|
t.true(headHtml.includes('.red{color:red}'))
|
|
|
|
|
2016-12-21 14:03:37 +00:00
|
|
|
const element = window.document.querySelector('.red')
|
|
|
|
t.not(element, null)
|
|
|
|
t.is(element.textContent, 'This is red')
|
|
|
|
t.is(element.className, 'red')
|
2017-12-29 08:33:13 +00:00
|
|
|
// t.is(window.getComputedStyle(element), 'red')
|
2016-12-21 14:03:37 +00:00
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/stateful', async t => {
|
2016-12-21 14:03:37 +00:00
|
|
|
const window = await nuxt.renderAndGetWindow(url('/stateful'))
|
|
|
|
const html = window.document.body.innerHTML
|
|
|
|
t.true(html.includes('<div><p>The answer is 42</p></div>'))
|
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/head', async t => {
|
|
|
|
const logSpy = await interceptLog()
|
2016-12-21 14:03:37 +00:00
|
|
|
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('<div><h1>I can haz meta tags</h1></div>'))
|
2017-12-17 19:30:26 +00:00
|
|
|
release()
|
|
|
|
t.is(logSpy.getCall(0).args[0], 'Body script!')
|
2016-12-21 14:03:37 +00:00
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/async-data', async t => {
|
2016-12-21 14:03:37 +00:00
|
|
|
const window = await nuxt.renderAndGetWindow(url('/async-data'))
|
|
|
|
const html = window.document.body.innerHTML
|
|
|
|
t.true(html.includes('<p>Nuxt.js</p>'))
|
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/users/1/index.html', async t => {
|
|
|
|
const html = await rp(url('/users/1/index.html'))
|
2016-12-21 19:51:43 +00:00
|
|
|
t.true(html.includes('<h1>User: 1</h1>'))
|
2018-01-13 05:22:11 +00:00
|
|
|
t.true(
|
|
|
|
existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1/index.html'))
|
|
|
|
)
|
2017-11-17 00:27:30 +00:00
|
|
|
t.false(existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1.html')))
|
2016-12-21 19:51:43 +00:00
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/users/2', async t => {
|
2016-12-21 19:51:43 +00:00
|
|
|
const html = await rp(url('/users/2'))
|
|
|
|
t.true(html.includes('<h1>User: 2</h1>'))
|
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/users/3 (payload given)', async t => {
|
2016-12-21 19:51:43 +00:00
|
|
|
const html = await rp(url('/users/3'))
|
2017-05-30 10:00:31 +00:00
|
|
|
t.true(html.includes('<h1>User: 3000</h1>'))
|
2016-12-21 19:51:43 +00:00
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/users/4 -> Not found', async t => {
|
|
|
|
const error = await t.throws(rp(url('/users/4')))
|
|
|
|
t.true(error.statusCode === 404)
|
|
|
|
t.true(error.response.body.includes('Cannot GET /users/4'))
|
2016-12-21 19:51:43 +00:00
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/validate should not be server-rendered', async t => {
|
2016-12-21 14:03:37 +00:00
|
|
|
const html = await rp(url('/validate'))
|
|
|
|
t.true(html.includes('<div id="__nuxt"></div>'))
|
|
|
|
t.true(html.includes('serverRendered:!1'))
|
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/validate -> should display a 404', async t => {
|
2016-12-21 14:03:37 +00:00
|
|
|
const window = await nuxt.renderAndGetWindow(url('/validate'))
|
|
|
|
const html = window.document.body.innerHTML
|
|
|
|
t.true(html.includes('This page could not be found'))
|
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/validate?valid=true', async t => {
|
2017-10-28 20:42:51 +00:00
|
|
|
const window = await nuxt.renderAndGetWindow(url('/validate?valid=true'))
|
|
|
|
const html = window.document.body.innerHTML
|
|
|
|
t.true(html.includes('I am valid</h1>'))
|
|
|
|
})
|
2016-12-21 14:03:37 +00:00
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/redirect should not be server-rendered', async t => {
|
2016-12-21 14:03:37 +00:00
|
|
|
const html = await rp(url('/redirect'))
|
|
|
|
t.true(html.includes('<div id="__nuxt"></div>'))
|
|
|
|
t.true(html.includes('serverRendered:!1'))
|
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/redirect -> check redirected source', async t => {
|
2016-12-21 14:03:37 +00:00
|
|
|
const window = await nuxt.renderAndGetWindow(url('/redirect'))
|
|
|
|
const html = window.document.body.innerHTML
|
|
|
|
t.true(html.includes('<h1>Index page</h1>'))
|
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/users/1 not found', async t => {
|
|
|
|
await remove(resolve(rootDir, 'dist/users'))
|
|
|
|
const error = await t.throws(rp(url('/users/1')))
|
|
|
|
t.true(error.statusCode === 404)
|
|
|
|
t.true(error.response.body.includes('Cannot GET /users/1'))
|
|
|
|
})
|
|
|
|
|
|
|
|
test.serial('nuxt re-generating with no subfolders', async t => {
|
|
|
|
const logSpy = await interceptLog()
|
|
|
|
nuxt.options.generate.subFolders = false
|
|
|
|
await generator.generate()
|
|
|
|
release()
|
|
|
|
t.true(logSpy.calledWithMatch('DONE'))
|
|
|
|
})
|
|
|
|
|
|
|
|
test.serial('/users/1.html', async t => {
|
|
|
|
const html = await rp(url('/users/1.html'))
|
|
|
|
t.true(html.includes('<h1>User: 1</h1>'))
|
|
|
|
t.true(existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1.html')))
|
2018-01-13 05:22:11 +00:00
|
|
|
t.false(
|
|
|
|
existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1/index.html'))
|
|
|
|
)
|
2017-12-17 19:30:26 +00:00
|
|
|
})
|
|
|
|
|
2017-12-18 15:53:36 +00:00
|
|
|
test.serial('/-ignored', async t => {
|
|
|
|
const error = await t.throws(rp(url('/-ignored')))
|
|
|
|
t.true(error.statusCode === 404)
|
|
|
|
t.true(error.response.body.includes('Cannot GET /-ignored'))
|
|
|
|
})
|
|
|
|
|
2018-01-15 09:50:42 +00:00
|
|
|
test.serial('/ignored.test', async t => {
|
|
|
|
const error = await t.throws(rp(url('/ignored.test')))
|
|
|
|
t.true(error.statusCode === 404)
|
|
|
|
t.true(error.response.body.includes('Cannot GET /ignored.test'))
|
|
|
|
})
|
|
|
|
|
2016-12-21 14:03:37 +00:00
|
|
|
// Close server and ask nuxt to stop listening to file changes
|
2017-12-17 19:30:26 +00:00
|
|
|
test.after.always('Closing server', async t => {
|
|
|
|
await server.close()
|
2016-12-21 14:03:37 +00:00
|
|
|
})
|