test(data-source,utils): 生成默认值函数放到utils库中

This commit is contained in:
roymondchen 2023-10-02 17:14:14 +08:00
parent 9072642f22
commit 65854d9c0a
2 changed files with 75 additions and 78 deletions

View File

@ -1,78 +0,0 @@
import { describe, expect, test } from 'vitest';
import { DataSchema } from '@tmagic/schema';
import * as util from '@data-source/util';
describe('getDefaultValueFromFields', () => {
test('最简单', () => {
const fileds = [
{
name: 'name',
},
];
const data = util.getDefaultValueFromFields(fileds);
expect(data).toHaveProperty('name');
});
test('默认值为string', () => {
const fileds = [
{
name: 'name',
defaultValue: 'name',
},
];
const data = util.getDefaultValueFromFields(fileds);
expect(data.name).toBe('name');
});
test('type 为 object', () => {
const fileds: DataSchema[] = [
{
type: 'object',
name: 'name',
},
];
const data = util.getDefaultValueFromFields(fileds);
expect(data.name).toEqual({});
});
test('type 为 array', () => {
const fileds: DataSchema[] = [
{
type: 'array',
name: 'name',
},
];
const data = util.getDefaultValueFromFields(fileds);
expect(data.name).toEqual([]);
});
test('type 为 null', () => {
const fileds: DataSchema[] = [
{
type: 'null',
name: 'name',
},
];
const data = util.getDefaultValueFromFields(fileds);
expect(data.name).toBeNull();
});
test('object 嵌套', () => {
const fileds: DataSchema[] = [
{
type: 'object',
name: 'name',
fields: [
{
name: 'key',
defaultValue: 'key',
},
],
},
];
const data = util.getDefaultValueFromFields(fileds);
expect(data.name.key).toBe('key');
});
});

View File

@ -18,6 +18,8 @@
import { describe, expect, test } from 'vitest';
import type { DataSchema } from '@tmagic/schema';
import * as util from '../../src';
describe('datetimeFormatter', () => {
@ -590,3 +592,76 @@ describe('compiledNode', () => {
expect(node.text).toBe('');
});
});
describe('getDefaultValueFromFields', () => {
test('最简单', () => {
const fileds = [
{
name: 'name',
},
];
const data = util.getDefaultValueFromFields(fileds);
expect(data).toHaveProperty('name');
});
test('默认值为string', () => {
const fileds = [
{
name: 'name',
defaultValue: 'name',
},
];
const data = util.getDefaultValueFromFields(fileds);
expect(data.name).toBe('name');
});
test('type 为 object', () => {
const fileds: DataSchema[] = [
{
type: 'object',
name: 'name',
},
];
const data = util.getDefaultValueFromFields(fileds);
expect(data.name).toEqual({});
});
test('type 为 array', () => {
const fileds: DataSchema[] = [
{
type: 'array',
name: 'name',
},
];
const data = util.getDefaultValueFromFields(fileds);
expect(data.name).toEqual([]);
});
test('type 为 null', () => {
const fileds: DataSchema[] = [
{
type: 'null',
name: 'name',
},
];
const data = util.getDefaultValueFromFields(fileds);
expect(data.name).toBeNull();
});
test('object 嵌套', () => {
const fileds: DataSchema[] = [
{
type: 'object',
name: 'name',
fields: [
{
name: 'key',
defaultValue: 'key',
},
],
},
];
const data = util.getDefaultValueFromFields(fileds);
expect(data.name.key).toBe('key');
});
});