Testing React component that using hook

Viewed 27

enter image description here

Find the attached image

I am getting this following error, while running Unit test for my react file. I couldn't able to set value for a state method.

TestFile.tsx


 const [attr, setAttributes] = useState<any>(initialState);

const getDetail = async () => {
    if (Id) {
      const form: IForm = await getResponse(Id);
      console.log("||" + form.attributes.sourceName)
      setFormResponseAttributes(form.attributes);
      console.log("***" +  JSON.stringify(attr));
};
 useEffect(() => {
    getDetail();
  }, []);

return(
 {attr.sourceName === 'ECR' ? 
             <div className='fe_u_padding--right-medium'>
             <Button
               id='saveDraftButtonId' 
               text='Save as Draft'
               onClick={() => saveForm(true)}
               variant='secondary'
               className='hide-while-printing'
             />
           </div>
           : null
          );
}
export


TestFile.test.tsx


it('load form response', () => {
const getResponseSpy = jest.spyOn(ResponseApi, 'getResponse');
    getResponseSpy.mockImplementation(() => Promise.resolve(testUtils.Response));
     const setAppContextSpy = jest.fn();
      let setResponseAttributes;
     const wrapper = mount(
      <BrowserRouter>
        <RegisteredApplicationContext.Provider
          value={{ appContext: testUtils.registeredApplication, setAppContext: setAppContextSpy }}
        >
      
          <FormResponse match={{ params: { formResponseId: 'formResponseId1' } }} />
        </RegisteredApplicationContext.Provider>
      </BrowserRouter>
    );
    wrapper.find(Button).find('#saveDraftButtonId').first().simulate('click', {});
    wrapper.update();


expect(getFormResponseSpy).toBeCalled();


TestUtils

export const Response: IForm = {
    id: uuidv4(),
    attributes: {"contextId": "111","sourceName" : "ECR"}
}

I am getting error while reading the SaveDraftButtonId,

Method “simulate” is meant to be run on 1 node. 0 found instead.,
it could able to find that button coz it couldn't set the value for attr state value

Any inputs would be helpful TIA

0 Answers
Related