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

Commit

Permalink
test: unit test a successful name update
Browse files Browse the repository at this point in the history
- also allow to only pass 1 parameter to the loggers
  • Loading branch information
severo committed Jan 21, 2020
1 parent d0cdf6e commit 912c746
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/shared/logger/console.logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ describe('Loggers', () => {
try {
logger.info(infos[0].context, infos[0].message)
logger.error(errors[0].context, errors[0].message)
logger.info(infos[1].context, infos[1].message)
logger.info(infos[1].context)
logger.error(errors[0].context)
done()
} catch (e) {
done(e)
Expand Down
4 changes: 2 additions & 2 deletions src/shared/logger/console.logger.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { MockLogger } from './mock.logger'

export class ConsoleLogger extends MockLogger {
public error = (context: string, message: string): void =>
public error = (context: string, message?: string): void =>
console.error(MockLogger.forgeLog(context, message))
public info = (context: string, message: string): void =>
public info = (context: string, message?: string): void =>
console.info(MockLogger.forgeLog(context, message))
public getInfoLogs = (): string[] => {
throw new ReferenceError('Not implemented')
Expand Down
8 changes: 4 additions & 4 deletions src/shared/logger/mock.logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ describe('Loggers', () => {
expect(logger.getInfoLogs()).to.have.length(0)
})

it('should concatenate the context and message strings', () => {
it('should concatenate context and message (if present)', () => {
// arrange
const strings = [
{ context: 'in a function', message: 'undefined variable' },
{ context: 'at startup', message: 'server started normally' },
{ context: 'at shutdown', message: 'server stopped normally' },
{ context: 'at shutdown' },
]

// assert
Expand All @@ -26,10 +26,10 @@ describe('Loggers', () => {
).to.equal('in a function - undefined variable')
expect(
MockLogger.forgeLog(strings[1].context, strings[1].message)
).to.equal('at startup - server started normally')
).to.equal('at shutdown - server stopped normally')
expect(
MockLogger.forgeLog(strings[2].context, strings[2].message)
).to.equal('at shutdown - server stopped normally')
).to.equal('at shutdown')
})

it('should keep track of error and info logs', () => {
Expand Down
8 changes: 4 additions & 4 deletions src/shared/logger/mock.logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ export class MockLogger {
private infoLogs: string[] = []
) {}

public error = (context: string, message: string): void => {
public error = (context: string, message?: string): void => {
this.errorLogs.push(MockLogger.forgeLog(context, message))
}
public info = (context: string, message: string): void => {
public info = (context: string, message?: string): void => {
this.infoLogs.push(MockLogger.forgeLog(context, message))
}

public static forgeLog = (context: string, message: string): string =>
`${context} - ${message}`
public static forgeLog = (context: string, message?: string): string =>
message ? `${context} - ${message}` : context

public getErrorLogs(): string[] {
return this.errorLogs
Expand Down
73 changes: 71 additions & 2 deletions src/socket.io/socket.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,47 @@ describe('Server', () => {
})

describe('update-user-name', () => {
it('should log info message for empty name and access should be false', (done: Function) => {
it('should log info message for empty data and updated should be false', (done: Function) => {
// arrange
const updateUserNameEventArgs: undefined = undefined
const updateUserNameEvent = new UpdateUserNameEvent(
updateUserNameEventArgs
)

new Promise(resolve =>
client.on('connect', () => {
// act
client.emit(
UpdateUserNameEvent.eventName,
updateUserNameEvent.data,
(value: UpdateUserNameAckArgs) => resolve(value)
)
})
)
.then((value: UpdateUserNameAckArgs) => {
// assert
expect(value).to.not.be.undefined
expect(value).to.have.property('updated', false)
expect(value).to.have.property('error')
expect(value.error).to.have.property('name', 'Error')
expect(value.error).to.have.property(
'message',
`Parameter data is required`
)

const logs = mockLogger.getInfoLogs()
logs.should.include.something.that.equals(
`User name could not be updated - Parameter data is required`
)
done()
})
.catch(e => done(e))
})

it('should log info message for empty name and updated should be false', (done: Function) => {
// arrange
const expectedParameter: string = 'name'
const updateUserNameEventArgs: { name: string } = { name: '' }
const updateUserNameEventArgs: UpdateUserNameEventArgs = { name: '' }
const updateUserNameEvent = new UpdateUserNameEvent(
updateUserNameEventArgs
)
Expand Down Expand Up @@ -98,6 +135,38 @@ describe('Server', () => {
})
.catch(e => done(e))
})

it('should log info message for correct name, updated should be true and error should not exist', (done: Function) => {
// arrange
const updateUserNameEventArgs: UpdateUserNameEventArgs = {
name: 'George',
}
const updateUserNameEvent = new UpdateUserNameEvent(
updateUserNameEventArgs
)

new Promise(resolve =>
client.on('connect', () => {
// act
client.emit(
UpdateUserNameEvent.eventName,
updateUserNameEvent.data,
(value: UpdateUserNameAckArgs) => resolve(value)
)
})
)
.then((value: UpdateUserNameAckArgs) => {
// assert
expect(value).to.not.be.undefined
expect(value).to.have.property('updated', true)
expect(value).to.not.have.property('error')

const logs = mockLogger.getInfoLogs()
logs.should.include.something.that.equals(`User name updated`)
done()
})
.catch(e => done(e))
})
})

//
Expand Down
20 changes: 10 additions & 10 deletions src/socket.io/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ class Socket {
this.io
.of('/occupapp-beta')
.on(ConnectionEvent.eventName, (socket: SocketIOClient.Socket) => {
const socketUser: User = this.getOrCreateUser(socket.id)
// Note that a new socket (and this socket.id) is created on each
// connection. There is no persistence for a same user between
// connections
const socketUser: User = this.createUser(socket.id)

socket.on(
UpdateUserNameEvent.eventName,
Expand Down Expand Up @@ -48,6 +51,7 @@ class Socket {
// TODO: alternative: send mutation : room-guest-name-updated
// this.emitLoggedUsersToRoom(socket, room)

this.log.info('User name updated')
ack({ updated: true })
}

Expand Down Expand Up @@ -101,15 +105,11 @@ class Socket {
})
}

private getOrCreateUser = (id: SocketIOClient.Socket['id']): User => {
const user = this.users.get(id)
if (user === undefined) {
const newUser = new User(id)
this.users.set(id, newUser)
this.log.info(`getOrCreateUser`, `New user created for socket ${id}`)
return newUser
}
return user
private createUser = (id: SocketIOClient.Socket['id']): User => {
const newUser = new User(id)
this.users.set(id, newUser)
this.log.info(`getOrCreateUser`, `New user created for socket ${id}`)
return newUser
}

// private emitInternalServerError(socket: SocketIOClient.Socket, error: Error) {
Expand Down

0 comments on commit 912c746

Please sign in to comment.