mirror of
https://gitee.com/bootx/dax-pay-ui.git
synced 2025-09-07 12:48:06 +00:00
feat(test): add jest test suite
This commit is contained in:
15
tests/server/README.md
Normal file
15
tests/server/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# Test Server
|
||||
|
||||
It is used to start the test interface service, which can test the upload, websocket, login and other interfaces.
|
||||
|
||||
## Usage
|
||||
|
||||
```bash
|
||||
|
||||
cd ./test/server
|
||||
|
||||
yarn
|
||||
|
||||
yarn start
|
||||
|
||||
```
|
18
tests/server/controller/FileController.ts
Normal file
18
tests/server/controller/FileController.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import FileService from '../service/FileService';
|
||||
|
||||
class FileController {
|
||||
private service: FileService = new FileService();
|
||||
|
||||
upload = async (ctx) => {
|
||||
const files = ctx.request.files.file;
|
||||
console.log(files);
|
||||
|
||||
if (files.length === undefined) {
|
||||
this.service.upload(ctx, files, false);
|
||||
} else {
|
||||
this.service.upload(ctx, files, true);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default new FileController();
|
15
tests/server/controller/UserController.ts
Normal file
15
tests/server/controller/UserController.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import UserService from '../service/UserService';
|
||||
|
||||
class UserController {
|
||||
private service: UserService = new UserService();
|
||||
|
||||
login = async (ctx) => {
|
||||
ctx.body = await this.service.login();
|
||||
};
|
||||
|
||||
getUserInfoById = async (ctx) => {
|
||||
ctx.body = await this.service.getUserInfoById();
|
||||
};
|
||||
}
|
||||
|
||||
export default new UserController();
|
18
tests/server/ecosystem.config.js
Normal file
18
tests/server/ecosystem.config.js
Normal file
@@ -0,0 +1,18 @@
|
||||
const { name } = require('./package.json');
|
||||
const path = require('path');
|
||||
|
||||
module.exports = {
|
||||
apps: [
|
||||
{
|
||||
name,
|
||||
script: path.resolve(__dirname, './dist/index.js'),
|
||||
instances: require('os').cpus().length,
|
||||
autorestart: true,
|
||||
watch: true,
|
||||
env_production: {
|
||||
NODE_ENV: 'production',
|
||||
PORT: 8080,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
63
tests/server/index.ts
Normal file
63
tests/server/index.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import Koa from 'koa';
|
||||
import path from 'path';
|
||||
import Router from 'koa-router';
|
||||
import body from 'koa-body';
|
||||
import cors from 'koa2-cors';
|
||||
import koaStatic from 'koa-static';
|
||||
import websockify from 'koa-websocket';
|
||||
import route from 'koa-route';
|
||||
|
||||
import AppRoutes from './routes';
|
||||
|
||||
const PORT = 3300;
|
||||
|
||||
const app = websockify(new Koa());
|
||||
|
||||
app.ws.use(function (ctx, next) {
|
||||
ctx.websocket.send('connection succeeded!');
|
||||
return next(ctx);
|
||||
});
|
||||
|
||||
app.ws.use(
|
||||
route.all('/test', function (ctx) {
|
||||
// ctx.websocket.send('Hello World');
|
||||
ctx.websocket.on('message', function (message) {
|
||||
// do something with the message from client
|
||||
|
||||
if (message !== 'ping') {
|
||||
const data = JSON.stringify({
|
||||
id: Math.ceil(Math.random() * 1000),
|
||||
time: new Date().getTime(),
|
||||
res: `${message}`,
|
||||
});
|
||||
ctx.websocket.send(data);
|
||||
}
|
||||
console.log(message);
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
const router = new Router();
|
||||
|
||||
// router
|
||||
AppRoutes.forEach((route) => router[route.method](route.path, route.action));
|
||||
|
||||
app.use(cors());
|
||||
app.use(
|
||||
body({
|
||||
encoding: 'gzip',
|
||||
multipart: true,
|
||||
formidable: {
|
||||
// uploadDir: path.join(__dirname, '/upload/'), // 设置文件上传目录
|
||||
keepExtensions: true,
|
||||
maxFieldsSize: 20 * 1024 * 1024,
|
||||
},
|
||||
})
|
||||
);
|
||||
app.use(router.routes());
|
||||
app.use(router.allowedMethods());
|
||||
app.use(koaStatic(path.join(__dirname)));
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Application started successfully: http://localhost:${PORT}`);
|
||||
});
|
8
tests/server/nodemon.json
Normal file
8
tests/server/nodemon.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"watch": ["src"],
|
||||
"ext": "ts",
|
||||
"exec": "ts-node -r tsconfig-paths/register index.ts",
|
||||
"events": {
|
||||
"restart": "clear"
|
||||
}
|
||||
}
|
36
tests/server/package.json
Normal file
36
tests/server/package.json
Normal file
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"name": "server",
|
||||
"version": "1.0.0",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"start": "nodemon",
|
||||
"build": "rimraf ./dist && tsup ./index.ts --dts --format cjs,esm ",
|
||||
"prod": "npx pm2 start ecosystem.config.js --env production",
|
||||
"restart": "pm2 restart ecosystem.config.js --env production",
|
||||
"stop": "npx pm2 stop ecosystem.config.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"fs-extra": "^10.0.0",
|
||||
"koa": "^2.7.0",
|
||||
"koa-body": "^4.2.0",
|
||||
"koa-bodyparser": "^4.2.1",
|
||||
"koa-route": "^3.2.0",
|
||||
"koa-router": "^10.0.0",
|
||||
"koa-static": "^5.0.0",
|
||||
"koa-websocket": "^6.0.0",
|
||||
"koa2-cors": "^2.0.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/koa": "^2.0.48",
|
||||
"@types/koa-bodyparser": "^4.2.2",
|
||||
"@types/koa-router": "^7.0.40",
|
||||
"@types/node": "^15.12.1",
|
||||
"nodemon": "^2.0.7",
|
||||
"pm2": "^4.5.6",
|
||||
"rimraf": "^3.0.2",
|
||||
"ts-node": "^10.0.0",
|
||||
"tsconfig-paths": "^3.8.0",
|
||||
"tsup": "^4.11.2",
|
||||
"typescript": "^4.2.5"
|
||||
}
|
||||
}
|
23
tests/server/routes.ts
Normal file
23
tests/server/routes.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import UserController from './controller/UserController';
|
||||
import FileController from './controller/FileController';
|
||||
|
||||
export default [
|
||||
// user
|
||||
{
|
||||
path: '/login',
|
||||
method: 'post',
|
||||
action: UserController.login,
|
||||
},
|
||||
{
|
||||
path: '/getUserInfoById',
|
||||
method: 'get',
|
||||
action: UserController.getUserInfoById,
|
||||
},
|
||||
|
||||
// file
|
||||
{
|
||||
path: '/upload',
|
||||
method: 'post',
|
||||
action: FileController.upload,
|
||||
},
|
||||
];
|
54
tests/server/service/FileService.ts
Normal file
54
tests/server/service/FileService.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
|
||||
const uploadUrl = 'http://localhost:3300/static/upload';
|
||||
const filePath = path.join(__dirname, '../static/upload/');
|
||||
|
||||
fs.ensureDir(filePath);
|
||||
export default class UserService {
|
||||
async upload(ctx, files, isMultiple) {
|
||||
let fileReader, fileResource, writeStream;
|
||||
|
||||
const fileFunc = function (file) {
|
||||
fileReader = fs.createReadStream(file.path);
|
||||
fileResource = filePath + `/${file.name}`;
|
||||
console.log(fileResource);
|
||||
|
||||
writeStream = fs.createWriteStream(fileResource);
|
||||
fileReader.pipe(writeStream);
|
||||
};
|
||||
|
||||
const returnFunc = function (flag) {
|
||||
if (flag) {
|
||||
let url = '';
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
url += uploadUrl + `/${files[i].name},`;
|
||||
}
|
||||
url = url.replace(/,$/gi, '');
|
||||
ctx.body = {
|
||||
url: url,
|
||||
code: 0,
|
||||
message: 'upload Success!',
|
||||
};
|
||||
} else {
|
||||
ctx.body = {
|
||||
url: uploadUrl + `/${files.name}`,
|
||||
code: 0,
|
||||
message: 'upload Success!',
|
||||
};
|
||||
}
|
||||
};
|
||||
console.log(isMultiple, files.length);
|
||||
|
||||
if (isMultiple) {
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const f1 = files[i];
|
||||
fileFunc(f1);
|
||||
}
|
||||
} else {
|
||||
fileFunc(files);
|
||||
}
|
||||
fs.ensureDir(filePath);
|
||||
returnFunc(isMultiple);
|
||||
}
|
||||
}
|
25
tests/server/service/UserService.ts
Normal file
25
tests/server/service/UserService.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { Result } from '../utils';
|
||||
|
||||
const fakeUserInfo = {
|
||||
userId: '1',
|
||||
username: 'vben',
|
||||
realName: 'Vben Admin',
|
||||
desc: 'manager',
|
||||
password: '123456',
|
||||
token: 'fakeToken1',
|
||||
roles: [
|
||||
{
|
||||
roleName: 'Super Admin',
|
||||
value: 'super',
|
||||
},
|
||||
],
|
||||
};
|
||||
export default class UserService {
|
||||
async login() {
|
||||
return Result.success(fakeUserInfo);
|
||||
}
|
||||
|
||||
async getUserInfoById() {
|
||||
return Result.success(fakeUserInfo);
|
||||
}
|
||||
}
|
BIN
tests/server/static/upload/11.jpg
Normal file
BIN
tests/server/static/upload/11.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 215 KiB |
BIN
tests/server/static/upload/5ab46a3cN616bdc41.jpg
Normal file
BIN
tests/server/static/upload/5ab46a3cN616bdc41.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 114 KiB |
BIN
tests/server/static/upload/5ac1bf5fN2522b9dc.jpg
Normal file
BIN
tests/server/static/upload/5ac1bf5fN2522b9dc.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 405 KiB |
BIN
tests/server/static/upload/5c9ccca8a27f0.png
Normal file
BIN
tests/server/static/upload/5c9ccca8a27f0.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.7 KiB |
BIN
tests/server/static/upload/5c9ccca8b27f1.jpg
Normal file
BIN
tests/server/static/upload/5c9ccca8b27f1.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 38 KiB |
BIN
tests/server/static/upload/5c9ccca8bc1e0.png
Normal file
BIN
tests/server/static/upload/5c9ccca8bc1e0.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.1 KiB |
15
tests/server/tsconfig.json
Normal file
15
tests/server/tsconfig.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"declaration": false,
|
||||
"removeComments": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
"target": "es6",
|
||||
"sourceMap": false,
|
||||
"esModuleInterop": true,
|
||||
"outDir": "./dist",
|
||||
"baseUrl": "./"
|
||||
},
|
||||
"exclude": ["node_modules"]
|
||||
}
|
9
tests/server/utils.ts
Normal file
9
tests/server/utils.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export class Result {
|
||||
static success(data: any) {
|
||||
return {
|
||||
code: 0,
|
||||
success: true,
|
||||
result: data,
|
||||
};
|
||||
}
|
||||
}
|
2955
tests/server/yarn.lock
Normal file
2955
tests/server/yarn.lock
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user