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

fix: 616 #622

Merged
merged 3 commits into from
Jan 19, 2020
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
80 changes: 77 additions & 3 deletions packages/react/src/__tests__/form.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import React from 'react'
import React, { useState } from 'react'
import {
Form,
Field,
createFormActions,
FormEffectHooks,
IFieldStateUIProps
IFieldStateUIProps,
FormSpy,
} from '../index'
import { render } from '@testing-library/react'
import { render, fireEvent, waitForElement } from '@testing-library/react'
import { LifeCycleTypes } from '@uform/core'

const Input: React.FC<IFieldStateUIProps> = props => (
<Field {...props}>
Expand Down Expand Up @@ -60,6 +62,78 @@ describe('test all apis', () => {
await actions.submit()
expect(onSubmitHandler).toBeCalledWith({ aaa: 'hello world' })
})

test('onSubmit async', async () => {
const actions = createFormActions()
const onSubmitEndHandler = jest.fn()
const App = () => {
return <div>
<Form
onSubmit={() => {
return new Promise((resolve) => {
setTimeout(resolve)
})
}}
actions={actions}
>
<Input name="aaa" />
<FormSpy>
{({ type }) => {
return <button data-testid="submit" onClick={() => {
actions.submit()
}}>{type}</button>
}}
</FormSpy>
</Form>
</div>
}
const { getByTestId, queryByTestId } = render(<App />)

expect(onSubmitEndHandler).toBeCalledTimes(0)
fireEvent.click(queryByTestId('submit'))
const submitEle = await waitForElement(
() => getByTestId('submit')
)

expect(submitEle.textContent).not.toEqual(LifeCycleTypes.ON_FORM_INIT)
})

test('onSubmit unmount promise', async () => {
const actions = createFormActions()
const onSubmitEndHandler = jest.fn()
const App = () => {
const [visible, setVisible] = useState(true)
return <div>
{visible ? <Form
onSubmit={() => {
return new Promise((resolve) => {
setVisible(false)
setTimeout(resolve)
})
}}
actions={actions}
>
<Input name="aaa" />
<FormSpy>
{({ type }) => {
return <button data-testid="submit" onClick={() => {
actions.submit()
}}>{type}</button>
}}
</FormSpy>
</Form> : null }
</div>
}
const { getByTestId, queryByTestId } = render(<App />)

expect(onSubmitEndHandler).toBeCalledTimes(0)
fireEvent.click(queryByTestId('submit'))
const submitEle = await waitForElement(
() => getByTestId('submit')
)

expect(submitEle.textContent).toEqual(LifeCycleTypes.ON_FORM_INIT)
})
})

describe('major scenes', () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/react/src/hooks/useFormSpy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ export const useFormSpy = (props: IFormSpyProps): ISpyHook => {
const broadcast = useContext(BroadcastContext)
const form = useContext(FormContext)
const initializedRef = useRef(false)
const unmountRef = useRef(false)
const subscriberId = useRef<number>()
const [type, setType] = useState<string>(LifeCycleTypes.ON_FORM_INIT)
const [state, dispatch] = useReducer(
(state, action) => props.reducer(state, action, form),
{}
)
const subscriber = useCallback<FormHeartSubscriber>(({ type, payload }) => {
const subscriber = useCallback<FormHeartSubscriber>(({ type, payload }) => {
if (initializedRef.current) return
setTimeout(() => {
if (unmountRef.current) return
if (isStr(props.selector) && FormPath.parse(props.selector).match(type)) {
setType(type)
dispatch({
Expand Down Expand Up @@ -56,6 +58,7 @@ export const useFormSpy = (props: IFormSpyProps): ISpyHook => {
} else if (broadcast) {
broadcast.unsubscribe(subscriberId.current)
}
unmountRef.current = true
}
}, [])
const formApi: IForm = form ? form : broadcast && broadcast.getContext()
Expand Down