Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update schema.ts #493

Merged
merged 3 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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