import { StreamRequestOptions } from "@lensesio/cypress-websocket-testing" ; import { WebSocketSubjectConfig } from "rxjs/webSocket" ; type MessageType = "CONNECTED" | "LOGIN" | "RECORD" | "END" ; interface IMessage { type : MessageType ; data : any ; } const config = { url : "ws://localhost:8080/" } ; let options ; context ( "Websocket cy.streamRequests() Command" , ( ) => { before ( ( ) => { } ) ; it ( "Without any options set it will close on first message" , ( ) => { options = { } ; cy . wrap ( null , { timeout : 10000 } ) . then ( ( ) => cy . streamRequest < IMessage > ( config , options ) . then ( results => { const result = results && results [ 0 ] ; expect ( result ) . to . not . be . undefined ; expect ( result ) . to . have . property ( "type" , "CONNECTED" ) ; expect ( result ) . to . not . have . property ( "data" ) ; } ) ) ; } ) ; it ( "With startUpMessage and takeWhileFn" , ( ) => { options = { takeWhileFn : ( message ) => message && message . type !== "END" , startUpMessage : { type : "LOGIN" , data : "Developer" } } ; cy . wrap ( null , { timeout : 10000 } ) . then ( ( ) => cy . streamRequest < IMessage > ( config , options ) . then ( results => { const length = ( results && results . length ) || 0 ; const result = results && results [ length - 1 ] ; expect ( length ) . to . eq ( 3 ) ; expect ( result ) . to . not . be . undefined ; expect ( result ) . to . have . property ( "type" , "END" ) ; expect ( result ) . to . have . property ( "data" , "Bye, Developer!" ) ; } ) ) ; } ) ; it ( "With retryUntilFn set" , ( ) => { options = { takeWhileFn : ( message ) => message && message . type !== "END" , streamTimeout : 20000 , retryUntilFn : ( messages ) => messages . filter ( message => message . data > 5 ) . length > 0 , startUpMessage : { type : "LOGIN" , data : "Developer" } } ; cy . wrap ( null , { timeout : options . streamTimeout } ) . then ( ( ) => cy . streamRequest < IMessage > ( config , options ) . then ( results => { const result = results && results [ 1 ] ; const data = result && result . data ; expect ( result ) . to . not . be . undefined ; expect ( result ) . to . have . property ( "type" , "RECORD" ) ; expect ( data > 5 ) . to . be . true ; } ) ) ; } ) ; } ) ;