import { OpenYoloCredential } from '../protocol/data' ; import { AUTHENTICATION_METHODS } from '../protocol/data' ; import { OpenYoloErrorType , OpenYoloInternalError } from '../protocol/errors' ; import { errorMessage , saveMessage , saveResultMessage } from '../protocol/rpc_messages' ; import { SecureChannel } from '../protocol/secure_channel' ; import { FakeProviderConnection } from '../test_utils/channels' ; import { createSpyFrame } from '../test_utils/frames' ; import { CredentialSave } from './credential_save' ; describe ( 'CredentialSave' , ( ) => { let clientChannel ; let providerChannel ; let request ; let frame ; let credential = { id : 'user@example.com' , authMethod : AUTHENTICATION_METHODS . ID_AND_PASSWORD , displayName : 'User' , password : 'password' } ; beforeEach ( ( ) => { let connection = new FakeProviderConnection ( ) ; clientChannel = connection . clientChannel ; providerChannel = connection . providerChannel ; frame = createSpyFrame ( 'frameId' ) ; request = new CredentialSave ( frame , clientChannel ) ; spyOn ( request , 'dispose' ) . and . callThrough ( ) ; } ) ; afterEach ( ( ) => { request . dispose ( ) ; } ) ; describe ( 'dispatch' , ( ) => { it ( 'should send a RPC message to the frame' , ( ) => { spyOn ( clientChannel , 'send' ) . and . callThrough ( ) ; request . dispatch ( credential ) ; expect ( clientChannel . send ) . toHaveBeenCalledWith ( saveMessage ( request . id , credential ) ) ; } ) ; } ) ; it ( 'should resolve if successful' , async function ( done ) { let promise = request . dispatch ( credential ) ; providerChannel . send ( saveResultMessage ( request . id , true ) ) ; try { await promise ; expect ( request . dispose ) . toHaveBeenCalled ( ) ; done ( ) ; } catch ( err ) { done . fail ( 'Promise should resolve' ) ; } } ) ; it ( 'should reject if canceled' , async function ( done ) { let promise = request . dispatch ( credential ) ; providerChannel . send ( saveResultMessage ( request . id , false ) ) ; try { await promise ; done . fail ( 'promise should be rejected' ) ; } catch ( err ) { expect ( err . type ) . toEqual ( OpenYoloErrorType . userCanceled ) ; expect ( request . dispose ) . toHaveBeenCalled ( ) ; done ( ) ; } } ) ; it ( 'should reject if error received' , async function ( done ) { let promise = request . dispatch ( credential ) ; providerChannel . send ( errorMessage ( request . id , OpenYoloInternalError . requestFailed ( 'ERROR!' ) . toExposedError ( ) ) ) ; try { await promise ; done . fail ( 'Promise should be rejected' ) ; } catch ( err ) { expect ( err . type ) . toEqual ( OpenYoloErrorType . requestFailed ) ; expect ( request . dispose ) . toHaveBeenCalled ( ) ; done ( ) ; } } ) ; } ) ;