diff --git a/alfa-client/libs/admin/keycloak-shared/src/lib/keycloak-formservice.spec.ts b/alfa-client/libs/admin/keycloak-shared/src/lib/keycloak-formservice.spec.ts index c2afe9895a5617abd0414a25d2b6b21d00a6cfdb..6e71337e1f2789dd36b80944ca9795d595729966 100644 --- a/alfa-client/libs/admin/keycloak-shared/src/lib/keycloak-formservice.spec.ts +++ b/alfa-client/libs/admin/keycloak-shared/src/lib/keycloak-formservice.spec.ts @@ -284,14 +284,14 @@ describe('KeycloakFormService', () => { }); it('should return delayed empty state resource', fakeAsync(() => { - const results: StateResource<unknown>[] = []; + const expectedEmits: StateResource<unknown>[] = []; service._processInvalidForm().subscribe((value: StateResource<unknown>) => { - results.push(value); + expectedEmits.push(value); }); tick(200); - expect(results).toEqual([createEmptyStateResource(true), createEmptyStateResource()]); + expect(expectedEmits).toEqual([createEmptyStateResource(true), createEmptyStateResource()]); })); }); diff --git a/alfa-client/libs/admin/user/src/lib/user-form/user-form-roles/user-form-roles.component.html b/alfa-client/libs/admin/user/src/lib/user-form/user-form-roles/user-form-roles.component.html index f71da37b73284f6af6c4db060b6fa51ba2cce6f8..29de0bc1bced5b0b4cfbf4922f3594240ffcf37a 100644 --- a/alfa-client/libs/admin/user/src/lib/user-form/user-form-roles/user-form-roles.component.html +++ b/alfa-client/libs/admin/user/src/lib/user-form/user-form-roles/user-form-roles.component.html @@ -12,7 +12,7 @@ <div class="flex items-center gap-2"> <ods-checkbox-editor [formControlName]="UserFormService.ADMIN" - (inputChange)="removeCheckboxError()" + (inputChange)="handleAdminRoleChange()" data-test-id="checkbox-admin" label="Admin" inputId="admin" @@ -27,7 +27,7 @@ <div class="flex items-center gap-2"> <ods-checkbox-editor [formControlName]="UserFormService.DATENBEAUFTRAGUNG" - (inputChange)="removeCheckboxError()" + (inputChange)="handleAdminRoleChange()" data-test-id="checkbox-datenbeauftragung" label="Datenbeauftragung" inputId="datenbeauftragung" @@ -45,7 +45,7 @@ <div class="flex items-center gap-2"> <ods-checkbox-editor [formControlName]="UserFormService.LOESCHEN" - (inputChange)="updateAlfaCheckboxes(UserFormService.LOESCHEN, $event)" + (inputChange)="handleAlfaRoleChange(UserFormService.LOESCHEN, $event)" label="Löschen" inputId="delete" data-test-id="checkbox-loeschen" @@ -60,7 +60,7 @@ <div class="flex items-center gap-2"> <ods-checkbox-editor [formControlName]="UserFormService.USER" - (inputChange)="updateAlfaCheckboxes(UserFormService.USER, $event)" + (inputChange)="handleAlfaRoleChange(UserFormService.USER, $event)" label="User" inputId="user" data-test-id="checkbox-user" @@ -75,7 +75,7 @@ <div class="flex items-center gap-2"> <ods-checkbox-editor [formControlName]="UserFormService.POSTSTELLE" - (inputChange)="updateAlfaCheckboxes(UserFormService.POSTSTELLE, $event)" + (inputChange)="handleAlfaRoleChange(UserFormService.POSTSTELLE, $event)" label="Poststelle" inputId="post_office" data-test-id="checkbox-poststelle" diff --git a/alfa-client/libs/admin/user/src/lib/user-form/user-form-roles/user-form-roles.component.spec.ts b/alfa-client/libs/admin/user/src/lib/user-form/user-form-roles/user-form-roles.component.spec.ts index 8bd40289a895f00859b5e8e09dacfefa60f08a86..d657690baab3ba8e17c0e36085a698633ae61674 100644 --- a/alfa-client/libs/admin/user/src/lib/user-form/user-form-roles/user-form-roles.component.spec.ts +++ b/alfa-client/libs/admin/user/src/lib/user-form/user-form-roles/user-form-roles.component.spec.ts @@ -97,20 +97,20 @@ describe('UserFormRolesComponent', () => { }); }); - describe('update alfa checkboxes', () => { + describe('handle alfa role change', () => { it('should call form service updateAlfaCheckboxes', () => { const formControlName: string = 'dummy'; const value: boolean = true; - component.updateAlfaCheckboxes(formControlName, value); + component.handleAlfaRoleChange(formControlName, value); expect(formService.updateAlfaCheckboxes).toHaveBeenCalledWith(formControlName, value); }); }); - describe('remove checkbox error', () => { + describe('handle admin role change', () => { it('should call form service removeCheckboxError', () => { - component.removeCheckboxError(); + component.handleAdminRoleChange(); expect(formService.removeCheckboxError).toHaveBeenCalled(); }); @@ -138,8 +138,8 @@ describe('UserFormRolesComponent', () => { }); describe('checkbox admin', () => { - it('should call removeCheckboxError on inputChange emit', () => { - component.removeCheckboxError = jest.fn(); + it('should call handleAdminRoleChange on inputChange emit', () => { + component.handleAdminRoleChange = jest.fn(); triggerEvent({ fixture, @@ -148,13 +148,13 @@ describe('UserFormRolesComponent', () => { data: true, }); - expect(component.removeCheckboxError).toHaveBeenCalled(); + expect(component.handleAdminRoleChange).toHaveBeenCalled(); }); }); describe('checkbox datenbeauftragung', () => { - it('should call removeCheckboxError on inputChange emit', () => { - component.removeCheckboxError = jest.fn(); + it('should call handleAdminRoleChange on inputChange emit', () => { + component.handleAdminRoleChange = jest.fn(); triggerEvent({ fixture, @@ -163,13 +163,13 @@ describe('UserFormRolesComponent', () => { data: true, }); - expect(component.removeCheckboxError).toHaveBeenCalled(); + expect(component.handleAdminRoleChange).toHaveBeenCalled(); }); }); describe('checkbox loeschen', () => { - it('should call updateOtherAlfaCheckboxes on inputChange emit', () => { - component.updateAlfaCheckboxes = jest.fn(); + it('should call handleAlfaRoleChange on inputChange emit', () => { + component.handleAlfaRoleChange = jest.fn(); triggerEvent({ fixture, @@ -178,13 +178,13 @@ describe('UserFormRolesComponent', () => { data: true, }); - expect(component.updateAlfaCheckboxes).toHaveBeenCalledWith(UserFormService.LOESCHEN, true); + expect(component.handleAlfaRoleChange).toHaveBeenCalledWith(UserFormService.LOESCHEN, true); }); }); describe('checkbox user', () => { - it('should call updateOtherAlfaCheckboxes on inputChange emit', () => { - component.updateAlfaCheckboxes = jest.fn(); + it('should call handleAlfaRoleChange on inputChange emit', () => { + component.handleAlfaRoleChange = jest.fn(); triggerEvent({ fixture, @@ -193,13 +193,13 @@ describe('UserFormRolesComponent', () => { data: true, }); - expect(component.updateAlfaCheckboxes).toHaveBeenCalledWith(UserFormService.USER, true); + expect(component.handleAlfaRoleChange).toHaveBeenCalledWith(UserFormService.USER, true); }); }); describe('checkbox poststelle', () => { - it('should call updateOtherAlfaCheckboxes on inputChange emit', () => { - component.updateAlfaCheckboxes = jest.fn(); + it('should call handleAlfaRoleChange on inputChange emit', () => { + component.handleAlfaRoleChange = jest.fn(); triggerEvent({ fixture, @@ -208,7 +208,7 @@ describe('UserFormRolesComponent', () => { data: true, }); - expect(component.updateAlfaCheckboxes).toHaveBeenCalledWith(UserFormService.POSTSTELLE, true); + expect(component.handleAlfaRoleChange).toHaveBeenCalledWith(UserFormService.POSTSTELLE, true); }); }); }); diff --git a/alfa-client/libs/admin/user/src/lib/user-form/user-form-roles/user-form-roles.component.ts b/alfa-client/libs/admin/user/src/lib/user-form/user-form-roles/user-form-roles.component.ts index 79e41b9b0e57aefae57261971f34f36f9a74a928..9d3288e4c0225f22f78a72c948e32ec61c744551 100644 --- a/alfa-client/libs/admin/user/src/lib/user-form/user-form-roles/user-form-roles.component.ts +++ b/alfa-client/libs/admin/user/src/lib/user-form/user-form-roles/user-form-roles.component.ts @@ -40,11 +40,11 @@ export class UserFormRolesComponent implements OnInit { ); } - updateAlfaCheckboxes(formControlName: string, value: boolean) { + public handleAlfaRoleChange(formControlName: string, value: boolean) { this.formService.updateAlfaCheckboxes(formControlName, value); } - removeCheckboxError() { + public handleAdminRoleChange() { this.formService.removeCheckboxError(); } } 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 92c01765582e86f26850302c08102b61ed2f485c..f4eece3f17ff3899627903e43df570d9dd6cab57 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,10 +40,9 @@ 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, Subscription } from 'rxjs'; +import { Observable, of } 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 { singleCold, singleColdCompleted } from '../../../../../tech-shared/test/marbles'; import { createKeycloakHttpErrorResponse } from '../../../../keycloak-shared/src/test/keycloak'; import { createAdminOrganisationsEinheit } from '../../../../organisations-einheit-shared/src/test/organisations-einheit'; import { UserFormService } from './user.formservice'; @@ -108,64 +107,6 @@ describe('UserFormService', () => { expect(service).toBeTruthy(); }); - describe('init', () => { - beforeEach(() => { - service._initOrganisationsEinheiten = jest.fn().mockReturnValue(of()); - service._updateAlfaCheckboxStatesOnPatch = jest.fn().mockReturnValue(of()); - }); - - it('should call initOrganisationsEinheiten', () => { - service.init(); - - expect(service._initOrganisationsEinheiten).toHaveBeenCalled(); - }); - - it('should set initOrganisationsEinheiten$', () => { - service.init(); - - expect(service._initOrganisationsEinheiten$).toBeDefined(); - }); - - it('should call updateAlfaCheckboxStatesOnPatch', () => { - service.init(); - - 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(); - }); - }); - describe('build patch config', () => { describe('on matching route', () => { it('should contains id and value for patch indication', () => { @@ -194,7 +135,8 @@ describe('UserFormService', () => { const loadedUser: StateResource<User> = createStateResource(createUser()); beforeEach(() => { - userService.getUserById.mockReturnValue(singleHot(loadedUser)); + userService.getUserById.mockReturnValue(singleCold(loadedUser)); + service._updateAlfaCheckboxStates = jest.fn(); }); it('should call service to get user by id', () => { @@ -208,6 +150,22 @@ describe('UserFormService', () => { expect(response).toBeObservable(singleCold(loadedUser)); }); + + it('should update alfa roles after user is loaded', () => { + userService.getUserById.mockReturnValue(of(loadedUser)); + + service._load(id).subscribe(); + + expect(service._updateAlfaCheckboxStates).toHaveBeenCalled(); + }); + + it('should not update alfa roles if user is not loaded', () => { + userService.getUserById.mockReturnValue(of(createEmptyStateResource())); + + service._load(id).subscribe(); + + expect(service._updateAlfaCheckboxStates).not.toHaveBeenCalled(); + }); }); describe('initOrganisationsEinheiten', () => { @@ -508,28 +466,6 @@ describe('UserFormService', () => { }); }); - describe('ngOnDestroy', () => { - beforeEach(() => { - service._initOrganisationsEinheiten$ = new Subscription(); - service._initOrganisationsEinheiten$.unsubscribe = jest.fn(); - - service._updateAlfaCheckboxesOnPatch$ = new Subscription(); - service._updateAlfaCheckboxesOnPatch$.unsubscribe = jest.fn(); - }); - - it('should unsubscribe from initOrganisationsEinheiten$', () => { - service.ngOnDestroy(); - - expect(service._initOrganisationsEinheiten$.unsubscribe).toHaveBeenCalled(); - }); - - it('should unsubscribe from updateAlfaCheckboxesOnPatch$', () => { - service.ngOnDestroy(); - - expect(service._updateAlfaCheckboxesOnPatch$.unsubscribe).toHaveBeenCalled(); - }); - }); - describe('get userName', () => { it('should return form control value of userName', () => { service.form = new FormGroup({ [UserFormService.USERNAME]: new FormControl('userNameDummy') }); 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 812d64c1f6c28b4d2a28ca877594f2e45da84af6..e083adaeac2e317d0f2ab37ee470ee0dcf3a4126 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 } from '@angular/core'; import { AbstractControl, FormControl, UntypedFormBuilder, UntypedFormGroup } from '@angular/forms'; import { UrlSegment } from '@angular/router'; -import { filter, Observable, Subscription, tap } from 'rxjs'; +import { filter, Observable, take, tap } from 'rxjs'; @Injectable() -export class UserFormService extends KeycloakFormService<User> implements OnDestroy { +export class UserFormService extends KeycloakFormService<User> { public static readonly FIRST_NAME: string = 'firstName'; public static readonly LAST_NAME: string = 'lastName'; public static readonly USERNAME: string = 'username'; @@ -65,9 +65,6 @@ export class UserFormService extends KeycloakFormService<User> implements OnDest public static readonly USER: string = 'VERWALTUNG_USER'; public static readonly POSTSTELLE: string = 'VERWALTUNG_POSTSTELLE'; - _initOrganisationsEinheiten$: Subscription; - _updateAlfaCheckboxesOnPatch$: Subscription; - _organisationsEinheitToGroupIdMap: Map<string, string> = new Map<string, string>(); constructor( @@ -82,15 +79,7 @@ export class UserFormService extends KeycloakFormService<User> implements OnDest } init() { - this._initOrganisationsEinheiten$ = this._initOrganisationsEinheiten().subscribe(); - this._updateAlfaCheckboxesOnPatch$ = this._updateAlfaCheckboxStatesOnPatch().subscribe(); - } - - _updateAlfaCheckboxStatesOnPatch() { - return this.get().pipe( - filter(isLoaded), - tap(() => this._updateAlfaCheckboxStates()), - ); + this._initOrganisationsEinheiten().pipe(take(1)).subscribe(); } _buildPatchConfig(url: UrlSegment[]): PatchConfig { @@ -106,7 +95,10 @@ export class UserFormService extends KeycloakFormService<User> implements OnDest } _load(id: string): Observable<StateResource<User>> { - return this.userService.getUserById(id); + return this.userService.getUserById(id).pipe( + filter(isLoaded), + tap(() => this._updateAlfaCheckboxStates()), + ); } _initForm(): UntypedFormGroup { @@ -276,11 +268,6 @@ export class UserFormService extends KeycloakFormService<User> implements OnDest return <UntypedFormGroup>this.form.get(UserFormService.CLIENT_ROLES).get(roleGroup); } - ngOnDestroy(): void { - this._initOrganisationsEinheiten$.unsubscribe(); - this._updateAlfaCheckboxesOnPatch$.unsubscribe(); - } - public getUserName(): string { return this.form.get(UserFormService.USERNAME).value; }