How do you test for the non-existence of an element using jest and react-testing-library?

后端 未结 6 682
旧时难觅i
旧时难觅i 2021-01-30 15:36

I have a component library that I\'m writing unit tests for using Jest and react-testing-library. Based on certain props or events I want to verify that certain elements aren\'t

6条回答
  •  隐瞒了意图╮
    2021-01-30 16:04

    You have to use queryByTestId instead of getByTestId.

    Here a code example where i want to test if the component with "car" id isn't existing.

     describe('And there is no car', () => {
      it('Should not display car mark', () => {
        const props = {
          ...defaultProps,
          base: null,
        }
        const { queryByTestId } = render(
          
            
          ,
        );
        expect(queryByTestId(/car/)).toBeNull();
      });
    });
    

提交回复
热议问题