import { WindowLike } from '../protocol/comms' ; import { verifyAckMessage , verifyPingMessage } from '../protocol/post_messages' ; import { MockWindow } from '../test_utils/frames' ; import { createMessageEvent , createUntypedMessageEvent } from '../test_utils/messages' ; import * as verify from './verify' ; describe ( 'verify' , ( ) => { describe ( 'respondToHandshake' , ( ) => { let rootFrame ; let middleFrame ; let providerFrame ; let providerFrameMessageSpy ; beforeEach ( ( ) => { rootFrame = new MockWindow ( ) ; middleFrame = new MockWindow ( middleFrame ) ; providerFrame = new MockWindow ( middleFrame ) ; providerFrameMessageSpy = spyOn ( providerFrame , 'postMessage' ) ; } ) ; it ( 'should acknowledge valid ping message' , ( ) => { verify . respondToHandshake ( rootFrame ) ; let event = createMessageEvent ( verifyPingMessage ( '123' ) , 'https://provider.example.com' , undefined , providerFrame ) ; rootFrame . dispatchEvent ( event ) ; expect ( providerFrameMessageSpy ) . toHaveBeenCalledWith ( verifyAckMessage ( '123' ) , 'https://provider.example.com' ) ; } ) ; it ( 'should acknowledge more than one ping messages' , ( ) => { verify . respondToHandshake ( rootFrame ) ; let event = createMessageEvent ( verifyPingMessage ( '123' ) , 'https://provider.example.com' , undefined , providerFrame ) ; rootFrame . dispatchEvent ( event ) ; rootFrame . dispatchEvent ( event ) ; expect ( providerFrameMessageSpy . calls . count ( ) ) . toEqual ( 2 ) ; } ) ; it ( 'should not acknowledge invalid ping message' , ( ) => { verify . respondToHandshake ( rootFrame ) ; let event = createUntypedMessageEvent ( { type : 'not-a-ping' , data : '123' } ) ; rootFrame . dispatchEvent ( event ) ; expect ( providerFrameMessageSpy ) . not . toHaveBeenCalled ( ) ; } ) ; } ) ; } ) ;