-
Notifications
You must be signed in to change notification settings - Fork 6
fix: transition import & SubmitError handling #230
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
324300e
fix: transition import
tenphi c52b91d
fix(SubmitError): handle not valid error messages
tenphi 7e29b81
fix(SubmitError): handle not valid error messages * 2
tenphi dda5aa1
fix(Form): submit handling logic
tenphi 499317c
fix(Form): submit handling logic * 2
tenphi 9577da2
fix(Form): submit handling logic * 3
tenphi 5d75e7d
Create thin-birds-press.md
tenphi 38f6cb7
fix(Form): submit handling logic * 4
tenphi c164f84
fix(Form): submit handling logic * 5
tenphi b9464e4
fix(Form): submit handling logic * 6
tenphi ab11d01
Merge branch 'main' into minor-fixes-2
MrFlashAccount 55eea61
feat: draft of test
MrFlashAccount f3e4986
fix(SubmitError): add tests
tenphi d781d47
test(Form): add more cases
tenphi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@cube-dev/ui-kit": patch | ||
--- | ||
|
||
The Form component set `submitError` no more on failed validation. | ||
`SubmitError` component now handles non-valid errors as `Internal error`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,33 @@ | ||
import { ReactNode, useContext } from 'react'; | ||
import { ReactNode, useContext, isValidElement } from 'react'; | ||
|
||
import { Alert, CubeAlertProps } from '../../content/Alert'; | ||
|
||
import { FormContext } from './Form'; | ||
|
||
type SubmitErrorProps = { | ||
submitError?: ReactNode; | ||
type SubmitErrorContextProps = { | ||
submitError?: unknown; | ||
}; | ||
|
||
/** | ||
* An alert that shows a form error message received from the onSubmit callback. | ||
*/ | ||
export function SubmitError(props: CubeAlertProps) { | ||
const { submitError } = useContext(FormContext) as SubmitErrorProps; | ||
export const SubmitError = function SubmitError(props: CubeAlertProps) { | ||
let { submitError } = useContext(FormContext) as SubmitErrorContextProps; | ||
|
||
if (!submitError) { | ||
return null; | ||
} | ||
|
||
if ( | ||
!isValidElement(submitError as ReactNode) && | ||
typeof submitError !== 'string' | ||
) { | ||
submitError = 'Internal error'; | ||
} | ||
|
||
return ( | ||
<Alert theme="danger" {...props}> | ||
{submitError} | ||
{submitError as ReactNode} | ||
</Alert> | ||
); | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import userEvents from '@testing-library/user-event'; | ||
import { waitFor } from '@testing-library/react'; | ||
|
||
import { renderWithForm } from '../../../test'; | ||
import { Submit } from '../../actions'; | ||
import { TextInput } from '../TextInput/TextInput'; | ||
|
||
import { Form } from '.'; | ||
|
||
describe('<SubmitError />', () => { | ||
it('should display a submit error if onSubmit callback is failed', async () => { | ||
const onSubmit = jest.fn(() => Promise.reject('Custom Error')); | ||
const onSubmitFailed = jest.fn(() => {}); | ||
|
||
const { getByRole, getByText } = renderWithForm( | ||
<> | ||
<Form.Item name="test" label="Test"> | ||
<TextInput /> | ||
</Form.Item> | ||
|
||
<Submit>Submit</Submit> | ||
|
||
<Form.SubmitError /> | ||
</>, | ||
{ formProps: { onSubmit, onSubmitFailed } }, | ||
); | ||
|
||
const submit = getByRole('button'); | ||
const input = getByRole('textbox'); | ||
|
||
await userEvents.type(input, 'test'); | ||
await userEvents.click(submit); | ||
|
||
await waitFor(() => { | ||
// onSubmitFailed callback should only be called if onSubmit callback is called and failed | ||
expect(onSubmitFailed).toBeCalledTimes(1); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(onSubmit).toBeCalledTimes(1); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(getByText('Custom Error')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('should display an error placeholder if error is not handled properly', async () => { | ||
const onSubmit = jest.fn(() => { | ||
return Promise.reject([]); // non-valid error | ||
}); | ||
const onSubmitFailed = jest.fn(() => {}); | ||
|
||
const { getByRole, getByText } = renderWithForm( | ||
<> | ||
<Form.Item name="test" label="Test"> | ||
<TextInput /> | ||
</Form.Item> | ||
|
||
<Submit>Submit</Submit> | ||
|
||
<Form.SubmitError /> | ||
</>, | ||
{ formProps: { onSubmit, onSubmitFailed } }, | ||
); | ||
|
||
const submit = getByRole('button'); | ||
const input = getByRole('textbox'); | ||
|
||
await userEvents.type(input, 'test'); | ||
await userEvents.click(submit); | ||
|
||
await waitFor(() => { | ||
// onSubmitFailed callback should only be called if onSubmit callback is called and failed | ||
expect(onSubmitFailed).toBeCalledTimes(1); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(onSubmit).toBeCalledTimes(1); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(getByText('Internal error')).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import userEvents from '@testing-library/user-event'; | ||
import { act, waitFor } from '@testing-library/react'; | ||
|
||
import { renderWithForm } from '../../../test'; | ||
import { Submit } from '../../actions'; | ||
import { TextInput } from '../TextInput/TextInput'; | ||
|
||
import { Form } from '.'; | ||
|
||
describe('<Form />', () => { | ||
it('should not be displayed if validation is failed on submit', async () => { | ||
const onSubmit = jest.fn(() => Promise.reject('Custom Error')); | ||
const onSubmitFailed = jest.fn(() => {}); | ||
tenphi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const { getByRole, formInstance } = renderWithForm( | ||
<> | ||
<Form.Item | ||
name="test" | ||
label="Test" | ||
rules={[ | ||
{ | ||
validator(rule, value) { | ||
return Promise.reject('invalid'); | ||
}, | ||
}, | ||
]} | ||
> | ||
<TextInput /> | ||
</Form.Item> | ||
|
||
<Submit>Submit</Submit> | ||
|
||
<Form.SubmitError /> | ||
</>, | ||
{ formProps: { onSubmit, onSubmitFailed } }, | ||
); | ||
|
||
const submit = getByRole('button'); | ||
const input = getByRole('textbox'); | ||
|
||
await userEvents.type(input, 'test'); | ||
await userEvents.click(submit); | ||
|
||
await waitFor(() => { | ||
// onSubmitFailed callback should only be called if onSubmit callback is called and failed | ||
expect(onSubmitFailed).not.toBeCalled(); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(formInstance.submitError).toBeNull(); | ||
}); | ||
}); | ||
|
||
it('should throw uncaught rejection if error is not handled', async () => { | ||
const onSubmit = jest.fn(() => { | ||
throw new Error('Custom Error'); | ||
}); | ||
const onSubmitFailed = jest.fn(() => {}); | ||
|
||
const { getByRole, getByText, formInstance } = renderWithForm( | ||
<> | ||
<Form.Item name="test" label="Test"> | ||
<TextInput /> | ||
</Form.Item> | ||
|
||
<Submit>Submit</Submit> | ||
|
||
<Form.SubmitError /> | ||
</>, | ||
{ formProps: { onSubmit, onSubmitFailed } }, | ||
); | ||
|
||
const input = getByRole('textbox'); | ||
|
||
await userEvents.type(input, 'test'); | ||
|
||
await act(async () => { | ||
await expect(formInstance.submit()).rejects.toThrow('Custom Error'); | ||
}); | ||
|
||
await expect(onSubmitFailed).toBeCalledTimes(1); | ||
await expect(onSubmit).toBeCalledTimes(1); | ||
|
||
await waitFor(() => { | ||
expect(getByText('Internal error')).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.