Skip to content

Commit d63e3de

Browse files
committed
test: return cleanup handles from AppState and InteractionManager mocks
useAppState.test.ts and useInteractionManager.test.ts each define a local jest.mock("react-native", ...) that overrides jest.setup.js, so their mocks returned undefined. The hooks call subscription.remove() and interactionPromise.cancel() in effect cleanup, which threw during unmount (TypeError + React "detached tree" internal error). Tests passed only because the throw was swallowed as console.error. Return { remove } / { cancel } from both the default mock and the per-test mockImplementationOnce to honor the real API contract.
1 parent b93c00d commit d63e3de

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

src/useAppState.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { useAppState } from "./useAppState"
55
jest.mock("react-native", () => ({
66
AppState: {
77
currentState: "mock-currentState",
8-
addEventListener: jest.fn(),
8+
addEventListener: jest.fn(() => ({ remove: jest.fn() })),
99
removeEventListener: jest.fn(),
1010
},
1111
}))
@@ -17,6 +17,8 @@ describe("useAppState", () => {
1717

1818
addEventListenerMock.mockImplementationOnce((_, fn) => {
1919
listener = fn
20+
21+
return { remove: jest.fn() }
2022
})
2123

2224
return (newStatus: AppStateStatus) => listener(newStatus)

src/useInteractionManager.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { InteractionManager } from "react-native"
44

55
jest.mock("react-native", () => ({
66
InteractionManager: {
7-
runAfterInteractions: jest.fn(),
7+
runAfterInteractions: jest.fn(() => ({ cancel: jest.fn() })),
88
},
99
}))
1010

@@ -22,6 +22,8 @@ describe("useInteractionManager", () => {
2222

2323
runAfterInteractionsMock.mockImplementationOnce((cb) => {
2424
emitAfterInteractions = cb
25+
26+
return { cancel: jest.fn() }
2527
})
2628

2729
const { result } = renderHook(() => useInteractionManager())

0 commit comments

Comments
 (0)