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

Commit

Permalink
feat: add 'id' property to exported users
Browse files Browse the repository at this point in the history
It will give the possibility to add new funtionalities to:
- send individual changes (ie. "that user has changed their color")
- track other users interactions, ie. show the point moved by a specific
user in Occupapp, setting the color of the point according to the user's
color
- possibly some direct exchanges between users
  • Loading branch information
severo committed Jan 22, 2020
1 parent 05e9355 commit 184918a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
12 changes: 10 additions & 2 deletions src/domain/events/toclient/users.list.event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ describe('Events', () => {
it('should initialize exportedUsers object', () => {
// arrange
const exportedUsers: ExportedUser[] = [
{ name: 'Usain', color: '#FFFF00' },
{ name: 'Bob', color: '#333' },
{
id: '/occupapp-beta#Mx8Ed_B2YStTu6O9AAAT',
name: 'Usain',
color: '#FFFF00',
},
{
id: '/occupapp-beta#yhlItvFv5dn7TC5PAAAU',
name: 'Bob',
color: '#333',
},
]

// act
Expand Down
2 changes: 2 additions & 0 deletions src/domain/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import nanoid from 'nanoid'
import rnd from 'randomcolor'

export interface ExportedUser {
id: string
name: string
color: string
}
Expand Down Expand Up @@ -73,6 +74,7 @@ export class User {

public export(): ExportedUser {
return {
id: this.id,
name: this._name,
color: this._color,
}
Expand Down
21 changes: 12 additions & 9 deletions src/socket.io/socket.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('Server', () => {
// requires the clients to be connected in that order
const ACTIVE_CLIENT_IDX = 0
const PASSIVE_CLIENT_IDX = 1
const NEW_CLIENT_IDX = 2

beforeEach(() => {
server = io().listen(5000)
Expand Down Expand Up @@ -64,7 +65,7 @@ describe('Server', () => {
'New user created for socket'
)
})
it('should send the list of users to all the clients', async () => {
it('should send the ordered list of users, and their id should correspond to client sockets ids', async () => {
await new Promise(resolve => passiveClient.on('connect', resolve))
const getUsersList = (): Promise<ExportedUser[]> =>
new Promise(resolve =>
Expand All @@ -81,6 +82,12 @@ describe('Server', () => {
expect(list).to.have.length(3)
list.should.all.have.property('name')
list.should.all.have.property('color')
expect(list[ACTIVE_CLIENT_IDX]).to.have.property('id', client.id)
expect(list[PASSIVE_CLIENT_IDX]).to.have.property(
'id',
passiveClient.id
)
expect(list[NEW_CLIENT_IDX]).to.have.property('id', newClient.id)

// after
newClient.disconnect()
Expand Down Expand Up @@ -174,10 +181,8 @@ describe('Server', () => {
.getInfoLogs()
.should.include.something.that.equals(`User name updated`)
expect(list).to.have.length(2)
expect(list[ACTIVE_CLIENT_IDX]).to.have.property('name', 'George')
expect(list[ACTIVE_CLIENT_IDX]).to.have.property('color')
expect(list[PASSIVE_CLIENT_IDX]).to.have.property('name')
expect(list[PASSIVE_CLIENT_IDX]).to.have.property('color')
const clientUser = list.find(user => user.id === client.id)
expect(clientUser).to.have.property('name', 'George')
})
})

Expand Down Expand Up @@ -291,10 +296,8 @@ describe('Server', () => {
.getInfoLogs()
.should.include.something.that.equals(`User color updated`)
expect(list).to.have.length(2)
expect(list[ACTIVE_CLIENT_IDX]).to.have.property('name')
expect(list[ACTIVE_CLIENT_IDX]).to.have.property('color', '#123BCA')
expect(list[PASSIVE_CLIENT_IDX]).to.have.property('name')
expect(list[PASSIVE_CLIENT_IDX]).to.have.property('color')
const clientUser = list.find(user => user.id === client.id)
expect(clientUser).to.have.property('color', '#123BCA')
})
})

Expand Down

0 comments on commit 184918a

Please sign in to comment.