mirror of
https://github.com/smartxworks/sunmao-ui.git
synced 2025-01-24 17:03:56 +08:00
add test for parseTypeBox
This commit is contained in:
parent
8ff0c996d4
commit
01318d0c2a
7
babel.config.js
Normal file
7
babel.config.js
Normal file
@ -0,0 +1,7 @@
|
||||
// only for jest
|
||||
module.exports = {
|
||||
presets: [
|
||||
['@babel/preset-env', { targets: { node: 'current' } }],
|
||||
'@babel/preset-typescript',
|
||||
],
|
||||
};
|
25
packages/runtime/__tests__/parseTypeBox.spec.ts
Normal file
25
packages/runtime/__tests__/parseTypeBox.spec.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { Type } from '@sinclair/typebox';
|
||||
import { m } from 'framer-motion';
|
||||
import { parseTypeBox } from '../src/utils/parseTypeBox';
|
||||
|
||||
describe('parseTypeBox function', () => {
|
||||
it('can parse array', () => {
|
||||
const type = Type.Array(Type.Object({}));
|
||||
expect(parseTypeBox(type)).toMatchObject([]);
|
||||
});
|
||||
it('can parse number', () => {
|
||||
const type = Type.Number();
|
||||
expect(parseTypeBox(type)).toEqual(0);
|
||||
});
|
||||
it('can parse optional', () => {
|
||||
const type = Type.Optional(Type.String());
|
||||
expect(parseTypeBox(type)).toEqual(undefined);
|
||||
});
|
||||
it('can parse object', () => {
|
||||
const type = Type.Object({
|
||||
key: Type.String(),
|
||||
value: Type.Array(Type.String()),
|
||||
});
|
||||
expect(parseTypeBox(type)).toMatchObject({ key: '', value: [] });
|
||||
});
|
||||
});
|
@ -41,10 +41,6 @@ export const ImplWrapper = React.forwardRef<
|
||||
globalHandlerMap.set(c.id, {});
|
||||
}
|
||||
|
||||
if (!stateStore[c.id]) {
|
||||
stateStore[c.id] = {};
|
||||
}
|
||||
|
||||
let handlerMap = globalHandlerMap.get(c.id)!;
|
||||
useEffect(() => {
|
||||
const handler = (s: {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import React, { useCallback, useEffect, useState } from 'react';
|
||||
import React from 'react';
|
||||
|
||||
type TablePaginationProps = {
|
||||
pageNumber: number;
|
||||
|
@ -24,7 +24,7 @@ export default {
|
||||
...DataPropertySchema,
|
||||
},
|
||||
{
|
||||
name: 'marjorKey',
|
||||
name: 'majorKey',
|
||||
...MajorKeyPropertySchema,
|
||||
},
|
||||
{
|
||||
|
@ -1,50 +1,8 @@
|
||||
import {
|
||||
ArrayKind,
|
||||
BooleanKind,
|
||||
IntegerKind,
|
||||
NumberKind,
|
||||
ObjectKind,
|
||||
Static,
|
||||
StringKind,
|
||||
TSchema,
|
||||
OptionalModifier,
|
||||
} from '@sinclair/typebox';
|
||||
import { TSchema } from '@sinclair/typebox';
|
||||
import { RuntimeApplication } from '../../../core/typings';
|
||||
import { registry } from '../registry';
|
||||
import { stateStore } from '../store';
|
||||
|
||||
function parseTypeBox(tSchema: TSchema): Static<typeof tSchema> {
|
||||
if (tSchema.modifier === OptionalModifier) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
switch (tSchema.kind) {
|
||||
case StringKind:
|
||||
return '';
|
||||
case BooleanKind:
|
||||
return false;
|
||||
case ArrayKind:
|
||||
return [];
|
||||
case NumberKind:
|
||||
return 0;
|
||||
case IntegerKind:
|
||||
return 0;
|
||||
case ArrayKind:
|
||||
return [];
|
||||
case ArrayKind:
|
||||
return [];
|
||||
|
||||
case ObjectKind:
|
||||
const obj: Static<typeof tSchema> = {};
|
||||
for (let key in tSchema.properties) {
|
||||
obj[key] = parseTypeBox(tSchema.properties[key]);
|
||||
}
|
||||
return obj;
|
||||
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
import { parseTypeBox } from './parseTypeBox';
|
||||
|
||||
export function initStateAndMethod(
|
||||
components: RuntimeApplication['spec']['components']
|
||||
|
40
packages/runtime/src/utils/parseTypeBox.ts
Normal file
40
packages/runtime/src/utils/parseTypeBox.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import {
|
||||
ArrayKind,
|
||||
BooleanKind,
|
||||
IntegerKind,
|
||||
NumberKind,
|
||||
ObjectKind,
|
||||
Static,
|
||||
StringKind,
|
||||
TSchema,
|
||||
OptionalModifier,
|
||||
} from '@sinclair/typebox';
|
||||
|
||||
export function parseTypeBox(tSchema: TSchema): Static<typeof tSchema> {
|
||||
if (tSchema.modifier === OptionalModifier) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
switch (tSchema.kind) {
|
||||
case StringKind:
|
||||
return '';
|
||||
case BooleanKind:
|
||||
return false;
|
||||
case ArrayKind:
|
||||
return [];
|
||||
case NumberKind:
|
||||
return 0;
|
||||
case IntegerKind:
|
||||
return 0;
|
||||
|
||||
case ObjectKind:
|
||||
const obj: Static<typeof tSchema> = {};
|
||||
for (let key in tSchema.properties) {
|
||||
obj[key] = parseTypeBox(tSchema.properties[key]);
|
||||
}
|
||||
return obj;
|
||||
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user