feat: 新增管理端demo代码
feat: 补充遗漏的文件 fix: 移除license
This commit is contained in:
129
magic-admin/server/tests/unit/act.spec.ts
Normal file
129
magic-admin/server/tests/unit/act.spec.ts
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making MagicEditor available.
|
||||
*
|
||||
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import 'regenerator-runtime/runtime';
|
||||
import actController from '@src/controller/act';
|
||||
|
||||
jest.mock('@src/service/act', () =>
|
||||
jest.fn().mockImplementation(() => ({
|
||||
getActList: jest.fn(() => []),
|
||||
getCount: jest.fn(() => 0),
|
||||
create: jest.fn(() => 1),
|
||||
copy: jest.fn(),
|
||||
getActInfo: jest.fn((id) => ({ actId: id })),
|
||||
})),
|
||||
);
|
||||
|
||||
const mockCtx = {
|
||||
request: {
|
||||
body: {
|
||||
data: '{}',
|
||||
},
|
||||
},
|
||||
body: {
|
||||
data: [],
|
||||
total: 0,
|
||||
fetch: false,
|
||||
errorMsg: '',
|
||||
},
|
||||
};
|
||||
|
||||
describe('act controller测试', () => {
|
||||
it('获取活动列表', async () => {
|
||||
await actController.getList(mockCtx);
|
||||
|
||||
expect(mockCtx.body).toEqual({
|
||||
data: [],
|
||||
total: 0,
|
||||
fetch: true,
|
||||
errorMsg: '',
|
||||
});
|
||||
});
|
||||
|
||||
it('获取活动列表异常', async () => {
|
||||
const mockErroCtx = { body: {}, logger:{error:jest.fn()} };
|
||||
await actController.getList(mockErroCtx);
|
||||
expect(mockErroCtx.body).toEqual({
|
||||
data: [],
|
||||
total: 0,
|
||||
fetch: false,
|
||||
errorMsg: "Cannot read property 'body' of undefined",
|
||||
});
|
||||
});
|
||||
|
||||
it('新建活动', async () => {
|
||||
await actController.create(mockCtx);
|
||||
expect(mockCtx.body).toEqual({
|
||||
ret: 0,
|
||||
msg: '新建活动成功',
|
||||
data: { actId: 1 },
|
||||
});
|
||||
});
|
||||
|
||||
it('新建活动异常', async () => {
|
||||
const mockErroCtx = { body: {}};
|
||||
await actController.create(mockErroCtx);
|
||||
expect(mockErroCtx.body).toEqual({
|
||||
ret: -1,
|
||||
msg: "Cannot read property 'body' of undefined",
|
||||
});
|
||||
});
|
||||
|
||||
it('复制活动', async () => {
|
||||
await actController.copy(mockCtx);
|
||||
expect(mockCtx.body).toEqual({
|
||||
ret: 0,
|
||||
msg: '复制成功',
|
||||
});
|
||||
});
|
||||
|
||||
it('复制活动异常', async () => {
|
||||
const mockErroCtx = { body: {} };
|
||||
await actController.copy(mockErroCtx);
|
||||
expect(mockErroCtx.body).toEqual({
|
||||
ret: -1,
|
||||
msg: "Cannot read property 'body' of undefined",
|
||||
});
|
||||
});
|
||||
|
||||
it('查询活动详情', async () => {
|
||||
const getInfoCtx = {
|
||||
query: {
|
||||
id: '1',
|
||||
},
|
||||
body: {},
|
||||
};
|
||||
await actController.getInfo(getInfoCtx);
|
||||
expect(getInfoCtx.body).toEqual({
|
||||
ret: 0,
|
||||
msg: '获取活动信息成功',
|
||||
data: { actId: 1 },
|
||||
});
|
||||
});
|
||||
|
||||
it('查询活动详情异常', async () => {
|
||||
const getInfoErrorCtx = {
|
||||
body: {},
|
||||
};
|
||||
await actController.getInfo(getInfoErrorCtx);
|
||||
expect(getInfoErrorCtx.body).toEqual({
|
||||
ret: -1,
|
||||
msg: "Cannot read property 'id' of undefined",
|
||||
});
|
||||
});
|
||||
});
|
||||
56
magic-admin/server/tests/unit/editor.spec.ts
Normal file
56
magic-admin/server/tests/unit/editor.spec.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making MagicEditor available.
|
||||
*
|
||||
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import 'regenerator-runtime/runtime';
|
||||
import editorController from '@src/controller/editor';
|
||||
|
||||
jest.mock('@src/service/editor', () =>
|
||||
jest.fn().mockImplementation(() => ({
|
||||
getComponentList: jest.fn(() => []),
|
||||
getWebPlugins: jest.fn(() => []),
|
||||
})),
|
||||
);
|
||||
|
||||
const mockCtx = {
|
||||
request: {
|
||||
body: {
|
||||
data: '{}',
|
||||
},
|
||||
},
|
||||
body: {
|
||||
data: [],
|
||||
total: 0,
|
||||
fetch: false,
|
||||
errorMsg: '',
|
||||
},
|
||||
};
|
||||
|
||||
describe('editor controller测试', () => {
|
||||
it('获取组件列表', async () => {
|
||||
await editorController.getComponentList(mockCtx);
|
||||
expect(mockCtx.body).toEqual({
|
||||
ret: 0,
|
||||
msg: '获取组件列表成功',
|
||||
data: [],
|
||||
});
|
||||
});
|
||||
it('获取web插件', async () => {
|
||||
await editorController.getWebPlugins(mockCtx);
|
||||
expect(mockCtx.body).toEqual([]);
|
||||
});
|
||||
});
|
||||
138
magic-admin/server/tests/unit/publish.spec.ts
Normal file
138
magic-admin/server/tests/unit/publish.spec.ts
Normal file
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making MagicEditor available.
|
||||
*
|
||||
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import 'regenerator-runtime/runtime';
|
||||
import publishController from '@src/controller/publish';
|
||||
import serialize from 'serialize-javascript';
|
||||
jest.mock('@src/service/publish', () =>
|
||||
jest.fn().mockImplementation(() => ({
|
||||
publish: jest.fn((res) => {
|
||||
res.ret = 0;
|
||||
res.msg = '发布成功';
|
||||
return res;
|
||||
}),
|
||||
saveActInfo: jest.fn((res) => {
|
||||
res.ret = 0;
|
||||
res.msg = '保存成功';
|
||||
return res;
|
||||
}),
|
||||
'ctx.cookie.get': jest.fn(() => {}),
|
||||
})),
|
||||
);
|
||||
const serializeConfig = (value): string =>
|
||||
serialize(value, {
|
||||
space: 2,
|
||||
unsafe: true,
|
||||
}).replace(/"(\w+)":\s/g, '$1: ');
|
||||
|
||||
const mockSaveData = {
|
||||
actId: 1,
|
||||
type: 'app',
|
||||
items: [],
|
||||
};
|
||||
const mockPublishData = {
|
||||
actId: '123',
|
||||
publishPages: ['index'],
|
||||
rootInfo: '',
|
||||
};
|
||||
const uiConfigStr = serializeConfig(mockSaveData);
|
||||
const publishDataStr = serializeConfig(mockPublishData);
|
||||
|
||||
describe('保存与发布', () => {
|
||||
it('保存成功', async () => {
|
||||
const ctx = {
|
||||
request: {
|
||||
body: {
|
||||
data: uiConfigStr,
|
||||
},
|
||||
},
|
||||
body: {
|
||||
data: [],
|
||||
total: 0,
|
||||
fetch: false,
|
||||
errorMsg: '',
|
||||
},
|
||||
};
|
||||
await publishController.saveActInfo(ctx);
|
||||
expect(ctx.body).toEqual({
|
||||
ret: 0,
|
||||
msg: '保存成功',
|
||||
});
|
||||
});
|
||||
it('保存失败', async () => {
|
||||
const ctx = {
|
||||
request: {},
|
||||
body: {},
|
||||
};
|
||||
await publishController.saveActInfo(ctx);
|
||||
expect(ctx.body).toEqual({
|
||||
ret: -1,
|
||||
msg: "Cannot read property 'data' of undefined",
|
||||
});
|
||||
});
|
||||
it('发布成功', async () => {
|
||||
const ctx = {
|
||||
request: {
|
||||
body: {
|
||||
data: publishDataStr,
|
||||
},
|
||||
},
|
||||
body: {
|
||||
data: [],
|
||||
total: 0,
|
||||
fetch: false,
|
||||
errorMsg: '',
|
||||
},
|
||||
cookies: {
|
||||
get(key) {
|
||||
if (key === 'user') return 'default';
|
||||
},
|
||||
},
|
||||
};
|
||||
await publishController.publish(ctx);
|
||||
expect(ctx.body).toEqual({
|
||||
ret: 0,
|
||||
msg: '发布成功',
|
||||
});
|
||||
});
|
||||
it('发布失败', async () => {
|
||||
const ctx = {
|
||||
request: {
|
||||
body: {
|
||||
data: '',
|
||||
},
|
||||
},
|
||||
body: {
|
||||
data: [],
|
||||
total: 0,
|
||||
fetch: false,
|
||||
errorMsg: '',
|
||||
},
|
||||
cookies: {
|
||||
get(key) {
|
||||
if (key === 'user') return 'default';
|
||||
},
|
||||
},
|
||||
};
|
||||
await publishController.publish(ctx);
|
||||
expect(ctx.body).toEqual({
|
||||
ret: -1,
|
||||
msg: "Unexpected token ')'",
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user