Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Nested component in Enzyme React testing

import React from "react";
import Adapter from "enzyme-adapter-react-16";
import { configure, shallow, mount } from "enzyme";
import Banking, { BankingForm } from './Banking';

configure({ adapter: new Adapter() });
describe('FormikHandlers', () => {
    describe('handleChange', () => {
      it('change value for firstBankName', async () => {
      const component = shallow(<Banking />);

      expect(component.find(BankingForm)).toHaveLength(1);

    });
  });
});

here I've Banking as a Parent component and bankingForm as a child component which has all the fields. I'm using formik render prop inside the Baking parent component. This test fails because I'm not able to get "BankingForm" component inside the component when it's shallowly rendered. Am I doing something wrong enyzme point of view?

like image 500
Pavan Avatar asked Aug 06 '26 00:08

Pavan


1 Answers

You can either use mount to make the whole component get rendered or use dive to get one level deeper into the tree

like image 62
Andreas Köberle Avatar answered Aug 08 '26 18:08

Andreas Köberle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!