feat: init and fork some code from angular-auth-oidc-client
Some checks are pending
Build, Lint & Test Lib / Built, Lint and Test Library (push) Waiting to run
Build, Lint & Test Lib / Angular latest (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest Standalone & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular 16 & RxJs 6 (push) Blocked by required conditions
Build, Lint & Test Lib / Angular V16 (push) Blocked by required conditions
Docs / Build and Deploy Docs Job (push) Waiting to run
Docs / Close Pull Request Job (push) Waiting to run

This commit is contained in:
2025-01-18 01:05:00 +08:00
parent 276d9fbda8
commit 983254164b
201 changed files with 35689 additions and 0 deletions

View File

@@ -0,0 +1,252 @@
import { TestBed } from '@angular/core/testing';
import { AbstractLoggerService } from './abstract-logger.service';
import { ConsoleLoggerService } from './console-logger.service';
import { LogLevel } from './log-level';
import { LoggerService } from './logger.service';
describe('Logger Service', () => {
let loggerService: LoggerService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
LoggerService,
{ provide: AbstractLoggerService, useClass: ConsoleLoggerService },
],
});
});
beforeEach(() => {
loggerService = TestBed.inject(LoggerService);
});
it('should create', () => {
expect(loggerService).toBeTruthy();
});
describe('logError', () => {
it('should not log error if loglevel is None', () => {
const spy = spyOn(console, 'error');
loggerService.logError(
{ configId: 'configId1', logLevel: LogLevel.None },
'some message'
);
expect(spy).not.toHaveBeenCalled();
});
it('should log error as default if error is string', () => {
const spy = spyOn(console, 'error');
loggerService.logError({ configId: 'configId1' }, 'some message');
expect(spy).toHaveBeenCalledOnceWith('[ERROR] configId1 - some message');
});
it('should log error as default if error is object', () => {
const spy = spyOn(console, 'error');
loggerService.logError({ configId: 'configId1' }, { some: 'message' });
expect(spy).toHaveBeenCalledOnceWith(
'[ERROR] configId1 - {"some":"message"}'
);
});
it('should always log error with args', () => {
const spy = spyOn(console, 'error');
loggerService.logError(
{ configId: 'configId1' },
'some message',
'arg1',
'arg2'
);
expect(spy).toHaveBeenCalledOnceWith(
'[ERROR] configId1 - some message',
'arg1',
'arg2'
);
});
});
describe('logWarn', () => {
it('should not log if no log level is set (null)', () => {
const spy = spyOn(console, 'warn');
loggerService.logWarning(
{ configId: 'configId1', logLevel: undefined },
'some message'
);
expect(spy).not.toHaveBeenCalled();
});
it('should not log if no config is given', () => {
const spy = spyOn(console, 'warn');
loggerService.logWarning({}, 'some message');
expect(spy).not.toHaveBeenCalled();
});
it('should not log if no log level is set (undefined)', () => {
const spy = spyOn(console, 'warn');
loggerService.logWarning({ configId: 'configId1' }, 'some message');
expect(spy).not.toHaveBeenCalled();
});
it('should not log if log level is turned off', () => {
const spy = spyOn(console, 'warn');
loggerService.logWarning(
{ configId: 'configId1', logLevel: LogLevel.None },
'some message'
);
expect(spy).not.toHaveBeenCalled();
});
it('should log warning when loglevel is Warn and message is string', () => {
const spy = spyOn(console, 'warn');
loggerService.logWarning(
{ configId: 'configId1', logLevel: LogLevel.Warn },
'some message'
);
expect(spy).toHaveBeenCalledOnceWith('[WARN] configId1 - some message');
});
it('should log warning when loglevel is Warn and message is object', () => {
const spy = spyOn(console, 'warn');
loggerService.logWarning(
{ configId: 'configId1', logLevel: LogLevel.Warn },
{ some: 'message' }
);
expect(spy).toHaveBeenCalledOnceWith(
'[WARN] configId1 - {"some":"message"}'
);
});
it('should log warning when loglevel is Warn with args', () => {
const spy = spyOn(console, 'warn');
loggerService.logWarning(
{ configId: 'configId1', logLevel: LogLevel.Warn },
'some message',
'arg1',
'arg2'
);
expect(spy).toHaveBeenCalledOnceWith(
'[WARN] configId1 - some message',
'arg1',
'arg2'
);
});
it('should log warning when loglevel is Debug', () => {
const spy = spyOn(console, 'warn');
loggerService.logWarning(
{ configId: 'configId1', logLevel: LogLevel.Debug },
'some message'
);
expect(spy).toHaveBeenCalledOnceWith('[WARN] configId1 - some message');
});
it('should not log warning when loglevel is error', () => {
const spy = spyOn(console, 'warn');
loggerService.logWarning(
{ configId: 'configId1', logLevel: LogLevel.Error },
'some message'
);
expect(spy).not.toHaveBeenCalled();
});
});
describe('logDebug', () => {
it('should not log if no log level is set (null)', () => {
const spy = spyOn(console, 'debug');
loggerService.logDebug(
{ configId: 'configId1', logLevel: undefined },
'some message'
);
expect(spy).not.toHaveBeenCalled();
});
it('should not log if no log level is set (undefined)', () => {
const spy = spyOn(console, 'debug');
loggerService.logDebug({ configId: 'configId1' }, 'some message');
expect(spy).not.toHaveBeenCalled();
});
it('should not log if log level is turned off', () => {
const spy = spyOn(console, 'debug');
loggerService.logDebug(
{ configId: 'configId1', logLevel: LogLevel.None },
'some message'
);
expect(spy).not.toHaveBeenCalled();
});
it('should log when loglevel is Debug and value is string', () => {
const spy = spyOn(console, 'debug');
loggerService.logDebug(
{ configId: 'configId1', logLevel: LogLevel.Debug },
'some message'
);
expect(spy).toHaveBeenCalledOnceWith('[DEBUG] configId1 - some message');
});
it('should log when loglevel is Debug and value is object', () => {
const spy = spyOn(console, 'debug');
loggerService.logDebug(
{ configId: 'configId1', logLevel: LogLevel.Debug },
{ some: 'message' }
);
expect(spy).toHaveBeenCalledOnceWith(
'[DEBUG] configId1 - {"some":"message"}'
);
});
it('should log when loglevel is Debug with args', () => {
const spy = spyOn(console, 'debug');
loggerService.logDebug(
{ configId: 'configId1', logLevel: LogLevel.Debug },
'some message',
'arg1',
'arg2'
);
expect(spy).toHaveBeenCalledOnceWith(
'[DEBUG] configId1 - some message',
'arg1',
'arg2'
);
});
it('should not log when loglevel is Warn', () => {
const spy = spyOn(console, 'debug');
loggerService.logDebug(
{ configId: 'configId1', logLevel: LogLevel.Warn },
'some message'
);
expect(spy).not.toHaveBeenCalled();
});
it('should not log when loglevel is error', () => {
const spy = spyOn(console, 'debug');
loggerService.logDebug(
{ configId: 'configId1', logLevel: LogLevel.Error },
'some message'
);
expect(spy).not.toHaveBeenCalled();
});
});
});