1- import { expect } from 'chai'
1+ import chai , { expect } from 'chai'
2+ import spies from 'chai-spies'
23import { describe , it } from 'mocha'
34
5+ chai . use ( spies )
6+
47import Backendless , {
58 forTest ,
69 prepareMockRequest ,
710 createMockRTServer ,
8- APP_PATH ,
911 API_KEY ,
1012 Utils
1113} from '../../helpers/sandbox'
@@ -19,12 +21,17 @@ describe('<RT> Basic', function() {
1921 let rtClient
2022
2123 const channelName = 'TEST_CHANNEL_NAME'
24+ const host = 'http://localhost:12345'
2225
2326 describe ( 'with AppID and API Key' , function ( ) {
2427 const backendlessApp = Backendless
2528
2629 before ( async ( ) => {
2730 rtClient = await createMockRTServer ( )
31+
32+ backendlessApp . appInfoPromise = chai . spy ( ( ) =>
33+ Promise . resolve ( { rtURL : host } )
34+ )
2835 } )
2936
3037 after ( async ( ) => {
@@ -33,8 +40,8 @@ describe('<RT> Basic', function() {
3340 rtClient . stop ( )
3441 } )
3542
36- it ( 'lookup ' , async ( ) => {
37- const req1 = prepareMockRequest ( rtClient . host )
43+ it ( 'connect and subscribe ' , async ( ) => {
44+ expect ( backendlessApp . appInfoPromise ) . to . not . have . been . called
3845
3946 backendlessApp . Messaging . subscribe ( channelName )
4047
@@ -46,17 +53,13 @@ describe('<RT> Basic', function() {
4653 expect ( con1 . userToken ) . to . be . equal ( null )
4754 expect ( con1 . clientId ) . to . be . a ( 'string' )
4855
49- expect ( req1 ) . to . deep . include ( {
50- method : 'GET' ,
51- path : `${ APP_PATH } /rt/lookup` ,
52- headers : { } ,
53- body : undefined
54- } )
56+ expect ( backendlessApp . appInfoPromise ) . to . have . been . called
5557 } )
5658
57- it ( 'lookup with user token' , async ( ) => {
59+ it ( 'connected with user token' , async ( ) => {
60+ expect ( backendlessApp . appInfoPromise ) . to . not . have . been . called
61+
5862 prepareMockRequest ( { objectId : 'test-id' , 'user-token' : 'test-token-1' } )
59- prepareMockRequest ( rtClient . host )
6063
6164 await backendlessApp . UserService . login ( 'login' , 'password' )
6265
@@ -67,12 +70,11 @@ describe('<RT> Basic', function() {
6770 expect ( con1 . apiKey ) . to . be . equal ( API_KEY )
6871 expect ( con1 . userToken ) . to . be . equal ( 'test-token-1' )
6972 expect ( con1 . clientId ) . to . be . a ( 'string' )
73+ expect ( backendlessApp . appInfoPromise ) . to . have . been . called
7074 } )
7175
7276 it ( 'reconnects after changing debug mode' , async ( ) => {
73- prepareMockRequest ( rtClient . host )
74- prepareMockRequest ( rtClient . host )
75- prepareMockRequest ( rtClient . host )
77+ expect ( backendlessApp . appInfoPromise ) . to . not . have . been . called
7678
7779 backendlessApp . Messaging . subscribe ( channelName )
7880
@@ -90,6 +92,10 @@ describe('<RT> Basic', function() {
9092 . to . be . equal ( con2 . clientId )
9193 . to . be . equal ( con3 . clientId )
9294
95+ expect ( con1 . connectionId )
96+ . to . not . equal ( con2 . connectionId )
97+ . to . not . equal ( con3 . connectionId )
98+
9399 expect ( con1 . apiKey ) . to . be . equal ( API_KEY )
94100 expect ( con1 . userToken ) . to . be . equal ( null )
95101 expect ( con1 . clientId ) . to . be . a ( 'string' )
@@ -101,10 +107,13 @@ describe('<RT> Basic', function() {
101107 expect ( con3 . apiKey ) . to . be . equal ( API_KEY )
102108 expect ( con3 . userToken ) . to . be . equal ( null )
103109 expect ( con3 . clientId ) . to . be . a ( 'string' )
110+
111+ expect ( backendlessApp . appInfoPromise ) . to . have . been . called
104112 } )
105113
106114 it ( 'updates connection user-token after login/logout' , async ( ) => {
107- prepareMockRequest ( rtClient . host )
115+ expect ( backendlessApp . appInfoPromise ) . to . not . have . been . called
116+
108117 prepareMockRequest ( { objectId : 'test-id-1' , 'user-token' : 'test-token-1' } )
109118 prepareMockRequest ( { objectId : 'test-id-2' , 'user-token' : 'test-token-2' } )
110119 prepareMockRequest ( )
@@ -144,11 +153,12 @@ describe('<RT> Basic', function() {
144153 expect ( met1 . id ) . to . be . not . equal ( met2 . id )
145154 expect ( met1 . id ) . to . be . not . equal ( met3 . id )
146155 expect ( met2 . id ) . to . be . not . equal ( met3 . id )
156+
157+ expect ( backendlessApp . appInfoPromise ) . to . have . been . called
147158 } )
148159
149160 it ( 'should update connection user-token after setCurrentUserToken' , async ( ) => {
150- prepareMockRequest ( rtClient . host )
151- prepareMockRequest ( )
161+ expect ( backendlessApp . appInfoPromise ) . to . not . have . been . called
152162
153163 backendlessApp . Messaging . subscribe ( channelName )
154164
@@ -173,6 +183,8 @@ describe('<RT> Basic', function() {
173183 expect ( met2 . id ) . to . be . a ( 'string' )
174184 expect ( met2 . name ) . to . be . equal ( 'SET_USER_TOKEN' )
175185 expect ( met2 . options ) . to . be . eql ( { userToken : null } )
186+
187+ expect ( backendlessApp . appInfoPromise ) . to . have . been . called
176188 } )
177189
178190 it ( 'should not set user-token after setCurrentUserToken' , async ( ) => {
@@ -196,6 +208,10 @@ describe('<RT> Basic', function() {
196208
197209 before ( async ( ) => {
198210 rtClient = await createMockRTServer ( rtAppId )
211+
212+ backendlessApp . appInfoPromise = chai . spy ( ( ) =>
213+ Promise . resolve ( { appId : rtAppId , apiKey : rtAPIKey , rtURL : host } )
214+ )
199215 } )
200216
201217 afterEach ( async ( ) => {
@@ -208,9 +224,8 @@ describe('<RT> Basic', function() {
208224 rtClient . stop ( )
209225 } )
210226
211- it ( 'lookup' , async ( ) => {
212- const req1 = prepareMockRequest ( rtClient . host )
213- const req2 = prepareMockRequest ( { appId : rtAppId , apiKey : rtAPIKey } )
227+ it ( 'connected' , async ( ) => {
228+ expect ( backendlessApp . appInfoPromise ) . to . not . have . been . called
214229
215230 backendlessApp . Messaging . subscribe ( channelName )
216231
@@ -222,25 +237,13 @@ describe('<RT> Basic', function() {
222237 expect ( con1 . userToken ) . to . be . equal ( null )
223238 expect ( con1 . clientId ) . to . be . a ( 'string' )
224239
225- expect ( req1 ) . to . deep . include ( {
226- method : 'GET' ,
227- path : `${ domain } /api/rt/lookup` ,
228- headers : { } ,
229- body : undefined
230- } )
231-
232- expect ( req2 ) . to . deep . include ( {
233- method : 'GET' ,
234- path : `${ domain } /api/info` ,
235- headers : { } ,
236- body : undefined
237- } )
240+ expect ( backendlessApp . appInfoPromise ) . to . have . been . called
238241 } )
239242
240- it ( 'lookup with user token' , async ( ) => {
243+ it ( 'connection with user token' , async ( ) => {
244+ expect ( backendlessApp . appInfoPromise ) . to . not . have . been . called
245+
241246 prepareMockRequest ( { objectId : 'test-id' , 'user-token' : 'test-token-1' } )
242- prepareMockRequest ( rtClient . host )
243- prepareMockRequest ( { appId : rtAppId , apiKey : rtAPIKey } )
244247
245248 await backendlessApp . UserService . login ( 'login' , 'password' )
246249
@@ -251,15 +254,12 @@ describe('<RT> Basic', function() {
251254 expect ( con1 . apiKey ) . to . be . equal ( rtAPIKey )
252255 expect ( con1 . userToken ) . to . be . equal ( 'test-token-1' )
253256 expect ( con1 . clientId ) . to . be . a ( 'string' )
257+
258+ expect ( backendlessApp . appInfoPromise ) . to . have . been . called
254259 } )
255260
256261 it ( 'reconnects after changing debug mode' , async ( ) => {
257- prepareMockRequest ( rtClient . host )
258- prepareMockRequest ( { appId : rtAppId , apiKey : rtAPIKey } )
259- prepareMockRequest ( rtClient . host )
260- prepareMockRequest ( { appId : rtAppId , apiKey : rtAPIKey } )
261- prepareMockRequest ( rtClient . host )
262- prepareMockRequest ( { appId : rtAppId , apiKey : rtAPIKey } )
262+ expect ( backendlessApp . appInfoPromise ) . to . not . have . been . called
263263
264264 backendlessApp . Messaging . subscribe ( channelName )
265265
@@ -277,6 +277,10 @@ describe('<RT> Basic', function() {
277277 . to . be . equal ( con2 . clientId )
278278 . to . be . equal ( con3 . clientId )
279279
280+ expect ( con1 . connectionId )
281+ . to . not . equal ( con2 . connectionId )
282+ . to . not . equal ( con3 . connectionId )
283+
280284 expect ( con1 . apiKey ) . to . be . equal ( rtAPIKey )
281285 expect ( con1 . userToken ) . to . be . equal ( null )
282286 expect ( con1 . clientId ) . to . be . a ( 'string' )
@@ -288,11 +292,13 @@ describe('<RT> Basic', function() {
288292 expect ( con3 . apiKey ) . to . be . equal ( rtAPIKey )
289293 expect ( con3 . userToken ) . to . be . equal ( null )
290294 expect ( con3 . clientId ) . to . be . a ( 'string' )
295+
296+ expect ( backendlessApp . appInfoPromise ) . to . have . been . called
291297 } )
292298
293299 it ( 'updates connection user-token after login/logout' , async ( ) => {
294- prepareMockRequest ( rtClient . host )
295- prepareMockRequest ( { appId : rtAppId , apiKey : rtAPIKey } )
300+ expect ( backendlessApp . appInfoPromise ) . to . not . have . been . called
301+
296302 prepareMockRequest ( { objectId : 'test-id-1' , 'user-token' : 'test-token-1' } )
297303 prepareMockRequest ( { objectId : 'test-id-2' , 'user-token' : 'test-token-2' } )
298304 prepareMockRequest ( )
@@ -332,11 +338,13 @@ describe('<RT> Basic', function() {
332338 expect ( met1 . id ) . to . be . not . equal ( met2 . id )
333339 expect ( met1 . id ) . to . be . not . equal ( met3 . id )
334340 expect ( met2 . id ) . to . be . not . equal ( met3 . id )
341+
342+ expect ( backendlessApp . appInfoPromise ) . to . have . been . called
335343 } )
336344
337345 it ( 'should update connection user-token after setCurrentUserToken' , async ( ) => {
338- prepareMockRequest ( rtClient . host )
339- prepareMockRequest ( { appId : rtAppId , apiKey : rtAPIKey } )
346+ expect ( backendlessApp . appInfoPromise ) . to . not . have . been . called
347+
340348 prepareMockRequest ( )
341349
342350 backendlessApp . Messaging . subscribe ( channelName )
@@ -362,6 +370,8 @@ describe('<RT> Basic', function() {
362370 expect ( met2 . id ) . to . be . a ( 'string' )
363371 expect ( met2 . name ) . to . be . equal ( 'SET_USER_TOKEN' )
364372 expect ( met2 . options ) . to . be . eql ( { userToken : null } )
373+
374+ expect ( backendlessApp . appInfoPromise ) . to . have . been . called
365375 } )
366376
367377 it ( 'should not set user-token after setCurrentUserToken' , async ( ) => {
0 commit comments