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

OZG-3114 refactor after code review

parent 82d7ba1a
No related branches found
No related tags found
No related merge requests found
...@@ -150,7 +150,7 @@ describe('VorgangSearchComponent', () => { ...@@ -150,7 +150,7 @@ describe('VorgangSearchComponent', () => {
component.submit(); component.submit();
expect(component.searchValueFocusIn).toEqual('Gewerbe'); expect(component.previouslyEnteredSearchValue).toEqual('Gewerbe');
}); });
it('should submit form', () => { it('should submit form', () => {
...@@ -169,25 +169,32 @@ describe('VorgangSearchComponent', () => { ...@@ -169,25 +169,32 @@ describe('VorgangSearchComponent', () => {
component.focusIn(); component.focusIn();
expect(component.searchValueFocusIn).toEqual('Gewerbe'); expect(component.previouslyEnteredSearchValue).toEqual('Gewerbe');
}); });
}); });
describe('focusOut', () => { describe('focusOut', () => {
it('should set new form value when focus out to search button (Lupe)', () => { function createSearchSubmitMockButton(nativeElement: any): MatButton {
const lupeElement = {}; return {
component.searchValueFocusIn = 'Gewerbe'; // previous value
component.searchSubmitButton = {
_elementRef: { _elementRef: {
nativeElement: lupeElement nativeElement: nativeElement
} }
} as MatButton; } as MatButton;
}
const searchFieldFormControl = new UntypedFormControl('Gewe'); // input form value function createMockFormGroup(searchFieldFormControl: UntypedFormControl): UntypedFormGroup {
component.formService.form = new UntypedFormGroup({ return new UntypedFormGroup({
search: searchFieldFormControl search: searchFieldFormControl
}) });
}
it('should set new form value when focus out to search button (Lupe)', () => {
const lupeElement = {};
component.previouslyEnteredSearchValue = 'Gewerbe';
component.searchSubmitButton = createSearchSubmitMockButton(lupeElement);
const searchFieldFormControl = new UntypedFormControl('Gewe');
component.formService.form = createMockFormGroup(searchFieldFormControl);
component.focusOut({ relatedTarget: lupeElement } as FocusEvent); component.focusOut({ relatedTarget: lupeElement } as FocusEvent);
...@@ -196,17 +203,10 @@ describe('VorgangSearchComponent', () => { ...@@ -196,17 +203,10 @@ describe('VorgangSearchComponent', () => {
it('should set previous value', () => { it('should set previous value', () => {
component.searchValueFocusIn = 'Gewe'; // previous value component.previouslyEnteredSearchValue = 'Gewe';
component.searchSubmitButton = { component.searchSubmitButton = createSearchSubmitMockButton(null);
_elementRef: { const searchFieldFormControl = new UntypedFormControl('Gewerbe');
nativeElement: null component.formService.form = createMockFormGroup(searchFieldFormControl);
}
} as MatButton;
const searchFieldFormControl = new UntypedFormControl('Gewerbe'); // input form value
component.formService.form = new UntypedFormGroup({
search: searchFieldFormControl
})
component.focusOut({ relatedTarget: {} } as FocusEvent); component.focusOut({ relatedTarget: {} } as FocusEvent);
......
...@@ -42,7 +42,7 @@ export class VorgangSearchComponent { ...@@ -42,7 +42,7 @@ export class VorgangSearchComponent {
@ViewChild('searchInput') searchInput: ElementRef; @ViewChild('searchInput') searchInput: ElementRef;
@ViewChild('searchSubmitButton') searchSubmitButton: MatButton; @ViewChild('searchSubmitButton') searchSubmitButton: MatButton;
searchValueFocusIn: string; previouslyEnteredSearchValue: string;
readonly vorgangHeaderLinkRel = VorgangHeaderLinkRel; readonly vorgangHeaderLinkRel = VorgangHeaderLinkRel;
readonly vorgangListLinkRel = VorgangListLinkRel; readonly vorgangListLinkRel = VorgangListLinkRel;
...@@ -55,21 +55,21 @@ export class VorgangSearchComponent { ...@@ -55,21 +55,21 @@ export class VorgangSearchComponent {
} }
submit(): void { submit(): void {
this.searchValueFocusIn = this.formService.getValue(); this.previouslyEnteredSearchValue = this.formService.getValue();
this.formService.submit(); this.formService.submit();
} }
focusIn(): void { focusIn(): void {
this.searchValueFocusIn = this.formService.getValue(); this.previouslyEnteredSearchValue = this.formService.getValue();
} }
focusOut(event: FocusEvent): void { focusOut(event: FocusEvent): void {
if (!this.isRelatedFocusedElementSearchSubmitButton(event)) { if (!this.isRelatedTargetSearchButton(event)) {
this.formService.form.controls[this.formService.SEARCH_FIELD].setValue(this.searchValueFocusIn); this.formService.setSearchValue(this.previouslyEnteredSearchValue);
} }
} }
private isRelatedFocusedElementSearchSubmitButton(event: FocusEvent): boolean { private isRelatedTargetSearchButton(event: FocusEvent): boolean {
return event.relatedTarget === this.searchSubmitButton._elementRef.nativeElement; return event.relatedTarget === this.searchSubmitButton._elementRef.nativeElement;
} }
} }
\ No newline at end of file
...@@ -149,6 +149,10 @@ export class VorgangSearchFormService implements OnDestroy { ...@@ -149,6 +149,10 @@ export class VorgangSearchFormService implements OnDestroy {
return isEmpty(value) ? null : value; return isEmpty(value) ? null : value;
} }
setSearchValue(value: string): void {
this.form.controls[this.SEARCH_FIELD].setValue(value);
}
ngOnDestroy(): void { ngOnDestroy(): void {
if (isNotNil(this.subscription)) this.subscription.unsubscribe(); if (isNotNil(this.subscription)) this.subscription.unsubscribe();
if (isNotNil(this.previewSubscription)) this.previewSubscription.unsubscribe(); if (isNotNil(this.previewSubscription)) this.previewSubscription.unsubscribe();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment