Nuxt/test/basic.generate.test.js

191 lines
6.1 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-18 23:41:14 +00:00
import { Nuxt, Generator } from '..'
import { loadFixture, getPort } 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
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', () => {
beforeAll(async () => {
2018-03-18 23:41:14 +00:00
const config = loadFixture('basic')
nuxt = new Nuxt(config)
2018-03-18 23:41:14 +00:00
generator = new Generator(nuxt)
2018-03-18 23:41:14 +00:00
await generator.generate({ build: false })
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))
})
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
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')
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)
// 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
2018-03-18 23:41:14 +00:00
await generator.generate({ build: false })
2018-03-18 19:31:32 +00:00
// 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
})