Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access class objects declared with `this` with Jest/Enzyme

I have a class that looks like:

class Dummy {
    constructor(props) {
        super(props)

        this.dummyObj = 'dummy'
    }
}

and I want to access this.dummyObj and change its value to say: 'hi' is this possible with Jest/Enzyme?

thanks!

like image 628
nrion Avatar asked Nov 22 '25 13:11

nrion


1 Answers

You can do that by making an instance:

import React from 'react';
import Enzyme, { shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({ adapter: new Adapter() });

describe('<YourComponent />', () => {
  const wrapper = shallow(
    <YourComponent
      {...yourProps}
    />
  );
  const instance = wrapper.instance(); 

  it('Should have correct dummyObj value', () => {
     expect(instance.dummyObj).toBe('dummy');
  });
});
like image 72
YuraGon Avatar answered Nov 24 '25 03:11

YuraGon



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!