Nuxt/test/dynamic-routes.test.js

65 lines
2.3 KiB
JavaScript
Raw Normal View History

2016-12-24 17:50:28 +00:00
import test from 'ava'
import { resolve } from 'path'
import fs from 'fs'
import pify from 'pify'
const readFile = pify(fs.readFile)
// Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => {
const Nuxt = require('../')
const nuxt = new Nuxt({
rootDir: resolve(__dirname, 'fixtures/dynamic-routes'),
dev: false
})
await nuxt.build()
})
test('Check .nuxt/router.js', t => {
return readFile(resolve(__dirname, './fixtures/dynamic-routes/.nuxt/router.js'), 'utf-8')
.then((routerFile) => {
routerFile = routerFile.slice(
routerFile.indexOf('routes: ['),
-3
)
.replace('routes: [', '[')
.replace(/ _[0-9A-z]+,/g, ' "",')
let routes = eval('( ' + routerFile + ')') // eslint-disable-line no-eval
2017-01-11 14:03:26 +00:00
// pages/index.vue
t.is(routes[0].path, '/')
t.is(routes[0].name, 'index')
2016-12-24 17:50:28 +00:00
// pages/test/index.vue
2017-01-11 14:03:26 +00:00
t.is(routes[1].path, '/test')
t.is(routes[1].name, 'test')
// pages/posts.vue
t.is(routes[2].path, '/posts')
t.is(routes[2].name, 'posts')
t.is(routes[2].children.length, 1)
// pages/posts/_id.vue
t.is(routes[2].children[0].path, ':id?')
t.is(routes[2].children[0].name, 'posts-id')
2016-12-24 17:50:28 +00:00
// pages/parent.vue
2017-01-11 14:03:26 +00:00
t.is(routes[3].path, '/parent')
t.falsy(routes[3].name) // parent route has no name
2016-12-24 17:50:28 +00:00
// pages/parent/*.vue
2017-01-11 14:03:26 +00:00
t.is(routes[3].children.length, 3) // parent has 3 children
t.deepEqual(routes[3].children.map((r) => r.path), ['', 'teub', 'child'])
t.deepEqual(routes[3].children.map((r) => r.name), ['parent', 'parent-teub', 'parent-child'])
2016-12-24 17:50:28 +00:00
// pages/test/users.vue
2017-01-11 14:03:26 +00:00
t.is(routes[4].path, '/test/users')
t.falsy(routes[4].name) // parent route has no name
2016-12-24 17:50:28 +00:00
// pages/test/users/*.vue
2017-01-11 14:03:26 +00:00
t.is(routes[4].children.length, 3) // parent has 3 children
t.deepEqual(routes[4].children.map((r) => r.path), ['', ':id', ':index/teub'])
t.deepEqual(routes[4].children.map((r) => r.name), ['test-users', 'test-users-id', 'test-users-index-teub'])
// pages/test/songs/_id.vue
t.is(routes[5].path, '/test/songs/:id?')
t.is(routes[5].name, 'test-songs-id')
2016-12-24 17:50:28 +00:00
// pages/_slug.vue
2017-01-11 14:03:26 +00:00
t.is(routes[6].path, '/:slug')
t.is(routes[6].name, 'slug')
2016-12-24 17:50:28 +00:00
// pages/_key/_id.vue
2017-01-11 14:03:26 +00:00
t.is(routes[7].path, '/:key/:id?')
t.is(routes[7].name, 'key-id')
2016-12-24 17:50:28 +00:00
})
})