renderAndGetWindow does not need jsdom

This commit is contained in:
Sébastien Chopin 2016-12-20 17:56:06 +01:00
parent 42dc15e72e
commit 37e7a01955
3 changed files with 15 additions and 6 deletions

View File

@ -2,7 +2,6 @@
** Test with Ava can be written in ES6 \o/ ** Test with Ava can be written in ES6 \o/
*/ */
import test from 'ava' import test from 'ava'
import jsdom from 'jsdom'
import { createServer } from 'http' import { createServer } from 'http'
import { resolve } from 'path' import { resolve } from 'path'
@ -39,7 +38,7 @@ test('Route / exits and render HTML', async t => {
** Example of testing via dom checking ** Example of testing via dom checking
*/ */
test('Route / exits and render HTML', async t => { test('Route / exits and render HTML', async t => {
const window = await nuxt.renderAndGetWindow(jsdom, 'http://localhost:4000/') const window = await nuxt.renderAndGetWindow('http://localhost:4000/')
const element = window.document.querySelector('.red-color') const element = window.document.querySelector('.red-color')
t.not(element, null) t.not(element, null)
t.is(element.textContent, 'Hello world!') t.is(element.textContent, 'Hello world!')

View File

@ -94,7 +94,18 @@ exports.renderRoute = function (url, context = {}) {
} }
// Function used to do dom checking via jsdom // Function used to do dom checking via jsdom
exports.renderAndGetWindow = function renderAndGetWindow (jsdom, url) { let jsdom = null
exports.renderAndGetWindow = function renderAndGetWindow (url) {
if (!jsdom) {
try {
jsdom = require('jsdom')
} catch (e) {
console.error('Fail when calling nuxt.renderAndGetWindow(url)') // eslint-disable-line no-console
console.error('jsdom module is not installed') // eslint-disable-line no-console
console.error('Please install jsdom with: npm install --save-dev jsdom') // eslint-disable-line no-console
process.exit(1)
}
}
const virtualConsole = jsdom.createVirtualConsole().sendTo(console) const virtualConsole = jsdom.createVirtualConsole().sendTo(console)
url = url || 'http://localhost:3000' url = url || 'http://localhost:3000'
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

View File

@ -1,5 +1,4 @@
import test from 'ava' import test from 'ava'
import jsdom from 'jsdom'
import { resolve } from 'path' import { resolve } from 'path'
const port = 4002 const port = 4002
const url = (route) => 'http://localhost:' + port + route const url = (route) => 'http://localhost:' + port + route
@ -31,7 +30,7 @@ test('/stateless', async t => {
** Example of testing via dom checking ** Example of testing via dom checking
*/ */
test('/css', async t => { test('/css', async t => {
const window = await nuxt.renderAndGetWindow(jsdom, url('/css')) const window = await nuxt.renderAndGetWindow(url('/css'))
const element = window.document.querySelector('.red') const element = window.document.querySelector('.red')
t.not(element, null) t.not(element, null)
t.is(element.textContent, 'This is red') t.is(element.textContent, 'This is red')
@ -45,7 +44,7 @@ test('/stateful', async t => {
}) })
test('/head', async t => { test('/head', async t => {
const window = await nuxt.renderAndGetWindow(jsdom, url('/head')) const window = await nuxt.renderAndGetWindow(url('/head'))
const html = window.document.body.innerHTML const html = window.document.body.innerHTML
const metas = window.document.getElementsByTagName('meta') const metas = window.document.getElementsByTagName('meta')
t.is(window.document.title, 'My title') t.is(window.document.title, 'My title')