Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change mock files to commonjs #3246

Merged
merged 1 commit into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mock/article.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Mock from 'mockjs'
const Mock = require('mockjs')

const List = []
const count = 100
Expand Down Expand Up @@ -27,7 +27,7 @@ for (let i = 0; i < count; i++) {
}))
}

export default [
module.exports = [
{
url: '/vue-element-admin/article/list',
type: 'get',
Expand Down
19 changes: 11 additions & 8 deletions mock/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Mock from 'mockjs'
import { param2Obj } from '../src/utils'
const Mock = require('mockjs')
const { param2Obj } = require('./utils')

import user from './user'
import role from './role'
import article from './article'
import search from './remote-search'
const user = require('./user')
const role = require('./role')
const article = require('./article')
const search = require('./remote-search')

const mocks = [
...user,
Expand All @@ -16,7 +16,7 @@ const mocks = [
// for front mock
// please use it cautiously, it will redefine XMLHttpRequest,
// which will cause many of your third-party libraries to be invalidated(like progress event).
export function mockXHR() {
function mockXHR() {
// mock patch
// https://github.com/nuysoft/Mock/issues/300
Mock.XHR.prototype.proxy_send = Mock.XHR.prototype.send
Expand Down Expand Up @@ -54,4 +54,7 @@ export function mockXHR() {
}
}

export default mocks
module.exports = {
mocks,
mockXHR
}
5 changes: 1 addition & 4 deletions mock/mock-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const mockDir = path.join(process.cwd(), 'mock')

function registerRoutes(app) {
let mockLastIndex
const { default: mocks } = require('./index.js')
const { mocks } = require('./index.js')
const mocksForServer = mocks.map(route => {
return responseFake(route.url, route.type, route.response)
})
Expand Down Expand Up @@ -44,9 +44,6 @@ const responseFake = (url, type, respond) => {
}

module.exports = app => {
// es6 polyfill
require('@babel/register')

// parse app.body
// https://expressjs.com/en/4x/api.html#req.body
app.use(bodyParser.json())
Expand Down
4 changes: 2 additions & 2 deletions mock/remote-search.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Mock from 'mockjs'
const Mock = require('mockjs')

const NameList = []
const count = 100
Expand All @@ -10,7 +10,7 @@ for (let i = 0; i < count; i++) {
}
NameList.push({ name: 'mock-Pan' })

export default [
module.exports = [
// username search
{
url: '/vue-element-admin/search/user',
Expand Down
8 changes: 4 additions & 4 deletions mock/role/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Mock from 'mockjs'
import { deepClone } from '../../src/utils/index.js'
import { asyncRoutes, constantRoutes } from './routes.js'
const Mock = require('mockjs')
const { deepClone } = require('../utils')
const { asyncRoutes, constantRoutes } = require('./routes.js')

const routes = deepClone([...constantRoutes, ...asyncRoutes])

Expand Down Expand Up @@ -35,7 +35,7 @@ const roles = [
}
]

export default [
module.exports = [
// mock get all routes form server
{
url: '/vue-element-admin/routes',
Expand Down
9 changes: 7 additions & 2 deletions mock/role/routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Just a mock data

export const constantRoutes = [
const constantRoutes = [
{
path: '/redirect',
component: 'layout/Layout',
Expand Down Expand Up @@ -72,7 +72,7 @@ export const constantRoutes = [
}
]

export const asyncRoutes = [
const asyncRoutes = [
{
path: '/permission',
component: 'layout/Layout',
Expand Down Expand Up @@ -523,3 +523,8 @@ export const asyncRoutes = [

{ path: '*', redirect: '/404', hidden: true }
]

module.exports = {
constantRoutes,
asyncRoutes
}
2 changes: 1 addition & 1 deletion mock/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const users = {
}
}

export default [
module.exports = [
// user login
{
url: '/vue-element-admin/user/login',
Expand Down
46 changes: 46 additions & 0 deletions mock/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @param {string} url
* @returns {Object}
*/
function param2Obj(url) {
const search = url.split('?')[1]
if (!search) {
return {}
}
return JSON.parse(
'{"' +
decodeURIComponent(search)
.replace(/"/g, '\\"')
.replace(/&/g, '","')
.replace(/=/g, '":"')
.replace(/\+/g, ' ') +
'"}'
)
}

/**
* This is just a simple version of deep copy
* Has a lot of edge cases bug
* If you want to use a perfect deep copy, use lodash's _.cloneDeep
* @param {Object} source
* @returns {Object}
*/
function deepClone(source) {
if (!source && typeof source !== 'object') {
throw new Error('error arguments', 'deepClone')
}
const targetObj = source.constructor === Array ? [] : {}
Object.keys(source).forEach(keys => {
if (source[keys] && typeof source[keys] === 'object') {
targetObj[keys] = deepClone(source[keys])
} else {
targetObj[keys] = source[keys]
}
})
return targetObj
}

module.exports = {
param2Obj,
deepClone
}
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@
"xlsx": "0.14.1"
},
"devDependencies": {
"@babel/core": "7.0.0",
"@babel/register": "7.0.0",
"@vue/cli-plugin-babel": "3.5.3",
"@vue/cli-plugin-eslint": "^3.9.1",
"@vue/cli-plugin-unit-jest": "3.5.3",
Expand Down