This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reimplement update name with basic test coverage
- 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
Showing
9 changed files
with
359 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './console.logger' | ||
export * from './mock.logger' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.