2016-12-24 17:50:28 +00:00
|
|
|
import { resolve } from 'path'
|
|
|
|
import fs from 'fs'
|
2017-12-12 07:43:56 +00:00
|
|
|
import { promisify } from 'util'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
2017-12-12 07:43:56 +00:00
|
|
|
const readFile = promisify(fs.readFile)
|
2016-12-24 17:50:28 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
describe('dynamic routes', () => {
|
|
|
|
test('Check .nuxt/router.js', () => {
|
|
|
|
return readFile(
|
2018-03-19 10:06:45 +00:00
|
|
|
resolve(__dirname, '..', 'fixtures/dynamic-routes/.nuxt/router.js'),
|
2018-03-18 19:31:32 +00:00
|
|
|
'utf-8'
|
2018-08-06 00:12:44 +00:00
|
|
|
).then((routerFile) => {
|
2018-03-18 19:31:32 +00:00
|
|
|
routerFile = routerFile
|
|
|
|
.slice(routerFile.indexOf('routes: ['))
|
|
|
|
.replace('routes: [', '[')
|
|
|
|
.replace(/ _[0-9A-z]+,/g, ' "",')
|
|
|
|
routerFile = routerFile.substr(
|
|
|
|
routerFile.indexOf('['),
|
|
|
|
routerFile.lastIndexOf(']') + 1
|
|
|
|
)
|
2018-08-08 10:54:05 +00:00
|
|
|
const routes = eval('( ' + routerFile + ')') // eslint-disable-line no-eval
|
2018-03-18 19:31:32 +00:00
|
|
|
// pages/test/index.vue
|
2018-11-14 16:50:06 +00:00
|
|
|
expect(routes[0].path).toBe('/parent')
|
|
|
|
expect(routes[0].name).toBeFalsy() // parent route has no name
|
|
|
|
// pages/parent/*.vue
|
|
|
|
expect(routes[0].children.length).toBe(3) // parent has 3 children
|
|
|
|
expect(routes[0].children.map(r => r.path)).toEqual(['', 'child', 'teub'])
|
|
|
|
expect(routes[0].children.map(r => r.name)).toEqual([
|
|
|
|
'parent',
|
|
|
|
'parent-child',
|
|
|
|
'parent-teub'
|
|
|
|
])
|
2018-03-18 19:31:32 +00:00
|
|
|
// pages/posts.vue
|
|
|
|
expect(routes[1].path).toBe('/posts')
|
|
|
|
expect(routes[1].name).toBe('posts')
|
|
|
|
expect(routes[1].children.length).toBe(1)
|
|
|
|
// pages/posts/_id.vue
|
|
|
|
expect(routes[1].children[0].path).toBe(':id?')
|
|
|
|
expect(routes[1].children[0].name).toBe('posts-id')
|
|
|
|
// pages/parent.vue
|
2018-11-14 16:50:06 +00:00
|
|
|
expect(routes[2].path).toBe('/test')
|
|
|
|
expect(routes[2].name).toBe('test')
|
2018-03-18 19:31:32 +00:00
|
|
|
// pages/test/projects/index.vue
|
|
|
|
expect(routes[3].path).toBe('/test/projects')
|
|
|
|
expect(routes[3].name).toBe('test-projects')
|
|
|
|
// pages/test/users.vue
|
|
|
|
expect(routes[4].path).toBe('/test/users')
|
|
|
|
expect(routes[4].name).toBeFalsy() // parent route has no name
|
|
|
|
// pages/test/users/*.vue
|
|
|
|
expect(routes[4].children.length).toBe(5) // parent has 5 children
|
|
|
|
expect(routes[4].children.map(r => r.path)).toEqual([
|
|
|
|
'',
|
|
|
|
'projects',
|
|
|
|
'projects/:category',
|
|
|
|
':id',
|
|
|
|
':index/teub'
|
|
|
|
])
|
|
|
|
expect(routes[4].children.map(r => r.name)).toEqual([
|
|
|
|
'test-users',
|
|
|
|
'test-users-projects',
|
|
|
|
'test-users-projects-category',
|
|
|
|
'test-users-id',
|
|
|
|
'test-users-index-teub'
|
|
|
|
])
|
|
|
|
// pages/test/songs/toto.vue
|
|
|
|
expect(routes[5].path).toBe('/test/songs/toto')
|
|
|
|
expect(routes[5].name).toBe('test-songs-toto')
|
|
|
|
// pages/test/projects/_category.vue
|
|
|
|
expect(routes[6].path).toBe('/test/projects/:category')
|
|
|
|
expect(routes[6].name).toBe('test-projects-category')
|
|
|
|
// pages/test/songs/_id.vue
|
|
|
|
expect(routes[7].path).toBe('/test/songs/:id?')
|
|
|
|
expect(routes[7].name).toBe('test-songs-id')
|
|
|
|
// pages/users/_id.vue
|
|
|
|
expect(routes[8].path).toBe('/users/:id?')
|
|
|
|
expect(routes[8].name).toBe('users-id')
|
|
|
|
// pages/test/_.vue
|
|
|
|
expect(routes[9].path).toBe('/test/*')
|
|
|
|
expect(routes[9].name).toBe('test-all')
|
2018-01-23 07:20:56 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
// pages/index.vue
|
|
|
|
expect(routes[10].path).toBe('/')
|
|
|
|
expect(routes[10].name).toBe('index')
|
2018-01-23 07:20:56 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
// pages/_slug.vue
|
|
|
|
expect(routes[11].path).toBe('/:slug')
|
|
|
|
expect(routes[11].name).toBe('slug')
|
|
|
|
// pages/_key/_id.vue
|
|
|
|
expect(routes[12].path).toBe('/:key/:id?')
|
|
|
|
expect(routes[12].name).toBe('key-id')
|
|
|
|
// pages/_.vue
|
|
|
|
expect(routes[13].path).toBe('/*/p/*')
|
|
|
|
expect(routes[13].name).toBe('all-p-all')
|
|
|
|
// pages/_/_.vue
|
|
|
|
expect(routes[14].path).toBe('/*/*')
|
|
|
|
expect(routes[14].name).toBe('all-all')
|
|
|
|
// pages/_.vue
|
|
|
|
expect(routes[15].path).toBe('/*')
|
|
|
|
expect(routes[15].name).toBe('all')
|
|
|
|
})
|
2018-01-13 05:22:11 +00:00
|
|
|
})
|
2016-12-24 17:50:28 +00:00
|
|
|
})
|