Farmbot-Web-App/frontend/__tests__/error_boundary_test.tsx

36 lines
1.0 KiB
TypeScript
Raw Permalink Normal View History

2020-02-07 16:05:16 -07:00
jest.unmock("../error_boundary");
2018-04-02 12:11:00 -06:00
jest.mock("../util/errors.ts", () => ({ catchErrors: jest.fn() }));
import * as React from "react";
import { mount } from "enzyme";
2018-04-02 12:11:00 -06:00
import { ErrorBoundary } from "../error_boundary";
import { catchErrors } from "../util";
class Kaboom extends React.Component<{}, {}> {
TRUE = (1 + 1) === 2;
render() {
if (this.TRUE) {
throw new Error("ALWAYS");
} else {
return <div />;
}
}
}
describe("<ErrorBoundary/>", () => {
it("handles exceptions", () => {
2019-06-05 14:48:18 -06:00
console.error = jest.fn();
2018-04-02 12:11:00 -06:00
const nodes = <ErrorBoundary><Kaboom /></ErrorBoundary>;
const el = mount<ErrorBoundary>(nodes);
2018-04-02 12:11:00 -06:00
expect(el.text()).toContain("can't render this part of the page");
const i = el.instance();
expect(i.state.hasError).toBe(true);
expect(catchErrors).toHaveBeenCalled();
2019-06-05 14:48:18 -06:00
expect(console.error).toHaveBeenCalledTimes(2);
expect(console.error).toHaveBeenCalledWith(expect.any(String), Error("ALWAYS"));
expect(console.error).toHaveBeenCalledWith(expect.stringContaining("Kaboom"));
2018-04-02 12:11:00 -06:00
});
});