You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
Unable to get input value of wrapped component in <TypedController /> on Jest.
Both codes work correctly in the browser. (Chrome 85.0.4183.102)
use <TypedController /> sample test code
Only the last test will not pass.
importReactfrom'react';import{render,fireEvent,act}from'@testing-library/react';import{SubmitHandler,useForm}from'react-hook-form';import{useTypedController}from'@hookform/strictly-typed';constFORM_ID='test-textarea-id';constSUBMIT_ID='test-submit-id';typeCustomeTextareaProps={testId: string;
name: string;}&React.TextareaHTMLAttributes<HTMLTextAreaElement>;constCustomeTextarea: React.FC<CustomeTextareaProps> = (props: CustomeTextareaProps) =>{const{ testId, ...textAreaProps}=props;return(<div><textareadata-testid={testId}{...textAreaProps}/></div>);};typeSampleFormValue={sample: string;};typeSampleFormProps={onSubmit: (value: SampleFormValue)=>void;};constSampleForm: React.FC<SampleFormProps>=(props: SampleFormProps)=>{const{ handleSubmit, control }=useForm<SampleFormValue>({criteriaMode: 'all',});constTypedController=useTypedController<SampleFormValue>({ control });constonSubmit: SubmitHandler<SampleFormValue> = (data) =>{props.onSubmit(data);};
return (
<formonSubmit={handleSubmit(onSubmit)}><TypedControllername={'sample'}render={(formProps)=><CustomeTextareatestId={FORM_ID}name={'sample'}{...formProps}/>}/><inputtype="submit"data-testid={SUBMIT_ID}/></form>
);
};describe('SampleForm',()=>{test('Input value should be equal to the form value',async()=>{constmockOnSubmit=jest.fn();awaitact(async()=>{constrenderResult=render(<SampleFormonSubmit={mockOnSubmit}/>);constinput=renderResult.getByTestId(FORM_ID)asHTMLTextAreaElement;fireEvent.change(input,{target: {value: 'test-value'}});expect(input.value).toBe('test-value');// No Error});});test('Submitted data should be equal to the input data',async()=>{constmockOnSubmit=jest.fn();awaitact(async()=>{constrenderResult=render(<SampleFormonSubmit={mockOnSubmit}/>);constinput=renderResult.getByTestId(FORM_ID)asHTMLTextAreaElement;fireEvent.change(input,{target: {value: 'test-value'}});expect(input.value).toBe('test-value');fireEvent.submit(renderResult.getByTestId(SUBMIT_ID));});expect(mockOnSubmit).toBeCalled();// No Errorexpect(mockOnSubmit).toBeCalledWith({});// No Errorexpect(mockOnSubmit).toBeCalledWith({sample: 'test-value'});// -> Error});});
Error log
SampleForm
√ Input value should be equal to the form value (122 ms)
× Submitted data should be equal to the input data (89 ms)
● SampleForm › Submitted data should be equal to the input data
expect(jest.fn()).toBeCalledWith(...expected)
- Expected
+ Received
- Object {
- "sample": "test-value",
- }
+ Object {},
Number of calls: 1
71 | expect(mockOnSubmit).toBeCalled(); // No Error
72 | expect(mockOnSubmit).toBeCalledWith({}); // No Error
> 73 | expect(mockOnSubmit).toBeCalledWith({ sample: 'test-value' }); // -> Error
| ^
74 | });
75 | });
76 |
without <TypedController /> sample test code
All tests pass.
importReactfrom'react';import{render,fireEvent,act}from'@testing-library/react';import{SubmitHandler,useForm}from'react-hook-form';import{useTypedController}from'@hookform/strictly-typed';constFORM_ID='test-textarea-id';constSUBMIT_ID='test-submit-id';typeCustomeTextareaProps={testId: string;
name: string;}&React.TextareaHTMLAttributes<HTMLTextAreaElement>;constCustomeTextarea=React.forwardRef<HTMLTextAreaElement,CustomeTextareaProps>((props: CustomeTextareaProps,ref)=>{const{ testId, ...textAreaProps}=props;return(<div><textareadata-testid={testId}ref={ref}{...textAreaProps}/></div>);});typeSampleFormValue={sample: string;};typeSampleFormProps={onSubmit: (value: SampleFormValue)=>void;};constSampleForm: React.FC<SampleFormProps>=(props: SampleFormProps)=>{const{ handleSubmit, register }=useForm<SampleFormValue>({criteriaMode: 'all',});constonSubmit: SubmitHandler<SampleFormValue> = (data) =>{props.onSubmit(data);};
return (
<formonSubmit={handleSubmit(onSubmit)}><CustomeTextareatestId={FORM_ID}name={'sample'}ref={register()}/><inputtype="submit"data-testid={SUBMIT_ID}/></form>
);
};describe('SampleForm',()=>{test('Input value should be equal to the form value',async()=>{constmockOnSubmit=jest.fn();awaitact(async()=>{constrenderResult=render(<SampleFormonSubmit={mockOnSubmit}/>);constinput=renderResult.getByTestId(FORM_ID)asHTMLTextAreaElement;fireEvent.change(input,{target: {value: 'test-value'}});expect(input.value).toBe('test-value');// -> No Error});});test('Submitted data should be equal to the input data',async()=>{constmockOnSubmit=jest.fn();awaitact(async()=>{constrenderResult=render(<SampleFormonSubmit={mockOnSubmit}/>);constinput=renderResult.getByTestId(FORM_ID)asHTMLTextAreaElement;fireEvent.change(input,{target: {value: 'test-value'}});expect(input.value).toBe('test-value');// -> No ErrorfireEvent.submit(renderResult.getByTestId(SUBMIT_ID));});expect(mockOnSubmit).toBeCalled();// -> No Errorexpect(mockOnSubmit).toBeCalledWith({sample: 'test-value'});// -> No Error});});
@redshoga Thanks for your feedback!
I haven't tried it, but it looks like fireEvent.change doesn't fire an onChange event and an onChange callback passed by TypedController is not being executed. Can you verify this? 🙏
You may also be able to resolve using userEvent.type instead of fireEvent.change.
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Unable to get input value of wrapped component in
<TypedController />
on Jest.Both codes work correctly in the browser. (Chrome 85.0.4183.102)
use
<TypedController />
sample test codeOnly the last test will not pass.
Error log
without
<TypedController />
sample test codeAll tests pass.
dependencies (package.json)
The text was updated successfully, but these errors were encountered: