Skip to content

Commit

Permalink
fix(core): Increase lastValidateValue value processing during initial…
Browse files Browse the repository at this point in the history
…ization (#276)

* fix(core): Increase lastValidateValue value processing during initialization

* test: add lastValidateValue prop handler test

* test: test lastValidateValue prop handling from SchemaForm
  • Loading branch information
atzcl authored and janryWang committed Aug 25, 2019
1 parent 77e6c21 commit 045f6fe
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
46 changes: 46 additions & 0 deletions packages/core/src/__test__/form.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { createForm } from '../index'

test('Increase lastValidateValue value processing during initialization', async () => {
const inpueFieldValidate = jest.fn()
const requriedFieldValidate = jest.fn()

const form = createForm({
initialValues: {
requriedField: 'defaultValue'
}
})

form.registerField('inpueField', {
props: {
requried: true,
'x-rules': () =>
new Promise(resolve => {
inpueFieldValidate()

resolve()
})
}
})

form.registerField('requriedField', {
props: {
requried: true,
'x-rules': () =>
new Promise(resolve => {
requriedFieldValidate()

resolve()
})
}
})

form.setValue('inpueField', 1111)
await sleep(1000)
expect(inpueFieldValidate).toHaveBeenCalledTimes(1)
expect(requriedFieldValidate).toHaveBeenCalledTimes(0)

form.setValue('requriedField', 2222)
await sleep(1000)
expect(inpueFieldValidate).toHaveBeenCalledTimes(1)
expect(requriedFieldValidate).toHaveBeenCalledTimes(1)
})
4 changes: 4 additions & 0 deletions packages/core/src/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ export class Field implements IField {
this.editable = !isEmpty(editable) ? editable : this.getContextEditable()
}

if (options.initialValue) {
this.lastValidateValue = options.initialValue
}

if (
this.pristine &&
!isEmpty(this.initialValue) &&
Expand Down
60 changes: 60 additions & 0 deletions packages/react/src/__tests__/schema_form.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React from 'react'
import { render, fireEvent } from '@testing-library/react'

import SchemaForm, { Field, registerFormField, connect } from '../index'

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

test('Increase lastValidateValue value processing during initialization', async () => {
const inpueFieldValidate = jest.fn()
const requriedFieldValidate = jest.fn()

const TestComponent = () => (
<SchemaForm initialValues={{ requriedField: 'defaultValue' }}>
<Field
requried
type="string"
title="inpueField"
name="inpueField"
x-props={{ testid: 'inpueField' }}
x-rules={() =>
new Promise(resolve => {
inpueFieldValidate()

resolve()
})
}
/>
<Field
requried
type="string"
title="requriedField"
name="requriedField"
x-props={{ testid: 'requriedField' }}
x-rules={() =>
new Promise(resolve => {
requriedFieldValidate()

resolve()
})
}
/>
</SchemaForm>
)

const { getByTestId } = render(<TestComponent />)
fireEvent.change(getByTestId('inpueField'), { target: { value: 1111 } })
await sleep(1000)
expect(inpueFieldValidate).toHaveBeenCalledTimes(1)
expect(requriedFieldValidate).toHaveBeenCalledTimes(0)

fireEvent.change(getByTestId('requriedField'), { target: { value: 2222 } })
await sleep(1000)
expect(inpueFieldValidate).toHaveBeenCalledTimes(1)
expect(requriedFieldValidate).toHaveBeenCalledTimes(1)
})

0 comments on commit 045f6fe

Please sign in to comment.