import { createUntypedMessageEvent } from '../test_utils/messages' ; import { createMessageListener , isPermittedOrigin , PostMessageListener , sendMessage } from './comms' ; import { channelReadyMessage , PostMessageType } from './post_messages' ; describe ( 'comms' , ( ) => { describe ( 'isPermittedOrigin' , ( ) => { it ( 'should return true when the origin is allowed' , ( ) => { let permittedOrigins = [ 'http://localhost:8000' , 'https://example.com' ] ; expect ( isPermittedOrigin ( 'http://localhost:8000' , permittedOrigins ) ) . toBe ( true ) ; expect ( isPermittedOrigin ( 'https://example.com' , permittedOrigins ) ) . toBe ( true ) ; expect ( isPermittedOrigin ( 'http://example.com' , permittedOrigins ) ) . toBe ( false ) ; expect ( isPermittedOrigin ( 'https://other.com' , permittedOrigins ) ) . toBe ( false ) ; } ) ; } ) ; describe ( 'sendMessage' , ( ) => { it ( 'should send a postMessage to the target' , ( ) => { let target = jasmine . createSpyObj ( 'target' , [ 'postMessage' ] ) ; let message = channelReadyMessage ( '123' ) ; sendMessage ( target , message ) ; expect ( target . postMessage ) . toHaveBeenCalledWith ( message , '*' ) ; } ) ; it ( 'should send a postMessage to the target with target origin' , ( ) => { let target = jasmine . createSpyObj ( 'target' , [ 'postMessage' ] ) ; let origin = 'https://origin.com' ; let message = channelReadyMessage ( '123' ) ; sendMessage ( target , message , origin ) ; expect ( target . postMessage ) . toHaveBeenCalledWith ( message , origin ) ; } ) ; } ) ; describe ( 'createMessageListener' , ( ) => { let spy ; let listener ; beforeEach ( ( ) => { spy = jasmine . createSpy ( 'listener' ) ; listener = createMessageListener ( PostMessageType . verifyPing , spy ) ; } ) ; it ( 'ignores events without content' , ( ) => { listener ( createUntypedMessageEvent ( { } ) ) ; expect ( spy ) . not . toHaveBeenCalled ( ) ; } ) ; it ( 'ignores events without type' , ( ) => { let event = createUntypedMessageEvent ( { data : { } } ) ; listener ( event ) ; expect ( spy ) . not . toHaveBeenCalled ( ) ; } ) ; it ( 'ignores events without data' , ( ) => { let event = createUntypedMessageEvent ( { data : { type : 'TYPE' } } ) ; listener ( event ) ; expect ( spy ) . not . toHaveBeenCalled ( ) ; } ) ; it ( 'filters events with invalid data' , ( ) => { let event = createUntypedMessageEvent ( { data : { type : PostMessageType . verifyPing , data : 0 } } ) ; listener ( event ) ; expect ( spy ) . not . toHaveBeenCalled ( ) ; } ) ; it ( 'passes valid messages' , ( ) => { let event = { data : { type : PostMessageType . verifyPing , data : '123' } } ; listener ( event as MessageEvent ) ; expect ( spy ) . toHaveBeenCalledWith ( '123' , PostMessageType . verifyPing , event ) ; } ) ; } ) ; } ) ;