简体   繁体   中英

How can I set child props when testing a React component?

I have this component

const InputWithLabel = ({
                          id,
                          value,
                          type = 'text',
                          onInputChange,
                          isFocused,
                          children,
                        }) => {...
return(<>
        <label htmlFor={id} className="label">
                {children}
        </label>
...
</>)}

and I am doing testing with:

describe('InputWithLabel',()=>{
    const inputWithLabelProps={
        id:"search",
        value:"React",
        isFocused:true,
        onInputChange:jest.fn,
        children:[]
    }
    test('check the existence of InputWithLabel',()=>{
        render(<InputWithLabel {...inputWithLabelProps}/>);
        screen.debug();// This to see the renders of the Element 
    });
})

What value I can put on children:[] for do the test. I tried with array of object but don't work. Any Ideas?

In my case this is the solution, this the calling of component

<InputWithLabel
          id="search"
          value={searchTerm}
          isFocused
          onInputChange={onSearchInput}
      >
        <strong>Search:</strong>
      </InputWithLabel>

and the test will be:

describe('InputWithLabel',()=>{
    const inputWithLabelProps={
        id:"search",
        value:"React",
        isFocused:true,
        onInputChange:jest.fn,
        children:<strong>Search:</strong>
    }
    test('check the existence of InputWithLabel',()=>{
        render(<InputWithLabel {...inputWithLabelProps}/>);
        screen.debug();
    });
})

pls notice

<strong>Search:</strong>

is the children of the my component

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM