Skip to content

Commit

Permalink
test(data-source,utils): 生成默认值函数放到utils库中
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen committed Oct 2, 2023
1 parent 9072642 commit 65854d9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 78 deletions.
78 changes: 0 additions & 78 deletions packages/data-source/tests/utils.spec.ts

This file was deleted.

75 changes: 75 additions & 0 deletions packages/utils/tests/unit/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import { describe, expect, test } from 'vitest';

import type { DataSchema } from '@tmagic/schema';

import * as util from '../../src';

describe('datetimeFormatter', () => {
Expand Down Expand Up @@ -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');
});
});

0 comments on commit 65854d9

Please sign in to comment.