-
Notifications
You must be signed in to change notification settings - Fork 12
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
EVER-14024 : Form : Implemented form/input HTML5 ValidityState valida… #321
Open
wendigolabs
wants to merge
6
commits into
cision:master
Choose a base branch
from
wendigolabs:feature-EVER-14024-rover-ui-form-component-alfa
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
58c86fa
EVER-14024 : Form : Implemented form/input HTML5 ValidityState valida…
wendigolabs 684d826
Checkpoint for functionality.
wendigolabs 356f651
Checkpoint.
wendigolabs 5b1f060
Checkpoint.
wendigolabs 06203ec
Checkpoint.
wendigolabs ba3435b
Moved Form to Galaxies.
wendigolabs 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 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 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,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="WEB_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$" /> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
This file contains 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,218 @@ | ||
import React from 'react'; | ||
import { render, fireEvent, act } from '@testing-library/react'; | ||
|
||
import Form from './index'; | ||
import { FormProps, FormContext } from './Form'; | ||
|
||
const defaultProps = { | ||
className: 'some_classname', | ||
initialValues: { | ||
textInput: '', | ||
radioInput: false, | ||
checkboxInput: false, | ||
}, | ||
validationSchema: {}, | ||
onSubmit: jest.fn(), | ||
}; | ||
|
||
interface DefaultPropsType extends FormProps { | ||
onCustom?: { | ||
fieldName: string; | ||
callback: () => string; | ||
}; | ||
} | ||
|
||
const renderForm = (props: DefaultPropsType = defaultProps) => | ||
render( | ||
<Form {...defaultProps} {...props}> | ||
<FormContext.Consumer> | ||
{(context) => { | ||
const { | ||
formState, | ||
values, | ||
handleChange, | ||
handleBlur, | ||
handleCustom, | ||
} = context as FormContext; | ||
return ( | ||
<> | ||
<pre data-testid="form-data"> | ||
{JSON.stringify({ values, formState })} | ||
</pre> | ||
{formState?.submitError && ( | ||
<div>{formState.submitError.message}</div> | ||
)} | ||
<input | ||
data-testid="text-input" | ||
type="text" | ||
name="textInput" | ||
value={values.textInput as string} | ||
onChange={handleChange} | ||
onBlur={handleBlur} | ||
/> | ||
<input | ||
data-testid="radio-input" | ||
type="radio" | ||
name="radioInput" | ||
onChange={handleChange} | ||
checked={values.radioInput as boolean} | ||
/> | ||
<input | ||
data-testid="checkbox-input" | ||
type="checkbox" | ||
name="checkboxInput" | ||
onChange={handleChange} | ||
checked={values.checkboxInput as boolean} | ||
/> | ||
{props.onCustom && ( | ||
<button | ||
type="submit" | ||
data-testid="custom-button" | ||
onClick={ | ||
handleCustom( | ||
props.onCustom.fieldName, | ||
props.onCustom.callback | ||
) as any | ||
} | ||
> | ||
Evolve it! | ||
</button> | ||
)} | ||
<button data-testid="submit-button" type="submit"> | ||
Submit | ||
</button> | ||
</> | ||
); | ||
}} | ||
</FormContext.Consumer> | ||
</Form> | ||
); | ||
|
||
describe('<Form />', () => { | ||
it.skip('handles inputs', () => { | ||
const { getByTestId } = renderForm(); | ||
const someText = { target: { value: 'some text' } }; | ||
const textInput = getByTestId('text-input') as HTMLInputElement; | ||
const radioInput = getByTestId('radio-input') as HTMLInputElement; | ||
const checkboxInput = getByTestId('checkbox-input') as HTMLInputElement; | ||
|
||
fireEvent.change(textInput, someText); | ||
fireEvent.click(radioInput); | ||
fireEvent.click(checkboxInput); | ||
|
||
const { values } = JSON.parse(getByTestId('form-data').innerHTML); | ||
|
||
expect(textInput.value).toEqual(someText.target.value); | ||
expect(values.textInput).toEqual(someText.target.value); | ||
|
||
expect(radioInput.checked).toEqual(true); | ||
expect(values.radioInput).toEqual('on'); // this is odd | ||
|
||
expect(checkboxInput.checked).toEqual(true); | ||
expect(values.checkboxInput).toEqual(true); | ||
}); | ||
|
||
it.skip('handles blur events', () => { | ||
const { getByTestId } = renderForm(); | ||
const textInput = getByTestId('text-input'); | ||
|
||
fireEvent.blur(textInput); | ||
|
||
const { formState } = JSON.parse(getByTestId('form-data').innerHTML); | ||
|
||
expect(formState?.touched.textInput).toEqual(true); | ||
}); | ||
|
||
it.skip('handles custom events', () => { | ||
const { getByTestId } = renderForm({ | ||
onCustom: { | ||
fieldName: 'textInput', | ||
callback: () => 'raichu', | ||
}, | ||
}); | ||
|
||
const someText = { target: { value: 'pikachu' } }; | ||
const textInput = getByTestId('text-input') as HTMLInputElement; | ||
fireEvent.change(textInput, someText); | ||
|
||
const customBtn = getByTestId('custom-button'); | ||
fireEvent.click(customBtn); | ||
|
||
const { values } = JSON.parse(getByTestId('form-data').innerHTML); | ||
|
||
expect(textInput.value).toEqual('raichu'); | ||
expect(values.textInput).toEqual('raichu'); | ||
}); | ||
|
||
it.skip('validates inputs', async () => { | ||
const { getByTestId } = renderForm({ | ||
validationSchema: { | ||
textInput: { | ||
nonBidoof: { | ||
message: 'anything but bidoof', | ||
validator: (value) => value !== 'bidoof', | ||
}, | ||
}, | ||
}, | ||
}); | ||
const textInput = getByTestId('text-input'); | ||
|
||
fireEvent.change(textInput, { target: { value: 'bidoof' } }); | ||
|
||
const { formState } = JSON.parse(getByTestId('form-data').innerHTML); | ||
|
||
expect(formState?.validationErrors.textInput).toEqual( | ||
'anything but bidoof' | ||
); | ||
}); | ||
|
||
it.skip('handles successful submit with all form values', async () => { | ||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
const { getByTestId } = renderForm({ onSubmit }); | ||
const submitBtn = getByTestId('submit-button'); | ||
const formData = getByTestId('form-data'); | ||
|
||
// assert isSubmitting false | ||
const { formState: initialFormState } = JSON.parse(formData.innerHTML); | ||
expect(initialFormState.isSubmitting).toEqual(false); | ||
|
||
await act(async () => { | ||
fireEvent.click(submitBtn); | ||
}); | ||
|
||
// assert isSubmitting false when done | ||
const { formState: finalFormState } = JSON.parse(formData.innerHTML); | ||
expect(finalFormState.isSubmitting).toEqual(false); | ||
|
||
// using hoisting to get access to getByTestId and onSubmit | ||
async function onSubmit(values) { | ||
// assert isSubmitting true while running | ||
const { formState: intermediateFormState } = JSON.parse( | ||
formData.innerHTML | ||
); | ||
expect(values).toEqual(defaultProps.initialValues); | ||
await Promise.resolve(() => | ||
expect(intermediateFormState.isSubmitting).toEqual(true) | ||
); | ||
} | ||
}); | ||
|
||
it.skip('handles errors in onSubmit prop', async () => { | ||
const { getByTestId, queryByText } = renderForm({ | ||
onSubmit: () => { | ||
throw new Error('Error in user defined onSubmit prop'); | ||
}, | ||
}); | ||
const submitBtn = getByTestId('submit-button'); | ||
const formData = getByTestId('form-data'); | ||
|
||
const { formState: initialFormState } = JSON.parse(formData.innerHTML); | ||
expect(initialFormState.submitError).toEqual(null); | ||
|
||
await act(async () => { | ||
fireEvent.click(submitBtn); | ||
}); | ||
|
||
expect(queryByText('Error in user defined onSubmit prop')).toBeTruthy(); | ||
}); | ||
}); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need something like
it('handles native events', ...
.