vendredi 23 août 2019

How to get full test coverage for the react code

Im new to unit testing , there is a functional component for which I have written some test cases , but unable to get full test coverage with jest and enzyme . Other than function coverage all are covered which I tested with npm test -- --coverage

```const TargetAudience = ({ searchResults }) => (
    <React.Fragment>
        <div className="target-audience-header">Target Audience</div>
    {searchResults.length > 0 ?
      <Table columns={columns} dataSource={data} />
      :
      null
    }

    </React.Fragment>
);```

```
       describe('search results props test',() => {
    it('expect to render targetAudience component with search results 
          props length > 0', ()=> {
        const searchRes=['a','b'];
        expect(shallow(<TargetAudience searchResults={searchRes} />)).toMatchSnapshot();
    })

    it('expect to render targetAudience component with search results props length = 0', ()=> {
        const searchRes=[];
        expect(shallow(<TargetAudience searchResults={searchRes} />)).toMatchSnapshot();
    })
})
``````
        describe('test the table component',() => {
    it('expect to render table component when searchResults props has some 
          values', ()=> {
        const searchRes=['a','b'];

        const wrapper = shallow(<TargetAudience searchResults={searchRes} />)

        expect(wrapper.find('Table').length).toBe(1)
        expect(wrapper.find('.target-audience-header').length).toBe(1)
    })
    it('render date input correctly with null value', () => {  
        const searchRes = [1,2];
        const columns = {};
        const wrapper = shallow(<TargetAudience searchResults={searchRes} />);
        expect(wrapper.find('Table').props().columns).toBe({});
    });
})
```

Aucun commentaire:

Enregistrer un commentaire