Nuxt/test/unit/basic.generate.test.js

203 lines
6.6 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'
import { Builder, Generator, getPort, loadFixture, Nuxt, rp } from '../utils'
2018-03-16 19:52:17 +00:00
2018-03-18 23:41:14 +00:00
let port
2018-01-13 05:22:11 +00:00
const url = route => 'http://localhost:' + port + route
2018-03-19 10:06:45 +00:00
const rootDir = resolve(__dirname, '..', 'fixtures/basic')
2018-03-19 04:21:04 +00:00
const distDir = resolve(rootDir, '.nuxt-generate')
2016-12-20 19:44:42 +00:00
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', () => {
beforeAll(async () => {
const config = await loadFixture('basic', { generate: { dir: '.nuxt-generate' } })
const nuxt = new Nuxt(config)
const builder = new Builder(nuxt)
builder.build = jest.fn()
generator = new Generator(nuxt, builder)
await generator.generate()
2018-03-19 04:21:04 +00:00
const serve = serveStatic(distDir)
2018-03-18 19:31:32 +00:00
server = http.createServer((req, res) => {
serve(req, res, finalhandler(req, res))
})
2018-03-18 23:41:14 +00:00
port = await getPort()
2018-03-18 19:31:32 +00:00
server.listen(port)
2018-03-18 23:41:14 +00:00
})
2016-12-20 19:44:42 +00:00
test('Check builder', () => {
expect(generator.builder.isStatic).toBe(true)
expect(generator.builder.build).toHaveBeenCalledTimes(1)
})
test('Check ready hook called', () => {
2018-08-20 14:20:45 +00:00
expect(generator.nuxt.__hook_ready_called__).toBe(true)
})
test('Format errors', () => {
const error = generator._formatErrors([
{ type: 'handled', route: '/h1', error: 'page not found' },
{ type: 'unhandled', route: '/h2', error: { stack: 'unhandled error stack' } }
])
expect(error).toMatch(' GEN ERR ')
expect(error).toMatch(' /h1')
expect(error).toMatch(' /h2')
expect(error).toMatch('"page not found"')
expect(error).toMatch('unhandled error stack')
2018-03-18 19:31:32 +00:00
})
2017-10-31 11:33:15 +00:00
2018-03-18 19:31:32 +00:00
test('/stateless', async () => {
const window = await generator.nuxt.renderAndGetWindow(url('/stateless'))
2018-03-18 19:31:32 +00:00
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 generator.nuxt.renderAndGetWindow(url('/css'))
2018-03-18 19:31:32 +00:00
const headHtml = window.document.head.innerHTML
2018-08-06 17:26:14 +00:00
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 generator.nuxt.renderAndGetWindow(url('/stateful'))
2018-03-18 19:31:32 +00:00
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 window = await generator.nuxt.renderAndGetWindow(url('/head'))
2018-03-18 19:31:32 +00:00
const html = window.document.body.innerHTML
const metas = window.document.getElementsByTagName('meta')
2018-03-18 23:41:14 +00:00
expect(window.document.title).toBe('My title - Nuxt.js')
2018-03-18 19:31:32 +00:00
expect(metas[0].getAttribute('content')).toBe('my meta')
expect(html.includes('<div><h1>I can haz meta tags</h1></div>')).toBe(true)
})
2016-12-21 14:03:37 +00:00
2018-03-18 19:31:32 +00:00
test('/async-data', async () => {
const window = await generator.nuxt.renderAndGetWindow(url('/async-data'))
2018-03-18 19:31:32 +00:00
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(
2018-03-19 04:21:04 +00:00
existsSync(resolve(distDir, 'users/1/index.html'))
2018-03-18 19:31:32 +00:00
).toBe(true)
2018-03-19 04:21:04 +00:00
expect(existsSync(resolve(distDir, 'users/1.html'))).toBe(false)
2018-03-18 19:31:32 +00:00
})
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 generator.nuxt.renderAndGetWindow(url('/validate'))
2018-03-18 19:31:32 +00:00
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 generator.nuxt.renderAndGetWindow(url('/validate?valid=true'))
2018-03-18 19:31:32 +00:00
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 generator.nuxt.renderAndGetWindow(url('/redirect'))
2018-03-18 19:31:32 +00:00
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 () => {
2018-03-19 04:21:04 +00:00
await remove(resolve(distDir, 'users'))
2018-03-18 19:31:32 +00:00
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 () => {
generator.nuxt.options.generate.subFolders = false
2018-04-18 15:56:03 +00:00
await expect(generator.generate({ build: false })).resolves.toBeTruthy()
2018-03-18 19:31:32 +00:00
})
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)
2018-03-19 04:21:04 +00:00
expect(existsSync(resolve(distDir, 'users/1.html'))).toBe(true)
2018-03-18 19:31:32 +00:00
expect(
2018-03-19 04:21:04 +00:00
existsSync(resolve(distDir, 'users/1/index.html'))
2018-03-18 19:31:32 +00:00
).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
2018-03-30 09:20:16 +00:00
afterAll(async () => {
2018-03-18 19:31:32 +00:00
await server.close()
})
2016-12-21 14:03:37 +00:00
})