Browse Source

Don’t warn when a navigationButtonPress event was not handled by a component

The motivation behind this feature was to warn when a user declared a button but forgot register
a handler in a component to handle button press event.
Since buttons can be handled globally using Navigation.events().registerNavigationButtonPressedListener, this feature
is meaningless.
closes #4017
Guy Carmeli 6 years ago
parent
commit
9cc1702463

+ 1
- 30
lib/src/events/ComponentEventsObserver.test.tsx View File

5
 
5
 
6
 describe('ComponentEventsObserver', () => {
6
 describe('ComponentEventsObserver', () => {
7
   const mockEventsReceiver = new NativeEventsReceiver();
7
   const mockEventsReceiver = new NativeEventsReceiver();
8
-  let uut;
8
+  const uut = new ComponentEventsObserver(mockEventsReceiver);
9
   const didAppearFn = jest.fn();
9
   const didAppearFn = jest.fn();
10
   const didDisappearFn = jest.fn();
10
   const didDisappearFn = jest.fn();
11
   const didMountFn = jest.fn();
11
   const didMountFn = jest.fn();
70
     }
70
     }
71
   }
71
   }
72
 
72
 
73
-  beforeEach(() => {
74
-    uut = new ComponentEventsObserver(mockEventsReceiver);
75
-  });
76
-
77
   it(`bindComponent expects a component with componentId`, () => {
73
   it(`bindComponent expects a component with componentId`, () => {
78
     const tree = renderer.create(<SimpleScreen />);
74
     const tree = renderer.create(<SimpleScreen />);
79
     expect(() => uut.bindComponent(tree.getInstance() as any)).toThrow('');
75
     expect(() => uut.bindComponent(tree.getInstance() as any)).toThrow('');
202
     expect(mockEventsReceiver.registerSearchBarCancelPressedListener).toHaveBeenCalledTimes(1);
198
     expect(mockEventsReceiver.registerSearchBarCancelPressedListener).toHaveBeenCalledTimes(1);
203
     expect(mockEventsReceiver.registerPreviewCompletedListener).toHaveBeenCalledTimes(1);
199
     expect(mockEventsReceiver.registerPreviewCompletedListener).toHaveBeenCalledTimes(1);
204
   });
200
   });
205
-
206
-  it(`warn when button event is not getting handled`, () => {
207
-    const tree1 = renderer.create(<SimpleScreen componentId={'myCompId'} />);
208
-    const instance1 = tree1.getInstance() as any;
209
-    console.warn = jest.fn();
210
-    uut.bindComponent(instance1);
211
-
212
-    uut.notifyNavigationButtonPressed({ componentId: 'myCompId', buttonId: 'myButtonId' });
213
-
214
-    expect(console.warn).toHaveBeenCalledTimes(1);
215
-    expect(console.warn).toHaveBeenCalledWith(`navigationButtonPressed for button 'myButtonId' was not handled`);
216
-  });
217
-
218
-  it(`doesn't warn when button event is getting handled`, () => {
219
-    const tree1 = renderer.create(<SimpleScreen componentId={'myCompId'} />);
220
-    const instance1 = tree1.getInstance() as any;
221
-    console.warn = jest.fn();
222
-    
223
-    instance1.navigationButtonPressed = jest.fn();
224
-    uut.bindComponent(instance1);
225
-
226
-    uut.notifyNavigationButtonPressed({ componentId: 'myCompId', buttonId: 'myButtonId' });
227
-
228
-    expect(console.warn).toHaveBeenCalledTimes(0);
229
-  });
230
 });
201
 });

+ 1
- 9
lib/src/events/ComponentEventsObserver.ts View File

65
   }
65
   }
66
 
66
 
67
   notifyNavigationButtonPressed(event: NavigationButtonPressedEvent) {
67
   notifyNavigationButtonPressed(event: NavigationButtonPressedEvent) {
68
-    const listenersTriggered = this.triggerOnAllListenersByComponentId(event, 'navigationButtonPressed');
69
-    if (listenersTriggered === 0) {
70
-      // tslint:disable-next-line:no-console
71
-      console.warn(`navigationButtonPressed for button '${event.buttonId}' was not handled`);
72
-    }
68
+    this.triggerOnAllListenersByComponentId(event, 'navigationButtonPressed');
73
   }
69
   }
74
 
70
 
75
   notifyModalDismissed(event: ModalDismissedEvent) {
71
   notifyModalDismissed(event: ModalDismissedEvent) {
89
   }
85
   }
90
 
86
 
91
   private triggerOnAllListenersByComponentId(event: ComponentEvent, method: string) {
87
   private triggerOnAllListenersByComponentId(event: ComponentEvent, method: string) {
92
-    let listenersTriggered = 0;
93
     _.forEach(this.listeners[event.componentId], (component) => {
88
     _.forEach(this.listeners[event.componentId], (component) => {
94
       if (_.isObject(component) && _.isFunction(component[method])) {
89
       if (_.isObject(component) && _.isFunction(component[method])) {
95
         component[method](event);
90
         component[method](event);
96
-        listenersTriggered++;
97
       }
91
       }
98
     });
92
     });
99
-
100
-    return listenersTriggered;
101
   }
93
   }
102
 }
94
 }