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

Commit

Permalink
feat: reimplement update name with basic test coverage
Browse files Browse the repository at this point in the history
- also includes two classes to log in the server: in the console (by
default, for production for example), and in a local property (for
tests)
  • Loading branch information
severo committed Jan 21, 2020
1 parent 4f38e1f commit dec72bb
Show file tree
Hide file tree
Showing 9 changed files with 359 additions and 177 deletions.
1 change: 1 addition & 0 deletions src/shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './validation/guard'
export * from './validation/validation.methods'
export * from './logger/'
49 changes: 49 additions & 0 deletions src/shared/logger/console.logger.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { expect } from 'chai'
import { ConsoleLogger } from './console.logger'

describe('Loggers', () => {
describe('ConsoleLogger', () => {
it('should log info and errors', done => {
// arrange
const errors = [
{ context: 'in a function', message: 'undefined variable' },
]
const infos = [
{ context: 'at startup', message: 'server started normally' },
{ context: 'at shutdown', message: 'server stopped normally' },
]
const logger = new ConsoleLogger()

// act
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)
done()
} catch (e) {
done(e)
}
})

it('should throw if accesssing getErrorLogs or getInfoLogs', () => {
// arrange
const logger = new ConsoleLogger()

// assert
try {
logger.getInfoLogs()
} catch (e) {
expect(e).to.not.be.undefined
expect(e).to.have.property('name', 'ReferenceError')
expect(e).to.have.property('message', 'Not implemented')
}
try {
logger.getErrorLogs()
} catch (e) {
expect(e).to.not.be.undefined
expect(e).to.have.property('name', 'ReferenceError')
expect(e).to.have.property('message', 'Not implemented')
}
})
})
})
14 changes: 14 additions & 0 deletions src/shared/logger/console.logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MockLogger } from './mock.logger'

export class ConsoleLogger extends MockLogger {
public error = (context: string, message: string): void =>
console.error(MockLogger.forgeLog(context, message))
public info = (context: string, message: string): void =>
console.info(MockLogger.forgeLog(context, message))
public getInfoLogs = (): string[] => {
throw new ReferenceError('Not implemented')
}
public getErrorLogs = (): string[] => {
throw new ReferenceError('Not implemented')
}
}
2 changes: 2 additions & 0 deletions src/shared/logger/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './console.logger'
export * from './mock.logger'
61 changes: 61 additions & 0 deletions src/shared/logger/mock.logger.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { expect } from 'chai'
import { MockLogger } from './mock.logger'

describe('Loggers', () => {
describe('MockLogger', () => {
it('should initialize empty logs', () => {
// act
const logger = new MockLogger()

// assert
expect(logger.getErrorLogs()).to.have.length(0)
expect(logger.getInfoLogs()).to.have.length(0)
})

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

// assert
expect(
MockLogger.forgeLog(strings[0].context, strings[0].message)
).to.equal('in a function - undefined variable')
expect(
MockLogger.forgeLog(strings[1].context, strings[1].message)
).to.equal('at startup - server started normally')
expect(
MockLogger.forgeLog(strings[2].context, strings[2].message)
).to.equal('at shutdown - server stopped normally')
})

it('should keep track of error and info logs', () => {
// arrange
const errors = [
{ context: 'in a function', message: 'undefined variable' },
]
const infos = [
{ context: 'at startup', message: 'server started normally' },
{ context: 'at shutdown', message: 'server stopped normally' },
]
const logger = new MockLogger()

// act
logger.info(infos[0].context, infos[0].message)
logger.error(errors[0].context, errors[0].message)
logger.info(infos[1].context, infos[1].message)

// assert
const loggedErrors = logger.getErrorLogs()
const loggedInfos = logger.getInfoLogs()
expect(loggedErrors).to.have.length(1)
expect(loggedErrors[0]).to.equal('in a function - undefined variable')
expect(loggedInfos).to.have.length(2)
expect(loggedInfos[0]).to.equal('at startup - server started normally')
expect(loggedInfos[1]).to.equal('at shutdown - server stopped normally')
})
})
})
23 changes: 23 additions & 0 deletions src/shared/logger/mock.logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export class MockLogger {
constructor(
private errorLogs: string[] = [],
private infoLogs: string[] = []
) {}

public error = (context: string, message: string): void => {
this.errorLogs.push(MockLogger.forgeLog(context, message))
}
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 getErrorLogs(): string[] {
return this.errorLogs
}
public getInfoLogs(): string[] {
return this.infoLogs
}
}
Loading

0 comments on commit dec72bb

Please sign in to comment.