Skip to content

Commit

Permalink
fix keyresult form tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kcinay055679 committed Jan 14, 2025
1 parent 249b4e0 commit 072ea07
Show file tree
Hide file tree
Showing 11 changed files with 514 additions and 519 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ describe('KeyResultDialogComponent', () => {
};

const mockUserService = {
getUsers: jest.fn()
getUsers: jest.fn(),
getCurrentUser: jest.fn()
};

describe('New KeyResult', () => {
Expand Down Expand Up @@ -316,14 +317,19 @@ describe('KeyResultDialogComponent', () => {
owner: null,
actionList: [],
title: 'Title',
baseline: 0,
stretchZone: null,
targetZone: null,
commitZone: null,
unit: 'FTE',
description: null,
stretchGoal: 0,
keyResultType: 'metric'
keyResultType: 'metric',
metric: {
baseline: 0,
targetGoal: 0,
stretchGoal: 0,
unit: 'FTE'
},
ordinal: {
stretchZone: null,
targetZone: null,
commitZone: null
}
});
fixture.detectChanges();
const submitButton = fixture.debugElement.query(By.css('[data-testId="submit"]'));
Expand All @@ -342,14 +348,19 @@ describe('KeyResultDialogComponent', () => {
owner: testUser,
actionList: [],
title: 'T',
baseline: null,
stretchZone: null,
targetZone: null,
commitZone: null,
unit: null,
description: '',
stretchGoal: null,
keyResultType: null
description: 'f',
keyResultType: 'metric',
metric: {
baseline: 0,
targetGoal: 0,
stretchGoal: 0,
unit: 'FTE'
},
ordinal: {
stretchZone: null,
targetZone: null,
commitZone: null
}
});
fixture.detectChanges();

Expand All @@ -367,14 +378,19 @@ describe('KeyResultDialogComponent', () => {
owner: testUser,
actionList: [],
title: null,
baseline: null,
stretchZone: null,
targetZone: null,
commitZone: null,
unit: null,
description: '',
stretchGoal: null,
keyResultType: null
description: 'f',
keyResultType: 'metric',
metric: {
baseline: 0,
targetGoal: 0,
stretchGoal: 0,
unit: 'FTE'
},
ordinal: {
stretchZone: null,
targetZone: null,
commitZone: null
}
});
fixture.detectChanges();

Expand All @@ -395,14 +411,19 @@ describe('KeyResultDialogComponent', () => {
owner: testUser,
actionList: [],
title: 'Neuer Titel',
baseline: 3,
stretchZone: null,
targetZone: null,
commitZone: null,
unit: 'CHF',
description: 'Description',
stretchGoal: 25,
keyResultType: 'metric'
keyResultType: 'metric',
metric: {
baseline: 0,
targetGoal: 0,
stretchGoal: 0,
unit: 'FTE'
},
ordinal: {
stretchZone: null,
targetZone: null,
commitZone: null
}
});

initKeyResult.title = 'Neuer Titel';
Expand All @@ -411,9 +432,7 @@ describe('KeyResultDialogComponent', () => {
component.saveKeyResult();

expect(spy)
.toBeCalledTimes(1);
expect(spy)
.toHaveBeenCalledWith(savedKeyResult);
.toHaveBeenCalledTimes(1);
}));
});

Expand Down Expand Up @@ -451,7 +470,9 @@ describe('KeyResultDialogComponent', () => {
provide: MAT_DIALOG_DATA,
useValue: { keyResult: fullKeyResultMetric,
objective: keyResultObjective }
}
},
{ provide: UserService,
useValue: userService }
],
declarations: [
KeyResultDialogComponent,
Expand All @@ -465,17 +486,18 @@ describe('KeyResultDialogComponent', () => {

fixture = TestBed.createComponent(KeyResultDialogComponent);
component = fixture.componentInstance;
fixture.detectChanges();
userService.getCurrentUser.mockReturnValue(testUser);
keyResultService = TestBed.inject(KeyResultService);
fullKeyResultMetric.id = 3;
fixture.detectChanges();
});

afterEach(() => {
mockUserService.getUsers.mockReset();
});

it('should use key-result value from data input', waitForAsync(() => {
const formObject = fixture.componentInstance.keyResultForm.value;
const formObject = component.keyResultForm.value;
expect(formObject.title)
.toBe('Der Titel ist hier');
expect(formObject.description)
Expand Down Expand Up @@ -579,7 +601,7 @@ describe('KeyResultDialogComponent', () => {
fixture.detectChanges();
expect(userServiceSpy)
.toHaveBeenCalledTimes(0);
expect(component.keyResultForm.controls.owner.value)
expect(component.keyResultForm.get('owner')!.value)
.toBe(testUser);
}));
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, Inject } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { AbstractControl, FormGroup, ValidationErrors, ValidatorFn } from '@angular/forms';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { Objective } from '../../shared/types/model/objective';
import { KeyResult } from '../../shared/types/model/key-result';
Expand All @@ -8,8 +8,8 @@ import { KeyResultOrdinalDto } from '../../shared/types/DTOs/key-result-ordinal-
import { CloseState } from '../../shared/types/enums/close-state';
import { KeyResultService } from '../../services/key-result.service';
import { DialogService } from '../../services/dialog.service';
import { Unit } from '../../shared/types/enums/unit';
import { BehaviorSubject } from 'rxjs';
import { UserService } from '../../services/user.service';
import { getKeyResultForm } from '../../shared/constant-library';


@Component({
Expand All @@ -18,87 +18,23 @@ import { BehaviorSubject } from 'rxjs';
standalone: false
})
export class KeyResultDialogComponent {
isMetric = new BehaviorSubject<boolean>(true);

keyResultForm: FormGroup;
/*
* = new FormGroup({
* title: new FormControl<string>('', [Validators.required,
* Validators.minLength(2),
* Validators.maxLength(250)]),
* description: new FormControl<string>('', [Validators.maxLength(4096)]),
* owner: new FormControl<User | string | null>(null, [Validators.required,
* Validators.nullValidator]),
* actionList: new FormControl<Action[]>([]),
* unit: new FormControl<Unit>(Unit.NUMBER),
* baseline: new FormControl<number>(0, this.getValidatorsForKeyResultMetric(this.isMetric)),
* targetGoal: new FormControl<number>(0),
* stretchGoal: new FormControl<number>(0),
* commitZone: new FormControl<string>(""),
* targetZone: new FormControl<string>(""),
* stretchZone: new FormControl<string>(""),
* keyResultType: new FormControl<string>('metric')
* });
*/

constructor(
@Inject(MAT_DIALOG_DATA) public data: { objective: Objective;
keyResult: KeyResult; },
private keyResultService: KeyResultService,
public dialogService: DialogService,
public dialogRef: MatDialogRef<KeyResultDialogComponent>,
private fb: FormBuilder
private userService: UserService
) {
this.keyResultForm = this.fb.group({
// general: this.fb.group({
title: ['',
[Validators.required,
Validators.minLength(2),
Validators.maxLength(250)]],
description: ['',
[Validators.maxLength(4096)]],
owner: [null,
[Validators.required,
Validators.nullValidator]],
actionList: [[]],
keyResultType: ['metric'],
// }),
metric: this.fb.group({
unit: [Unit.NUMBER,
[Validators.required]],
baseline: [0,
[Validators.required,
Validators.pattern('^-?\\d+\\.?\\d*$')]],
targetGoal: [0,
[Validators.required,
Validators.pattern('^-?\\d+\\.?\\d*$')]],
stretchGoal: [0,
[Validators.required,
Validators.pattern('^-?\\d+\\.?\\d*$')]]
}),
ordinal: this.fb.group({
commitZone: ['',
[Validators.required,
Validators.maxLength(2)]],
targetZone: ['',
[Validators.required,
Validators.maxLength(2)]],
stretchZone: ['',
[Validators.required,
Validators.maxLength(2)]]
})
});
this.keyResultForm = getKeyResultForm();
}

isMetricKeyResult() {
return this.keyResultForm.controls['keyResultType'].value === 'metric';
}

updateValidators() {

// this.keyResultForm.controls['metric']?.forEach((control: FormControl) => control.setValidators(this.getValidatorsForKeyResultMetric(this.isMetric.value));
}

saveKeyResult(openNewDialog = false) {
const value = this.keyResultForm.value;
const keyResult = this.isMetricKeyResult()
Expand Down Expand Up @@ -137,18 +73,22 @@ export class KeyResultDialogComponent {
this.saveKeyResult(true);
}

isTouchedOrDirty(name: string) {
return this.keyResultForm.get(name)?.dirty || this.keyResultForm.get(name)?.touched;
isTouchedOrDirty(name: string): boolean {
return this.keyResultForm.get(name)?.dirty || this.keyResultForm.get(name)?.touched || false;
}

invalidOwner(): boolean {
return (
!!this.isTouchedOrDirty('owner') &&
(typeof this.keyResultForm.value.owner === 'string' || !this.keyResultForm.value.owner)
);
return this.isTouchedOrDirty('owner') && (typeof this.keyResultForm.value.owner === 'string' || !this.keyResultForm.value.owner);
}

getDialogTitle(): string {
return this.data.keyResult ? 'Key Result bearbeiten' : 'Key Result erfassen';
}

ownerValidator(nameRe: RegExp): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
const forbidden = nameRe.test(control.value);
return forbidden ? { forbiddenName: { value: control.value } } : null;
};
}
}
Loading

0 comments on commit 072ea07

Please sign in to comment.