feat: merge renderAndGetWindow options (#3761)

* feat: merge renderAndGetWindow options

* fix: typoe

* refactor: remove explicit comparison for truthy value

* fix: setup defaults correctly

* test: add custom params test
This commit is contained in:
Alexander Lichter 2018-08-22 16:14:15 +01:00 committed by Sébastien Chopin
parent 3612ecd435
commit 3e027269c0
2 changed files with 27 additions and 6 deletions

View File

@ -403,17 +403,20 @@ export default class Renderer {
throw e
}
}
const options = {
const options = Object.assign({
resources: 'usable', // load subresources (https://github.com/tmpvar/jsdom#loading-subresources)
runScripts: 'dangerously',
virtualConsole: true,
beforeParse(window) {
// Mock window.scrollTo
window.scrollTo = () => {}
}
}
}, opts)
const jsdomErrHandler = (err) => { throw err }
if (opts.virtualConsole !== false) {
if (options.virtualConsole) {
if (options.virtualConsole === true) {
options.virtualConsole = new jsdom.VirtualConsole().sendTo(consola)
}
// throw error when window creation failed
options.virtualConsole.on('jsdomError', jsdomErrHandler)
}
@ -433,7 +436,7 @@ export default class Renderer {
await timeout(new Promise((resolve) => {
window._onNuxtLoaded = () => resolve(window)
}), 20000, 'Components loading in renderAndGetWindow was not completed in 20s')
if (opts.virtualConsole !== false) {
if (options.virtualConsole) {
// after window initialized successfully
options.virtualConsole.removeListener('jsdomError', jsdomErrHandler)
}

View File

@ -1,4 +1,5 @@
import { loadFixture, getPort, Nuxt, rp } from '../utils'
import jsdom from 'jsdom'
import { getPort, loadFixture, Nuxt, rp } from '../utils'
let port
const url = route => 'http://localhost:' + port + route
@ -139,6 +140,23 @@ describe('with-config', () => {
.rejects.toMatchObject({ statusCode: 404 })
})
test('renderAndGetWindow options', async () => {
const fakeErrorLog = jest.fn()
const mockOptions = {
beforeParse: jest.fn((window) => {
// Mock window.scrollTo
window.scrollTo = () => {}
window._virtualConsole.emit('jsdomError', new Error('test'))
}),
virtualConsole: new jsdom.VirtualConsole().sendTo({error: fakeErrorLog})
}
try {
await nuxt.renderAndGetWindow(url('/test/error'), mockOptions)
} catch (e) {}
expect(mockOptions.beforeParse).toHaveBeenCalled()
expect(fakeErrorLog).toHaveBeenCalled()
})
// Close server and ask nuxt to stop listening to file changes
afterAll(async () => {
await nuxt.close()