Skip to content

Commit

Permalink
fix: add unit test && submit bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-huxiyang committed May 17, 2024
1 parent 29f543e commit 9903def
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
32 changes: 31 additions & 1 deletion src/packages/form/__tests__/form.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,37 @@ test('Both form and formItem set initialValue(s)', () => {
'NutUI-React-Form'
)
})

test('Both form and formItem set initialValue(s) to submit', async () => {
const handleSubmit = vi.fn()
const { container } = render(
<Form
initialValues={{ username: 'NutUI-React-Form', age: 18 }}
onFinish={handleSubmit}
>
<Form.Item name="username" label="UserName" initialValue="NutUI-React">
<Input />
</Form.Item>
<Form.Item name="age" label="Age" initialValue="30">
<Input />
</Form.Item>
<Form.Item name="phone" label="Phone" initialValue="123456">
<Input />
</Form.Item>
</Form>
)
const form = container.querySelector('form') as Element
fireEvent.submit(form)
await waitFor(() => {
expect(handleSubmit).toBeCalled()
expect(handleSubmit).toBeCalledWith(
expect.objectContaining({
username: 'NutUI-React-Form',
age: 18,
phone: '123456',
})
)
})
})
test('form validateTrigger', async () => {
const { container, rerender } = render(
<Form>
Expand Down
5 changes: 4 additions & 1 deletion src/packages/formitem/formitem.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ export class FormItem extends React.Component<
this.props.name &&
!Object.keys(store).includes(this.props.name)
) {
setInitialValues({ [this.props.name]: this.props.initialValue })
setInitialValues(
{ ...store, [this.props.name]: this.props.initialValue },
true
)
}
// 注册组件实例到FormStore
const { registerField, registerUpdate } = this.context.getInternal(SECRET)
Expand Down
5 changes: 4 additions & 1 deletion src/packages/formitem/formitem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ export class FormItem extends React.Component<
this.props.name &&
!Object.keys(store).includes(this.props.name)
) {
setInitialValues({ [this.props.name]: this.props.initialValue })
setInitialValues(
{ ...store, [this.props.name]: this.props.initialValue },
true
)
}
// 注册组件实例到FormStore
const { registerField, registerUpdate } = this.context.getInternal(SECRET)
Expand Down

0 comments on commit 9903def

Please sign in to comment.