Skip to content

Commit

Permalink
CHORE: Improved message mapping and tests on error message service
Browse files Browse the repository at this point in the history
  • Loading branch information
davidibl committed Apr 4, 2020
1 parent f4da05c commit ab40d6d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 9 deletions.
78 changes: 78 additions & 0 deletions src/app/services/errorMessageService.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { TestBed, async } from '@angular/core/testing';
import { ErrorMessageService } from './errorMessageService';
import { TokenTransformationService } from '@xnoname/web-components';

let cut: ErrorMessageService;
let tokenService: jasmine.SpyObj<TokenTransformationService>;

describe('ErrorMessageService', () => {
beforeEach(async(() => {
const tokenServiceSpy = jasmine.createSpyObj('TokenTransformationService', ['replaceTokens']);

TestBed.configureTestingModule({
providers: [
ErrorMessageService,
{ provide: TokenTransformationService, useValue: tokenServiceSpy }
]
});

cut = TestBed.inject(ErrorMessageService);
tokenService = <any>TestBed.inject(TokenTransformationService);
}));

describe('transform message', () => {

it('calls the token transformation service and returns its result', async(() => {

const initialMessage = 'aMessage';
const replacedMessage = 'bMessage';
tokenService.replaceTokens.and.returnValue(replacedMessage);

const result = cut.getErrorMessage(initialMessage);
expect(result).toEqual(replacedMessage);
expect(tokenService.replaceTokens).toHaveBeenCalledTimes(1);
}));

it('returns the default message result', async(() => {

const initialMessage = 'aMessage';
const defaultMessage = 'bMessage';
tokenService.replaceTokens.and.callFake(function(param) {
return param;
});

const result = cut.getErrorMessage(initialMessage, defaultMessage);
expect(result).toEqual(defaultMessage);
expect(tokenService.replaceTokens).toHaveBeenCalledTimes(1);
}));

it('passes tokens to token service', async(() => {

const initialMessage = 'aMessage';
const tokensInitial = { a: 'b' };
tokenService.replaceTokens.and.callFake(function(message, tokens) {
expect(message).toEqual(initialMessage);
expect(tokens).toEqual(tokensInitial);
return message;
});

const result = cut.getErrorMessage(initialMessage, null, tokensInitial);
expect(result).toEqual(initialMessage);
expect(tokenService.replaceTokens).toHaveBeenCalledTimes(1);
}));

it('translates specific defined messages', async(() => {

const initialMessage = 'ENOENT: no such file or directory';
const expectedMessage = 'Die Datei konnte nicht gefunden werden: {path}';
ErrorMessageService['MESSAGE_MAP'] = [{message: initialMessage, target: expectedMessage}];
const tokensInitial = {};
tokenService.replaceTokens.and.callFake(function(message, tokens) {
return message;
});

const result = cut.getErrorMessage(initialMessage, null, tokensInitial);
expect(result).toEqual(expectedMessage);
}));
});
});
16 changes: 7 additions & 9 deletions src/app/services/errorMessageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,21 @@ import { TokenTransformationService } from '@xnoname/web-components';
@Injectable()
export class ErrorMessageService {

private static FILE_NOT_FOUND_MESSAGE_PATTERN = 'ENOENT: no such file or directory';

private static FILE_NOT_FOUND_MESSAGE_TPL = 'Die Datei konnte nicht gefunden werden: {path}';
private static MESSAGE_MAP = [
{ message: 'ENOENT: no such file or directory', target: 'Die Datei konnte nicht gefunden werden: {path}' }
];

public constructor(private _tokenTransformation: TokenTransformationService) {}

public getErrorMessage(message: string, defaultMessage?: string, tokens?: Object) {
message = this.mapMessage(message);
message = this.mapMessage(message, defaultMessage);

return this._tokenTransformation.replaceTokens(message, tokens);
}

private mapMessage(message: string, defaultMessage?: string) {
if (message.indexOf(ErrorMessageService.FILE_NOT_FOUND_MESSAGE_PATTERN) > -1) {
return ErrorMessageService.FILE_NOT_FOUND_MESSAGE_TPL;
}

return (defaultMessage) ? defaultMessage : message;
const translation = ErrorMessageService.MESSAGE_MAP.find(msg => msg.message.indexOf(message) > -1)?.target;
return (!!translation) ? translation :
(!!defaultMessage) ? defaultMessage : message;
}
}

0 comments on commit ab40d6d

Please sign in to comment.