Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
feat: send the users list on every new connection
Browse files Browse the repository at this point in the history
  • Loading branch information
severo committed Jan 22, 2020
1 parent 8bb4d32 commit 05e9355
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/socket.io/socket.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Server', () => {
let mockLogger: MockLogger

// order of the clients in the Socket class' 'users' property
// requires the clients to be connect in that order
// requires the clients to be connected in that order
const ACTIVE_CLIENT_IDX = 0
const PASSIVE_CLIENT_IDX = 1

Expand All @@ -53,9 +53,6 @@ describe('Server', () => {
beforeEach(async () => {
await new Promise(resolve => client.on('connect', resolve))
})
afterEach(() => {
client.disconnect()
})

it('should connect socket', async () => {
expect(client.connected).to.be.true
Expand All @@ -67,6 +64,27 @@ describe('Server', () => {
'New user created for socket'
)
})
it('should send the list of users to all the clients', async () => {
await new Promise(resolve => passiveClient.on('connect', resolve))
const getUsersList = (): Promise<ExportedUser[]> =>
new Promise(resolve =>
passiveClient.on(UsersListEvent.eventName, resolve)
)

// act
const [list, newClient] = await Promise.all([
getUsersList(),
ioClient.connect(socketUrl + '/occupapp-beta', options),
])

// assert
expect(list).to.have.length(3)
list.should.all.have.property('name')
list.should.all.have.property('color')

// after
newClient.disconnect()
})
})

describe('update-user-name', () => {
Expand Down
1 change: 1 addition & 0 deletions src/socket.io/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Socket {
// connection. There is no persistence for a same user between
// connections
const socketUser: User = this.createUser(socket.id)
this.emitUsersListToAll()

socket.on(
UpdateUserNameEvent.eventName,
Expand Down

0 comments on commit 05e9355

Please sign in to comment.