Skip to content
Snippets Groups Projects
Commit 520dfa15 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-6301 OZG-6525 Replace subscription with host listener

parent e51c5b98
No related branches found
No related tags found
No related merge requests found
...@@ -8,7 +8,14 @@ import { ...@@ -8,7 +8,14 @@ import {
ToEmbeddedResourcesPipe, ToEmbeddedResourcesPipe,
createStateResource, createStateResource,
} from '@alfa-client/tech-shared'; } from '@alfa-client/tech-shared';
import { EventData, Mock, getMockComponent, mock, triggerEvent } from '@alfa-client/test-utils'; import {
EventData,
Mock,
dialogRefMock,
getMockComponent,
mock,
triggerEvent,
} from '@alfa-client/test-utils';
import { DialogRef } from '@angular/cdk/dialog'; import { DialogRef } from '@angular/cdk/dialog';
import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing';
import faker from '@faker-js/faker'; import faker from '@faker-js/faker';
...@@ -20,7 +27,7 @@ import { ...@@ -20,7 +27,7 @@ import {
} from 'libs/collaboration-shared/test/organisations-einheit'; } from 'libs/collaboration-shared/test/organisations-einheit';
import { getDataTestIdOf } from 'libs/tech-shared/test/data-test'; import { getDataTestIdOf } from 'libs/tech-shared/test/data-test';
import { MockComponent } from 'ng-mocks'; import { MockComponent } from 'ng-mocks';
import { Subscription, of } from 'rxjs'; import { of } from 'rxjs';
import { SearchOrganisationsEinheitContainerComponent } from './search-organisations-einheit-container.component'; import { SearchOrganisationsEinheitContainerComponent } from './search-organisations-einheit-container.component';
import { SearchOrganisationsEinheitFormComponent } from './search-organisations-einheit-form/search-organisations-einheit-form.component'; import { SearchOrganisationsEinheitFormComponent } from './search-organisations-einheit-form/search-organisations-einheit-form.component';
...@@ -57,7 +64,7 @@ describe('SearchOrganisationsEinheitContainerComponent', () => { ...@@ -57,7 +64,7 @@ describe('SearchOrganisationsEinheitContainerComponent', () => {
}, },
{ {
provide: DialogRef, provide: DialogRef,
useValue: { keydownEvents: of(KeyboardEvent), close: jest.fn() }, useValue: dialogRefMock,
}, },
], ],
}).compileComponents(); }).compileComponents();
...@@ -78,22 +85,19 @@ describe('SearchOrganisationsEinheitContainerComponent', () => { ...@@ -78,22 +85,19 @@ describe('SearchOrganisationsEinheitContainerComponent', () => {
expect(service.getSearchResultList).toHaveBeenCalled(); expect(service.getSearchResultList).toHaveBeenCalled();
}); });
it('should subscribe to dialogRef.keydownEvents', () => {
component.ngOnInit();
expect(component.keydownEventsSubscription).toBeInstanceOf(Subscription);
});
}); });
describe('ngOnDestroy', () => { describe('onKeyDownHandler', () => {
it('should unsubscribe keydownEventsSubscription', () => { it('should prevent default behavior for enter key', () => {
component.keydownEventsSubscription = new Subscription(); const keyboardEvent: KeyboardEvent = {
jest.spyOn(component.keydownEventsSubscription, 'unsubscribe'); ...new KeyboardEvent('enter'),
key: 'Enter',
preventDefault: jest.fn(),
};
component.ngOnDestroy(); component.onKeyDownHandler(keyboardEvent);
expect(component.keydownEventsSubscription.unsubscribe).toHaveBeenCalled(); expect(keyboardEvent.preventDefault).toHaveBeenCalled();
}); });
}); });
...@@ -170,12 +174,6 @@ describe('SearchOrganisationsEinheitContainerComponent', () => { ...@@ -170,12 +174,6 @@ describe('SearchOrganisationsEinheitContainerComponent', () => {
}); });
describe('select search result', () => { describe('select search result', () => {
it('should set select result', () => {
component.selectSearchResult(organisationsEinheitResource);
expect(service.selectSearchResult).toHaveBeenCalledWith(organisationsEinheitResource);
});
it('should call service', () => { it('should call service', () => {
component.selectSearchResult(organisationsEinheitResource); component.selectSearchResult(organisationsEinheitResource);
...@@ -185,7 +183,7 @@ describe('SearchOrganisationsEinheitContainerComponent', () => { ...@@ -185,7 +183,7 @@ describe('SearchOrganisationsEinheitContainerComponent', () => {
it('should close dialog', () => { it('should close dialog', () => {
component.selectSearchResult(organisationsEinheitResource); component.selectSearchResult(organisationsEinheitResource);
expect(component.dialogRef.close).toHaveBeenCalled(); expect(dialogRefMock.close).toHaveBeenCalled();
}); });
}); });
...@@ -209,7 +207,7 @@ describe('SearchOrganisationsEinheitContainerComponent', () => { ...@@ -209,7 +207,7 @@ describe('SearchOrganisationsEinheitContainerComponent', () => {
it('should close dialog', () => { it('should close dialog', () => {
component.closeDialog(); component.closeDialog();
expect(component.dialogRef.close).toHaveBeenCalled(); expect(dialogRefMock.close).toHaveBeenCalled();
}); });
}); });
}); });
...@@ -4,18 +4,18 @@ import { ...@@ -4,18 +4,18 @@ import {
} from '@alfa-client/collaboration-shared'; } from '@alfa-client/collaboration-shared';
import { StateResource } from '@alfa-client/tech-shared'; import { StateResource } from '@alfa-client/tech-shared';
import { DialogRef } from '@angular/cdk/dialog'; import { DialogRef } from '@angular/cdk/dialog';
import { Component, OnDestroy, OnInit } from '@angular/core'; import { Component, HostListener, OnInit } from '@angular/core';
import { import {
OrganisationsEinheitListResource, OrganisationsEinheitListResource,
OrganisationsEinheitResource, OrganisationsEinheitResource,
} from 'libs/collaboration-shared/src/lib/organisations-einheit.model'; } from 'libs/collaboration-shared/src/lib/organisations-einheit.model';
import { Observable, Subscription, filter } from 'rxjs'; import { Observable, Subscription } from 'rxjs';
@Component({ @Component({
selector: 'alfa-search-organisations-einheit-container', selector: 'alfa-search-organisations-einheit-container',
templateUrl: './search-organisations-einheit-container.component.html', templateUrl: './search-organisations-einheit-container.component.html',
}) })
export class SearchOrganisationsEinheitContainerComponent implements OnInit, OnDestroy { export class SearchOrganisationsEinheitContainerComponent implements OnInit {
public organisationsEinheitStateListResource$: Observable< public organisationsEinheitStateListResource$: Observable<
StateResource<OrganisationsEinheitListResource> StateResource<OrganisationsEinheitListResource>
>; >;
...@@ -25,20 +25,15 @@ export class SearchOrganisationsEinheitContainerComponent implements OnInit, OnD ...@@ -25,20 +25,15 @@ export class SearchOrganisationsEinheitContainerComponent implements OnInit, OnD
constructor( constructor(
private readonly service: OrganisationsEinheitService, private readonly service: OrganisationsEinheitService,
readonly dialogRef: DialogRef, private readonly dialogRef: DialogRef,
) {} ) {}
ngOnInit(): void { ngOnInit(): void {
this.organisationsEinheitStateListResource$ = this.service.getSearchResultList(); this.organisationsEinheitStateListResource$ = this.service.getSearchResultList();
this.keydownEventsSubscription = this.dialogRef.keydownEvents
.pipe(filter((e: KeyboardEvent) => e.key === 'Enter'))
.subscribe((e: KeyboardEvent) => {
e.preventDefault();
});
} }
ngOnDestroy(): void { @HostListener('document:keydown', ['$event']) onKeyDownHandler(e: KeyboardEvent) {
this.keydownEventsSubscription.unsubscribe(); if (e.key === 'Enter') e.preventDefault();
} }
public search(searchBy: string): void { public search(searchBy: string): void {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment