Quantcast
Channel: Test react forceUpdate custom hook useEffect/useState - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Answer by Rafael Simões for Test react forceUpdate custom hook useEffect/useState

$
0
0

I was able to successfully test this hook this way

import React from "react";
import useForceUpdate from "hooks/use-force-update";

const Component = ({ shouldUpdate }) => {
  const hasUpdated = useForceUpdate(shouldUpdate);
  return <div>{hasUpdated}</div>;
};

describe("useForceUpdate", () => {
  let subject;
  let props;

  beforeEach(() => {
    props = { shouldUpdate: true };
    subject = memoize(() => mount(<Component {...props} />));
  });

  describe("when the condition is true", () => {
    it("it calls forceUpdate", () => {
      expect(
        subject()
          .find("div")
          .text()
      ).toBe("1");
    });
  });

  describe("when the condition is false", () => {
    beforeEach(() => {
      props = { shouldUpdate: false };
    });
    it("it does not call forceUpdate", () => {
      expect(
        subject()
          .find("div")
          .text()
      ).toBe("0");
    });
  });
});

Viewing all articles
Browse latest Browse all 2

Trending Articles