diff --git a/packages/mobx-react-lite/__tests__/useAsObservableSource.deprecated.test.tsx b/packages/mobx-react-lite/__tests__/useAsObservableSource.deprecated.test.tsx index e9b525943..96bca7823 100644 --- a/packages/mobx-react-lite/__tests__/useAsObservableSource.deprecated.test.tsx +++ b/packages/mobx-react-lite/__tests__/useAsObservableSource.deprecated.test.tsx @@ -287,29 +287,62 @@ describe("combining observer with props and stores", () => { describe("enforcing actions", () => { it("'never' should work", () => { configure({ enforceActions: "never" }) - const { result } = renderHook(() => { - const [thing, setThing] = React.useState("world") - useAsObservableSource({ hello: thing }) - useEffect(() => setThing("react"), []) - }) - expect(result.error).not.toBeDefined() + const onError = jest.fn() + renderHook( + () => { + const [thing, setThing] = React.useState("world") + useAsObservableSource({ hello: thing }) + useEffect(() => setThing("react"), []) + }, + { + wrapper: class extends React.Component { + componentDidCatch = onError + render() { + return this.props.children + } + } + } + ) + expect(onError).not.toBeCalled() }) it("only when 'observed' should work", () => { configure({ enforceActions: "observed" }) - const { result } = renderHook(() => { - const [thing, setThing] = React.useState("world") - useAsObservableSource({ hello: thing }) - useEffect(() => setThing("react"), []) - }) - expect(result.error).not.toBeDefined() + const onError = jest.fn() + renderHook( + () => { + const [thing, setThing] = React.useState("world") + useAsObservableSource({ hello: thing }) + useEffect(() => setThing("react"), []) + }, + { + wrapper: class extends React.Component { + componentDidCatch = onError + render() { + return this.props.children + } + } + } + ) + expect(onError).not.toBeCalled() }) it("'always' should work", () => { configure({ enforceActions: "always" }) - const { result } = renderHook(() => { - const [thing, setThing] = React.useState("world") - useAsObservableSource({ hello: thing }) - useEffect(() => setThing("react"), []) - }) - expect(result.error).not.toBeDefined() + const onError = jest.fn() + renderHook( + () => { + const [thing, setThing] = React.useState("world") + useAsObservableSource({ hello: thing }) + useEffect(() => setThing("react"), []) + }, + { + wrapper: class extends React.Component { + componentDidCatch = onError + render() { + return this.props.children + } + } + } + ) + expect(onError).not.toBeCalled() }) }) diff --git a/packages/mobx-react-lite/__tests__/useAsObservableSource.test.tsx b/packages/mobx-react-lite/__tests__/useAsObservableSource.test.tsx index 61cccae0b..17a3d2319 100644 --- a/packages/mobx-react-lite/__tests__/useAsObservableSource.test.tsx +++ b/packages/mobx-react-lite/__tests__/useAsObservableSource.test.tsx @@ -350,30 +350,63 @@ describe("combining observer with props and stores", () => { describe("enforcing actions", () => { it("'never' should work", () => { configure({ enforceActions: "never" }) - const { result } = renderHook(() => { - const [thing, setThing] = React.useState("world") - useLocalObservable(() => ({ hello: thing })) - useEffect(() => setThing("react"), []) - }) - expect(result.error).not.toBeDefined() + const onError = jest.fn() + renderHook( + () => { + const [thing, setThing] = React.useState("world") + useLocalObservable(() => ({ hello: thing })) + useEffect(() => setThing("react"), []) + }, + { + wrapper: class extends React.Component { + componentDidCatch = onError + render() { + return this.props.children + } + } + } + ) + expect(onError).not.toBeCalled() }) it("only when 'observed' should work", () => { configure({ enforceActions: "observed" }) - const { result } = renderHook(() => { - const [thing, setThing] = React.useState("world") - useLocalObservable(() => ({ hello: thing })) - useEffect(() => setThing("react"), []) - }) - expect(result.error).not.toBeDefined() + const onError = jest.fn() + renderHook( + () => { + const [thing, setThing] = React.useState("world") + useLocalObservable(() => ({ hello: thing })) + useEffect(() => setThing("react"), []) + }, + { + wrapper: class extends React.Component { + componentDidCatch = onError + render() { + return this.props.children + } + } + } + ) + expect(onError).not.toBeCalled() }) it("'always' should work", () => { configure({ enforceActions: "always" }) - const { result } = renderHook(() => { - const [thing, setThing] = React.useState("world") - useLocalObservable(() => ({ hello: thing })) - useEffect(() => setThing("react"), []) - }) - expect(result.error).not.toBeDefined() + const onError = jest.fn() + renderHook( + () => { + const [thing, setThing] = React.useState("world") + useLocalObservable(() => ({ hello: thing })) + useEffect(() => setThing("react"), []) + }, + { + wrapper: class extends React.Component { + componentDidCatch = onError + render() { + return this.props.children + } + } + } + ) + expect(onError).not.toBeCalled() }) }) diff --git a/packages/mobx-react-lite/__tests__/useLocalObservable.test.tsx b/packages/mobx-react-lite/__tests__/useLocalObservable.test.tsx index 957747881..6ee6a45f6 100644 --- a/packages/mobx-react-lite/__tests__/useLocalObservable.test.tsx +++ b/packages/mobx-react-lite/__tests__/useLocalObservable.test.tsx @@ -439,75 +439,108 @@ describe("enforcing actions", () => { mobx.configure({ enforceActions: "never" }) consoleWarnMock = jest.spyOn(console, "warn").mockImplementation(() => {}) - const { result } = renderHook(() => { - const [multiplier, setMultiplier] = React.useState(2) - const store = useLocalObservable(() => ({ - multiplier, - count: 10, - get multiplied() { - return this.multiplier * this.count - }, - inc() { - this.count += 1 + const onError = jest.fn() + const { result } = renderHook( + () => { + const [multiplier, setMultiplier] = React.useState(2) + const store = useLocalObservable(() => ({ + multiplier, + count: 10, + get multiplied() { + return this.multiplier * this.count + }, + inc() { + this.count += 1 + } + })) + useEffect(() => { + store.multiplier = multiplier + }, [multiplier]) + useEffect(() => setMultiplier(3), []) + }, + { + wrapper: class extends React.Component { + componentDidCatch = onError + render() { + return this.props.children + } } - })) - useEffect(() => { - store.multiplier = multiplier - }, [multiplier]) - useEffect(() => setMultiplier(3), []) - }) + } + ) - expect(result.error).not.toBeDefined() + expect(onError).not.toBeCalled() expect(consoleWarnMock).not.toBeCalled() }) it("only when 'observed' should work", () => { mobx.configure({ enforceActions: "observed" }) consoleWarnMock = jest.spyOn(console, "warn").mockImplementation(() => {}) - const { result } = renderHook(() => { - const [multiplier, setMultiplier] = React.useState(2) - const store = useLocalObservable(() => ({ - multiplier, - count: 10, - get multiplied() { - return this.multiplier * this.count - }, - inc() { - this.count += 1 + const onError = jest.fn() + renderHook( + () => { + const [multiplier, setMultiplier] = React.useState(2) + const store = useLocalObservable(() => ({ + multiplier, + count: 10, + get multiplied() { + return this.multiplier * this.count + }, + inc() { + this.count += 1 + } + })) + useEffect(() => { + store.multiplier = multiplier + }, [multiplier]) + useEffect(() => setMultiplier(3), []) + }, + { + wrapper: class extends React.Component { + componentDidCatch = onError + render() { + return this.props.children + } } - })) - useEffect(() => { - store.multiplier = multiplier - }, [multiplier]) - useEffect(() => setMultiplier(3), []) - }) + } + ) - expect(result.error).not.toBeDefined() + expect(onError).not.toBeCalled() expect(consoleWarnMock).not.toBeCalled() }) it("'always' should work", () => { mobx.configure({ enforceActions: "always" }) consoleWarnMock = jest.spyOn(console, "warn").mockImplementation(() => {}) - const { result } = renderHook(() => { - const [multiplier, setMultiplier] = React.useState(2) - const store = useLocalObservable(() => ({ - multiplier, - count: 10, - get multiplied() { - return this.multiplier * this.count - }, - inc() { - this.count += 1 + const onError = jest.fn() + renderHook( + () => { + const [multiplier, setMultiplier] = React.useState(2) + const store = useLocalObservable(() => ({ + multiplier, + count: 10, + get multiplied() { + return this.multiplier * this.count + }, + inc() { + this.count += 1 + } + })) + useEffect(() => { + store.multiplier = multiplier + }, [multiplier]) + useEffect(() => setMultiplier(3), []) + }, + { + wrapper: class extends React.Component { + componentDidCatch = onError + render() { + return this.props.children + } } - })) - useEffect(() => { - store.multiplier = multiplier - }, [multiplier]) - useEffect(() => setMultiplier(3), []) - }) + } + ) - expect(result.error).not.toBeDefined() + expect(onError).not.toBeCalled() expect(consoleWarnMock).toBeCalledTimes(2) }) })