@@ -48,6 +48,10 @@ import { expect } from 'chai';
48
48
import { getFakeFirebaseDependencies } from '../testing/fakes/firebase-dependencies' ;
49
49
import { getFakeTokenDetails } from '../testing/fakes/token-details' ;
50
50
51
+ const LOCAL_HOST = self . location . host ;
52
+ const TEST_LINK = 'https://' + LOCAL_HOST + '/test-link.org' ;
53
+ const TEST_CLICK_ACTION = 'https://' + LOCAL_HOST + '/test-click-action.org' ;
54
+
51
55
// Add fake SW types.
52
56
declare const self : Window & Writable < ServiceWorkerGlobalScope > ;
53
57
@@ -59,7 +63,7 @@ const DISPLAY_MESSAGE: MessagePayloadInternal = {
59
63
body : 'body'
60
64
} ,
61
65
fcmOptions : {
62
- link : 'https://example.org'
66
+ link : TEST_LINK
63
67
} ,
64
68
from : 'from' ,
65
69
// eslint-disable-next-line camelcase
@@ -454,9 +458,29 @@ describe('SwController', () => {
454
458
expect ( matchAllSpy ) . not . to . have . been . called ;
455
459
} ) ;
456
460
461
+ it ( 'does not redirect if link is not from origin' , async ( ) => {
462
+ // Remove link.
463
+ NOTIFICATION_CLICK_PAYLOAD . notification ! . data ! [ FCM_MSG ] . fcmOptions . link =
464
+ 'https://www.youtube.com' ;
465
+
466
+ const event = makeEvent ( 'notificationclick' , NOTIFICATION_CLICK_PAYLOAD ) ;
467
+ const stopImmediatePropagationSpy = spy (
468
+ event ,
469
+ 'stopImmediatePropagation'
470
+ ) ;
471
+ const notificationCloseSpy = spy ( event . notification , 'close' ) ;
472
+ const matchAllSpy = spy ( self . clients , 'matchAll' ) ;
473
+
474
+ await callEventListener ( event ) ;
475
+
476
+ expect ( stopImmediatePropagationSpy ) . to . have . been . called ;
477
+ expect ( notificationCloseSpy ) . to . have . been . called ;
478
+ expect ( matchAllSpy ) . not . to . have . been . called ;
479
+ } ) ;
480
+
457
481
it ( 'focuses on and sends the message to an open WindowClient' , async ( ) => {
458
482
const client : Writable < WindowClient > = ( await self . clients . openWindow (
459
- 'https://example.org'
483
+ TEST_LINK
460
484
) ) ! ;
461
485
const focusSpy = spy ( client , 'focus' ) ;
462
486
const matchAllSpy = spy ( self . clients , 'matchAll' ) ;
@@ -485,15 +509,15 @@ describe('SwController', () => {
485
509
await callEventListener ( event ) ;
486
510
487
511
expect ( matchAllSpy ) . to . have . been . called ;
488
- expect ( openWindowSpy ) . to . have . been . calledWith ( 'https://example.org' ) ;
512
+ expect ( openWindowSpy ) . to . have . been . calledWith ( TEST_LINK ) ;
489
513
} ) ;
490
514
491
515
it ( 'works with click_action' , async ( ) => {
492
516
// Replace link with the deprecated click_action.
493
517
delete NOTIFICATION_CLICK_PAYLOAD . notification ! . data ! [ FCM_MSG ] . fcmOptions ;
494
518
NOTIFICATION_CLICK_PAYLOAD . notification ! . data ! [
495
519
FCM_MSG
496
- ] . notification . click_action = 'https://example.org' ; // eslint-disable-line camelcase
520
+ ] . notification . click_action = TEST_CLICK_ACTION ; // eslint-disable-line camelcase
497
521
498
522
const matchAllSpy = spy ( self . clients , 'matchAll' ) ;
499
523
const openWindowSpy = spy ( self . clients , 'openWindow' ) ;
@@ -503,7 +527,7 @@ describe('SwController', () => {
503
527
await callEventListener ( event ) ;
504
528
505
529
expect ( matchAllSpy ) . to . have . been . called ;
506
- expect ( openWindowSpy ) . to . have . been . calledWith ( 'https://example.org' ) ;
530
+ expect ( openWindowSpy ) . to . have . been . calledWith ( TEST_CLICK_ACTION ) ;
507
531
} ) ;
508
532
509
533
it ( 'redirects to origin if message was sent from the FN Console' , async ( ) => {
0 commit comments