@@ -355,7 +355,7 @@ describe('getInAppMessages', () => {
355355 const secondCloseLink = (
356356 document . getElementById ( 'iterable-iframe' ) as HTMLIFrameElement
357357 ) ?. contentWindow ?. document . body ?. querySelector (
358- 'a[href ="action://close-second-iframe"]'
358+ 'a[data-qa-original-link ="action://close-second-iframe"]'
359359 ) ;
360360 expect ( secondCloseLink ) . not . toBe ( null ) ;
361361 expect ( secondCloseLink ) . not . toBeUndefined ( ) ;
@@ -683,5 +683,137 @@ describe('getInAppMessages', () => {
683683 ) ;
684684 expect ( secondCloseLink ) . toBeUndefined ( ) ;
685685 } ) ;
686+
687+ it ( 'should call global.postMessage when action:// link is clicked' , async ( ) => {
688+ const { request } = getInAppMessages (
689+ { count : 10 , packageName : 'my-lil-website' } ,
690+ true
691+ ) ;
692+ await request ( ) ;
693+
694+ const mockHandler = jest . fn ( ) ;
695+ global . postMessage = mockHandler ;
696+
697+ const iframe = document . getElementById (
698+ 'iterable-iframe'
699+ ) as HTMLIFrameElement ;
700+ const element = iframe ?. contentWindow ?. document . body ?. querySelector (
701+ 'a[data-qa-original-link="action://close-first-iframe"]'
702+ ) as Element ;
703+
704+ const clickEvent = new MouseEvent ( 'click' ) ;
705+ await element . dispatchEvent ( clickEvent ) ;
706+ expect ( mockHandler ) . toHaveBeenCalledWith (
707+ {
708+ data : 'close-first-iframe' ,
709+ type : 'iterable-action-link'
710+ } ,
711+ '*'
712+ ) ;
713+ expect ( document . getElementById ( 'iterable-iframe' ) ) . toBe ( null ) ;
714+ expect (
715+ JSON . parse (
716+ mockRequest . history . post . filter (
717+ ( e ) => ! ! e . url ?. match ( / I n A p p C l i c k / gim)
718+ ) [ 0 ] . data
719+ ) . clickedUrl
720+ ) . toBe ( 'action://close-first-iframe' ) ;
721+ } ) ;
722+
723+ it ( 'should do nothing upon clicking itbl:// links' , async ( ) => {
724+ mockRequest . onGet ( '/inApp/getMessages' ) . reply ( 200 , {
725+ inAppMessages : [
726+ {
727+ ...messages [ 0 ] ,
728+ content : {
729+ ...messages [ 0 ] . content ,
730+ html : '<a href="itbl://whatever">profile</a>'
731+ }
732+ }
733+ ]
734+ } ) ;
735+
736+ const { request } = getInAppMessages (
737+ { count : 10 , packageName : 'my-lil-website' } ,
738+ true
739+ ) ;
740+ await request ( ) ;
741+
742+ const iframe = document . getElementById (
743+ 'iterable-iframe'
744+ ) as HTMLIFrameElement ;
745+ const element = iframe ?. contentWindow ?. document . body ?. querySelector (
746+ 'a[href="itbl://whatever"]'
747+ ) as Element ;
748+
749+ const clickEvent = new MouseEvent ( 'click' ) ;
750+ await element . dispatchEvent ( clickEvent ) ;
751+ expect ( document . getElementById ( 'iterable-iframe' ) ) . not . toBe ( null ) ;
752+ expect ( document . getElementById ( 'iterable-iframe' ) ) . not . toBeUndefined ( ) ;
753+ } ) ;
754+
755+ it ( 'should do nothing upon clicking itbl://dismiss links' , async ( ) => {
756+ mockRequest . onGet ( '/inApp/getMessages' ) . reply ( 200 , {
757+ inAppMessages : [
758+ {
759+ ...messages [ 0 ] ,
760+ content : {
761+ ...messages [ 0 ] . content ,
762+ html : '<a href="itbl://dismiss">profile</a>'
763+ }
764+ }
765+ ]
766+ } ) ;
767+
768+ const { request } = getInAppMessages (
769+ { count : 10 , packageName : 'my-lil-website' } ,
770+ true
771+ ) ;
772+ await request ( ) ;
773+
774+ const iframe = document . getElementById (
775+ 'iterable-iframe'
776+ ) as HTMLIFrameElement ;
777+ const element = iframe ?. contentWindow ?. document . body ?. querySelector (
778+ 'a[href="itbl://dismiss"]'
779+ ) as Element ;
780+
781+ const clickEvent = new MouseEvent ( 'click' ) ;
782+ await element . dispatchEvent ( clickEvent ) ;
783+ expect ( document . getElementById ( 'iterable-iframe' ) ) . not . toBe ( null ) ;
784+ expect ( document . getElementById ( 'iterable-iframe' ) ) . not . toBeUndefined ( ) ;
785+ } ) ;
786+
787+ it ( 'should do nothing upon clicking iterable:// non-dismiss links' , async ( ) => {
788+ mockRequest . onGet ( '/inApp/getMessages' ) . reply ( 200 , {
789+ inAppMessages : [
790+ {
791+ ...messages [ 0 ] ,
792+ content : {
793+ ...messages [ 0 ] . content ,
794+ html : '<a href="iterable://whatever">profile</a>'
795+ }
796+ }
797+ ]
798+ } ) ;
799+
800+ const { request } = getInAppMessages (
801+ { count : 10 , packageName : 'my-lil-website' } ,
802+ true
803+ ) ;
804+ await request ( ) ;
805+
806+ const iframe = document . getElementById (
807+ 'iterable-iframe'
808+ ) as HTMLIFrameElement ;
809+ const element = iframe ?. contentWindow ?. document . body ?. querySelector (
810+ 'a[href="iterable://whatever"]'
811+ ) as Element ;
812+
813+ const clickEvent = new MouseEvent ( 'click' ) ;
814+ await element . dispatchEvent ( clickEvent ) ;
815+ expect ( document . getElementById ( 'iterable-iframe' ) ) . not . toBe ( null ) ;
816+ expect ( document . getElementById ( 'iterable-iframe' ) ) . not . toBeUndefined ( ) ;
817+ } ) ;
686818 } ) ;
687819} ) ;
0 commit comments