Nuxt/test/with-config.test.js

218 lines
7.6 KiB
JavaScript
Raw Normal View History

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