Nuxt/examples/with-ava/test/index.test.js

56 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-11-07 01:34:58 +00:00
/*
** Test with Ava can be written in ES6 \o/
*/
import test from 'ava'
import jsdom from 'jsdom'
import { createServer } from 'http'
import { resolve } from 'path'
let nuxt = null
let server = null
// Init nuxt.js and create server listening on localhost:4000
2016-12-07 17:37:19 +00:00
test.before('Init Nuxt.js', (t) => {
2016-11-07 01:34:58 +00:00
const Nuxt = require('../../../')
const options = {
rootDir: resolve(__dirname, '..'),
dev: false
2016-11-07 01:34:58 +00:00
}
2016-12-07 17:37:19 +00:00
nuxt = new Nuxt(options)
return nuxt.build()
.then(function (lol) {
console.log(lol)
2016-11-07 01:34:58 +00:00
server = createServer((req, res) => nuxt.render(req, res))
2016-12-07 17:37:19 +00:00
server.listen(4000, 'localhost')
2016-11-07 01:34:58 +00:00
})
})
/*
** Example of testing only the html
*/
test('Route / exits and render HTML', async t => {
let context = {}
2016-11-10 01:19:47 +00:00
const { html } = await nuxt.renderRoute('/', context)
2016-11-07 01:34:58 +00:00
t.true(html.includes('<p class="red-color">Hello world!</p>'))
t.is(context.nuxt.error, null)
t.is(context.nuxt.data[0].name, 'world')
})
/*
** Example of testing via dom checking
*/
test('Route / exits and render HTML', async t => {
2016-12-07 17:37:19 +00:00
const window = await nuxt.renderAndGetWindow(jsdom, 'http://localhost:4000/')
const element = window.document.querySelector('.red-color')
t.not(element, null)
t.is(element.textContent, 'Hello world!')
t.is(element.className, 'red-color')
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
test.after('Closing server and nuxt.js', t => {
server.close()
nuxt.close()
2016-11-07 01:34:58 +00:00
})