Skip to content

Commit 501939b

Browse files
committed
feat: session separately + isOnline()
1 parent 4cb78bc commit 501939b

File tree

5 files changed

+101
-29
lines changed

5 files changed

+101
-29
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,21 @@ amioChat.events.onConnectionStateChanged((online) => {
300300

301301
## Tests
302302

303+
### Tips - quicker testing
303304
During development, comment the **lib** import and replace it with **src** one:
304305
```js
305306
// import amioChat from '../lib/amio-chat-sdk-web'
306307
import {amioChat} from '../src/amio-chat-client'
307308
```
308309

310+
### Tips - promises
311+
Since we can't use `async/await`, always pay attention that every promised base test has this form:
312+
```js
313+
it('test', () => {
314+
return createPromise() // mocha handles it
315+
})
316+
```
317+
309318
### Execute all tests
310319
1. Build the code - `npm run build`
311320
2. Run the test suite - `npm run test`.

src/amio-chat-client.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ class AmioChatClient {
1616
return connection.connect(config)
1717
}
1818

19+
// TODO to docs
20+
isConnected() {
21+
return connection.online
22+
}
23+
24+
// TODO to docs
1925
getSessionId() {
2026
return session.getId()
2127
}

src/connection/connection.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import {
1616
class Connection {
1717

1818
constructor() {
19+
this.online = false
20+
1921
this.messageReceivedHandler = () => {
2022
}
2123
this.messageEchoHandler = () => {
@@ -57,7 +59,10 @@ class Connection {
5759

5860
this.socket.on(SOCKET_CONNECTION_ACCEPTED, data => {
5961
session.setId(data.session_id)
60-
this.connectionStateChangedHandler(true)
62+
63+
this.online = true
64+
this.connectionStateChangedHandler(this.online)
65+
6166
resolve()
6267
})
6368

@@ -75,7 +80,8 @@ class Connection {
7580
})
7681

7782
this.socket.on(SOCKET_IO_DISCONNECT, () => {
78-
this.connectionStateChangedHandler(false)
83+
this.online = false
84+
this.connectionStateChangedHandler(this.online)
7985
})
8086

8187
this.socket.on(SOCKET_IO_ERROR, (err) => {
@@ -96,13 +102,9 @@ class Connection {
96102
})
97103
}
98104

99-
isConnected() {
100-
return !!this.socket
101-
}
102-
103105
emit(event, data) {
104106
return new Promise((resolve, reject) => {
105-
if(!this.isConnected()) {
107+
if(!this.socket) {
106108
reject(ERROR_MESSAGE_NOT_CONNECTED)
107109
return
108110
}

src/connection/session.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@ import {
55
class Session {
66

77
constructor() {
8-
this.storage = window.localStorage
9-
if(!this.storage) {
10-
// for tests
11-
this.storage = {}
12-
this.storage.getItem = () => {
13-
}
14-
this.storage.setItem = () => {
15-
}
8+
if(window.localStorage) {
9+
this.storage = window.localStorage
10+
} else {
11+
this.storage = new TestStorage()
1612
}
1713
}
1814

@@ -29,4 +25,23 @@ class Session {
2925
}
3026
}
3127

28+
class TestStorage {
29+
30+
constructor() {
31+
this.storage = {}
32+
}
33+
34+
getItem(key) {
35+
return this.storage[key]
36+
}
37+
38+
setItem(key, value) {
39+
this.storage[key] = value
40+
}
41+
42+
removeItem(key) {
43+
delete this.storage[key]
44+
}
45+
}
46+
3247
export default new Session()

test/connect.spec.js

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
11
/* global describe, it, before */
22

33
import 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

87
chai.expect()
98
const expect = chai.expect
9+
const CHANNEL_ID = process.env.TEST_AMIO_CHANNEL_ID
10+
const CHANNEL_ID2 = process.env.TEST_AMIO_CHANNEL_ID2
1011

1112
describe('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

Comments
 (0)