Skip to content
Snippets Groups Projects
Commit f297593d authored by OZGCloud's avatar OZGCloud
Browse files

OZG-1389 OZG-1790 cleanup

parent ce68ef31
Branches
Tags
No related merge requests found
import { replaceAllWhitespaces, replacePlaceholder, replacePlaceholders } from './tech.util'; import * as faker from 'faker';
import { getFirstLetter, replaceAllWhitespaces, replacePlaceholder, replacePlaceholders } from './tech.util';
describe('TechUtil', () => { describe('TechUtil', () => {
...@@ -57,4 +58,21 @@ describe('TechUtil', () => { ...@@ -57,4 +58,21 @@ describe('TechUtil', () => {
expect(replaced).toBe('replace this and that'); expect(replaced).toBe('replace this and that');
}) })
}) })
describe('get first letter', () => {
it('should return on valid string value', () => {
const value: string = `e ${faker.random.word()}`;
expect(getFirstLetter(value)).toEqual('e');
})
it('should return undefined on undefined value', () => {
expect(getFirstLetter(undefined)).toBeNull();
})
it('should return undefined on null value', () => {
expect(getFirstLetter(null)).toBeNull();
})
})
}) })
\ No newline at end of file
...@@ -35,3 +35,7 @@ export function sleep(delayInMs: number) { ...@@ -35,3 +35,7 @@ export function sleep(delayInMs: number) {
var start = new Date().getTime(); var start = new Date().getTime();
while (new Date().getTime() < start + delayInMs); while (new Date().getTime() < start + delayInMs);
} }
export function getFirstLetter(value: string): string {
return isNil(value) ? null : value.substr(0, 1);
}
\ No newline at end of file
import { MessageCode } from '@goofy-client/tech-shared'; import { MessageCode } from '@goofy-client/tech-shared';
export const UserProfileMessage = { export const userProfileMessage = {
[MessageCode.SERVICE_UNAVAILABLE]: 'Die Benutzerdaten konnten nicht geladen werden', [MessageCode.SERVICE_UNAVAILABLE]: 'Die Benutzerdaten konnten nicht geladen werden',
[MessageCode.RESOURCE_NOT_FOUND]: 'Der zugewiesene Bearbeiter konnte nicht gefunden werden', [MessageCode.RESOURCE_NOT_FOUND]: 'Der zugewiesene Bearbeiter konnte nicht gefunden werden',
UNASSIGNED: 'Kein Bearbeiter zugewiesen', UNASSIGNED: 'Kein Bearbeiter zugewiesen',
......
import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MatIcon } from '@angular/material/icon'; import { MatIcon } from '@angular/material/icon';
import { MatTooltipModule } from '@angular/material/tooltip'; import { MatTooltipModule } from '@angular/material/tooltip';
import { createEmptyStateResource, createErrorStateResource, createStateResource, MessageCode, StateResource } from '@goofy-client/tech-shared'; import { ApiError, createEmptyStateResource, createErrorStateResource, createStateResource, MessageCode } from '@goofy-client/tech-shared';
import { getElementFromFixture } from '@goofy-client/test-utils'; import { getElementFromFixture } from '@goofy-client/test-utils';
import { userProfileMessage, UserProfileResource } from '@goofy-client/user-profile-shared';
import * as faker from 'faker';
import { createUserProfileResource } from 'libs/user-profile-shared/test/user-profile'; import { createUserProfileResource } from 'libs/user-profile-shared/test/user-profile';
import { createApiError } from '../../../../tech-shared/test/error'; import { createApiError, createIssue } from '../../../../tech-shared/test/error';
import { UserIconComponent } from './user-icon.component'; import { UserIconComponent } from './user-icon.component';
import { UserProfileMessage, UserProfileResource } from '@goofy-client/user-profile-shared';
describe('UserIconComponent', () => { describe('UserIconComponent', () => {
let component: UserIconComponent; let component: UserIconComponent;
...@@ -17,6 +18,8 @@ describe('UserIconComponent', () => { ...@@ -17,6 +18,8 @@ describe('UserIconComponent', () => {
const profileUserNotFound: string = '[data-test-id="user-profile-user-not-found"]'; const profileUserNotFound: string = '[data-test-id="user-profile-user-not-found"]';
const profileServiceUnavailable: string = '[data-test-id="user-profile-service-unavailable"]'; const profileServiceUnavailable: string = '[data-test-id="user-profile-service-unavailable"]';
const userProfile: UserProfileResource = createUserProfileResource();
beforeEach(async () => { beforeEach(async () => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [MatTooltipModule], imports: [MatTooltipModule],
...@@ -30,7 +33,7 @@ describe('UserIconComponent', () => { ...@@ -30,7 +33,7 @@ describe('UserIconComponent', () => {
beforeEach(() => { beforeEach(() => {
fixture = TestBed.createComponent(UserIconComponent); fixture = TestBed.createComponent(UserIconComponent);
component = fixture.componentInstance; component = fixture.componentInstance;
component.userProfileStateResource = createStateResource(createUserProfileResource()); component.userProfileStateResource = createStateResource(userProfile);
fixture.detectChanges(); fixture.detectChanges();
}); });
...@@ -50,7 +53,7 @@ describe('UserIconComponent', () => { ...@@ -50,7 +53,7 @@ describe('UserIconComponent', () => {
}) })
it('should show icon assigned', () => { it('should show icon assigned', () => {
component.userProfileStateResource = createStateResource(createUserProfileResource()); component.userProfileStateResource = createStateResource(userProfile);
fixture.detectChanges(); fixture.detectChanges();
const element = getElementFromFixture(fixture, profileAssignedIcon); const element = getElementFromFixture(fixture, profileAssignedIcon);
...@@ -59,8 +62,7 @@ describe('UserIconComponent', () => { ...@@ -59,8 +62,7 @@ describe('UserIconComponent', () => {
}) })
it('should show user not found icon', () => { it('should show user not found icon', () => {
component.userProfileStateResource = createErrorStateResource(createApiError()); component.userProfileStateResource = createErrorStateResource(createApiErrorWithMessageCode(MessageCode.RESOURCE_NOT_FOUND));
component.userProfileStateResource.error.issues[0].messageCode = MessageCode.RESOURCE_NOT_FOUND;
fixture.detectChanges(); fixture.detectChanges();
const element = getElementFromFixture(fixture, profileUserNotFound); const element = getElementFromFixture(fixture, profileUserNotFound);
...@@ -69,62 +71,80 @@ describe('UserIconComponent', () => { ...@@ -69,62 +71,80 @@ describe('UserIconComponent', () => {
}) })
it('should show service unavailable icon', () => { it('should show service unavailable icon', () => {
component.userProfileStateResource = createErrorStateResource(createApiError()); component.userProfileStateResource = createErrorStateResource(createApiErrorWithMessageCode(MessageCode.SERVICE_UNAVAILABLE));
component.userProfileStateResource.error.issues[0].messageCode = MessageCode.SERVICE_UNAVAILABLE;
fixture.detectChanges(); fixture.detectChanges();
const element = getElementFromFixture(fixture, profileServiceUnavailable); const element = getElementFromFixture(fixture, profileServiceUnavailable);
expect(element).toBeInstanceOf(HTMLElement); expect(element).toBeInstanceOf(HTMLElement);
}) })
})
describe('tooltip', () => { describe('tooltip', () => {
it('should return user name', () => { it('should return user name', () => {
const userProfileStateResource = createStateResource(createUserProfileResource()); component.userProfileStateResource = createStateResource(userProfile);
const expectedToolTip = userProfileStateResource.resource.firstName + ' ' + userProfileStateResource.resource.lastName;
const tooltip = component.getTooltip(userProfileStateResource); const tooltip = component.getTooltip();
expect(tooltip).toEqual(expectedToolTip); expect(tooltip).toEqual(userProfile.firstName + ' ' + userProfile.lastName);
}) })
it('should return no user assigned', () => { it('should return no user assigned', () => {
const userProfileStateResource: StateResource<UserProfileResource> = createEmptyStateResource(); component.userProfileStateResource = createEmptyStateResource();
const tooltip = component.getTooltip(userProfileStateResource); const tooltip = component.getTooltip();
expect(tooltip).toEqual(UserProfileMessage.UNASSIGNED); expect(tooltip).toEqual(userProfileMessage.UNASSIGNED);
}) })
it('should return apiError as stateResource', () => { it('should return apiError as stateResource', () => {
const userProfileStateResource: StateResource<UserProfileResource> = createErrorStateResource(createApiError()); component.userProfileStateResource = createErrorStateResource(createApiError());
component.getTooltip = jest.fn(); component.getErrorTooltip = jest.fn();
component.getTooltip(userProfileStateResource); component.getTooltip();
expect(component.getTooltip).toHaveBeenCalledWith(userProfileStateResource); expect(component.getErrorTooltip).toHaveBeenCalled();
}) })
}); });
describe('error tooltip', () => { describe('error tooltip', () => {
it('should return user not found', () => { it('should return user not found', () => {
const userProfileStateResource: StateResource<UserProfileResource> = createErrorStateResource(createApiError()); component.userProfileStateResource = createErrorStateResource(createApiErrorWithMessageCode(MessageCode.RESOURCE_NOT_FOUND));
userProfileStateResource.error.issues[0].messageCode = MessageCode.RESOURCE_NOT_FOUND;
const tooltip = component.getErrorTooltip(userProfileStateResource); const tooltip = component.getErrorTooltip();
expect(tooltip).toEqual(UserProfileMessage[MessageCode.RESOURCE_NOT_FOUND]); expect(tooltip).toEqual(userProfileMessage[MessageCode.RESOURCE_NOT_FOUND]);
}) })
it('should return backend service unavailable', () => { it('should return backend service unavailable', () => {
const userProfileStateResource: StateResource<UserProfileResource> = createErrorStateResource(createApiError()); component.userProfileStateResource = createErrorStateResource(createApiErrorWithMessageCode(MessageCode.SERVICE_UNAVAILABLE));
userProfileStateResource.error.issues[0].messageCode = MessageCode.SERVICE_UNAVAILABLE;
const tooltip = component.getErrorTooltip(userProfileStateResource); const tooltip = component.getErrorTooltip();
expect(tooltip).toEqual(UserProfileMessage[MessageCode.SERVICE_UNAVAILABLE]); expect(tooltip).toEqual(userProfileMessage[MessageCode.SERVICE_UNAVAILABLE]);
}) })
});
describe('on unexpected error', () => {
it('should return empty string on non existing messageCode issue', () => {
component.userProfileStateResource = createErrorStateResource(createApiErrorWithMessageCode(faker.random.word()));
const tooltip = component.getErrorTooltip();
expect(tooltip).toEqual('');
}) })
it('should return empty string on null issue', () => {
component.userProfileStateResource = createErrorStateResource({ ...createApiError(), issues: [null] });
const tooltip = component.getErrorTooltip();
expect(tooltip).toEqual('');
})
})
})
function createApiErrorWithMessageCode(messageCode: string): ApiError {
return { ...createApiError(), issues: [{ ...createIssue(), messageCode }] };
}
}); });
import { Component, Input, SimpleChanges } from '@angular/core'; import { Component, Input, SimpleChanges } from '@angular/core';
import { createEmptyStateResource, MessageCode, StateResource } from '@goofy-client/tech-shared'; import { createEmptyStateResource, getFirstLetter, hasError, MessageCode, StateResource } from '@goofy-client/tech-shared';
import { UserProfileMessage, UserProfileResource } from '@goofy-client/user-profile-shared'; import { userProfileMessage, UserProfileResource } from '@goofy-client/user-profile-shared';
import { isNull, isUndefined } from 'lodash';
import { isNil } from 'lodash';
@Component({ @Component({
selector: 'goofy-client-user-icon', selector: 'goofy-client-user-icon',
...@@ -15,44 +15,46 @@ export class UserIconComponent { ...@@ -15,44 +15,46 @@ export class UserIconComponent {
@Input() disableTooltip: boolean = false; @Input() disableTooltip: boolean = false;
readonly messageCode = MessageCode; readonly messageCode = MessageCode;
tooltip: string = ''; readonly userMessage = userProfileMessage;
tooltip: string;
ngOnChanges(changes: SimpleChanges): void { ngOnChanges(changes: SimpleChanges): void {
if (changes.userProfileStateResource) { if (changes.userProfileStateResource) {
this.tooltip = this.getTooltip(this.userProfileStateResource); this.tooltip = this.getTooltip();
} }
} }
get initials(): string { getTooltip(): string {
return this.getFirstLetter(this.userProfileStateResource.resource.firstName) + this.getFirstLetter(this.userProfileStateResource.resource.lastName); if (this.userProfileStateResource.resource) {
return this.getUserTooltip();
} }
if (hasError(this.userProfileStateResource)) {
public get errorMessageCode(): string { return this.getErrorTooltip();
return this.userProfileStateResource.error?.issues[0].messageCode ?? '';
} }
return userProfileMessage.UNASSIGNED;
private getFirstLetter(value: string): string {
return isNil(value) ? undefined : value.substr(0, 1).toUpperCase();
} }
getTooltip(userProfileStateResource: StateResource<UserProfileResource>): string { getUserTooltip(): string {
if (userProfileStateResource.resource) { return `${this.userProfileStateResource.resource.firstName} ${this.userProfileStateResource.resource.lastName}`;
return userProfileStateResource.resource.firstName + ' ' + userProfileStateResource.resource.lastName;
} }
if (userProfileStateResource.error) { //TOASK: leerstring bei unerwartetem Fehler?
return this.getErrorTooltip(userProfileStateResource); getErrorTooltip(): string {
const message: string = userProfileMessage[this.errorMessageCode];
return isUndefined(message) ? '' : message;
} }
return UserProfileMessage.UNASSIGNED; get errorMessageCode(): string {
return this.userProfileStateResource.error?.issues[0]?.messageCode;
} }
getErrorTooltip(userProfileStateResource: StateResource<UserProfileResource>): string { get initials(): string {
if (!userProfileStateResource.error?.issues[0].messageCode) { return this.getFirstLetterUpperCase(this.userProfileStateResource.resource.firstName) + this.getFirstLetterUpperCase(this.userProfileStateResource.resource.lastName);
return '';
} }
const messageCode = userProfileStateResource.error.issues[0].messageCode; private getFirstLetterUpperCase(value: string) {
return UserProfileMessage[messageCode] ?? messageCode; const firstLetter: string = getFirstLetter(value);
return isNull(firstLetter) ? null : firstLetter.toUpperCase();
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment