Skip to content

Commit fea1feb

Browse files
committed
feat(events): add new registerScreenPoppedListener
BREAKING CHANGE: RNN version was upgraded to 4.0.7
1 parent a061d21 commit fea1feb

File tree

4 files changed

+119
-40
lines changed

4 files changed

+119
-40
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"peerDependencies": {
2121
"react": ">=16.8.3",
2222
"react-native": ">=0.59.0",
23-
"react-native-navigation": ">=2.21.0"
23+
"react-native-navigation": ">=4.0.7"
2424
},
2525
"scripts": {
2626
"lint": "eslint *.js",
@@ -52,7 +52,7 @@
5252
"prettier": "1.19.1",
5353
"react": ">=16.8.3",
5454
"react-native": ">=0.59.0",
55-
"react-native-navigation": ">=2.21.0",
55+
"react-native-navigation": ">=4.0.7",
5656
"react-test-renderer": "16.12.0",
5757
"semantic-release": "15.13.31",
5858
"typescript": "3.7.2"

src/index.test.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ComponentDidAppearEvent,
55
ComponentDidDisappearEvent,
66
CommandCompletedEvent,
7+
ScreenPoppedEvent,
78
ModalDismissedEvent,
89
BottomTabSelectedEvent,
910
NavigationButtonPressedEvent,
@@ -18,6 +19,7 @@ import {
1819
useNavigationCommand,
1920
useNavigationCommandComplete,
2021
useNavigationModalDismiss,
22+
useNavigationScreenPop,
2123
useNavigationBottomTabSelect,
2224
useNavigationButtonPress,
2325
useNavigationSearchBarUpdate,
@@ -385,6 +387,97 @@ describe('useNavigationCommandComplete', () => {
385387
})
386388
})
387389

390+
describe('useNavigationScreenPop', () => {
391+
let triggerEvent: (event: ScreenPoppedEvent) => void
392+
let mockRemoveSubscription: () => void
393+
let mockHandler: () => void
394+
395+
beforeEach(() => {
396+
mockHandler = jest.fn(() => {})
397+
mockRemoveSubscription = jest.fn()
398+
399+
Navigation.events = jest.fn().mockReturnValue({
400+
registerScreenPoppedListener: jest.fn(callback => {
401+
triggerEvent = callback
402+
403+
return { remove: mockRemoveSubscription }
404+
}),
405+
})
406+
})
407+
408+
it('should remove the event listener on unmount', () => {
409+
const { result, unmount } = renderHook(() => {
410+
useNavigationScreenPop(() => {})
411+
})
412+
413+
unmount()
414+
415+
expect(mockRemoveSubscription).toBeCalledTimes(1)
416+
417+
expect(result.current).toBeUndefined()
418+
expect(result.error).toBeUndefined()
419+
})
420+
421+
it('should never call the handler if no event was triggered', () => {
422+
const { result } = renderHook(() => {
423+
useNavigationScreenPop(() => {})
424+
})
425+
426+
expect(mockHandler).toBeCalledTimes(0)
427+
428+
expect(result.current).toBeUndefined()
429+
expect(result.error).toBeUndefined()
430+
})
431+
432+
it('should call handler twice when componentId is not provided', () => {
433+
const { result } = renderHook(() => {
434+
useNavigationScreenPop(mockHandler)
435+
})
436+
437+
const event1 = { componentId: 'COMPONENT_ID_1' }
438+
triggerEvent(event1)
439+
440+
const event2 = { componentId: 'COMPONENT_ID_2' }
441+
triggerEvent(event2)
442+
443+
expect(mockHandler).toBeCalledTimes(2)
444+
expect(mockHandler).toHaveBeenNthCalledWith(1, event1)
445+
expect(mockHandler).toHaveBeenNthCalledWith(2, event2)
446+
447+
expect(result.current).toBeUndefined()
448+
expect(result.error).toBeUndefined()
449+
})
450+
451+
it('should call handler once if componentId provided', () => {
452+
const { result } = renderHook(() => {
453+
useNavigationScreenPop(mockHandler, 'COMPONENT_ID_1')
454+
})
455+
456+
const event = { componentId: 'COMPONENT_ID_1' }
457+
triggerEvent(event)
458+
459+
expect(mockHandler).toBeCalledTimes(1)
460+
expect(mockHandler).toBeCalledWith(event)
461+
462+
expect(result.current).toBeUndefined()
463+
expect(result.error).toBeUndefined()
464+
})
465+
466+
it('should never call the handler if componentId does not match', () => {
467+
const { result } = renderHook(() => {
468+
useNavigationScreenPop(mockHandler, 'COMPONENT_ID_1')
469+
})
470+
471+
const event = { componentId: 'COMPONENT_ID_2' }
472+
triggerEvent(event)
473+
474+
expect(mockHandler).toBeCalledTimes(0)
475+
476+
expect(result.current).toBeUndefined()
477+
expect(result.error).toBeUndefined()
478+
})
479+
})
480+
388481
describe('useNavigationModalDismiss', () => {
389482
let triggerEvent: (event: ModalDismissedEvent) => void
390483
let mockRemoveSubscription: () => void

src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
ComponentDidDisappearEvent,
66
CommandCompletedEvent,
77
ModalDismissedEvent,
8+
ScreenPoppedEvent,
89
BottomTabSelectedEvent,
910
NavigationButtonPressedEvent,
1011
SearchBarUpdatedEvent,
@@ -95,6 +96,22 @@ function useNavigationModalDismiss(handler: (event: ModalDismissedEvent) => void
9596
}, [handler, componentId])
9697
}
9798

99+
function useNavigationScreenPop(handler: (event: ScreenPoppedEvent) => void, componentId?: string) {
100+
useLayoutEffect(() => {
101+
const subscription = Navigation.events().registerScreenPoppedListener(event => {
102+
const equalCommandId = event.componentId === componentId
103+
104+
if (componentId && !equalCommandId) {
105+
return
106+
}
107+
108+
handler(event)
109+
})
110+
111+
return () => subscription.remove()
112+
}, [handler, componentId])
113+
}
114+
98115
function useNavigationBottomTabSelect(handler: (event: BottomTabSelectedEvent) => void) {
99116
useLayoutEffect(() => {
100117
const subscription = Navigation.events().registerBottomTabSelectedListener(handler)
@@ -181,6 +198,7 @@ export {
181198
useNavigationCommand,
182199
useNavigationCommandComplete,
183200
useNavigationModalDismiss,
201+
useNavigationScreenPop,
184202
useNavigationBottomTabSelect,
185203
useNavigationButtonPress,
186204
useNavigationSearchBarUpdate,

yarn.lock

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2870,7 +2870,7 @@ debug@^4.0.0, debug@^4.0.1, debug@^4.1.0, debug@^4.1.1:
28702870
dependencies:
28712871
ms "^2.1.1"
28722872

2873-
debuglog@*, debuglog@^1.0.1:
2873+
debuglog@^1.0.1:
28742874
version "1.0.1"
28752875
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
28762876
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
@@ -4294,7 +4294,7 @@ import-local@^2.0.0:
42944294
pkg-dir "^3.0.0"
42954295
resolve-cwd "^2.0.0"
42964296

4297-
imurmurhash@*, imurmurhash@^0.1.4:
4297+
imurmurhash@^0.1.4:
42984298
version "0.1.4"
42994299
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
43004300
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
@@ -5573,11 +5573,6 @@ lockfile@^1.0.4:
55735573
dependencies:
55745574
signal-exit "^3.0.2"
55755575

5576-
lodash._baseindexof@*:
5577-
version "3.1.0"
5578-
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
5579-
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=
5580-
55815576
lodash._baseuniq@~4.6.0:
55825577
version "4.6.0"
55835578
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
@@ -5586,33 +5581,11 @@ lodash._baseuniq@~4.6.0:
55865581
lodash._createset "~4.0.0"
55875582
lodash._root "~3.0.0"
55885583

5589-
lodash._bindcallback@*:
5590-
version "3.0.1"
5591-
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
5592-
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=
5593-
5594-
lodash._cacheindexof@*:
5595-
version "3.0.2"
5596-
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
5597-
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=
5598-
5599-
lodash._createcache@*:
5600-
version "3.1.2"
5601-
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
5602-
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
5603-
dependencies:
5604-
lodash._getnative "^3.0.0"
5605-
56065584
lodash._createset@~4.0.0:
56075585
version "4.0.3"
56085586
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
56095587
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=
56105588

5611-
lodash._getnative@*, lodash._getnative@^3.0.0:
5612-
version "3.9.1"
5613-
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
5614-
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=
5615-
56165589
lodash._root@~3.0.0:
56175590
version "3.0.1"
56185591
resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692"
@@ -5653,11 +5626,6 @@ lodash.isstring@^4.0.1:
56535626
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"
56545627
integrity sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=
56555628

5656-
lodash.restparam@*:
5657-
version "3.6.1"
5658-
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
5659-
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=
5660-
56615629
lodash.set@^4.3.2:
56625630
version "4.3.2"
56635631
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
@@ -7547,10 +7515,10 @@ [email protected]:
75477515
resolved "https://registry.yarnpkg.com/react-lifecycles-compat/-/react-lifecycles-compat-2.0.0.tgz#71d9c4cde47114c4102454f76da055c2bc48c948"
75487516
integrity sha512-txfpPCQYiazVdcbMRhatqWKcAxJweUu2wDXvts5/7Wyp6+Y9cHojqXHsLPEckzutfHlxZhG8Oiundbmp8Fd6eQ==
75497517

7550-
react-native-navigation@>=2.21.0:
7551-
version "3.6.0"
7552-
resolved "https://registry.yarnpkg.com/react-native-navigation/-/react-native-navigation-3.6.0.tgz#4772231b6a5dc06b9562f70f03c5a4b90797ae18"
7553-
integrity sha512-ZVlN86gTrCFKIvnnN5/+eBT3DNrmPo0mUdzWvXHLbLixhpAbrhkybpfiC72KUrsitQBIeiqP/R2XYYAt4L6NuA==
7518+
react-native-navigation@>=4.0.7:
7519+
version "4.8.4"
7520+
resolved "https://registry.yarnpkg.com/react-native-navigation/-/react-native-navigation-4.8.4.tgz#a9e4c272054d831c66bb406c5f164c108ba8e942"
7521+
integrity sha512-aORJK7B2Jih29wmszRwV3zwuIwrXWtQjdVm0bVMMzk0iPKJMMrCvW+GZIAxxHhJfKEdy7GNCdyRv0g43/Frj2A==
75547522
dependencies:
75557523
hoist-non-react-statics "3.x.x"
75567524
lodash "4.17.x"

0 commit comments

Comments
 (0)