Skip to content

Commit

Permalink
fix: issue 853 and 860 (#928)
Browse files Browse the repository at this point in the history
fix issue
  • Loading branch information
JohnIsOnTheRoad authored Jul 1, 2020
1 parent 9df3eee commit c177430
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
26 changes: 25 additions & 1 deletion packages/react-schema-renderer/src/__tests__/schema.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,24 @@ describe('major scene', () => {
},
p5: {
'x-index': 5
},
p6: {
'x-index': 6
},
p7: {
'x-index': 7
},
p8: {
'x-index': 8
},
p9: {
'x-index': 9
},
p10: {
'x-index': 10
},
p11: {
'x-index': 11
}
}
})
Expand All @@ -149,7 +167,13 @@ describe('major scene', () => {
'p3',
'p2',
'p4',
'p5'
'p5',
'p6',
'p7',
'p8',
'p9',
'p10',
'p11',
])

// 多排序
Expand Down
33 changes: 33 additions & 0 deletions packages/react-schema-renderer/src/__tests__/validate.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,39 @@ test('basic validate', async () => {
expect(getByText('This field is required')).toBeVisible()
})

test('object validate', async () => {
const handleSubmit = jest.fn()
const handleValidateFailed = jest.fn()
const TestComponent = () => (
<SchemaForm onSubmit={handleSubmit} onValidateFailed={handleValidateFailed}>
<Fragment>
<Field
type="object"
name="user"
required={['username', 'age']}
>
<Field type="string" name="username" title="username" />
<Field type="string" name="age" title="age" />
</Field>
<button type="submit" data-testid="btn">
Submit
</button>
</Fragment>
</SchemaForm>
)

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

fireEvent.click(getByTestId('btn'))
await wait()
fireEvent.click(getByTestId('btn'))
await wait()
expect(handleSubmit).toHaveBeenCalledTimes(0)
expect(handleValidateFailed).toHaveBeenCalledTimes(2)
const errTexts = await findAllByText('This field is required')
expect(errTexts).toHaveLength(2)
})

test('validate in init', async () => {
const handleSubmit = jest.fn()
const handleValidateFailed = jest.fn()
Expand Down
4 changes: 3 additions & 1 deletion packages/react-schema-renderer/src/shared/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ export class Schema implements ISchema {
getExtendsRequired() {
if (isBool(this.required)) {
return this.required
} else if (isArr(this.parent?.required) && this.parent?.required.includes(this.key)) {
return true
}
}
getExtendsEditable(): boolean {
Expand Down Expand Up @@ -512,7 +514,7 @@ export class Schema implements ISchema {
const unorderProperties = []
each(newSchema[propertiesName], (item, key) => {
const index = item['x-index']
if (typeof index === 'number') {
if (!isNaN(index)) {
orderProperties[index] = { schema: item, key }
} else {
unorderProperties.push({ schema: item, key })
Expand Down

0 comments on commit c177430

Please sign in to comment.