2017-11-02 16:48:20 +00:00
|
|
|
import { resolve } from 'path'
|
2018-08-06 09:47:54 +00:00
|
|
|
import test from 'ava'
|
2017-12-12 09:42:29 +00:00
|
|
|
import { Nuxt, Builder } from '..'
|
2017-11-02 16:48:20 +00:00
|
|
|
import * as browser from './helpers/browser'
|
2017-12-17 19:30:26 +00:00
|
|
|
import { interceptLog } from './helpers/console'
|
2017-11-02 16:48:20 +00:00
|
|
|
|
|
|
|
const port = 4003
|
2018-01-13 05:22:11 +00:00
|
|
|
const url = route => 'http://localhost:' + port + route
|
2017-11-02 16:48:20 +00:00
|
|
|
|
|
|
|
let nuxt = null
|
|
|
|
let page = null
|
|
|
|
|
|
|
|
// Init nuxt.js and create server listening on localhost:4003
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('Init Nuxt.js', async (t) => {
|
2017-11-02 16:48:20 +00:00
|
|
|
const options = {
|
|
|
|
rootDir: resolve(__dirname, 'fixtures/basic'),
|
2017-12-17 19:30:26 +00:00
|
|
|
buildDir: '.nuxt-csr',
|
|
|
|
dev: true,
|
2017-11-02 16:48:20 +00:00
|
|
|
head: {
|
|
|
|
titleTemplate(titleChunk) {
|
|
|
|
return titleChunk ? `${titleChunk} - Nuxt.js` : 'Nuxt.js'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
const logSpy = await interceptLog(async () => {
|
|
|
|
nuxt = new Nuxt(options)
|
|
|
|
await new Builder(nuxt).build()
|
|
|
|
await nuxt.listen(port, 'localhost')
|
|
|
|
})
|
|
|
|
|
|
|
|
t.true(logSpy.calledWithMatch('DONE'))
|
|
|
|
t.true(logSpy.calledWithMatch('OPEN'))
|
2017-11-02 16:48:20 +00:00
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('Start browser', async (t) => {
|
2017-12-17 19:30:26 +00:00
|
|
|
t.plan(0) // suppress 'no assertions' warning
|
2017-11-02 16:48:20 +00:00
|
|
|
await browser.start({
|
|
|
|
// slowMo: 50,
|
|
|
|
// headless: false
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('Open /', async (t) => {
|
2017-11-02 16:48:20 +00:00
|
|
|
page = await browser.page(url('/'))
|
|
|
|
|
|
|
|
t.is(await page.$text('h1'), 'Index page')
|
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('/stateless', async (t) => {
|
2017-11-07 15:26:15 +00:00
|
|
|
const { hook } = await page.nuxt.navigate('/stateless', false)
|
2017-11-02 16:48:20 +00:00
|
|
|
const loading = await page.nuxt.loadingData()
|
|
|
|
|
|
|
|
t.is(loading.show, true)
|
2017-11-07 15:26:15 +00:00
|
|
|
await hook
|
2017-11-02 16:48:20 +00:00
|
|
|
t.is(await page.$text('h1'), 'My component!')
|
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('/css', async (t) => {
|
2017-11-07 15:26:15 +00:00
|
|
|
await page.nuxt.navigate('/css')
|
2017-11-02 16:48:20 +00:00
|
|
|
|
|
|
|
t.is(await page.$text('.red'), 'This is red')
|
2018-01-13 05:22:11 +00:00
|
|
|
t.is(
|
|
|
|
await page.$eval('.red', red => window.getComputedStyle(red).color),
|
|
|
|
'rgb(255, 0, 0)'
|
|
|
|
)
|
2017-11-02 16:48:20 +00:00
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('/stateful', async (t) => {
|
2017-11-07 15:26:15 +00:00
|
|
|
await page.nuxt.navigate('/stateful')
|
2017-11-02 16:48:20 +00:00
|
|
|
|
|
|
|
t.is(await page.$text('p'), 'The answer is 42')
|
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('/store', async (t) => {
|
2017-11-07 15:26:15 +00:00
|
|
|
await page.nuxt.navigate('/store')
|
2017-11-02 16:48:20 +00:00
|
|
|
|
|
|
|
t.is(await page.$text('h1'), 'Vuex Nested Modules')
|
|
|
|
t.is(await page.$text('p'), '1')
|
2018-01-31 19:46:57 +00:00
|
|
|
t.is(await page.$text('h2'), '4')
|
2018-01-31 20:22:02 +00:00
|
|
|
t.is(await page.$text('h3'), '10')
|
2017-11-02 16:48:20 +00:00
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('/head', async (t) => {
|
2018-01-13 05:22:11 +00:00
|
|
|
const msg = new Promise(resolve =>
|
2018-01-15 11:22:51 +00:00
|
|
|
page.on('console', msg => resolve(msg.text()))
|
2018-01-13 05:22:11 +00:00
|
|
|
)
|
2017-11-07 15:26:15 +00:00
|
|
|
await page.nuxt.navigate('/head')
|
2017-11-02 16:48:20 +00:00
|
|
|
const metas = await page.$$attr('meta', 'content')
|
|
|
|
|
2017-11-06 12:49:24 +00:00
|
|
|
t.is(await msg, 'Body script!')
|
2017-11-02 16:48:20 +00:00
|
|
|
t.is(await page.title(), 'My title - Nuxt.js')
|
|
|
|
t.is(await page.$text('h1'), 'I can haz meta tags')
|
|
|
|
t.is(metas[0], 'my meta')
|
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('/async-data', async (t) => {
|
2017-11-07 15:26:15 +00:00
|
|
|
await page.nuxt.navigate('/async-data')
|
2017-11-02 16:48:20 +00:00
|
|
|
|
|
|
|
t.is(await page.$text('p'), 'Nuxt.js')
|
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('/await-async-data', async (t) => {
|
2017-11-07 15:26:15 +00:00
|
|
|
await page.nuxt.navigate('/await-async-data')
|
2017-11-02 16:48:20 +00:00
|
|
|
|
|
|
|
t.is(await page.$text('p'), 'Await Nuxt.js')
|
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('/callback-async-data', async (t) => {
|
2017-11-07 15:26:15 +00:00
|
|
|
await page.nuxt.navigate('/callback-async-data')
|
2017-11-02 16:48:20 +00:00
|
|
|
|
|
|
|
t.is(await page.$text('p'), 'Callback Nuxt.js')
|
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('/users/1', async (t) => {
|
2017-11-07 15:26:15 +00:00
|
|
|
await page.nuxt.navigate('/users/1')
|
2017-11-02 16:48:20 +00:00
|
|
|
|
|
|
|
t.is(await page.$text('h1'), 'User: 1')
|
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('/validate should display a 404', async (t) => {
|
2017-11-07 15:26:15 +00:00
|
|
|
await page.nuxt.navigate('/validate')
|
2017-11-02 16:48:20 +00:00
|
|
|
const error = await page.nuxt.errorData()
|
|
|
|
|
|
|
|
t.is(error.statusCode, 404)
|
|
|
|
t.is(error.message, 'This page could not be found')
|
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('/validate?valid=true', async (t) => {
|
2017-11-07 15:26:15 +00:00
|
|
|
await page.nuxt.navigate('/validate?valid=true')
|
2017-11-02 16:48:20 +00:00
|
|
|
|
|
|
|
t.is(await page.$text('h1'), 'I am valid')
|
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('/redirect', async (t) => {
|
2017-11-07 15:26:15 +00:00
|
|
|
await page.nuxt.navigate('/redirect')
|
2017-11-02 16:48:20 +00:00
|
|
|
|
|
|
|
t.is(await page.$text('h1'), 'Index page')
|
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('/error', async (t) => {
|
2017-11-07 15:26:15 +00:00
|
|
|
await page.nuxt.navigate('/error')
|
2017-11-02 16:48:20 +00:00
|
|
|
|
|
|
|
t.deepEqual(await page.nuxt.errorData(), { statusCode: 500 })
|
|
|
|
t.is(await page.$text('.title'), 'Error mouahahah')
|
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('/error2', async (t) => {
|
2017-11-07 15:26:15 +00:00
|
|
|
await page.nuxt.navigate('/error2')
|
2017-11-02 16:48:20 +00:00
|
|
|
|
|
|
|
t.is(await page.$text('.title'), 'Custom error')
|
|
|
|
t.deepEqual(await page.nuxt.errorData(), { message: 'Custom error' })
|
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('/redirect-middleware', async (t) => {
|
2017-12-21 04:55:32 +00:00
|
|
|
await page.nuxt.navigate('/redirect-middleware')
|
2017-11-02 16:48:20 +00:00
|
|
|
|
|
|
|
t.is(await page.$text('h1'), 'Index page')
|
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('/redirect-external', async (t) => {
|
2017-11-28 09:10:44 +00:00
|
|
|
// New page for redirecting to external link.
|
|
|
|
const page = await browser.page(url('/'))
|
2017-12-21 04:55:32 +00:00
|
|
|
await page.nuxt.navigate('/redirect-external', false)
|
2018-01-13 05:22:11 +00:00
|
|
|
await page.waitForFunction(
|
|
|
|
() => window.location.href === 'https://nuxtjs.org/'
|
|
|
|
)
|
2017-11-28 09:10:44 +00:00
|
|
|
page.close()
|
|
|
|
t.pass()
|
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('/redirect-name', async (t) => {
|
2017-12-21 04:55:32 +00:00
|
|
|
await page.nuxt.navigate('/redirect-name')
|
|
|
|
|
|
|
|
t.is(await page.$text('h1'), 'My component!')
|
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('/no-ssr', async (t) => {
|
2017-11-07 15:26:15 +00:00
|
|
|
await page.nuxt.navigate('/no-ssr')
|
2017-11-02 16:48:20 +00:00
|
|
|
|
|
|
|
t.is(await page.$text('h1'), 'Displayed only on client-side')
|
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('/meta', async (t) => {
|
2017-11-07 15:26:15 +00:00
|
|
|
await page.nuxt.navigate('/meta')
|
2017-11-02 17:07:33 +00:00
|
|
|
|
|
|
|
const state = await page.nuxt.storeState()
|
|
|
|
t.deepEqual(state.meta, [{ works: true }])
|
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('/fn-midd', async (t) => {
|
2017-11-07 15:26:15 +00:00
|
|
|
await page.nuxt.navigate('/fn-midd')
|
2017-11-03 16:14:05 +00:00
|
|
|
|
|
|
|
t.is(await page.$text('.title'), 'You need to ask the permission')
|
2018-01-13 05:22:11 +00:00
|
|
|
t.deepEqual(await page.nuxt.errorData(), {
|
|
|
|
message: 'You need to ask the permission',
|
|
|
|
statusCode: 403
|
|
|
|
})
|
2017-11-03 16:14:05 +00:00
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('/fn-midd?please=true', async (t) => {
|
2017-11-07 15:26:15 +00:00
|
|
|
await page.nuxt.navigate('/fn-midd?please=true')
|
2017-11-03 16:14:05 +00:00
|
|
|
|
|
|
|
const h1 = await page.$text('h1')
|
|
|
|
t.true(h1.includes('Date:'))
|
|
|
|
})
|
|
|
|
|
2018-08-06 09:47:54 +00:00
|
|
|
test.serial('/router-guard', async (t) => {
|
2017-12-01 09:25:21 +00:00
|
|
|
await page.nuxt.navigate('/router-guard')
|
|
|
|
|
2018-01-15 11:22:51 +00:00
|
|
|
const p = await page.$text('p')
|
|
|
|
t.is(p, 'Nuxt.js')
|
2017-12-01 09:25:21 +00:00
|
|
|
})
|
|
|
|
|
2018-01-15 11:22:51 +00:00
|
|
|
test.after.always('Stop browser', async () => {
|
|
|
|
process.on('unhandledRejection', () => {})
|
|
|
|
await browser.stop()
|
2017-11-02 16:48:20 +00:00
|
|
|
})
|
|
|
|
|
2018-01-15 11:22:51 +00:00
|
|
|
// Close server and ask nuxt to stop listening to file changes
|
|
|
|
test.after.always('Closing server and nuxt.js', async () => {
|
|
|
|
await nuxt.close()
|
2017-11-02 16:48:20 +00:00
|
|
|
})
|