add test for parseTypeBox

This commit is contained in:
Bowen Tan 2021-09-13 16:59:59 +08:00
parent 8ff0c996d4
commit 01318d0c2a
7 changed files with 76 additions and 50 deletions

7
babel.config.js Normal file
View File

@ -0,0 +1,7 @@
// only for jest
module.exports = {
presets: [
['@babel/preset-env', { targets: { node: 'current' } }],
'@babel/preset-typescript',
],
};

View 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: [] });
});
});

View File

@ -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: {

View File

@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useState } from 'react';
import React from 'react';
type TablePaginationProps = {
pageNumber: number;

View File

@ -24,7 +24,7 @@ export default {
...DataPropertySchema,
},
{
name: 'marjorKey',
name: 'majorKey',
...MajorKeyPropertySchema,
},
{

View File

@ -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']

View 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;
}
}