Skip to content

Commit

Permalink
test(json-schema): add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Feb 1, 2021
1 parent 2b623ba commit 6813974
Show file tree
Hide file tree
Showing 12 changed files with 346 additions and 66 deletions.
80 changes: 60 additions & 20 deletions .umirc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@ export default {
path: '/guide',
},
{
title: '核心库API',
path: '/api',
title: '核心库',
children: [
{
title: 'Core API',
title: '基础内核层',
path: '//core.formilyjs.org',
},
{
title: 'React API',
title: 'React桥接层',
path: '//react.formilyjs.org',
},
{
title: 'Vue API',
title: 'Vue桥接层',
path: '//vue.formilyjs.org',
},
],
Expand Down Expand Up @@ -80,60 +79,85 @@ export default {
title: '介绍',
path: '/guide',
},
{
title: '如何看文档',
path: '/guide',
},

{
title: '概念',
path: '/guide/concept/mvvm',
children: [
{
title: 'MVVM',
title: '表单架构',
path: '/guide/concept/mvvm',
},
{
title: '充血模型',
title: '领域模型',
path: '/guide/concept/model',
},
{
title: '生命周期',
title: '表单生命周期',
path: '/guide/concept/lifecycle',
},
{
title: '路径系统',
title: '表单路径系统',
path: '/guide/concept/path',
},
{
title: 'JSON Schema',
title: '表单值管理',
path: '/guide/concept/model',
},
{
title: '表单协议标准',
path: '/guide/concept/json-schema',
},
{
title: '三种研发模式',
path: '/guide/concept/json-schema',
},
{
title: '表单扩展机制',
path: '/guide/concept/json-schema',
},
],
},
{
title: '开发实践',
path: '/guide/practice/jsx-develop',
title: '场景案例',
children: [
{
title: '纯JSX驱动开发',
path: '/guide/practice/jsx-develop',
title: '登陆表单',
},
{
title: 'JSON Schema驱动开发',
path: '/guide/practice/schema-develop',
title: '查询列表',
},
{
title: 'JSX Schema驱动开发',
path: '/guide/practice/jsx-schema-develop',
title: '编辑与详情',
},
{
title: '混合开发',
path: '/guide/practice/mixin-develop',
title: '弹窗与抽屉',
},
],
},
{
title: '进阶指南',
children: [
{
title: '实现表单校验',
path: '/guide/practice/validations',
},
{
title: '实现表单布局',
path: '/guide/practice/validations',
},
{
title: '实现异步数据源',
path: '/guide/practice/asyncs',
},
{
title: '实现值受控',
path: '/guide/practice/validations',
},
{
title: '实现联动逻辑',
path: '/guide/practice/linkages',
Expand All @@ -150,8 +174,24 @@ export default {
title: '管理业务逻辑',
path: '/guide/practice/best-practice',
},
{
title: '国际化',
path: '/guide/practice/validations',
},
{
title: '按需打包',
path: '/guide/practice/validations',
},
],
},
{
title: 'v2升级指南',
path: '/guide',
},
{
title: '贡献指南',
path: '/guide',
},
],
},
}
7 changes: 6 additions & 1 deletion packages/json-schema/src/__tests__/patches.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Schema } from '../schema'
import {
registerTypeDefaultComponents,
registerVoidComponents,
} from '../patches'
} from '../polyfills'

registerVoidComponents(['MyCard'])
registerTypeDefaultComponents({
Expand Down Expand Up @@ -153,4 +153,9 @@ test('v1 polyfill', () => {
'x-component': 'MyCard',
'x-linkages': [{}],
} as any)
const schema8 = new Schema({
type: 'string',
'x-rules': ['phone'],
} as any)
expect(schema8.toFieldProps().validator).toEqual(['phone'])
})
212 changes: 212 additions & 0 deletions packages/json-schema/src/__tests__/transformer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
import { Schema } from '../schema'

test('transform field props', () => {
const schema = new Schema({
type: 'string',
})
expect(schema.toFieldProps()).not.toBeUndefined()
})

test('transform required', () => {
const schema = new Schema({
type: 'object',
required: ['string'],
properties: {
string: {
type: 'string',
},
number: {
type: 'number',
required: true,
},
date: {
type: 'date',
},
},
})
expect(schema.toFieldProps().required).toBeFalsy()
expect(schema.properties.string.toFieldProps().required).toBeTruthy()
expect(schema.properties.number.toFieldProps().required).toBeTruthy()
expect(schema.properties.date.toFieldProps().required).toBeFalsy()
const schema2 = new Schema({
type: 'object',
required: 'string',
properties: {
string: {
type: 'string',
},
number: {
type: 'number',
required: true,
},
date: {
type: 'date',
},
},
})
expect(schema2.toFieldProps().required).toBeFalsy()
expect(schema2.properties.string.toFieldProps().required).toBeTruthy()
expect(schema2.properties.number.toFieldProps().required).toBeTruthy()
expect(schema2.properties.date.toFieldProps().required).toBeFalsy()
})

describe('transform validator', () => {
test('maxItems/minItems', () => {
const schema = new Schema({
type: 'string',
maxItems: 100,
minItems: 10,
})
const props = schema.toFieldProps()
expect(props.validator).toEqual([
{
max: 100,
},
{
min: 10,
},
])
})
test('maxLength/minLength', () => {
const schema = new Schema({
type: 'string',
maxLength: 100,
minLength: 10,
})
const props = schema.toFieldProps()
expect(props.validator).toEqual([
{
max: 100,
},
{
min: 10,
},
])
})
test('maximum/minimum', () => {
const schema = new Schema({
type: 'string',
maximum: 100,
minimum: 10,
})
const props = schema.toFieldProps()
expect(props.validator).toEqual([
{
maximum: 100,
},
{
minimum: 10,
},
])
})
test('exclusiveMaximum/exclusiveMinimum', () => {
const schema = new Schema({
type: 'string',
exclusiveMaximum: 100,
exclusiveMinimum: 10,
})
const props = schema.toFieldProps()
expect(props.validator).toEqual([
{
exclusiveMaximum: 100,
},
{
exclusiveMinimum: 10,
},
])
})
test('pattern', () => {
const schema = new Schema({
type: 'string',
pattern: /\d/,
})
const props = schema.toFieldProps()
expect(props.validator).toEqual([
{
pattern: /\d/,
},
])
})
test('pattern', () => {
const schema = new Schema({
type: 'string',
const: 123,
})
const props = schema.toFieldProps()
expect(props.validator.length).toEqual(1)
})
test('multipleOf', () => {
const schema = new Schema({
type: 'string',
multipleOf: 100,
})
const props = schema.toFieldProps()
expect(props.validator.length).toEqual(1)
})
test('maxProperties', () => {
const schema = new Schema({
type: 'string',
maxProperties: 10,
})
const props = schema.toFieldProps()
expect(props.validator.length).toEqual(1)
})
test('minProperties', () => {
const schema = new Schema({
type: 'string',
minProperties: 10,
})
const props = schema.toFieldProps()
expect(props.validator.length).toEqual(1)
})
test('uniqueItems', () => {
const schema = new Schema({
type: 'string',
uniqueItems: true,
})
const props = schema.toFieldProps()
expect(props.validator.length).toEqual(1)
})

test('format', () => {
const schema = new Schema({
type: 'string',
format: 'date',
})
const props = schema.toFieldProps()
expect(props.validator.length).toEqual(1)
})

test('enum', () => {
const schema = new Schema({
type: 'string',
enum: [
{ label: 'aaa', value: 1 },
{ label: 'bbb', value: 2 },
],
})
const props = schema.toFieldProps()
expect(props.dataSource).toEqual([
{ label: 'aaa', value: 1 },
{ label: 'bbb', value: 2 },
])
const schema2 = new Schema({
type: 'string',
enum: ['111', '222'],
})
const props2 = schema2.toFieldProps()
expect(props2.dataSource).toEqual([
{ label: '111', value: '111' },
{ label: '222', value: '222' },
])
})

test('x-validator', () => {
const schema = new Schema({
type: 'string',
'x-validator': () => {},
})
const props = schema.toFieldProps()
expect(props.validator.length).toEqual(1)
})
})
1 change: 0 additions & 1 deletion packages/json-schema/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './schema'
export * from './patches'
export * from './types'
Loading

0 comments on commit 6813974

Please sign in to comment.