roymondchen de47514f69 feat: 完善迭代器嵌套使用问题,重构事件配置处理代码
* feat(editor,core,data-source,dep,schema,ui,utils,vue-runtime-help): 完善迭代器

* test: 完善测试用例

* chore: 构建

* feat: 迭代器嵌套事件传递数据

---------

Co-authored-by: roymondchen <roymondchen@tencent.com>
2024-12-09 20:21:34 +08:00

114 lines
2.2 KiB
TypeScript

import { describe, expect, test } from 'vitest';
import App from '@tmagic/core';
import { DataSource } from '@data-source/index';
describe('DataSource', () => {
test('instance', () => {
const ds = new DataSource({
schema: {
type: 'base',
id: '1',
fields: [{ name: 'name' }],
methods: [],
events: [],
},
app: new App({}),
});
expect(ds).toBeInstanceOf(DataSource);
expect(ds.data).toHaveProperty('name');
});
test('init', () => {
const ds = new DataSource({
schema: {
type: 'base',
id: '1',
fields: [{ name: 'name' }],
methods: [],
events: [],
},
app: new App({}),
});
ds.init();
expect(ds.isInit).toBeTruthy();
});
});
describe('DataSource setData', () => {
test('setData', () => {
const ds = new DataSource({
schema: {
type: 'base',
id: '1',
fields: [{ name: 'name', defaultValue: 'name' }],
methods: [],
events: [],
},
app: new App({}),
});
ds.init();
expect(ds.data.name).toBe('name');
ds.setData({ name: 'name2' });
expect(ds.data.name).toBe('name2');
ds.setData('name3', 'name');
expect(ds.data.name).toBe('name3');
});
test('setDataByPath', () => {
const ds = new DataSource({
schema: {
type: 'base',
id: '1',
fields: [
{ name: 'name', defaultValue: 'name' },
{
name: 'obj',
type: 'object',
fields: [{ name: 'a' }, { name: 'b', type: 'array', fields: [{ name: 'c' }] }],
},
],
methods: [],
events: [],
},
app: new App({}),
});
ds.init();
expect(ds.data.name).toBe('name');
expect(ds.data.obj.b).toHaveLength(0);
ds.setData({
name: 'name',
obj: {
a: 'a',
b: [
{
c: 'c',
},
],
},
});
expect(ds.data.obj.b).toHaveLength(1);
expect(ds.data.obj.b[0].c).toBe('c');
ds.setData('c1', 'obj.b.0.c');
expect(ds.data.obj.b[0].c).toBe('c1');
expect(ds.data.obj.a).toBe('a');
ds.setData('a1', 'obj.a');
expect(ds.data.obj.a).toBe('a1');
});
});