Merge pull request #3151 from polyglotm/dev

refactor(examples/with-ava): replace 'nuxt.renderAndGetWindow' -> 'JS…
This commit is contained in:
Sébastien Chopin 2018-04-08 13:58:58 +02:00 committed by GitHub
commit 98fd8637ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,17 +1,20 @@
import { resolve } from 'path' import { resolve } from 'path'
import test from 'ava'
import { Nuxt, Builder } from 'nuxt' import { Nuxt, Builder } from 'nuxt'
import { JSDOM } from 'jsdom'
import test from 'ava'
// We keep the nuxt and server instance // We keep the nuxt and server instance
// So we can close them at the end of the test // So we can close them at the end of the test
let nuxt = null let nuxt = null
// Init Nuxt.js and create a server listening on localhost:4000 // Init Nuxt.js and create a server listening on localhost:4000
beforeAll(async () => { test.before(async () => {
const rootDir = resolve(__dirname, '..') const rootDir = resolve(__dirname, '..')
let config = {} let config = {}
try { config = require(resolve(rootDir, 'nuxt.config.js')) } catch (e) {} try {
config = require(resolve(rootDir, 'nuxt.config.js'))
} catch (e) {
}
config.rootDir = rootDir // project folder config.rootDir = rootDir // project folder
config.dev = false // production build config.dev = false // production build
nuxt = new Nuxt(config) nuxt = new Nuxt(config)
@ -21,14 +24,16 @@ beforeAll(async () => {
// Example of testing only generated html // Example of testing only generated html
test('Route / exits and render HTML', async t => { test('Route / exits and render HTML', async t => {
let context = {} const context = {}
const { html } = await nuxt.renderRoute('/', context) const { html } = await nuxt.renderRoute('/', context)
t.true(html.includes('<h1 class="red">Hello world!</h1>')) t.true(html.includes('<h1 class="red">Hello world!</h1>'))
}) })
// Example of testing via dom checking // Example of testing via dom checking
test('Route / exits and render HTML with CSS applied', async t => { test('Route / exits and render HTML with CSS applied', async t => {
const window = await nuxt.renderAndGetWindow('http://localhost:4000/') const context = {}
const { html } = await nuxt.renderRoute('/', context)
const { window } = new JSDOM(html).window
const element = window.document.querySelector('.red') const element = window.document.querySelector('.red')
t.not(element, null) t.not(element, null)
t.is(element.textContent, 'Hello world!') t.is(element.textContent, 'Hello world!')