From 65854d9c0a2bf61207e68ae560594623e1030c19 Mon Sep 17 00:00:00 2001 From: roymondchen Date: Mon, 2 Oct 2023 17:14:14 +0800 Subject: [PATCH] =?UTF-8?q?test(data-source,utils):=20=E7=94=9F=E6=88=90?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=80=BC=E5=87=BD=E6=95=B0=E6=94=BE=E5=88=B0?= =?UTF-8?q?utils=E5=BA=93=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/data-source/tests/utils.spec.ts | 78 ------------------------ packages/utils/tests/unit/index.spec.ts | 75 +++++++++++++++++++++++ 2 files changed, 75 insertions(+), 78 deletions(-) delete mode 100644 packages/data-source/tests/utils.spec.ts diff --git a/packages/data-source/tests/utils.spec.ts b/packages/data-source/tests/utils.spec.ts deleted file mode 100644 index 4cfe83a2e..000000000 --- a/packages/data-source/tests/utils.spec.ts +++ /dev/null @@ -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'); - }); -}); diff --git a/packages/utils/tests/unit/index.spec.ts b/packages/utils/tests/unit/index.spec.ts index 791a0ce7e..739f7db62 100644 --- a/packages/utils/tests/unit/index.spec.ts +++ b/packages/utils/tests/unit/index.spec.ts @@ -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'); + }); +});