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 73e57e188c214681d27323a19d7d63f1ec8bddb1..8629008efe5f6eb66d46f444488661b212be02c9 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 @@ -43,6 +43,7 @@ import { createUser } from 'libs/admin/user-shared/test/user'; 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 { createDummyResource } from '../../../../../tech-shared/test/resource'; import { createKeycloakHttpErrorResponse } from '../../../../keycloak-shared/src/test/keycloak'; import { createAdminOrganisationsEinheit } from '../../../../organisations-einheit-shared/src/test/organisations-einheit'; import { UserFormService } from './user.formservice'; @@ -107,29 +108,62 @@ describe('UserFormService', () => { expect(service).toBeTruthy(); }); - describe('ngOnInit', () => { + describe('init', () => { beforeEach(() => { service._initOrganisationsEinheiten = jest.fn().mockReturnValue(of()); }); it('should call initOrganisationsEinheiten', () => { - service.ngOnInit(); + service.init(); expect(service._initOrganisationsEinheiten).toHaveBeenCalled(); }); it('should set initOrganisationsEinheiten$', () => { - service.ngOnInit(); + service.init(); expect(service._initOrganisationsEinheiten$).toBeDefined(); }); - it('should call _disableAlfaCheckboxes', () => { - service._disableAlfaCheckboxes = jest.fn(); + it('should call updateAlfaCheckboxStatesOnPatch', () => { + service._updateAlfaCheckboxStatesOnPatch = jest.fn(); - service.ngOnInit(); + service.init(); - expect(service._disableAlfaCheckboxes).toHaveBeenCalled(); + expect(service._updateAlfaCheckboxStatesOnPatch).toHaveBeenCalled(); + }); + + it('should set updateAlfaCheckboxesOnPatch$', () => { + service.init(); + + expect(service._updateAlfaCheckboxesOnPatch$).toBeDefined(); + }); + }); + + describe('updateAlfaCheckboxStatesOnPatch', () => { + it('should call get', () => { + service.get = jest.fn().mockReturnValue(of(createEmptyStateResource())); + service._updateAlfaCheckboxStatesOnPatch(); + + expect(service.get).toHaveBeenCalled(); + }); + + it('should not call updateAlfaCheckboxStates before patch', () => { + service.get = jest.fn().mockReturnValue(of(createEmptyStateResource())); + service._updateAlfaCheckboxStates = jest.fn(); + + service._updateAlfaCheckboxStatesOnPatch().subscribe(); + + expect(service._updateAlfaCheckboxStates).not.toHaveBeenCalled(); + }); + + it('should call updateAlfaCheckboxStates after patch', () => { + service.get = jest.fn().mockReturnValue(of(createStateResource(createDummyResource()))); + service._updateAlfaCheckboxStates = jest.fn(); + + service._updateAlfaCheckboxStatesOnPatch().subscribe(); + + expect(service._updateAlfaCheckboxStates).toHaveBeenCalled(); }); }); @@ -227,15 +261,24 @@ describe('UserFormService', () => { }); }); - describe('handleAlfaGroupChange', () => { + describe('updateAlfaCheckboxStates', () => { it('should call disableUncheckedCheckboxes if any checkbox is checked', () => { service._isAnyAlfaCheckboxChecked = jest.fn().mockReturnValue(true); service._disableUncheckedAlfaCheckboxes = jest.fn(); - service._disableAlfaCheckboxes(); + service._updateAlfaCheckboxStates(); expect(service._disableUncheckedAlfaCheckboxes).toHaveBeenCalled(); }); + + it('should call enableAllAlfaCheckboxes if any checkbox is checked', () => { + service._isAnyAlfaCheckboxChecked = jest.fn().mockReturnValue(false); + service._enableAllAlfaCheckboxes = jest.fn(); + + service._updateAlfaCheckboxStates(); + + expect(service._enableAllAlfaCheckboxes).toHaveBeenCalled(); + }); }); describe('isAnyAlfaCheckboxChecked', () => { @@ -303,11 +346,11 @@ describe('UserFormService', () => { }); it('should call disableAlfaCheckboxes', () => { - service._disableAlfaCheckboxes = jest.fn(); + service._updateAlfaCheckboxStates = jest.fn(); service.updateAlfaCheckboxes(UserFormService.LOESCHEN, true); - expect(service._disableAlfaCheckboxes).toHaveBeenCalled(); + expect(service._updateAlfaCheckboxStates).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 e7795f858b47a1b4d3b1d8fd4724c709e7030e2f..b3fddbc3372b90d3136e3b59e8b51599ddfc0840 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, OnInit } from '@angular/core'; +import { Injectable, OnDestroy } 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, OnInit { +export class UserFormService extends KeycloakFormService<User> implements OnDestroy { public static readonly FIRST_NAME: string = 'firstName'; public static readonly LAST_NAME: string = 'lastName'; public static readonly USERNAME: string = 'username'; @@ -66,6 +66,7 @@ export class UserFormService extends KeycloakFormService<User> implements OnDest public static readonly POSTSTELLE: string = 'VERWALTUNG_POSTSTELLE'; _initOrganisationsEinheiten$: Subscription; + _updateAlfaCheckboxesOnPatch$: Subscription; _alfaGroupChanges: Subscription; _organisationsEinheitToGroupIdMap: Map<string, string> = new Map<string, string>(); @@ -78,11 +79,19 @@ export class UserFormService extends KeycloakFormService<User> implements OnDest private snackBarService: SnackBarService, ) { super(); + this.init(); } - ngOnInit() { + init() { this._initOrganisationsEinheiten$ = this._initOrganisationsEinheiten().subscribe(); - this._disableAlfaCheckboxes(); + this._updateAlfaCheckboxesOnPatch$ = this._updateAlfaCheckboxStatesOnPatch().subscribe(); + } + + _updateAlfaCheckboxStatesOnPatch() { + return this.get().pipe( + filter(isLoaded), + tap(() => this._updateAlfaCheckboxStates()), + ); } _buildPatchConfig(url: UrlSegment[]): PatchConfig { @@ -165,7 +174,7 @@ export class UserFormService extends KeycloakFormService<User> implements OnDest updateAlfaCheckboxes(formControlName: string, value: boolean) { this.setControlValueInAlfa(formControlName, value); this.removeCheckboxError(); - this._disableAlfaCheckboxes(); + this._updateAlfaCheckboxStates(); } private setControlValueInAlfa(formControlName: string, value: boolean): void { @@ -176,7 +185,7 @@ export class UserFormService extends KeycloakFormService<User> implements OnDest this.form.get(UserFormService.CLIENT_ROLES).setErrors(null); } - _disableAlfaCheckboxes(): void { + _updateAlfaCheckboxStates(): void { if (this._isAnyAlfaCheckboxChecked()) { this._disableUncheckedAlfaCheckboxes(); } else { @@ -269,6 +278,7 @@ export class UserFormService extends KeycloakFormService<User> implements OnDest } ngOnDestroy(): void { + console.log('destroy'); this._initOrganisationsEinheiten$.unsubscribe(); this._alfaGroupChanges.unsubscribe(); }