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', () => {
component.submit();
expect(component.searchValueFocusIn).toEqual('Gewerbe');
expect(component.previouslyEnteredSearchValue).toEqual('Gewerbe');
});
it('should submit form', () => {
......@@ -169,25 +169,32 @@ describe('VorgangSearchComponent', () => {
component.focusIn();
expect(component.searchValueFocusIn).toEqual('Gewerbe');
expect(component.previouslyEnteredSearchValue).toEqual('Gewerbe');
});
});
describe('focusOut', () => {
it('should set new form value when focus out to search button (Lupe)', () => {
const lupeElement = {};
component.searchValueFocusIn = 'Gewerbe'; // previous value
component.searchSubmitButton = {
function createSearchSubmitMockButton(nativeElement: any): MatButton {
return {
_elementRef: {
nativeElement: lupeElement
nativeElement: nativeElement
}
} as MatButton;
}
const searchFieldFormControl = new UntypedFormControl('Gewe'); // input form value
component.formService.form = new UntypedFormGroup({
function createMockFormGroup(searchFieldFormControl: UntypedFormControl): UntypedFormGroup {
return new UntypedFormGroup({
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);
......@@ -196,17 +203,10 @@ describe('VorgangSearchComponent', () => {
it('should set previous value', () => {
component.searchValueFocusIn = 'Gewe'; // previous value
component.searchSubmitButton = {
_elementRef: {
nativeElement: null
}
} as MatButton;
const searchFieldFormControl = new UntypedFormControl('Gewerbe'); // input form value
component.formService.form = new UntypedFormGroup({
search: searchFieldFormControl
})
component.previouslyEnteredSearchValue = 'Gewe';
component.searchSubmitButton = createSearchSubmitMockButton(null);
const searchFieldFormControl = new UntypedFormControl('Gewerbe');
component.formService.form = createMockFormGroup(searchFieldFormControl);
component.focusOut({ relatedTarget: {} } as FocusEvent);
......
......@@ -42,7 +42,7 @@ export class VorgangSearchComponent {
@ViewChild('searchInput') searchInput: ElementRef;
@ViewChild('searchSubmitButton') searchSubmitButton: MatButton;
searchValueFocusIn: string;
previouslyEnteredSearchValue: string;
readonly vorgangHeaderLinkRel = VorgangHeaderLinkRel;
readonly vorgangListLinkRel = VorgangListLinkRel;
......@@ -55,21 +55,21 @@ export class VorgangSearchComponent {
}
submit(): void {
this.searchValueFocusIn = this.formService.getValue();
this.previouslyEnteredSearchValue = this.formService.getValue();
this.formService.submit();
}
focusIn(): void {
this.searchValueFocusIn = this.formService.getValue();
this.previouslyEnteredSearchValue = this.formService.getValue();
}
focusOut(event: FocusEvent): void {
if (!this.isRelatedFocusedElementSearchSubmitButton(event)) {
this.formService.form.controls[this.formService.SEARCH_FIELD].setValue(this.searchValueFocusIn);
if (!this.isRelatedTargetSearchButton(event)) {
this.formService.setSearchValue(this.previouslyEnteredSearchValue);
}
}
private isRelatedFocusedElementSearchSubmitButton(event: FocusEvent): boolean {
private isRelatedTargetSearchButton(event: FocusEvent): boolean {
return event.relatedTarget === this.searchSubmitButton._elementRef.nativeElement;
}
}
\ No newline at end of file
......@@ -149,6 +149,10 @@ export class VorgangSearchFormService implements OnDestroy {
return isEmpty(value) ? null : value;
}
setSearchValue(value: string): void {
this.form.controls[this.SEARCH_FIELD].setValue(value);
}
ngOnDestroy(): void {
if (isNotNil(this.subscription)) this.subscription.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