-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(logging): fix unit tests for LoggingService
- Loading branch information
Showing
1 changed file
with
29 additions
and
26 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,61 +1,64 @@ | ||
import {LoggingService} from './logging.service'; | ||
import * as Raven from 'raven-js'; | ||
|
||
class LoggingServiceRavenMock extends LoggingService { | ||
|
||
public latestRavenCalls: Array<[string, Raven.RavenOptions]>; | ||
|
||
constructor() { | ||
super(); | ||
this.latestRavenCalls = []; | ||
} | ||
|
||
protected ravenCaptureMessage(message: string, options: Raven.RavenOptions): void { | ||
this.latestRavenCalls.push([message, options]); | ||
} | ||
} | ||
import {LogLevel} from './log-level'; | ||
|
||
|
||
describe('LoggingService', () => { | ||
const testMessage = 'FANCY_TEST_MESSAGE'; | ||
|
||
let loggingService: LoggingServiceRavenMock; | ||
let loggingService: LoggingService; | ||
beforeEach(() => { | ||
loggingService = new LoggingServiceRavenMock(); | ||
loggingService = new LoggingService(); | ||
}); | ||
|
||
function checkReceivedLogMessage(level: Raven.LogLevel) { | ||
const receivedLogRequest = loggingService.latestRavenCalls.pop(); | ||
expect(receivedLogRequest[0]).toEqual(testMessage); | ||
expect(receivedLogRequest[1].level).toEqual(level); | ||
} | ||
|
||
it('should be created', () => { | ||
expect(loggingService).toBeTruthy(); | ||
}); | ||
|
||
it('should log a debug message', function () { | ||
spyOn<any>(loggingService, 'logToConsole'); | ||
spyOn<any>(loggingService, 'logToRemoteMonitoring'); | ||
loggingService.debug(testMessage); | ||
|
||
checkReceivedLogMessage('debug'); | ||
expect(loggingService['logToConsole']).toHaveBeenCalledWith(testMessage, LogLevel.DEBUG); | ||
expect(loggingService['logToRemoteMonitoring']).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should log a info message', function () { | ||
spyOn<any>(loggingService, 'logToConsole'); | ||
spyOn<any>(loggingService, 'logToRemoteMonitoring'); | ||
loggingService.info(testMessage); | ||
|
||
checkReceivedLogMessage('info'); | ||
expect(loggingService['logToConsole']).toHaveBeenCalledWith(testMessage, LogLevel.INFO); | ||
expect(loggingService['logToRemoteMonitoring']).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should log a warn message', function () { | ||
spyOn<any>(loggingService, 'logToConsole'); | ||
spyOn<any>(loggingService, 'logToRemoteMonitoring'); | ||
loggingService.warn(testMessage); | ||
|
||
checkReceivedLogMessage('warn'); | ||
expect(loggingService['logToConsole']).toHaveBeenCalledWith(testMessage, LogLevel.WARN); | ||
expect(loggingService['logToRemoteMonitoring']).toHaveBeenCalledWith(testMessage, LogLevel.WARN); | ||
}); | ||
|
||
|
||
it('should log a error message', function () { | ||
spyOn<any>(loggingService, 'logToConsole'); | ||
spyOn<any>(loggingService, 'logToRemoteMonitoring'); | ||
loggingService.error(testMessage); | ||
|
||
checkReceivedLogMessage('error'); | ||
expect(loggingService['logToConsole']).toHaveBeenCalledWith(testMessage, LogLevel.ERROR); | ||
expect(loggingService['logToRemoteMonitoring']).toHaveBeenCalledWith(testMessage, LogLevel.ERROR); | ||
}); | ||
|
||
|
||
it('should log a message through the generic log method', function () { | ||
spyOn<any>(loggingService, 'logToConsole'); | ||
spyOn<any>(loggingService, 'logToRemoteMonitoring'); | ||
loggingService.log(testMessage, LogLevel.WARN); | ||
|
||
expect(loggingService['logToConsole']).toHaveBeenCalledWith(testMessage, LogLevel.WARN); | ||
expect(loggingService['logToRemoteMonitoring']).toHaveBeenCalledWith(testMessage, LogLevel.WARN); | ||
}); | ||
}); |