File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed
Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -181,4 +181,30 @@ describe('Web Plugin', () => {
181181
182182 expect ( handlerFunction ) . not . toHaveBeenCalled ( ) ;
183183 } ) ;
184+
185+ it ( 'Should not remove a listener if it is not found' , async ( ) => {
186+ const lf1 = ( event : any ) => {
187+ console . log ( event ) ;
188+ } ;
189+ const lf2 = ( event : any ) => {
190+ console . log ( event ) ;
191+ } ;
192+ const lf3 = ( event : any ) => {
193+ console . log ( event ) ;
194+ } ;
195+
196+ await plugin . addListener ( 'test' , lf1 ) ;
197+ await plugin . addListener ( 'test' , lf2 ) ;
198+
199+ const listenersBefore = plugin . getListeners ( ) [ 'test' ] ;
200+ expect ( listenersBefore . length ) . toEqual ( 2 ) ;
201+
202+ // Try to remove a listener that was never added
203+ await ( plugin as any ) . removeListener ( 'test' , lf3 ) ;
204+
205+ const listenersAfter = plugin . getListeners ( ) [ 'test' ] ;
206+ expect ( listenersAfter . length ) . toEqual ( 2 ) ;
207+ expect ( listenersAfter [ 0 ] ) . toBe ( lf1 ) ;
208+ expect ( listenersAfter [ 1 ] ) . toBe ( lf2 ) ;
209+ } ) ;
184210} ) ;
Original file line number Diff line number Diff line change @@ -98,7 +98,9 @@ export class WebPlugin implements Plugin {
9898 }
9999
100100 const index = listeners . indexOf ( listenerFunc ) ;
101- this . listeners [ eventName ] . splice ( index , 1 ) ;
101+ if ( index !== - 1 ) {
102+ this . listeners [ eventName ] . splice ( index , 1 ) ;
103+ }
102104
103105 // If there are no more listeners for this type of event,
104106 // remove the window listener
You can’t perform that action at this time.
0 commit comments