2018-03-29 06:26:33 +00:00
|
|
|
import { resolve } from 'path'
|
2017-10-20 06:41:06 +00:00
|
|
|
import { Nuxt, Builder } from 'nuxt'
|
2018-03-29 06:26:33 +00:00
|
|
|
import { JSDOM } from 'jsdom'
|
2018-03-29 12:33:30 +00:00
|
|
|
import test from 'ava'
|
2016-11-07 01:34:58 +00:00
|
|
|
|
2016-12-30 11:17:52 +00:00
|
|
|
// We keep the nuxt and server instance
|
|
|
|
// So we can close them at the end of the test
|
2016-11-07 01:34:58 +00:00
|
|
|
let nuxt = null
|
|
|
|
|
2016-12-30 11:17:52 +00:00
|
|
|
// Init Nuxt.js and create a server listening on localhost:4000
|
2018-03-29 06:26:33 +00:00
|
|
|
test.before(async () => {
|
2018-04-08 12:16:20 +00:00
|
|
|
const config = {
|
|
|
|
dev: false,
|
|
|
|
rootDir: resolve(__dirname, '..')
|
2018-03-29 06:26:33 +00:00
|
|
|
}
|
2017-05-31 14:21:16 +00:00
|
|
|
nuxt = new Nuxt(config)
|
2018-04-08 12:16:20 +00:00
|
|
|
await new Builder(nuxt).build()
|
2018-10-30 20:42:53 +00:00
|
|
|
await nuxt.server.listen(4000, 'localhost')
|
2018-03-18 19:31:32 +00:00
|
|
|
}, 30000)
|
2016-11-07 01:34:58 +00:00
|
|
|
|
2016-12-30 11:17:52 +00:00
|
|
|
// Example of testing only generated html
|
2018-08-06 00:12:44 +00:00
|
|
|
test('Route / exits and render HTML', async (t) => {
|
2018-03-29 06:26:33 +00:00
|
|
|
const context = {}
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/', context)
|
2016-12-30 11:17:52 +00:00
|
|
|
t.true(html.includes('<h1 class="red">Hello world!</h1>'))
|
2016-11-07 01:34:58 +00:00
|
|
|
})
|
|
|
|
|
2016-12-30 11:17:52 +00:00
|
|
|
// Example of testing via dom checking
|
2018-08-06 00:12:44 +00:00
|
|
|
test('Route / exits and render HTML with CSS applied', async (t) => {
|
2018-03-29 06:26:33 +00:00
|
|
|
const context = {}
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/', context)
|
2018-03-29 06:26:33 +00:00
|
|
|
const { window } = new JSDOM(html).window
|
2016-12-30 11:17:52 +00:00
|
|
|
const element = window.document.querySelector('.red')
|
2016-11-09 22:59:41 +00:00
|
|
|
t.not(element, null)
|
|
|
|
t.is(element.textContent, 'Hello world!')
|
2016-12-30 11:17:52 +00:00
|
|
|
t.is(element.className, 'red')
|
2016-11-09 22:59:41 +00:00
|
|
|
t.is(window.getComputedStyle(element).color, 'red')
|
2016-11-07 01:34:58 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Close server and ask nuxt to stop listening to file changes
|
2018-08-06 00:12:44 +00:00
|
|
|
test.after('Closing server and nuxt.js', (t) => {
|
2016-11-10 02:38:11 +00:00
|
|
|
nuxt.close()
|
2016-11-07 01:34:58 +00:00
|
|
|
})
|