Nuxt/test/basic.generate.test.js

200 lines
6.3 KiB
JavaScript
Raw Normal View History

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