2017-11-17 00:27:30 +00:00
|
|
|
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'
|
2018-03-27 22:28:17 +00:00
|
|
|
import { loadFixture, getPort, Nuxt, Generator, 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
|
|
|
|
|
|
|
let nuxt = null
|
2016-12-21 14:03:37 +00:00
|
|
|
let server = null
|
2017-12-17 19:30:26 +00:00
|
|
|
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-19 04:21:04 +00:00
|
|
|
const config = loadFixture('basic', {generate: {dir: '.nuxt-generate'}})
|
2017-12-17 19:30:26 +00:00
|
|
|
nuxt = new Nuxt(config)
|
2018-03-18 23:41:14 +00:00
|
|
|
generator = new Generator(nuxt)
|
2018-03-27 22:28:17 +00:00
|
|
|
generator.spinner.enabled = false
|
2017-12-17 19:30:26 +00:00
|
|
|
|
2018-03-18 23:41:14 +00:00
|
|
|
await generator.generate({ build: false })
|
2017-12-17 19:30:26 +00:00
|
|
|
|
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
|
|
|
|
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'))
|
2017-12-29 08:33:13 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
const headHtml = window.document.head.innerHTML
|
|
|
|
expect(headHtml.includes('.red{color:red}')).toBe(true)
|
2017-12-29 08:33:13 +00:00
|
|
|
|
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 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)
|
|
|
|
})
|
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(
|
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 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 () => {
|
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')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2017-12-17 19:30:26 +00:00
|
|
|
|
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)
|
|
|
|
})
|
2017-12-17 19:30:26 +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)
|
|
|
|
})
|
2017-12-17 19:30:26 +00:00
|
|
|
|
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')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2017-12-18 15:53:36 +00:00
|
|
|
|
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
|
|
|
})
|