mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-30 15:22:39 +00:00
test(ssr): add unique test
This commit is contained in:
parent
d3cacd0e58
commit
28d20ec262
17
test/fixtures/ssr/components/test.vue
vendored
Normal file
17
test/fixtures/ssr/components/test.vue
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
<template>
|
||||
<foobar>
|
||||
{{id}}
|
||||
</foobar>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { nextId } from '@/lib/db'
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
id: nextId()
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
4
test/fixtures/ssr/lib/db.js
vendored
Normal file
4
test/fixtures/ssr/lib/db.js
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
|
||||
let idCtr = 0
|
||||
|
||||
export const nextId = () => ++idCtr
|
15
test/fixtures/ssr/pages/asyncData.vue
vendored
Normal file
15
test/fixtures/ssr/pages/asyncData.vue
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
<template>
|
||||
<foobar>{{id}}</foobar>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { nextId } from '@/lib/db'
|
||||
|
||||
export default {
|
||||
async asyncData() {
|
||||
return {
|
||||
id: nextId()
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
15
test/fixtures/ssr/pages/component.vue
vendored
Normal file
15
test/fixtures/ssr/pages/component.vue
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
<template>
|
||||
<div>
|
||||
<test/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import test from '@/components/test'
|
||||
|
||||
export default {
|
||||
components : {
|
||||
test
|
||||
}
|
||||
}
|
||||
</script>
|
15
test/fixtures/ssr/pages/data.vue
vendored
Normal file
15
test/fixtures/ssr/pages/data.vue
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
<template>
|
||||
<foobar>{{id}}</foobar>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { nextId } from '@/lib/db'
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
id: nextId()
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
14
test/fixtures/ssr/pages/fetch.vue
vendored
Normal file
14
test/fixtures/ssr/pages/fetch.vue
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
<template>
|
||||
<foobar>{{$store.__id}}</foobar>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { nextId } from '@/lib/db'
|
||||
|
||||
export default {
|
||||
async fetch({store}) {
|
||||
// We use store just as a shared reference
|
||||
store.__id = nextId()
|
||||
},
|
||||
}
|
||||
</script>
|
5
test/fixtures/ssr/pages/store.vue
vendored
Normal file
5
test/fixtures/ssr/pages/store.vue
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
<template>
|
||||
<foobar>
|
||||
{{$store.state[$route.query.onServerInit === '1' ? 'id2': 'id']}}
|
||||
</foobar>
|
||||
</template>
|
22
test/fixtures/ssr/store/index.js
vendored
Normal file
22
test/fixtures/ssr/store/index.js
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
import { nextId } from '@/lib/db'
|
||||
|
||||
export const state = () => {
|
||||
return {
|
||||
id: nextId(),
|
||||
id2: 0
|
||||
}
|
||||
}
|
||||
|
||||
export const mutations = {
|
||||
setId2 (state, id) {
|
||||
state.id2 = id
|
||||
}
|
||||
}
|
||||
|
||||
export const actions = {
|
||||
nuxtServerInit ({ commit, state }, { route }) {
|
||||
if (route.query.onServerInit === '1') {
|
||||
commit('setId2', nextId())
|
||||
}
|
||||
}
|
||||
}
|
84
test/ssr.test.js
Executable file
84
test/ssr.test.js
Executable file
@ -0,0 +1,84 @@
|
||||
import test from 'ava'
|
||||
import { resolve } from 'path'
|
||||
import { Nuxt, Builder, Utils } from '..'
|
||||
import { uniq } from 'lodash'
|
||||
|
||||
const port = 4008
|
||||
let nuxt = null
|
||||
|
||||
// Utils
|
||||
const range = n => [...Array(n).keys()]
|
||||
const FOOBAR_REGEX = /<foobar>([\s\S]*)<\/foobar>/
|
||||
const match = (regex, text) => (regex.exec(text) || [])[1]
|
||||
|
||||
// Init nuxt.js and create server listening on localhost:4000
|
||||
test.before('Init Nuxt.js', async t => {
|
||||
const options = {
|
||||
rootDir: resolve(__dirname, 'fixtures/ssr'),
|
||||
dev: false,
|
||||
render: {
|
||||
resourceHints: false
|
||||
},
|
||||
build: {
|
||||
extractCSS: true
|
||||
}
|
||||
}
|
||||
nuxt = new Nuxt(options)
|
||||
await new Builder(nuxt).build()
|
||||
await nuxt.listen(port, 'localhost')
|
||||
})
|
||||
|
||||
// == Uniq Test ==
|
||||
// The idea behind is pages using a shared nextId() which retuns an increamenting id
|
||||
// So all responses should strictly be different and length of unique responses should equal to responses
|
||||
// We strictly compare <foorbar>{id}</foorbar> section
|
||||
// Because other response parts such as window.__NUXT may be different resulting false positive passes.
|
||||
const uniqueTest = async (t, url) => {
|
||||
let results = []
|
||||
|
||||
await Utils.parallel(range(20), async () => {
|
||||
let { html } = await nuxt.renderRoute(url)
|
||||
let foobar = match(FOOBAR_REGEX, html)
|
||||
results.push(parseInt(foobar))
|
||||
})
|
||||
|
||||
let isUnique = uniq(results).length === results.length
|
||||
|
||||
if (!isUnique) {
|
||||
/* eslint-disable no-console */
|
||||
console.log(url + '\n' + results.join(', ') + '\n')
|
||||
}
|
||||
|
||||
t.true(isUnique)
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
test('unique responses with data()', async t => {
|
||||
await uniqueTest(t, '/data')
|
||||
})
|
||||
|
||||
test('unique responses with component', async t => {
|
||||
await uniqueTest(t, '/component')
|
||||
})
|
||||
|
||||
test('unique responses with asyncData()', async t => {
|
||||
await uniqueTest(t, '/asyncData')
|
||||
})
|
||||
|
||||
test('unique responses with store initial state', async t => {
|
||||
await uniqueTest(t, '/store')
|
||||
})
|
||||
|
||||
test('unique responses with nuxtServerInit', async t => {
|
||||
await uniqueTest(t, '/store?onServerInit=1')
|
||||
})
|
||||
|
||||
test('unique responses with fetch', async t => {
|
||||
await uniqueTest(t, '/fetch')
|
||||
})
|
||||
|
||||
// Close server and ask nuxt to stop listening to file changes
|
||||
test.after('Closing server and nuxt.js', t => {
|
||||
nuxt.close()
|
||||
})
|
Loading…
Reference in New Issue
Block a user