Skip to content

Commit 52f429c

Browse files
committed
feat(events): add BottomTabPressed event
1 parent c20bf26 commit 52f429c

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/index.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
useNavigationModalDismiss,
2626
useNavigationScreenPop,
2727
useNavigationBottomTabSelect,
28+
useNavigationBottomTabPress,
2829
useNavigationBottomTabLongPress,
2930
useNavigationButtonPress,
3031
useNavigationSearchBarUpdate,
@@ -770,6 +771,68 @@ describe('useNavigationBottomTabSelect', () => {
770771
})
771772
})
772773

774+
describe('useNavigationBottomTabPress', () => {
775+
let triggerEvent: (event: BottomTabLongPressedEvent) => void
776+
let mockRemoveSubscription: () => void
777+
let mockHandler: () => void
778+
779+
beforeEach(() => {
780+
mockHandler = jest.fn(() => {})
781+
mockRemoveSubscription = jest.fn()
782+
783+
Navigation.events = jest.fn().mockReturnValue({
784+
registerBottomTabPressedListener: jest.fn(callback => {
785+
triggerEvent = callback
786+
787+
return { remove: mockRemoveSubscription }
788+
}),
789+
})
790+
})
791+
792+
it('should remove the event listener on unmount', () => {
793+
const { result, unmount } = renderHook(() => {
794+
useNavigationBottomTabPress(() => {})
795+
})
796+
797+
unmount()
798+
799+
expect(mockRemoveSubscription).toBeCalledTimes(1)
800+
801+
expect(result.current).toBeUndefined()
802+
expect(result.error).toBeUndefined()
803+
})
804+
805+
it('should never call the handler if no event was triggered', () => {
806+
const { result } = renderHook(() => {
807+
useNavigationBottomTabPress(() => {})
808+
})
809+
810+
expect(mockHandler).toBeCalledTimes(0)
811+
812+
expect(result.current).toBeUndefined()
813+
expect(result.error).toBeUndefined()
814+
})
815+
816+
it('should call handler twice', () => {
817+
const { result } = renderHook(() => {
818+
useNavigationBottomTabPress(mockHandler)
819+
})
820+
821+
const event1 = { selectedTabIndex: 1 }
822+
triggerEvent(event1)
823+
824+
const event2 = { selectedTabIndex: 1 }
825+
triggerEvent(event2)
826+
827+
expect(mockHandler).toBeCalledTimes(2)
828+
expect(mockHandler).toHaveBeenNthCalledWith(1, event1)
829+
expect(mockHandler).toHaveBeenNthCalledWith(2, event2)
830+
831+
expect(result.current).toBeUndefined()
832+
expect(result.error).toBeUndefined()
833+
})
834+
})
835+
773836
describe('useNavigationBottomTabLongPress', () => {
774837
let triggerEvent: (event: BottomTabLongPressedEvent) => void
775838
let mockRemoveSubscription: () => void

src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
ModalDismissedEvent,
99
ScreenPoppedEvent,
1010
BottomTabSelectedEvent,
11+
BottomTabPressedEvent,
1112
BottomTabLongPressedEvent,
1213
NavigationButtonPressedEvent,
1314
SearchBarUpdatedEvent,
@@ -141,6 +142,14 @@ function useNavigationBottomTabSelect(handler: (event: BottomTabSelectedEvent) =
141142
}, [handler])
142143
}
143144

145+
function useNavigationBottomTabPress(handler: (event: BottomTabPressedEvent) => void) {
146+
useLayoutEffect(() => {
147+
const subscription = Navigation.events().registerBottomTabPressedListener(handler)
148+
149+
return () => subscription.remove()
150+
}, [handler])
151+
}
152+
144153
function useNavigationBottomTabLongPress(handler: (event: BottomTabLongPressedEvent) => void) {
145154
useLayoutEffect(() => {
146155
const subscription = Navigation.events().registerBottomTabLongPressedListener(handler)
@@ -230,6 +239,7 @@ export {
230239
useNavigationModalDismiss,
231240
useNavigationScreenPop,
232241
useNavigationBottomTabSelect,
242+
useNavigationBottomTabPress,
233243
useNavigationBottomTabLongPress,
234244
useNavigationButtonPress,
235245
useNavigationSearchBarUpdate,

0 commit comments

Comments
 (0)