import { existsSync } from 'fs'
import http from 'http'
import { resolve } from 'path'
import { remove } from 'fs-extra'
import serveStatic from 'serve-static'
import finalhandler from 'finalhandler'
import rp from 'request-promise-native'
import { Nuxt, Generator } from '..'
import { loadFixture, getPort } from './utils'
let port
const url = route => 'http://localhost:' + port + route
const rootDir = resolve(__dirname, 'fixtures/basic')
let nuxt = null
let server = null
let generator = null
describe('basic generate', () => {
beforeAll(async () => {
const config = loadFixture('basic')
nuxt = new Nuxt(config)
generator = new Generator(nuxt)
await generator.generate({ build: false })
const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist'))
server = http.createServer((req, res) => {
serve(req, res, finalhandler(req, res))
})
port = await getPort()
server.listen(port)
})
test('Check ready hook called', async () => {
expect(nuxt.__hook_called__).toBe(true)
})
test('/stateless', async () => {
const window = await nuxt.renderAndGetWindow(url('/stateless'))
const html = window.document.body.innerHTML
expect(html.includes('
My component!
')).toBe(true)
})
test('/css', async () => {
const window = await nuxt.renderAndGetWindow(url('/css'))
const headHtml = window.document.head.innerHTML
expect(headHtml.includes('.red{color:red}')).toBe(true)
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')
})
test('/stateful', async () => {
const window = await nuxt.renderAndGetWindow(url('/stateful'))
const html = window.document.body.innerHTML
expect(html.includes('')).toBe(true)
})
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 - Nuxt.js')
expect(metas[0].getAttribute('content')).toBe('my meta')
expect(html.includes('I can haz meta tags
')).toBe(true)
// release()
// expect(logSpy.getCall(0).args[0]).toBe('Body script!')
})
test('/async-data', async () => {
const window = await nuxt.renderAndGetWindow(url('/async-data'))
const html = window.document.body.innerHTML
expect(html.includes('Nuxt.js
')).toBe(true)
})
test('/users/1/index.html', async () => {
const html = await rp(url('/users/1/index.html'))
expect(html.includes('User: 1
')).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)
})
test('/users/2', async () => {
const html = await rp(url('/users/2'))
expect(html.includes('User: 2
')).toBe(true)
})
test('/users/3 (payload given)', async () => {
const html = await rp(url('/users/3'))
expect(html.includes('User: 3000
')).toBe(true)
})
test('/users/4 -> Not found', async () => {
await expect(rp(url('/users/4'))).rejects.toMatchObject({
statusCode: 404,
response: {
body: expect.stringContaining('Cannot GET /users/4')
}
})
})
test('/validate should not be server-rendered', async () => {
const html = await rp(url('/validate'))
expect(html.includes('')).toBe(true)
expect(html.includes('serverRendered:!1')).toBe(true)
})
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)
})
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')).toBe(true)
})
test('/redirect should not be server-rendered', async () => {
const html = await rp(url('/redirect'))
expect(html.includes('')).toBe(true)
expect(html.includes('serverRendered:!1')).toBe(true)
})
test('/redirect -> check redirected source', async () => {
const window = await nuxt.renderAndGetWindow(url('/redirect'))
const html = window.document.body.innerHTML
expect(html.includes('Index page
')).toBe(true)
})
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')
}
})
})
test('nuxt re-generating with no subfolders', async () => {
// const logSpy = await interceptLog()
nuxt.options.generate.subFolders = false
await generator.generate({ build: false })
// release()
// expect(logSpy.calledWithMatch('DONE')).toBe(true)
})
test('/users/1.html', async () => {
const html = await rp(url('/users/1.html'))
expect(html.includes('User: 1
')).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)
})
test('/-ignored', async () => {
await expect(rp(url('/-ignored'))).rejects.toMatchObject({
statusCode: 404,
response: {
body: expect.stringContaining('Cannot GET /-ignored')
}
})
})
test('/ignored.test', async () => {
await expect(rp(url('/ignored.test'))).rejects.toMatchObject({
statusCode: 404,
response: {
body: expect.stringContaining('Cannot GET /ignored.test')
}
})
})
// Close server and ask nuxt to stop listening to file changes
test('Closing server', async () => {
await server.close()
})
})