Nuxt/test/with-config.test.js

117 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-12-21 14:03:23 +00:00
import test from 'ava'
import { resolve } from 'path'
import rp from 'request-promise-native'
2017-06-16 12:49:35 +00:00
import { Nuxt, Server } from '../index.js'
2017-05-21 19:00:01 +00:00
const port = 4007
2016-12-21 14:03:23 +00:00
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 rootDir = resolve(__dirname, 'fixtures/with-config')
let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir
config.dev = false
2017-06-14 18:51:14 +00:00
config.runBuild = true
nuxt = new Nuxt(config)
2017-06-15 14:59:26 +00:00
await nuxt.ready()
2017-06-16 12:49:35 +00:00
server = new Server(nuxt)
2016-12-21 14:03:23 +00:00
server.listen(port, 'localhost')
})
test('/', async t => {
const { html } = await nuxt.renderRoute('/')
t.true(html.includes('<h1>I have custom configurations</h1>'))
})
test('/ (global styles inlined)', async t => {
2017-05-15 11:06:23 +00:00
// const { html } = await nuxt.renderRoute('/')
// t.true(html.includes('.global-css-selector'))
t.pass()
})
2017-03-24 17:35:30 +00:00
test('/ (custom app.html)', async t => {
const { html } = await nuxt.renderRoute('/')
t.true(html.includes('<p>Made by Nuxt.js team</p>'))
})
test('/ (custom build.publicPath)', async t => {
const { html } = await nuxt.renderRoute('/')
2017-03-24 17:54:54 +00:00
t.true(html.includes('src="/test/orion/vendor.bundle'))
2017-03-24 17:35:30 +00:00
})
2016-12-21 14:03:23 +00:00
test('/test/ (router base)', async t => {
const window = await nuxt.renderAndGetWindow(url('/test/'))
const html = window.document.body.innerHTML
2017-03-25 17:59:46 +00:00
t.is(window.__NUXT__.layout, 'default')
2016-12-24 17:50:40 +00:00
t.true(html.includes('<h1>Default layout</h1>'))
2016-12-21 14:03:23 +00:00
t.true(html.includes('<h1>I have custom configurations</h1>'))
})
2016-12-24 17:50:40 +00:00
test('/test/about (custom layout)', async t => {
const window = await nuxt.renderAndGetWindow(url('/test/about'))
const html = window.document.body.innerHTML
2017-03-25 17:59:46 +00:00
t.is(window.__NUXT__.layout, 'custom')
2016-12-24 17:50:40 +00:00
t.true(html.includes('<h1>Custom layout</h1>'))
t.true(html.includes('<h1>About page</h1>'))
})
2016-12-21 19:51:43 +00:00
test('/test/env', async t => {
const window = await nuxt.renderAndGetWindow(url('/test/env'))
const html = window.document.body.innerHTML
2016-12-25 20:09:14 +00:00
t.true(html.includes('<h1>Custom env layout</h1>'))
2016-12-21 19:51:43 +00:00
t.true(html.includes('"bool": true'))
t.true(html.includes('"num": 23'))
t.true(html.includes('"string": "Nuxt.js"'))
})
2017-03-25 17:59:46 +00:00
test('/test/error', async t => {
const window = await nuxt.renderAndGetWindow(url('/test/error'))
const html = window.document.body.innerHTML
t.is(window.__NUXT__.layout, 'custom')
t.true(html.includes('<h1>Custom layout</h1>'))
t.true(html.includes('Error page'))
})
2017-02-09 23:45:49 +00:00
test('/test/user-agent', async t => {
const window = await nuxt.renderAndGetWindow(url('/test/user-agent'))
const html = window.document.body.innerHTML
2017-05-30 16:18:01 +00:00
t.true(html.includes('<pre>Mozilla'))
2017-02-09 23:45:49 +00:00
})
2017-01-19 15:26:30 +00:00
test('/test/about-bis (added with extendRoutes)', async t => {
const window = await nuxt.renderAndGetWindow(url('/test/about-bis'))
const html = window.document.body.innerHTML
t.true(html.includes('<h1>Custom layout</h1>'))
t.true(html.includes('<h1>About page</h1>'))
})
2017-01-23 16:55:39 +00:00
test('Check stats.json generated by build.analyze', t => {
const stats = require(resolve(__dirname, 'fixtures/with-config/.nuxt/dist/stats.json'))
2017-06-15 22:47:47 +00:00
t.is(stats.assets.length, 26)
2017-01-23 16:55:39 +00:00
})
test('Check /test.txt with custom serve-static options', async t => {
const { headers } = await rp(url('/test.txt'), { resolveWithFullResponse: true })
t.is(headers['cache-control'], 'public, max-age=31536000')
})
2016-12-21 14:03:23 +00:00
// Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => {
server.close()
nuxt.close()
})
2016-12-21 19:51:43 +00:00
test.after('Should be able to start Nuxt with build done', async t => {
2016-12-21 19:51:43 +00:00
const rootDir = resolve(__dirname, 'fixtures/with-config')
let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir
config.dev = false
2017-06-14 18:51:14 +00:00
config.runBuild = true
nuxt = new Nuxt(config)
2017-06-15 14:59:26 +00:00
await nuxt.ready()
2016-12-21 19:51:43 +00:00
})