Skip to content

Commit

Permalink
RTL hooks error
Browse files Browse the repository at this point in the history
  • Loading branch information
imjordanxd committed Dec 9, 2024
1 parent e160182 commit 461c5e7
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<React.PropsWithChildren> {
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<React.PropsWithChildren> {
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<React.PropsWithChildren> {
componentDidCatch = onError
render() {
return this.props.children
}
}
}
)
expect(onError).not.toBeCalled()
})
})
69 changes: 51 additions & 18 deletions packages/mobx-react-lite/__tests__/useAsObservableSource.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<React.PropsWithChildren> {
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<React.PropsWithChildren> {
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<React.PropsWithChildren> {
componentDidCatch = onError
render() {
return this.props.children
}
}
}
)
expect(onError).not.toBeCalled()
})
})

Expand Down
135 changes: 84 additions & 51 deletions packages/mobx-react-lite/__tests__/useLocalObservable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<React.PropsWithChildren> {
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<React.PropsWithChildren> {
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<React.PropsWithChildren> {
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)
})
})

0 comments on commit 461c5e7

Please sign in to comment.