Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
Add tests for fs-routes
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaSemenov committed Jun 5, 2018
1 parent 5688c70 commit a40e6b4
Show file tree
Hide file tree
Showing 28 changed files with 899 additions and 24 deletions.
1 change: 1 addition & 0 deletions lib/plugins/fs-routes/__test__/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Run `WRITE_EXPECTED_ROUTES=1 yarn test` to rewrite expected `routes.json`
1 change: 1 addition & 0 deletions lib/plugins/fs-routes/__test__/presets/empty/routes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"path": "/page",
"component": "#base/page.vue"
}
]
Empty file.
Empty file.
Empty file.
Empty file.
24 changes: 24 additions & 0 deletions lib/plugins/fs-routes/__test__/presets/nested-children/routes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"path": "/foo",
"component": "#base/foo.vue",
"children": [
{
"path": "bar",
"component": "#base/foo/bar.vue",
"children": [
{
"path": "baz",
"component": "#base/foo/bar/baz.vue",
"children": [
{
"path": "qux",
"component": "#base/foo/bar/baz/qux.vue"
}
]
}
]
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

6 changes: 6 additions & 0 deletions lib/plugins/fs-routes/__test__/presets/nested/routes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"path": "/foo/bar/baz/qux",
"component": "#base/foo/bar/baz/qux.vue"
}
]
Empty file.
Empty file.
30 changes: 30 additions & 0 deletions lib/plugins/fs-routes/__test__/presets/typical/routes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"path": "/",
"component": "#base/index.vue"
},
{
"path": "/foo",
"component": "#base/foo.vue"
},
{
"path": "/user",
"component": "#base/user.vue",
"children": [
{
"path": ":user",
"component": "#base/user/[user].vue",
"children": [
{
"path": "",
"component": "#base/user/[user]/index.vue"
},
{
"path": "friends",
"component": "#base/user/[user]/friends.vue"
}
]
}
]
}
]
Empty file.
Empty file.
Empty file.
Empty file.
40 changes: 40 additions & 0 deletions lib/plugins/fs-routes/__test__/routes.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const path = require('path')
const fs = require('fs-extra')
const { collectRoutes } = require('../routes-template')

async function testCollectRoutes(dir, opts) {
const fullDir = path.resolve(__dirname, dir)
const routes = await collectRoutes({
dir: fullDir,
componentPrefix: '#base',
basePath: '/',
options: {
match: /\.vue$/
},
...opts
})
const expectedRoutesFile = path.join(fullDir, 'routes.json')
if (process.env.WRITE_EXPECTED_ROUTES) {
await fs.writeFile(expectedRoutesFile, JSON.stringify(routes, null, 2))
} else {
expect(routes).toEqual(await fs.readJson(expectedRoutesFile))
}
}

describe('Presets', () => {
for (const preset of fs.readdirSync(path.resolve(__dirname, 'presets'))) {
test(preset, () => testCollectRoutes(path.join('presets', preset)))
}
})

test('Typescript', () =>
testCollectRoutes('special/typescript', {
options: {
match: /\.(vue|ts)$/
}
}))

test('Custom base path', () =>
testCollectRoutes('special/custom-base-path', {
basePath: '/some'
}))
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"path": "/some/foo",
"component": "#base/foo.vue"
}
]
Empty file.
Empty file.
10 changes: 10 additions & 0 deletions lib/plugins/fs-routes/__test__/special/typescript/routes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"path": "/",
"component": "#base/index.vue"
},
{
"path": "/page",
"component": "#base/page.ts"
}
]
4 changes: 3 additions & 1 deletion lib/plugins/fs-routes/routes-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const sortBy = require('lodash.sortby')
module.exports = async (api, options) => {
const routes = await collectRoutes({
dir: api.resolveBaseDir(options.path),
componentPrefix: path.join('#base/', options.path),
componentPrefix: path.join('#base', options.path),
basePath: '/',
options
})
Expand Down Expand Up @@ -122,3 +122,5 @@ async function collectRoutes({ dir, componentPrefix, basePath, options }) {

return routes
}

module.exports.collectRoutes = collectRoutes
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"bin": "bin/cli.js",
"main": "lib/index.js",
"scripts": {
"test": "npm run lint",
"test": "xo && jest",
"lint": "xo",
"postinstall": "node -e \"console.log('\\u001b[35m\\u001b[1mLove Ream? You can now donate to support the author:\\u001b[22m\\u001b[39m\\n> \\u001b[34mhttps://patreon.com/egoist\\u001b[0m')\""
},
Expand Down Expand Up @@ -79,7 +79,11 @@
"xo": {
"extends": [
"rem",
"plugin:prettier/recommended"
"plugin:prettier/recommended",
"plugin:jest/recommended"
],
"plugins": [
"jest"
],
"rules": {
"unicorn/filename-case": "off",
Expand All @@ -106,10 +110,13 @@
"framework"
],
"devDependencies": {
"eslint": "^4.19.1",
"eslint-config-prettier": "^2.9.0",
"eslint-config-rem": "^4.0.0",
"eslint-plugin-jest": "^21.17.0",
"eslint-plugin-prettier": "^2.6.0",
"husky": "^1.0.0-rc.6",
"jest": "^23.1.0",
"lerna": "^2.11.0",
"lint-staged": "^7.1.0",
"prettier": "^1.12.1",
Expand Down
Loading

0 comments on commit a40e6b4

Please sign in to comment.