11/* global describe, it, before */
22
33import chai from 'chai'
4- import amioChat from '../lib/amio-chat-sdk-web'
5- // TODO put back
6- // import {amioChat} from '../src/amio-chat-client'
4+ // import amioChat from '../lib/amio-chat-sdk-web'
5+ import { amioChat } from '../src/amio-chat-client'
76
87chai . expect ( )
98const expect = chai . expect
9+ const CHANNEL_ID = process . env . TEST_AMIO_CHANNEL_ID
10+ const CHANNEL_ID2 = process . env . TEST_AMIO_CHANNEL_ID2
1011
1112describe ( 'connect()' , ( ) => {
1213 before ( ( ) => {
1314 } )
1415
1516 describe ( 'ERR - wrong configuration - channelId' , ( ) => {
1617 describe ( 'config.channelId' , ( ) => {
17- function testConnect ( opts , expectedErr ) {
18- return amioChat . connect ( opts )
19- . then (
20- ( ) => expect . fail ( null , null , 'Should have failed' ) ,
21- actualErr => {
22- expect ( actualErr ) . to . eql ( expectedErr )
23- } )
24- }
25-
26- const testChannelIdMissing = opts => testConnect ( opts , 'Could not connect: config.channelId is missing.' )
18+ const testChannelIdMissing = opts => testFailedConnect ( opts , 'Could not connect: config.channelId is missing.' )
2719 const testChannelIdIsString = ( opts , wrongValue ) => {
28- return testConnect ( opts , `Could not connect: config.channelId must be a string. The provided value is: ${ JSON . stringify ( wrongValue ) } ` )
20+ return testFailedConnect ( opts , `Could not connect: config.channelId must be a string. The provided value is: ${ JSON . stringify ( wrongValue ) } ` )
2921 }
3022
3123 it ( 'undefined configuration' , ( ) => testChannelIdMissing ( undefined ) )
@@ -39,10 +31,58 @@ describe('connect()', () => {
3931 return testChannelIdIsString ( { channelId : wrongValue } , wrongValue )
4032 } )
4133 } )
34+ } )
4235
36+ it ( 'ERR - channelId not found' , ( ) => {
37+ const channelId = 'i-do-not-exist'
38+ const expectedError = 'Connection rejected from server. ' +
39+ 'Error: {"error_code":2,"details":{' +
40+ `"message":"Channel with channelId ${ channelId } doesn't exist"}}`
41+ return testFailedConnect ( { channelId} , expectedError )
4342 } )
4443
4544 it ( 'connection accepted' , ( ) => {
45+ return amioChat . connect ( { channelId : CHANNEL_ID } )
46+ . then ( ( ) => {
47+ expect ( amioChat . getSessionId ( ) ) . to . not . be . undefined
48+ } )
49+ } )
50+
51+ it ( 'connection accepted - reconnect to an existing session' , ( ) => {
52+ return amioChat . connect ( { channelId : CHANNEL_ID } )
53+ . then ( ( ) => {
54+ expect ( amioChat . getSessionId ( ) ) . to . not . be . undefined
55+ const firstSessionId = amioChat . getSessionId ( )
4656
57+ return amioChat . connect ( { channelId : CHANNEL_ID } )
58+ . then ( ( ) => {
59+ expect ( amioChat . getSessionId ( ) ) . to . eql ( firstSessionId )
60+ } )
61+ } )
62+ } )
63+
64+ it ( 'connection accepted - old sessionId is invalidated' , ( ) => {
65+
66+ return amioChat . connect ( { channelId : CHANNEL_ID } )
67+ . then ( ( ) => {
68+ expect ( amioChat . getSessionId ( ) ) . to . not . be . undefined
69+ const firstSessionId = amioChat . getSessionId ( )
70+
71+ return amioChat . connect ( { channelId : CHANNEL_ID2 } )
72+ . then ( ( ) => {
73+ expect ( amioChat . getSessionId ( ) ) . to . not . be . undefined
74+ expect ( amioChat . getSessionId ( ) ) . to . not . eql ( firstSessionId )
75+ } )
76+ } )
4777 } )
4878} )
79+
80+ function testFailedConnect ( opts , expectedErr ) {
81+ return amioChat . connect ( opts )
82+ . then (
83+ ( ) => expect . fail ( null , null , 'Should have failed' ) ,
84+ actualErr => {
85+ expect ( actualErr ) . to . eql ( expectedErr )
86+ } )
87+ }
88+
0 commit comments