import { SubscriptionClient } from 'subscriptions-transport-ws' ; import { Observable , execute , ExecutionResult } from 'apollo-link' ; import { WebSocketLink } from '../webSocketLink' ; const query = `
  query SampleQuery {
    stub {
      id
    }
  }
` ; const mutation = `
  mutation SampleMutation {
    stub {
      id
    }
  }
` ; const subscription = `
  subscription SampleSubscription {
    stub {
      id
    }
  }
` ; describe ( 'WebSocketLink' , ( ) => { it ( 'constructs' , ( ) => { const client = { } ; client . __proto__ = SubscriptionClient . prototype ; expect ( ( ) => new WebSocketLink ( client ) ) . not . toThrow ( ) ; } ) ; it ( 'should call request on the client for a query' , done => { const result = { data : { data : 'result' } } ; const client = { } ; const observable = Observable . of ( result ) ; client . __proto__ = SubscriptionClient . prototype ; client . request = jest . fn ( ) ; client . request . mockReturnValueOnce ( observable ) ; const link = new WebSocketLink ( client ) ; const obs = execute ( link , { query } ) ; expect ( obs ) . toEqual ( observable ) ; obs . subscribe ( data => { expect ( data ) . toEqual ( result ) ; expect ( client . request ) . toHaveBeenCalledTimes ( 1 ) ; done ( ) ; } ) ; } ) ; it ( 'should call query on the client for a mutation' , done => { const result = { data : { data : 'result' } } ; const client = { } ; const observable = Observable . of ( result ) ; client . __proto__ = SubscriptionClient . prototype ; client . request = jest . fn ( ) ; client . request . mockReturnValueOnce ( observable ) ; const link = new WebSocketLink ( client ) ; const obs = execute ( link , { query : mutation } ) ; expect ( obs ) . toEqual ( observable ) ; obs . subscribe ( data => { expect ( data ) . toEqual ( result ) ; expect ( client . request ) . toHaveBeenCalledTimes ( 1 ) ; done ( ) ; } ) ; } ) ; it ( 'should call request on the subscriptions client for subscription' , done => { const result = { data : { data : 'result' } } ; const client = { } ; const observable = Observable . of ( result ) ; client . __proto__ = SubscriptionClient . prototype ; client . request = jest . fn ( ) ; client . request . mockReturnValueOnce ( observable ) ; const link = new WebSocketLink ( client ) ; const obs = execute ( link , { query : mutation } ) ; expect ( obs ) . toEqual ( observable ) ; obs . subscribe ( data => { expect ( data ) . toEqual ( result ) ; expect ( client . request ) . toHaveBeenCalledTimes ( 1 ) ; done ( ) ; } ) ; } ) ; it ( 'should call next with multiple results for subscription' , done => { const results = [ { data : { data : 'result1' } } , { data : { data : 'result2' } } , ] ; const client = { } ; client . __proto__ = SubscriptionClient . prototype ; client . request = jest . fn ( ( ) => { const copy = [ ... results ] ; return new Observable < ExecutionResult > ( observer => { observer . next ( copy [ 0 ] ) ; observer . next ( copy [ 1 ] ) ; } ) ; } ) ; const link = new WebSocketLink ( client ) ; execute ( link , { query : subscription } ) . subscribe ( data => { expect ( client . request ) . toHaveBeenCalledTimes ( 1 ) ; expect ( data ) . toEqual ( results . shift ( ) ) ; if ( results . length === 0 ) { done ( ) ; } } ) ; } ) ; } ) ;