diff --git a/alfa-client/libs/admin/user/src/lib/user-form/user.formservice.spec.ts b/alfa-client/libs/admin/user/src/lib/user-form/user.formservice.spec.ts index caabe30992d06bce0dffefa1c988f2e405a8793a..71f4fffde321b879a76111d565f31e603d623d5c 100644 --- a/alfa-client/libs/admin/user/src/lib/user-form/user.formservice.spec.ts +++ b/alfa-client/libs/admin/user/src/lib/user-form/user.formservice.spec.ts @@ -40,7 +40,7 @@ import { ActivatedRoute, UrlSegment } from '@angular/router'; import { faker } from '@faker-js/faker/locale/de'; import { expect } from '@jest/globals'; import { createUser } from 'libs/admin/user-shared/test/user'; -import { Observable, of } from 'rxjs'; +import { Observable, of, Subscription } from 'rxjs'; import { createUrlSegment } from '../../../../../navigation-shared/test/navigation-test-factory'; import { singleCold, singleColdCompleted, singleHot } from '../../../../../tech-shared/test/marbles'; import { createKeycloakHttpErrorResponse } from '../../../../keycloak-shared/src/test/keycloak'; @@ -107,6 +107,32 @@ describe('UserFormService', () => { expect(service).toBeTruthy(); }); + describe('ngOnInit', () => { + beforeEach(() => { + service._initOrganisationsEinheiten = jest.fn().mockReturnValue(of()); + }); + + it('should call initOrganisationsEinheiten', () => { + service.ngOnInit(); + + expect(service._initOrganisationsEinheiten).toHaveBeenCalled(); + }); + + it('should set initOrganisationsEinheiten$', () => { + service.ngOnInit(); + + expect(service._initOrganisationsEinheiten$).toBeDefined(); + }); + + it('should call _disableAlfaCheckboxes', () => { + service._disableAlfaCheckboxes = jest.fn(); + + service.ngOnInit(); + + expect(service._disableAlfaCheckboxes).toHaveBeenCalled(); + }); + }); + describe('build patch config', () => { describe('on matching route', () => { it('should contains id and value for patch indication', () => { @@ -151,26 +177,6 @@ describe('UserFormService', () => { }); }); - describe('listenToAlfaGroupChanges', () => { - it('should call handleAlfaGroupChange on initial change', () => { - service._handleAlfaGroupChange = jest.fn(); - - service.listenToAlfaGroupChanges(); - - expect(service._handleAlfaGroupChange).toHaveBeenCalled(); - }); - - it('should call handleAlfaGroupChange when value of form element changes', fakeAsync(() => { - service._handleAlfaGroupChange = jest.fn(); - - alfaGroup.get(UserFormService.LOESCHEN).setValue(true); - - tick(); - - expect(service._handleAlfaGroupChange).toHaveBeenCalled(); - })); - }); - describe('initOrganisationsEinheiten', () => { beforeEach(() => { service._addOrganisationsEinheitenToForm = jest.fn(); @@ -201,10 +207,6 @@ describe('UserFormService', () => { expect(service._organisationsEinheitToGroupIdMap.get(adminOrganisationsEinheit.name)).toEqual(adminOrganisationsEinheit.id); }); - it('should set initOrganisationsEinheiten$', () => { - expect(service['_initOrganisationsEinheiten$']).toBeDefined(); - }); - it('should not throw any exception on loading state resource', () => { adminOrganisationsEinheitService.getAll.mockReturnValue(of(createLoadingStateResource())); const errorMock: any = mockWindowError(); @@ -230,19 +232,10 @@ describe('UserFormService', () => { service._isAnyChecked = jest.fn().mockReturnValue(true); service._disableUncheckedCheckboxes = jest.fn(); - service._handleAlfaGroupChange(alfaGroup); + service._disableAlfaCheckboxes(); expect(service._disableUncheckedCheckboxes).toHaveBeenCalled(); }); - - it('should call enableAllCheckboxes if not any checkbox is checked', () => { - service._isAnyChecked = jest.fn().mockReturnValue(false); - service._enableAllCheckboxes = jest.fn(); - - service._handleAlfaGroupChange(alfaGroup); - - expect(service._enableAllCheckboxes).toHaveBeenCalled(); - }); }); describe('isAnyChecked', () => { @@ -292,12 +285,40 @@ describe('UserFormService', () => { }); }); - describe('enableAllCheckboxes', () => { + describe('updateAlfaCheckboxes', () => { + it('should set control value', () => { + const control: AbstractControl = alfaGroup.get(UserFormService.LOESCHEN); + + service.updateAlfaCheckboxes(UserFormService.LOESCHEN, true); + + expect(control.value).toBe(true); + }); + + it('should call disableUncheckedCheckboxes', () => { + service._disableUncheckedCheckboxes = jest.fn(); + + service.updateAlfaCheckboxes(UserFormService.LOESCHEN, true); + + expect(service._disableUncheckedCheckboxes).toHaveBeenCalledWith( + service.form.get(UserFormService.CLIENT_ROLES).get(UserFormService.ALFA_GROUP), + ); + }); + + it('should call enableAllAlfaCheckboxes if value is false', () => { + service._enableAllAlfaCheckboxes = jest.fn(); + + service.updateAlfaCheckboxes(UserFormService.LOESCHEN, false); + + expect(service._enableAllAlfaCheckboxes).toHaveBeenCalled(); + }); + }); + + describe('enableAllAlfaCheckboxes', () => { it('if control value is true then control should be enabled', () => { const control: AbstractControl = alfaGroup.get(UserFormService.LOESCHEN); - const enableSpy = jest.spyOn(control, 'enable'); + const enableSpy: jest.SpyInstance = jest.spyOn(control, 'enable'); - service._enableAllCheckboxes(alfaGroup); + service._enableAllAlfaCheckboxes(); expect(enableSpy).toHaveBeenCalled(); }); @@ -438,17 +459,21 @@ describe('UserFormService', () => { }); describe('ngOnDestroy', () => { - it('should unsubscribe from initOrganisationsEinheiten$', () => { + beforeEach(() => { + service._initOrganisationsEinheiten$ = new Subscription(); service._initOrganisationsEinheiten$.unsubscribe = jest.fn(); + service._alfaGroupChanges = new Subscription(); + service._alfaGroupChanges.unsubscribe = jest.fn(); + }); + + it('should unsubscribe from initOrganisationsEinheiten$', () => { service.ngOnDestroy(); expect(service._initOrganisationsEinheiten$.unsubscribe).toHaveBeenCalled(); }); it('should unsubscribe from initOrganisationsEinheiten$', () => { - service._alfaGroupChanges.unsubscribe = jest.fn(); - service.ngOnDestroy(); expect(service._alfaGroupChanges.unsubscribe).toHaveBeenCalled(); diff --git a/alfa-client/libs/admin/user/src/lib/user-form/user.formservice.ts b/alfa-client/libs/admin/user/src/lib/user-form/user.formservice.ts index d97dee1e2b586f2a1bf01ed02059bcc31f687afd..0e89e82cec35b44a352c32da15c82a23631ca0a0 100644 --- a/alfa-client/libs/admin/user/src/lib/user-form/user.formservice.ts +++ b/alfa-client/libs/admin/user/src/lib/user-form/user.formservice.ts @@ -43,13 +43,13 @@ import { StateResource, } from '@alfa-client/tech-shared'; import { SnackBarService } from '@alfa-client/ui'; -import { Injectable, OnDestroy } from '@angular/core'; +import { Injectable, OnDestroy, OnInit } from '@angular/core'; import { AbstractControl, FormControl, UntypedFormBuilder, UntypedFormGroup } from '@angular/forms'; import { UrlSegment } from '@angular/router'; import { filter, Observable, Subscription, tap } from 'rxjs'; @Injectable() -export class UserFormService extends KeycloakFormService<User> implements OnDestroy { +export class UserFormService extends KeycloakFormService<User> implements OnDestroy, OnInit { public static readonly FIRST_NAME: string = 'firstName'; public static readonly LAST_NAME: string = 'lastName'; public static readonly USERNAME: string = 'username'; @@ -78,9 +78,11 @@ export class UserFormService extends KeycloakFormService<User> implements OnDest private snackBarService: SnackBarService, ) { super(); + } + ngOnInit() { this._initOrganisationsEinheiten$ = this._initOrganisationsEinheiten().subscribe(); - this.listenToAlfaGroupChanges(); + this._disableAlfaCheckboxes(); } _buildPatchConfig(url: UrlSegment[]): PatchConfig { @@ -160,20 +162,11 @@ export class UserFormService extends KeycloakFormService<User> implements OnDest }); } - listenToAlfaGroupChanges(): void { + _disableAlfaCheckboxes(): void { const alfaGroup: UntypedFormGroup = this.getRoleGroup(UserFormService.ALFA_GROUP); - this._handleAlfaGroupChange(alfaGroup); - this._alfaGroupChanges = alfaGroup.valueChanges.subscribe(() => { - this._handleAlfaGroupChange(alfaGroup); - }); - } - - _handleAlfaGroupChange(group: UntypedFormGroup): void { - const anyChecked: boolean = this._isAnyChecked(group); + const anyChecked: boolean = this._isAnyChecked(alfaGroup); if (anyChecked) { - this._disableUncheckedCheckboxes(group); - } else { - this._enableAllCheckboxes(group); + this._disableUncheckedCheckboxes(alfaGroup); } } @@ -183,16 +176,36 @@ export class UserFormService extends KeycloakFormService<User> implements OnDest _disableUncheckedCheckboxes(alfaGroup: UntypedFormGroup): void { for (const control of Object.values<AbstractControl>(alfaGroup.controls)) { - if (!control.value) control.disable({ emitEvent: false }); + if (!control.value) this.disableCheckbox(control); } } - _enableAllCheckboxes(group: UntypedFormGroup): void { - for (const control of Object.values<AbstractControl>(group.controls)) { - control.enable({ emitEvent: false }); + updateAlfaCheckboxes(formControlName: string, value: boolean) { + const alfaGroup: UntypedFormGroup = this.getRoleGroup(UserFormService.ALFA_GROUP); + this.setControlValueInGroup(alfaGroup, formControlName, value); + this._disableUncheckedCheckboxes(alfaGroup); + if (!value) this._enableAllAlfaCheckboxes(); + } + + private setControlValueInGroup(group: UntypedFormGroup, formControlName: string, value: boolean): void { + group.get(formControlName).setValue(value, { emitEvent: false }); + } + + _enableAllAlfaCheckboxes() { + const alfaGroup: UntypedFormGroup = this.getRoleGroup(UserFormService.ALFA_GROUP); + for (const control of Object.values<AbstractControl>(alfaGroup.controls)) { + this.enableCheckbox(control); } } + private enableCheckbox(control: AbstractControl): void { + control.enable({ emitEvent: false }); + } + + private disableCheckbox(control: AbstractControl): void { + if (!control.value) control.disable({ emitEvent: false }); + } + _doSubmit(): Observable<StateResource<User>> { const user: User = this._createUser(); return this._createOrSave(user).pipe(