Testing onChange function in Jest

前端 未结 5 2041
悲哀的现实
悲哀的现实 2021-02-01 04:07

I\'m relatively new to Jest and testing in general. I have a component with an input element:

import * as React from \"react\";

export interface inputProps{
            


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-01 04:27

    I struggled with this for hours. Plus since I had multiple select fields on one page. What I found is that Textfield solution works differently from Select.test given on docs.

    On the code I defined SelectProps with id. (You can also go with data-testid)

    I could only trigger dropdown by clicking this field.

     input.onChange(value) }
      error = { Boolean(meta.touched && meta.error) }
      open = { open }
      SelectProps = {
        {
          id: `${input.name}-select`,
          MenuProps: {
            anchorOrigin: {
              vertical: "bottom",
              horizontal: "left"
            },
            transformOrigin: {
              vertical: "top",
              horizontal: "left"
            },
            getContentAnchorEl: null
          }
        }
      } 
      { ...props} >
    
      //yourOptions Goes here
    
     

    And in my test.

    const pickUpAddress = document.getElementById("address-select");
    
    UserEvent.click(pickUpAddress);
    UserEvent.click(screen.getByTestId("address-select-option-0"));

    Worked like a charm afterwards. Hope this helps.

提交回复
热议问题