2017-05-14 22:33:31 +00:00
|
|
|
import test from 'ava'
|
2017-06-12 20:15:28 +00:00
|
|
|
import { resolve, normalize } from 'path'
|
2017-05-14 22:33:31 +00:00
|
|
|
import rp from 'request-promise-native'
|
2017-12-12 09:42:29 +00:00
|
|
|
import { Nuxt, Builder } from '..'
|
2017-12-17 19:30:26 +00:00
|
|
|
import { intercept } from './helpers/console'
|
2017-05-14 22:33:31 +00:00
|
|
|
|
2017-05-21 19:00:01 +00:00
|
|
|
const port = 4006
|
2018-01-13 05:22:11 +00:00
|
|
|
const url = route => 'http://localhost:' + port + route
|
2017-05-14 22:33:31 +00:00
|
|
|
|
|
|
|
let nuxt = null
|
2017-11-19 14:35:11 +00:00
|
|
|
let builder = null
|
2017-12-17 19:30:26 +00:00
|
|
|
let buildSpies = null
|
2017-05-14 22:33:31 +00:00
|
|
|
|
|
|
|
// Init nuxt.js and create server listening on localhost:4000
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('Init Nuxt.js', async t => {
|
2017-05-14 22:33:31 +00:00
|
|
|
const rootDir = resolve(__dirname, 'fixtures/module')
|
2017-12-17 19:30:26 +00:00
|
|
|
const config = require(resolve(rootDir, 'nuxt.config.js'))
|
2017-05-14 22:33:31 +00:00
|
|
|
config.rootDir = rootDir
|
|
|
|
config.dev = false
|
2017-05-31 14:21:16 +00:00
|
|
|
nuxt = new Nuxt(config)
|
2017-11-19 14:35:11 +00:00
|
|
|
builder = new Builder(nuxt)
|
2017-11-19 15:02:44 +00:00
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
buildSpies = await intercept({ log: true, error: true }, async () => {
|
|
|
|
await builder.build()
|
|
|
|
await nuxt.listen(port, 'localhost')
|
|
|
|
})
|
2017-06-20 11:44:47 +00:00
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
t.true(buildSpies.log.calledWithMatch('DONE'))
|
|
|
|
t.true(buildSpies.log.calledWithMatch('OPEN'))
|
2017-05-14 22:33:31 +00:00
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('Plugin', async t => {
|
2018-01-13 05:22:11 +00:00
|
|
|
t.true(
|
|
|
|
normalize(nuxt.options.plugins[0].src).includes(
|
|
|
|
normalize('fixtures/module/.nuxt/basic.reverse.')
|
|
|
|
),
|
|
|
|
'plugin added to config'
|
|
|
|
)
|
2017-05-14 22:33:31 +00:00
|
|
|
const { html } = await nuxt.renderRoute('/')
|
|
|
|
t.true(html.includes('<h1>TXUN</h1>'), 'plugin works')
|
|
|
|
})
|
|
|
|
|
2018-02-07 11:58:48 +00:00
|
|
|
test.serial('Layout', async t => {
|
|
|
|
t.true(
|
|
|
|
nuxt.options.layouts.layout.includes('layout'),
|
|
|
|
'layout added to config'
|
|
|
|
)
|
|
|
|
|
|
|
|
const { html } = await nuxt.renderRoute('/layout')
|
|
|
|
t.true(html.includes('<h1>Module Layouts</h1>'), 'layout works')
|
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('Hooks', async t => {
|
2017-07-17 19:38:02 +00:00
|
|
|
t.is(nuxt.__module_hook, 1)
|
|
|
|
t.is(nuxt.__renderer_hook, 2)
|
|
|
|
t.is(nuxt.__builder_hook, 3)
|
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('Hooks - Functional', async t => {
|
2017-11-19 14:35:11 +00:00
|
|
|
t.true(nuxt.__ready_called__)
|
|
|
|
t.true(builder.__build_done__)
|
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('Hooks - Error', async t => {
|
|
|
|
t.true(buildSpies.error.calledWithMatch(/build:extendRoutes/))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('Middleware', async t => {
|
|
|
|
let response = await rp(url('/api'))
|
|
|
|
t.is(response, 'It works!', '/api response is correct')
|
2017-11-19 15:02:44 +00:00
|
|
|
})
|
|
|
|
|
2017-12-07 08:09:49 +00:00
|
|
|
test('Hooks - Use external middleware before render', async t => {
|
|
|
|
let response = await rp(url('/use-middleware'))
|
|
|
|
t.is(response, 'Use external middleware')
|
|
|
|
})
|
|
|
|
|
2017-05-14 22:33:31 +00:00
|
|
|
// Close server and ask nuxt to stop listening to file changes
|
2017-12-17 19:30:26 +00:00
|
|
|
test.after.always('Closing server and nuxt.js', async t => {
|
|
|
|
await nuxt.close()
|
2017-05-14 22:33:31 +00:00
|
|
|
})
|