@@ -94,16 +94,16 @@ describe("MatrixClient opts", function () {
9494 client . stopClient ( ) ;
9595 } ) ;
9696
97- it ( "should be able to send messages" , function ( done ) {
97+ it ( "should be able to send messages" , async ( ) => {
9898 const eventId = "$flibble:wibble" ;
9999 httpBackend . when ( "PUT" , "/txn1" ) . respond ( 200 , {
100100 event_id : eventId ,
101101 } ) ;
102- client . sendTextMessage ( "!foo:bar" , "a body" , "txn1" ) . then ( function ( res ) {
103- expect ( res . event_id ) . toEqual ( eventId ) ;
104- done ( ) ;
105- } ) ;
106- httpBackend . flush ( "/txn1" , 1 ) ;
102+ const [ res ] = await Promise . all ( [
103+ client . sendTextMessage ( "!foo:bar" , "a body" , "txn1" ) ,
104+ httpBackend . flush ( "/txn1" , 1 ) ,
105+ ] ) ;
106+ expect ( res . event_id ) . toEqual ( eventId ) ;
107107 } ) ;
108108
109109 it ( "should be able to sync / get new events" , async function ( ) {
@@ -149,27 +149,25 @@ describe("MatrixClient opts", function () {
149149 client . stopClient ( ) ;
150150 } ) ;
151151
152- it ( "shouldn't retry sending events" , function ( done ) {
152+ it ( "shouldn't retry sending events" , async ( ) => {
153153 httpBackend . when ( "PUT" , "/txn1" ) . respond (
154154 500 ,
155155 new MatrixError ( {
156156 errcode : "M_SOMETHING" ,
157157 error : "Ruh roh" ,
158158 } ) ,
159159 ) ;
160- client . sendTextMessage ( "!foo:bar" , "a body" , "txn1" ) . then (
161- function ( res ) {
162- expect ( false ) . toBe ( true ) ;
163- } ,
164- function ( err ) {
165- expect ( err . errcode ) . toEqual ( "M_SOMETHING" ) ;
166- done ( ) ;
167- } ,
168- ) ;
169- httpBackend . flush ( "/txn1" , 1 ) ;
160+ try {
161+ await Promise . all ( [
162+ expect ( client . sendTextMessage ( "!foo:bar" , "a body" , "txn1" ) ) . rejects . toThrow ( ) ,
163+ httpBackend . flush ( "/txn1" , 1 ) ,
164+ ] ) ;
165+ } catch ( err ) {
166+ expect ( ( < MatrixError > err ) . errcode ) . toEqual ( "M_SOMETHING" ) ;
167+ }
170168 } ) ;
171169
172- it ( "shouldn't queue events" , function ( done ) {
170+ it ( "shouldn't queue events" , async ( ) => {
173171 httpBackend . when ( "PUT" , "/txn1" ) . respond ( 200 , {
174172 event_id : "AAA" ,
175173 } ) ;
@@ -178,30 +176,38 @@ describe("MatrixClient opts", function () {
178176 } ) ;
179177 let sentA = false ;
180178 let sentB = false ;
181- client . sendTextMessage ( "!foo:bar" , "a body" , "txn1" ) . then ( function ( res ) {
179+ const messageASendPromise = client . sendTextMessage ( "!foo:bar" , "a body" , "txn1" ) . then ( function ( res ) {
182180 sentA = true ;
181+ // We expect messageB to be sent before messageA to ensure as we're
182+ // testing that there is no queueing that blocks each other
183183 expect ( sentB ) . toBe ( true ) ;
184184 } ) ;
185- client . sendTextMessage ( "!foo:bar" , "b body" , "txn2" ) . then ( function ( res ) {
185+ const messageBSendPromise = client . sendTextMessage ( "!foo:bar" , "b body" , "txn2" ) . then ( function ( res ) {
186186 sentB = true ;
187+ // We expect messageB to be sent before messageA to ensure as we're
188+ // testing that there is no queueing that blocks each other
187189 expect ( sentA ) . toBe ( false ) ;
188190 } ) ;
189- httpBackend . flush ( "/txn2" , 1 ) . then ( function ( ) {
190- httpBackend . flush ( "/txn1" , 1 ) . then ( function ( ) {
191- done ( ) ;
192- } ) ;
193- } ) ;
191+ // Allow messageB to succeed first
192+ await httpBackend . flush ( "/txn2" , 1 ) ;
193+ // Then allow messageA to succeed
194+ await httpBackend . flush ( "/txn1" , 1 ) ;
195+
196+ // Now await the message send promises to
197+ await messageBSendPromise ;
198+ await messageASendPromise ;
194199 } ) ;
195200
196- it ( "should be able to send messages" , function ( done ) {
201+ it ( "should be able to send messages" , async ( ) => {
197202 httpBackend . when ( "PUT" , "/txn1" ) . respond ( 200 , {
198203 event_id : "foo" ,
199204 } ) ;
200- client . sendTextMessage ( "!foo:bar" , "a body" , "txn1" ) . then ( function ( res ) {
201- expect ( res . event_id ) . toEqual ( "foo" ) ;
202- done ( ) ;
203- } ) ;
204- httpBackend . flush ( "/txn1" , 1 ) ;
205+ const [ res ] = await Promise . all ( [
206+ client . sendTextMessage ( "!foo:bar" , "a body" , "txn1" ) ,
207+ httpBackend . flush ( "/txn1" , 1 ) ,
208+ ] ) ;
209+
210+ expect ( res . event_id ) . toEqual ( "foo" ) ;
205211 } ) ;
206212 } ) ;
207213} ) ;
0 commit comments