I have a simple component see below, that basically attempts to grab some data from the formik FormContext using the useFormikContext hook.
However when attempting to write unit tests for this component it wants me to mock the hook which is fine, however, mocking the hook with typescript means returning well over 20 properties most of which are a variety of methods and functions.
Has anyone found a better way of doing this? Just seems a bit annoying even if I get it to work as I only need 1 field from the hook.
Component
const AphControlInput: React.FC<IAphControlInput> = ({ name, value, label, type, small, disabled, vertical = true, className = '', ...attributeOptions }) => {
const form = useFormikContext();
return (
<>
<div className={
`aph-control-input ${className}` +
`${small ? ' aph-control-input--small' : ''}` +
`${vertical ? ' aph-control-input--block' : ''}` +
`${form.getFieldMeta(name).error ? ' aph-control-input--invalid' : ''}`
}
>
<Field
className='aph-control-input__input'
name={name}
id={value ? value?.toString() : name}
type={type}
value={value ? value : undefined}
disabled={disabled}
{...attributeOptions}
/>
<label className='aph-control-input__text' htmlFor={value ? value?.toString() : name}>
{label}
</label>
</div>
</>
);
};
Unit test (Incomplete this was just a quick attempt at mocking all the returns for the hook)
describe('AphInputLabel UnitTests', () => {
let wrapper: any;
const useFormikContextMock = jest.spyOn(formik, 'useFormikContext');
beforeEach(() => {
useFormikContextMock.mockReturnValue({
values: { testName: 'testValue' },
getFieldMeta: getFieldMetaMock,
touched: true,
isSubmitting: false,
isValidating: false,
errors: false,
submitCount: 0,
setStatus: (status?: any) => { return null },
setErrors: (errors?: FormikErrors<any>) => { return null },
setSubmitting: (isSubmitting: boolean) => { return null },
setTouched: (touched: FormikTouched<any>, shouldValidate?: boolean) => { return null },
setValues: (values: React.SetStateAction<any>, shouldValidate?: boolean) => { return null },
setFieldValue: (field: string, value: any, shouldValidate?: boolean) => { return null },
setFieldError: (field: string, message: string | undefined) => { return null },
setFieldTouched: (field: string, isTouched?: boolean, shouldValidate?: boolean) => { return null },
resetForm: (nextState?: Partial<FormikState<any>>) => { return null },
validateField: (field: string) => { return null },
setFormikState: (f: FormikState<any> | ((prevState: FormikState<any>) => FormikState<any>), cb?: () => void) => null,
validateForm: (values?: any) => { return new Promise<FormikErrors<unknown>>((resolve, reject) => {
const formikErrors: FormikErrors<any> = {
'testName': ''
}
return formikErrors;
});
},
submitForm: () => new Promise<void>(() => null),
handleSubmit: (e?: React.FormEvent<HTMLFormElement> | undefined) => null,
handleReset: (e?: React.SyntheticEvent<any>) => null,
});
}
}
formik
on this line :const useFormikContextMock = jest.spyOn(formik, 'useFormikContext');
– CorrinecorrinnegetFieldMetaMock
as well please? – Corrinecorrinne