Skip to content

Commit

Permalink
test(logging): fix unit tests for LoggingService
Browse files Browse the repository at this point in the history
  • Loading branch information
sleidig committed Dec 3, 2019
1 parent 4f753c9 commit 2e46b9d
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions src/app/logging/logging.service.spec.ts
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);
});
});

0 comments on commit 2e46b9d

Please sign in to comment.