Skip to content

Commit

Permalink
fix schema property minItems (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
李力 authored and janryWang committed Dec 11, 2019
1 parent 2de0b32 commit 26e12aa
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
65 changes: 60 additions & 5 deletions packages/react-schema-renderer/src/__tests__/json-schema.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,48 @@ import {
registerFormField,
connect,
SchemaMarkupForm as SchemaForm,
SchemaMarkupField as Field
SchemaMarkupField as Field,
registerFieldMiddleware
} from '../index'
import '@testing-library/jest-dom/extend-expect'
import { toArr } from '@uform/shared'
import { render, fireEvent, wait } from '@testing-library/react'

registerFieldMiddleware(Field => {
return props => {
const index = props.schema['x-props'] && props.schema['x-props'].index
return (
<div>
<Field {...props} />
<div data-testid={`test-errors-${index}`}>{props.errors}</div>
</div>
)
}
})

registerFormField(
'string',
connect()(props => (
<input {...props} data-testid={props.testid} value={props.value || ''} />
))
)

registerFormField('array', props => {
const { value, renderField } = props
return (
<Fragment>
{toArr(value).map((item, index) => {
return (
<div data-testid="item" key={index}>
{renderField(index)}
</div>
)
})}
</Fragment>
)
})


describe('test all apis', () => {
//todo
test('title', () => {
Expand Down Expand Up @@ -56,11 +87,11 @@ describe('test all apis', () => {
//todo
})

test('exclusiveMaximum', () => {
test('minimum', () => {
//todo
})

test('minimum', () => {
test('exclusiveMaximum', () => {
//todo
})

Expand All @@ -84,9 +115,33 @@ describe('test all apis', () => {
//todo
})

test('minItems', () => {
//todo
test('array minItems', async () => {
const handleSubmit = jest.fn()
const handleValidateFailed = jest.fn()
const TestComponent = () => (
<SchemaForm onSubmit={handleSubmit} onValidateFailed={handleValidateFailed}>
<Fragment>
<Field name="text" type="array" minItems={2}>
<Field type="string"/>
</Field>
<button type="submit" data-testid="btn">
Submit
</button>
</Fragment>
</SchemaForm>
)

const { getByTestId, getByText } = render(<TestComponent />)

fireEvent.click(getByTestId('btn'))
await wait()
fireEvent.click(getByTestId('btn'))
await wait()
expect(handleSubmit).toHaveBeenCalledTimes(0)
expect(handleValidateFailed).toHaveBeenCalledTimes(2)
expect(getByText('The length or number of entries must be at least 2')).toBeVisible()
})

test('uniqueItems', () => {
//todo
})
Expand Down
2 changes: 1 addition & 1 deletion packages/react-schema-renderer/src/shared/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export class Schema implements ISchema {
rules.push({ max: this.maxItems })
}
if (isValid(this.minItems)) {
rules.push({ max: this.minItems })
rules.push({ min: this.minItems })
}
if (isValid(this.maxLength)) {
rules.push({ max: this.maxLength })
Expand Down

0 comments on commit 26e12aa

Please sign in to comment.