@applicaster/quick-brick-core
Version:
Core package for Applicaster's Quick Brick App
73 lines (58 loc) • 1.65 kB
JavaScript
import React from "react";
import { View, Text } from "react-native";
import { shallow } from "enzyme";
import { shallowToJson } from "enzyme-to-json";
import { ErrorBoundary } from "../index";
const TestComponent = ({ error }: { error: any }) => {
if (error) {
throw new Error("error !!");
}
return (
<View>
<Text>all is good</Text>
</View>
);
};
const errorState = {
hasError: true,
error: new Error("oh no"),
info: { componentStack: "error at ... \n ..." },
};
describe("<ErrorBoundary />", () => {
describe("when there is no error", () => {
const wrapper = shallow(
<ErrorBoundary>
<TestComponent />
</ErrorBoundary>
);
it("renders correctly", () => {
expect(shallowToJson(wrapper)).toMatchSnapshot();
});
});
describe("When an error is caught", () => {
const _consoleLog = console.log; // eslint-disable-line no-console
const didCatchSpy = jest.spyOn(
ErrorBoundary.prototype,
"componentDidCatch"
);
const wrapper = shallow(
<ErrorBoundary>
<TestComponent error />
</ErrorBoundary>
);
wrapper.setState(errorState);
beforeAll(() => {
console.log = jest.fn(); // eslint-disable-line no-console
});
afterAll(() => {
console.log = _consoleLog; // eslint-disable-line no-console
});
it.skip("the componentDidCatch method is called", () => {
// a current bug with enzyme doesn't allow this to be tested
expect(didCatchSpy).toHaveBeenCalled();
});
it("renders correctly", () => {
expect(shallowToJson(wrapper)).toMatchSnapshot();
});
});
});