user944513
user944513

Reputation: 12729

How to test or get value from state in react?

Hi could you please tell me How to test or get value from state in react ? getting error wrapper.instance(...).handleClickShowPassword is not a function

here is my code https://codesandbox.io/s/l2lk4n794l

it("toggle showpassword value", () => {
    wrapper.setState({ showPassword: false });
    wrapper.instance().handleClickShowPassword();

    expect(wrapper.state.showPassword).toEqual(true);
  });

enter image description here

Upvotes: 0

Views: 156

Answers (1)

Shubham Khatri
Shubham Khatri

Reputation: 281686

Since LoginContainer is an wrapped with an HOC, you either need to export the component without withStyles HOC or use dive on the wrapper to get the instance of the component. Also state is a function on component instance and hence you need to call it to access state

describe("<LoginContainer/>", () => {
  let wrapper;
  beforeEach(() => {
    wrapper = shallow(<LoginContainer />);
  });

  it("toggle showpassword value", () => {
    const comp = wrapper.dive();
    comp.dive().setState({ showPassword: false });
    comp.instance().handleClickShowPassword();
    expect(comp.state("showPassword")).toEqual(true);
  });
});

Working demo

Upvotes: 1

Related Questions