Skip to content

Instantly share code, notes, and snippets.

@codegagan
Created August 30, 2020 07:20
Show Gist options
  • Save codegagan/329b414ee72bf978b6db398d3a020fd0 to your computer and use it in GitHub Desktop.
Save codegagan/329b414ee72bf978b6db398d3a020fd0 to your computer and use it in GitHub Desktop.
Test conditional rendering based on state
import * as reactModule from "react";
import { shallow } from "enzyme";
import ConditionalComponent from "./ConditionalComponent";
describe("test Conditional component", () => {
it("should render while loading", () => {
const loadingValue = true;
reactModule.useState = jest.fn(initialLoadingValue => [
loadingValue,
() => {}
]);
wrapper = shallow(<ConditionalComponent />);
expect(wrapper).toMatchSnapshot();
});
it("should render after loading", () => {
const loadingValue = false;
reactModule.useState = jest.fn(initialLoadingValue => [
loadingValue,
() => {}
]);
wrapper = shallow(<ConditionalComponent />);
expect(wrapper).toMatchSnapshot();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment