1
0
mirror of synced 2026-03-22 18:48:34 +08:00

test: vue组件测试用例

This commit is contained in:
roymondchen
2022-09-26 21:28:42 +08:00
committed by jia000
parent 3570a67f30
commit ac065504f6
29 changed files with 504 additions and 769 deletions

View File

@@ -1,68 +0,0 @@
/*
* Tencent is pleased to support the open source community by making TMagicEditor 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 { describe, expect, test, vi } from 'vitest';
import { mount } from '@vue/test-utils';
import ElementPlus from 'element-plus';
import MagicForm from '@tmagic/form';
import { NodeType } from '@tmagic/schema';
import Editor from '@editor/Editor.vue';
vi.mock('@editor/utils/logger', () => ({
log: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
debug: vi.fn(),
error: vi.fn(),
}));
globalThis.ResizeObserver =
globalThis.ResizeObserver ||
vi.fn().mockImplementation(() => ({
disconnect: vi.fn(),
observe: vi.fn(),
unobserve: vi.fn(),
}));
describe.skip('编辑器', () => {
test('初始化', () => {
const wrapper = mount(Editor as any, {
global: {
plugins: [ElementPlus as any, MagicForm as any],
},
props: {
modelValue: {
type: NodeType.ROOT,
id: 1,
name: 'app',
items: [
{
type: NodeType.PAGE,
id: 2,
name: NodeType.PAGE,
items: [],
},
],
},
runtimeUrl: 'https://m.film.qq.com/magic-ui/production/1/1625056093304/magic/magic-ui.umd.min.js',
},
});
expect(wrapper.exists()).toBe(true);
});
});

View File

@@ -17,6 +17,7 @@
*/
import { describe, expect, test, vi } from 'vitest';
import { nextTick } from 'vue';
import { mount } from '@vue/test-utils';
import ElementPlus, { ElDropdown } from 'element-plus';
@@ -71,51 +72,7 @@ const getWrapper = (
},
});
describe.skip('ToolButton', () => {
test('删除', async () => {
const wrapper = getWrapper();
const icon = wrapper.find('.el-button');
await icon.trigger('click');
expect(editorService.remove.mock.calls[0][0]).toBe('node');
});
test('后退', async () => {
const wrapper = getWrapper({ data: 'undo' });
const icon = wrapper.find('.el-button');
await icon.trigger('click');
expect(editorService.undo).toBeCalled();
});
test('前进', async () => {
const wrapper = getWrapper({ data: 'redo' });
const icon = wrapper.find('.el-button');
await icon.trigger('click');
expect(editorService.redo).toBeCalled();
});
test('放大', async () => {
uiService.set('zoom', 1);
const wrapper = getWrapper({ data: 'zoom-in' });
const icon = wrapper.find('.el-button');
await icon.trigger('click');
expect(uiService.get('zoom')).toBe(1.1);
});
test('缩小', (done) => {
uiService.set('zoom', 1);
const wrapper = getWrapper({ data: 'zoom-out' });
setTimeout(async () => {
const icon = wrapper.find('.el-button');
await icon.trigger('click');
expect(uiService.get('zoom')).toBe(0.9);
done();
}, 0);
});
describe('ToolButton', () => {
test('data无匹配值', () => {
getWrapper({ data: 'default' });
});
@@ -128,25 +85,24 @@ describe.skip('ToolButton', () => {
expect(display).toBeCalled();
});
test('点击下拉菜单项', (done) => {
test('点击下拉菜单项', async () => {
const wrapper = getWrapper({
data: {
type: 'dropdown',
},
});
setTimeout(async () => {
const dropDown = wrapper.findComponent(ElDropdown);
const handler = vi.fn();
dropDown.vm.$emit('command', {
item: { handler },
});
expect(handler).toBeCalled();
done();
}, 0);
await nextTick();
const dropDown = wrapper.findComponent(ElDropdown);
const handler = vi.fn();
dropDown.vm.$emit('command', {
item: { handler },
});
expect(handler).toBeCalled();
});
test('按钮不可用', (done) => {
test('按钮不可用', async () => {
const wrapper = getWrapper({
data: {
icon: 'disabled-icon',
@@ -155,12 +111,11 @@ describe.skip('ToolButton', () => {
},
});
setTimeout(async () => {
// disabled 后会有 is-disabled class
const iconBtn = wrapper.find('.el-button.is-text.is-disabled');
await iconBtn.trigger('click');
done();
}, 0);
await nextTick();
// disabled 后会有 is-disabled class
const iconBtn = wrapper.find('.el-button.is-text.is-disabled');
expect(iconBtn).toBeDefined();
});
test('菜单项handler未定义', () => {

View File

@@ -1,77 +0,0 @@
/*
* Tencent is pleased to support the open source community by making TMagicEditor 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 { describe, expect, test, vi } from 'vitest';
import { mount } from '@vue/test-utils';
import ElementPlus from 'element-plus';
import { NodeType } from '@tmagic/schema';
import PageBar from '@editor/layouts/workspace/PageBar.vue';
globalThis.ResizeObserver =
globalThis.ResizeObserver ||
vi.fn().mockImplementation(() => ({
disconnect: vi.fn(),
observe: vi.fn(),
unobserve: vi.fn(),
}));
const editorState: Record<string, any> = {
root: {
items: [{ key: 0, id: 1, name: 'testName', type: NodeType.PAGE }],
},
page: { id: 1, type: NodeType.PAGE },
};
const editorService = {
get: vi.fn((key: string) => editorState[key]),
add: vi.fn(),
set: vi.fn(),
select: vi.fn(),
};
const getWrapper = () =>
mount(PageBar as any, {
global: {
plugins: [ElementPlus as any],
provide: {
services: {
editorService,
},
},
},
});
describe.skip('PageBar', () => {
test('新增page', async () => {
const wrapper = getWrapper();
await wrapper.find('#m-editor-page-bar-add-icon').trigger('click');
expect(editorService.add.mock.calls[0][0]).toEqual({
type: NodeType.PAGE,
name: 'page_1',
});
});
test('切换page', async () => {
const wrapper = getWrapper();
await wrapper.find('div[class="m-editor-page-bar-item active"]').trigger('click');
expect(editorService.set.mock.calls).toEqual([]);
});
});

View File

@@ -19,7 +19,7 @@
import { beforeAll, describe, expect, test } from 'vitest';
import { cloneDeep } from 'lodash-es';
import type { MApp, MContainer, MNode, MPage } from '@tmagic/schema';
import type { MApp, MContainer, MNode } from '@tmagic/schema';
import { NodeType } from '@tmagic/schema';
import editorService from '@editor/services/editor';
@@ -365,40 +365,6 @@ describe('copy', () => {
});
});
describe('paste', () => {
beforeAll(() => editorService.set('root', cloneDeep(root)));
test.skip('正常', async () => {
editorService.set('root', cloneDeep(root));
// 设置当前编辑的页面
await editorService.select(NodeId.PAGE_ID);
const page = editorService.get<MPage>('page');
expect(page.items).toHaveLength(2);
const newNodes = (await editorService.paste({ left: 0, top: 0 })) as MNode[];
expect(newNodes[0]?.id === NodeId.NODE_ID2).toBeFalsy();
expect(page.items).toHaveLength(3);
});
test('空', async () => {
await storageService.clear();
const newNode = await editorService.paste({ left: 0, top: 0 });
expect(newNode).toBeUndefined();
});
});
describe('alignCenter', () => {
beforeAll(() => editorService.set('root', cloneDeep(root)));
test.skip('正常', async () => {
// 设置当前编辑的页面
await editorService.select(NodeId.PAGE_ID);
await editorService.update({ id: NodeId.PAGE_ID, isAbsoluteLayout: true, type: NodeType.PAGE });
await editorService.select(NodeId.NODE_ID);
const node = editorService.get<MNode>('node');
await editorService.alignCenter(node);
expect(node.style?.left).toBeGreaterThan(0);
});
});
describe('moveLayer', () => {
beforeAll(() => editorService.set('root', cloneDeep(root)));
@@ -430,15 +396,3 @@ describe('undo redo', () => {
expect(redoNode?.id).toBeUndefined();
});
});
describe('use', () => {
beforeAll(() => editorService.set('root', cloneDeep(root)));
test.skip('before', () => {
editorService.usePlugin({
beforeRemove: () => new Error('不能删除'),
});
expect(() => editorService.remove({ id: NodeId.NODE_ID, type: 'text' })).toThrow();
});
});

View File

@@ -7,8 +7,6 @@ import props from '@editor/services/props';
test('createId', async () => {
const id = await props.createId('text');
console.log(id);
expect(id.startsWith('text')).toBeTruthy();
});