tests: Add more feature to browser helpers

This commit is contained in:
Atinux 2017-11-02 14:24:20 +01:00
parent 8015a29d31
commit c533ff9c46
2 changed files with 12 additions and 83 deletions

View File

@ -1,80 +0,0 @@
import test from 'ava'
import { resolve } from 'path'
import { Nuxt, Builder } from '../index.js'
import * as browser from './helpers/browser'
const port = 4005
const url = (route) => 'http://localhost:' + port + route
let nuxt = null
// Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => {
const options = {
rootDir: resolve(__dirname, 'fixtures/children'),
dev: false
}
nuxt = new Nuxt(options)
await new Builder(nuxt).build()
await nuxt.listen(port, 'localhost')
})
test.before('Start browser', async t => {
await browser.start()
})
let page
const dates = {}
test('Loading /patch and keep ', async t => {
page = await browser.page(url('/patch'))
const h1 = await page.$text('h1')
t.true(h1.includes('patch:'))
const h2 = await page.$text('h2')
t.is(h2, 'Index')
dates.patch = await page.$text('[data-date-patch]')
})
test('Navigate to /patch/1', async t => {
await page.nuxt.navigate('/patch/1')
const loading = await page.nuxt.loadingData()
t.is(loading.show, true)
await page.nuxt.waitForNavigation()
const h2 = await page.$text('h2')
t.true(h2.includes('_id:'))
dates.id = await page.$text('[data-date-id]')
t.is(dates.patch, await page.$text('[data-date-patch]'))
})
test('Navigate to /patch/2', async t => {
await page.nuxt.navigate('/patch/2', true)
const date = await page.$text('[data-date-id]')
t.is(dates.patch, await page.$text('[data-date-patch]'))
t.true(+dates.id < +date)
dates.id = date
})
test('Navigate to /patch/2?test=true', async t => {
await page.nuxt.navigate('/patch/2?test=true', true)
t.is(dates.patch, await page.$text('[data-date-patch]'))
t.is(dates.id, await page.$text('[data-date-id]'))
})
test('Navigate to /patch/2#test', async t => {
await page.nuxt.navigate('/patch/2#test', true)
t.is(dates.patch, await page.$text('[data-date-patch]'))
t.is(dates.id, await page.$text('[data-date-id]'))
})
// Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => {
nuxt.close()
})
test.after('Stop browser', async t => {
await page.close()
await browser.stop()
})

View File

@ -2,11 +2,11 @@ import puppeteer from 'puppeteer'
let browser = null
export async function start() {
export async function start(options = {}) {
// https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions
browser = await puppeteer.launch({
browser = await puppeteer.launch(Object.assign({
args: ['--no-sandbox', '--disable-setuid-sandbox']
})
}, options))
}
export async function stop() {
@ -21,6 +21,7 @@ export async function page(url) {
await page.waitForFunction('!!window.$nuxt')
page.html = () => page.evaluate(() => window.document.documentElement.outerHTML)
page.$text = (selector) => page.$eval(selector, (el) => el.textContent)
page.$$text = (selector) => page.$$eval(selector, (els) => els.map((el) => el.textContent))
page.$nuxt = await page.evaluateHandle('window.$nuxt')
page.nuxt = {
@ -30,6 +31,14 @@ export async function page(url) {
await this.waitForNavigation()
}
},
routeData() {
return page.evaluate(($nuxt) => {
return {
path: $nuxt.$route.path,
query: $nuxt.$route.query
}
}, page.$nuxt)
},
loadingData() {
return page.evaluate(($nuxt) => $nuxt.$loading.$data, page.$nuxt)
},