2016-12-21 14:03:23 +00:00
|
|
|
import test from 'ava'
|
|
|
|
import { resolve } from 'path'
|
2017-05-22 10:51:03 +00:00
|
|
|
import rp from 'request-promise-native'
|
2017-12-12 09:42:29 +00:00
|
|
|
import { Nuxt, Builder } from '..'
|
2017-12-29 09:48:47 +00:00
|
|
|
import styleLoader from '../lib/builder/webpack/style-loader'
|
2017-11-27 22:35:42 +00:00
|
|
|
import { interceptLog, release } from './helpers/console'
|
2018-03-16 16:12:06 +00:00
|
|
|
import { loadConfig } from './helpers/config'
|
2017-05-22 10:51:03 +00:00
|
|
|
|
2017-05-21 19:00:01 +00:00
|
|
|
const port = 4007
|
2018-01-13 05:22:11 +00:00
|
|
|
const url = route => 'http://localhost:' + port + route
|
2016-12-21 14:03:23 +00:00
|
|
|
|
|
|
|
let nuxt = null
|
2017-12-08 07:39:58 +00:00
|
|
|
let builder = null
|
2016-12-21 14:03:23 +00:00
|
|
|
|
|
|
|
// Init nuxt.js and create server listening on localhost:4000
|
|
|
|
test.before('Init Nuxt.js', async t => {
|
2018-03-16 16:12:06 +00:00
|
|
|
const config = loadConfig('with-config', {
|
|
|
|
dev: false
|
|
|
|
})
|
2017-12-17 19:30:26 +00:00
|
|
|
|
|
|
|
const logSpy = await interceptLog(async () => {
|
|
|
|
nuxt = new Nuxt(config)
|
|
|
|
builder = new Builder(nuxt)
|
2017-12-12 15:34:21 +00:00
|
|
|
await builder.build()
|
2017-12-17 19:30:26 +00:00
|
|
|
await nuxt.listen(port, 'localhost')
|
2017-11-24 21:19:39 +00:00
|
|
|
})
|
2017-12-17 19:30:26 +00:00
|
|
|
|
|
|
|
t.true(logSpy.calledWithMatch('DONE'))
|
|
|
|
t.true(logSpy.calledWithMatch('OPEN'))
|
2016-12-21 14:03:23 +00:00
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/', async t => {
|
|
|
|
const logSpy = await interceptLog()
|
|
|
|
const { html } = await nuxt.renderRoute('/')
|
|
|
|
t.true(html.includes('<h1>I have custom configurations</h1>'))
|
|
|
|
release()
|
2017-11-27 22:35:42 +00:00
|
|
|
t.true(logSpy.calledOnce)
|
|
|
|
t.is(logSpy.args[0][0], 'Test plugin!')
|
2016-12-21 14:03:23 +00:00
|
|
|
})
|
|
|
|
|
2017-05-05 14:25:17 +00:00
|
|
|
test('/ (global styles inlined)', async t => {
|
2017-08-30 14:18:37 +00:00
|
|
|
const { html } = await nuxt.renderRoute('/')
|
|
|
|
t.true(html.includes('.global-css-selector'))
|
|
|
|
})
|
|
|
|
|
2018-03-01 15:07:26 +00:00
|
|
|
test.skip('/ (preload fonts)', async t => {
|
2017-08-30 14:18:37 +00:00
|
|
|
const { html } = await nuxt.renderRoute('/')
|
2018-01-13 05:22:11 +00:00
|
|
|
t.true(
|
|
|
|
html.includes(
|
|
|
|
'<link rel="preload" href="/test/orion/fonts/roboto.7cf5d7c.woff2" as="font" type="font/woff2" crossorigin'
|
|
|
|
)
|
|
|
|
)
|
2017-05-05 14:25:17 +00:00
|
|
|
})
|
|
|
|
|
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('/')
|
2018-03-01 12:49:14 +00:00
|
|
|
t.true(html.includes('<script src="/test/orion/'))
|
2017-03-24 17:35:30 +00:00
|
|
|
})
|
|
|
|
|
2017-11-21 01:54:02 +00:00
|
|
|
test('/ (custom postcss.config.js)', async t => {
|
|
|
|
const { html } = await nuxt.renderRoute('/')
|
|
|
|
t.true(html.includes('::-webkit-input-placeholder'))
|
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/test/ (router base)', async t => {
|
|
|
|
const logSpy = await interceptLog()
|
|
|
|
const window = await nuxt.renderAndGetWindow(url('/test/'))
|
2017-11-27 22:35:42 +00:00
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
const html = window.document.body.innerHTML
|
|
|
|
t.is(window.__NUXT__.layout, 'default')
|
|
|
|
t.true(html.includes('<h1>Default layout</h1>'))
|
|
|
|
t.true(html.includes('<h1>I have custom configurations</h1>'))
|
|
|
|
release()
|
2017-11-27 22:35:42 +00:00
|
|
|
t.true(logSpy.calledOnce)
|
|
|
|
t.is(logSpy.args[0][0], 'Test plugin!')
|
2016-12-21 14:03:23 +00:00
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/test/about (custom layout)', async t => {
|
2017-11-27 22:35:42 +00:00
|
|
|
const logSpy = await interceptLog()
|
2016-12-24 17:50:40 +00:00
|
|
|
const window = await nuxt.renderAndGetWindow(url('/test/about'))
|
2017-11-27 22:35:42 +00:00
|
|
|
t.true(logSpy.calledOnce)
|
|
|
|
t.is(logSpy.args[0][0], 'Test plugin!')
|
|
|
|
release()
|
2017-11-24 21:19:39 +00:00
|
|
|
|
2016-12-24 17:50:40 +00:00
|
|
|
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>'))
|
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/test/desktop (custom layout in desktop folder)', async t => {
|
2017-11-27 22:35:42 +00:00
|
|
|
const logSpy = await interceptLog()
|
2017-10-15 19:31:01 +00:00
|
|
|
const window = await nuxt.renderAndGetWindow(url('/test/desktop'))
|
2017-11-27 22:35:42 +00:00
|
|
|
t.true(logSpy.calledOnce)
|
|
|
|
t.is(logSpy.args[0][0], 'Test plugin!')
|
|
|
|
release()
|
2017-11-24 21:19:39 +00:00
|
|
|
|
2017-10-15 19:31:01 +00:00
|
|
|
const html = window.document.body.innerHTML
|
|
|
|
t.is(window.__NUXT__.layout, 'desktop/default')
|
|
|
|
t.true(html.includes('<h1>Default desktop layout</h1>'))
|
|
|
|
t.true(html.includes('<h1>Desktop page</h1>'))
|
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/test/mobile (custom layout in mobile folder)', async t => {
|
2017-11-27 22:35:42 +00:00
|
|
|
const logSpy = await interceptLog()
|
2017-10-15 19:31:01 +00:00
|
|
|
const window = await nuxt.renderAndGetWindow(url('/test/mobile'))
|
2017-11-27 22:35:42 +00:00
|
|
|
t.true(logSpy.calledOnce)
|
|
|
|
t.is(logSpy.args[0][0], 'Test plugin!')
|
|
|
|
release()
|
2017-11-24 21:19:39 +00:00
|
|
|
|
2017-10-15 19:31:01 +00:00
|
|
|
const html = window.document.body.innerHTML
|
|
|
|
t.is(window.__NUXT__.layout, 'mobile/default')
|
|
|
|
t.true(html.includes('<h1>Default mobile layout</h1>'))
|
|
|
|
t.true(html.includes('<h1>Mobile page</h1>'))
|
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/test/env', async t => {
|
2017-11-27 22:35:42 +00:00
|
|
|
const logSpy = await interceptLog()
|
2016-12-21 19:51:43 +00:00
|
|
|
const window = await nuxt.renderAndGetWindow(url('/test/env'))
|
2017-11-27 22:35:42 +00:00
|
|
|
t.true(logSpy.calledOnce)
|
|
|
|
t.is(logSpy.args[0][0], 'Test plugin!')
|
|
|
|
release()
|
2017-11-24 21:19:39 +00:00
|
|
|
|
2016-12-21 19:51:43 +00:00
|
|
|
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-09-05 09:15:01 +00:00
|
|
|
t.true(html.includes('"bool": false'))
|
|
|
|
t.true(html.includes('"string": "ok"'))
|
|
|
|
t.true(html.includes('"num2": 8.23'))
|
|
|
|
t.true(html.includes('"obj": {'))
|
2016-12-21 19:51:43 +00:00
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/test/error', async t => {
|
2017-11-27 22:35:42 +00:00
|
|
|
const logSpy = await interceptLog()
|
2017-03-25 17:59:46 +00:00
|
|
|
const window = await nuxt.renderAndGetWindow(url('/test/error'))
|
2017-11-27 22:35:42 +00:00
|
|
|
t.true(logSpy.calledOnce)
|
|
|
|
t.is(logSpy.args[0][0], 'Test plugin!')
|
|
|
|
release()
|
2017-11-24 21:19:39 +00:00
|
|
|
|
2017-03-25 17:59:46 +00:00
|
|
|
const html = window.document.body.innerHTML
|
|
|
|
t.true(html.includes('Error page'))
|
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/test/user-agent', async t => {
|
2017-11-27 22:35:42 +00:00
|
|
|
const logSpy = await interceptLog()
|
2017-02-09 23:45:49 +00:00
|
|
|
const window = await nuxt.renderAndGetWindow(url('/test/user-agent'))
|
2017-11-27 22:35:42 +00:00
|
|
|
t.true(logSpy.calledOnce)
|
|
|
|
t.is(logSpy.args[0][0], 'Test plugin!')
|
|
|
|
release()
|
2017-11-24 21:19:39 +00:00
|
|
|
|
2017-02-09 23:45:49 +00:00
|
|
|
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-12-17 19:30:26 +00:00
|
|
|
test.serial('/test/about-bis (added with extendRoutes)', async t => {
|
2017-11-27 22:35:42 +00:00
|
|
|
const logSpy = await interceptLog()
|
2017-01-19 15:26:30 +00:00
|
|
|
const window = await nuxt.renderAndGetWindow(url('/test/about-bis'))
|
2017-11-27 22:35:42 +00:00
|
|
|
t.true(logSpy.calledOnce)
|
|
|
|
t.is(logSpy.args[0][0], 'Test plugin!')
|
|
|
|
release()
|
2017-11-24 21:19:39 +00:00
|
|
|
|
2017-01-19 15:26:30 +00:00
|
|
|
const html = window.document.body.innerHTML
|
|
|
|
t.true(html.includes('<h1>Custom layout</h1>'))
|
|
|
|
t.true(html.includes('<h1>About page</h1>'))
|
|
|
|
})
|
|
|
|
|
2018-03-15 11:57:00 +00:00
|
|
|
test.serial('/test/redirect/about-bis (redirect with extendRoutes)', async t => {
|
|
|
|
const logSpy = await interceptLog()
|
|
|
|
const window = await nuxt.renderAndGetWindow(url('/test/redirect/about-bis'))
|
|
|
|
t.true(logSpy.calledOnce)
|
|
|
|
t.is(logSpy.args[0][0], 'Test plugin!')
|
|
|
|
release()
|
|
|
|
|
|
|
|
const windowHref = window.location.href
|
|
|
|
t.true(windowHref.includes('/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 => {
|
2018-01-13 05:22:11 +00:00
|
|
|
const stats = require(resolve(
|
|
|
|
__dirname,
|
|
|
|
'fixtures/with-config/.nuxt/dist/stats.json'
|
|
|
|
))
|
2018-03-13 12:09:41 +00:00
|
|
|
t.is(stats.assets.length, 34)
|
2017-01-23 16:55:39 +00:00
|
|
|
})
|
|
|
|
|
2017-08-25 12:01:16 +00:00
|
|
|
test('Check /test/test.txt with custom serve-static options', async t => {
|
2018-01-13 05:22:11 +00:00
|
|
|
const { headers } = await rp(url('/test/test.txt'), {
|
|
|
|
resolveWithFullResponse: true
|
|
|
|
})
|
2017-05-22 10:51:03 +00:00
|
|
|
t.is(headers['cache-control'], 'public, max-age=31536000')
|
|
|
|
})
|
|
|
|
|
2017-08-25 12:01:16 +00:00
|
|
|
test('Check /test.txt should return 404', async t => {
|
|
|
|
const err = await t.throws(rp(url('/test.txt')))
|
|
|
|
t.is(err.response.statusCode, 404)
|
|
|
|
})
|
|
|
|
|
2017-12-08 07:39:58 +00:00
|
|
|
test('Check build.styleResources for style-resources-loader', async t => {
|
2017-12-29 09:48:47 +00:00
|
|
|
const loaders = styleLoader.call(builder, 'scss')
|
2017-12-08 07:39:58 +00:00
|
|
|
const loader = loaders.find(l => l.loader === 'style-resources-loader')
|
|
|
|
t.is(typeof loader, 'object')
|
|
|
|
t.deepEqual(loader.options, {
|
2018-01-08 09:37:10 +00:00
|
|
|
patterns: ['~/assets/pre-process.scss']
|
2017-12-08 07:39:58 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-12-21 14:03:23 +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 and nuxt.js', async t => {
|
2017-11-24 21:19:39 +00:00
|
|
|
await nuxt.close()
|
2016-12-21 14:03:23 +00:00
|
|
|
})
|