diff --git a/alfa-client/libs/collaboration/src/lib/search-organisations-einheit-container/search-organisations-einheit-container.component.spec.ts b/alfa-client/libs/collaboration/src/lib/search-organisations-einheit-container/search-organisations-einheit-container.component.spec.ts index 21d770d9c3bd231de7d5d039c5a29f079c04476b..cff95cc3e9a917cd6c38f19c706624f3c9f98dd0 100644 --- a/alfa-client/libs/collaboration/src/lib/search-organisations-einheit-container/search-organisations-einheit-container.component.spec.ts +++ b/alfa-client/libs/collaboration/src/lib/search-organisations-einheit-container/search-organisations-einheit-container.component.spec.ts @@ -8,14 +8,7 @@ import { ToEmbeddedResourcesPipe, createStateResource, } from '@alfa-client/tech-shared'; -import { - EventData, - Mock, - dialogRefMock, - getMockComponent, - mock, - triggerEvent, -} from '@alfa-client/test-utils'; +import { EventData, Mock, getMockComponent, mock, triggerEvent } from '@alfa-client/test-utils'; import { DialogRef } from '@angular/cdk/dialog'; import { ComponentFixture, TestBed } from '@angular/core/testing'; import faker from '@faker-js/faker'; @@ -27,7 +20,7 @@ import { } from 'libs/collaboration-shared/test/organisations-einheit'; import { getDataTestIdOf } from 'libs/tech-shared/test/data-test'; import { MockComponent } from 'ng-mocks'; -import { of } from 'rxjs'; +import { Subscription, of } from 'rxjs'; import { SearchOrganisationsEinheitContainerComponent } from './search-organisations-einheit-container.component'; import { SearchOrganisationsEinheitFormComponent } from './search-organisations-einheit-form/search-organisations-einheit-form.component'; @@ -64,7 +57,7 @@ describe('SearchOrganisationsEinheitContainerComponent', () => { }, { provide: DialogRef, - useValue: dialogRefMock, + useValue: { keydownEvents: of(KeyboardEvent), close: jest.fn() }, }, ], }).compileComponents(); @@ -85,6 +78,23 @@ describe('SearchOrganisationsEinheitContainerComponent', () => { expect(service.getSearchResultList).toHaveBeenCalled(); }); + + it('should subscribe to dialogRef.keydownEvents', () => { + component.ngOnInit(); + + expect(component.keydownEventsSubscription).toBeInstanceOf(Subscription); + }); + }); + + describe('ngOnDestroy', () => { + it('should unsubscribe keydownEventsSubscription', () => { + component.keydownEventsSubscription = new Subscription(); + jest.spyOn(component.keydownEventsSubscription, 'unsubscribe'); + + component.ngOnDestroy(); + + expect(component.keydownEventsSubscription.unsubscribe).toHaveBeenCalled(); + }); }); describe('search organisationsEinheit component', () => { @@ -169,7 +179,7 @@ describe('SearchOrganisationsEinheitContainerComponent', () => { it('should close dialog', () => { component.selectSearchResult(organisationsEinheitResource); - expect(dialogRefMock.close).toHaveBeenCalledWith(organisationsEinheitResource); + expect(component.dialogRef.close).toHaveBeenCalled(); }); }); @@ -193,7 +203,7 @@ describe('SearchOrganisationsEinheitContainerComponent', () => { it('should close dialog', () => { component.closeDialog(); - expect(dialogRefMock.close).toHaveBeenCalled(); + expect(component.dialogRef.close).toHaveBeenCalled(); }); }); }); diff --git a/alfa-client/libs/collaboration/src/lib/search-organisations-einheit-container/search-organisations-einheit-container.component.ts b/alfa-client/libs/collaboration/src/lib/search-organisations-einheit-container/search-organisations-einheit-container.component.ts index b27e9b6358068fe7e7871cdc27dc8d434abd8ad3..7d75af37e9b24c81a142a81e2356a78fee2fab43 100644 --- a/alfa-client/libs/collaboration/src/lib/search-organisations-einheit-container/search-organisations-einheit-container.component.ts +++ b/alfa-client/libs/collaboration/src/lib/search-organisations-einheit-container/search-organisations-einheit-container.component.ts @@ -4,31 +4,41 @@ import { } from '@alfa-client/collaboration-shared'; import { StateResource } from '@alfa-client/tech-shared'; import { DialogRef } from '@angular/cdk/dialog'; -import { Component, OnInit } from '@angular/core'; +import { Component, OnDestroy, OnInit } from '@angular/core'; import { OrganisationsEinheitListResource, OrganisationsEinheitResource, } from 'libs/collaboration-shared/src/lib/organisations-einheit.model'; -import { Observable } from 'rxjs'; +import { Observable, Subscription, filter } from 'rxjs'; @Component({ selector: 'alfa-search-organisations-einheit-container', templateUrl: './search-organisations-einheit-container.component.html', }) -export class SearchOrganisationsEinheitContainerComponent implements OnInit { +export class SearchOrganisationsEinheitContainerComponent implements OnInit, OnDestroy { public organisationsEinheitStateListResource$: Observable< StateResource<OrganisationsEinheitListResource> >; public readonly organisationsEinheitListLinkRel = OrganisationsEinheitListLinkRel; + keydownEventsSubscription: Subscription; constructor( private readonly service: OrganisationsEinheitService, - private readonly dialogRef: DialogRef, + readonly dialogRef: DialogRef, ) {} ngOnInit(): void { this.organisationsEinheitStateListResource$ = this.service.getSearchResultList(); + this.keydownEventsSubscription = this.dialogRef.keydownEvents + .pipe(filter((e: KeyboardEvent) => e.key === 'Enter')) + .subscribe((e: KeyboardEvent) => { + e.preventDefault(); + }); + } + + ngOnDestroy(): void { + this.keydownEventsSubscription.unsubscribe(); } public search(searchBy: string): void {