Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
imsamdez committed Oct 15, 2021
1 parent 6f28dd1 commit e8dfaeb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
12 changes: 7 additions & 5 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const getGlobal = () => (window != null ? window : global);

/**
* Swaler Enumerations
* - types: List of log type that can be done
Expand Down Expand Up @@ -37,11 +39,11 @@ export interface SwalerLogOptions {
* Define the console function to call for each type of log
*/
export const handlers = {
[SwalerTypes.TRACE]: (): any => window.console.trace,
[SwalerTypes.DEBUG]: (): any => window.console.debug,
[SwalerTypes.INFO]: (): any => window.console.info,
[SwalerTypes.WARN]: (): any => window.console.warn,
[SwalerTypes.ERROR]: (): any => window.console.error,
[SwalerTypes.TRACE]: (): any => getGlobal().console.trace,
[SwalerTypes.DEBUG]: (): any => getGlobal().console.debug,
[SwalerTypes.INFO]: (): any => getGlobal().console.info,
[SwalerTypes.WARN]: (): any => getGlobal().console.warn,
[SwalerTypes.ERROR]: (): any => getGlobal().console.error,
};

let defaultLevel = SwalerLevels.DEBUG;
Expand Down
42 changes: 22 additions & 20 deletions tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,46 @@ describe('Handlers', () => {
const logger = new Swaler(null, {level: SwalerLevels.TRACE});

it('logger.trace should call console.trace with expected parameters', () => {
expect(testsUtils.handlers[SwalerTypes.TRACE]).toEqual(console.trace);
const mockHandlerTrace = jest.spyOn(testsUtils.handlers, SwalerTypes.TRACE);
expect(testsUtils.handlers[SwalerTypes.TRACE]()).toEqual(
global.console.trace
);
const mockHandlerTrace = jest.spyOn(global.console, 'trace');

expect(mockHandlerTrace).not.toHaveBeenCalled();
logger.trace('A', 'trace', 'message');
expect(mockHandlerTrace).toHaveBeenCalledWith('A', 'trace', 'message');
mockHandlerTrace.mockRestore();
});
it('logger.debug should call console.debug with expected parameters', () => {
expect(testsUtils.handlers[SwalerTypes.DEBUG]).toEqual(console.debug);
const mockHandlerDebug = jest.spyOn(testsUtils.handlers, SwalerTypes.DEBUG);
expect(testsUtils.handlers[SwalerTypes.DEBUG]()).toEqual(console.debug);
const mockHandlerDebug = jest.spyOn(global.console, 'debug');

expect(mockHandlerDebug).not.toHaveBeenCalled();
logger.debug('A', 'debug', 'message');
expect(mockHandlerDebug).toHaveBeenCalledWith('A', 'debug', 'message');
mockHandlerDebug.mockRestore();
});
it('logger.info should call console.info with expected parameters', () => {
expect(testsUtils.handlers[SwalerTypes.INFO]).toEqual(console.info);
const mockHandlerInfo = jest.spyOn(testsUtils.handlers, SwalerTypes.INFO);
expect(testsUtils.handlers[SwalerTypes.INFO]()).toEqual(console.info);
const mockHandlerInfo = jest.spyOn(global.console, 'info');

expect(mockHandlerInfo).not.toHaveBeenCalled();
logger.info('An', 'info', 'message');
expect(mockHandlerInfo).toHaveBeenCalledWith('An', 'info', 'message');
mockHandlerInfo.mockRestore();
});
it('logger.warn should call console.warn with expected parameters', () => {
expect(testsUtils.handlers[SwalerTypes.WARN]).toEqual(console.warn);
const mockHandlerWarn = jest.spyOn(testsUtils.handlers, SwalerTypes.WARN);
expect(testsUtils.handlers[SwalerTypes.WARN]()).toEqual(console.warn);
const mockHandlerWarn = jest.spyOn(global.console, 'warn');

expect(mockHandlerWarn).not.toHaveBeenCalled();
logger.warn('A', 'warn', 'message');
expect(mockHandlerWarn).toHaveBeenCalledWith('A', 'warn', 'message');
mockHandlerWarn.mockRestore();
});
it('logger.error should call console.error with expected parameters', () => {
expect(testsUtils.handlers[SwalerTypes.ERROR]).toEqual(console.error);
const mockHandlerError = jest.spyOn(testsUtils.handlers, SwalerTypes.ERROR);
expect(testsUtils.handlers[SwalerTypes.ERROR]()).toEqual(console.error);
const mockHandlerError = jest.spyOn(global.console, 'error');

expect(mockHandlerError).not.toHaveBeenCalled();
logger.error('An', 'error', 'message');
Expand All @@ -79,11 +81,11 @@ describe('Level', () => {
let mockHandlerError: jest.SpyInstance;

beforeAll(() => {
mockHandlerTrace = jest.spyOn(testsUtils.handlers, SwalerTypes.TRACE);
mockHandlerDebug = jest.spyOn(testsUtils.handlers, SwalerTypes.DEBUG);
mockHandlerInfo = jest.spyOn(testsUtils.handlers, SwalerTypes.INFO);
mockHandlerWarn = jest.spyOn(testsUtils.handlers, SwalerTypes.WARN);
mockHandlerError = jest.spyOn(testsUtils.handlers, SwalerTypes.ERROR);
mockHandlerTrace = jest.spyOn(global.console, 'trace');
mockHandlerDebug = jest.spyOn(global.console, 'debug');
mockHandlerInfo = jest.spyOn(global.console, 'info');
mockHandlerWarn = jest.spyOn(global.console, 'warn');
mockHandlerError = jest.spyOn(global.console, 'error');
});
afterAll(() => {
mockHandlerTrace.mockRestore();
Expand Down Expand Up @@ -278,11 +280,11 @@ describe('Log with options', () => {
let mockHandlerError: jest.SpyInstance;

beforeAll(() => {
mockHandlerTrace = jest.spyOn(testsUtils.handlers, SwalerTypes.TRACE);
mockHandlerDebug = jest.spyOn(testsUtils.handlers, SwalerTypes.DEBUG);
mockHandlerInfo = jest.spyOn(testsUtils.handlers, SwalerTypes.INFO);
mockHandlerWarn = jest.spyOn(testsUtils.handlers, SwalerTypes.WARN);
mockHandlerError = jest.spyOn(testsUtils.handlers, SwalerTypes.ERROR);
mockHandlerTrace = jest.spyOn(global.console, 'trace');
mockHandlerDebug = jest.spyOn(global.console, 'debug');
mockHandlerInfo = jest.spyOn(global.console, 'info');
mockHandlerWarn = jest.spyOn(global.console, 'warn');
mockHandlerError = jest.spyOn(global.console, 'error');
});
afterAll(() => {
mockHandlerTrace.mockRestore();
Expand Down

0 comments on commit e8dfaeb

Please sign in to comment.