From 8c0048ad7f839531db6cc275f3ebe3e031ac1b93 Mon Sep 17 00:00:00 2001 From: Will Abson Date: Fri, 6 Jan 2017 11:00:14 +0000 Subject: [PATCH] Prevent live search from being run too soon (#1402) Refs #1356 --- .../alfresco-search-control.component.spec.ts | 32 +++++++++++++++++++ .../alfresco-search-control.component.ts | 4 +-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/ng2-components/ng2-alfresco-search/src/components/alfresco-search-control.component.spec.ts b/ng2-components/ng2-alfresco-search/src/components/alfresco-search-control.component.spec.ts index 39d992a951..cddf0971fd 100644 --- a/ng2-components/ng2-alfresco-search/src/components/alfresco-search-control.component.spec.ts +++ b/ng2-components/ng2-alfresco-search/src/components/alfresco-search-control.component.spec.ts @@ -86,12 +86,44 @@ describe('AlfrescoSearchControlComponent', () => { fixture.detectChanges(); fixture.componentInstance.searchChange.subscribe(e => { expect(e.value).toBe('customSearchTerm211'); + expect(e.valid).toBe(true); done(); }); component.searchControl.setValue('customSearchTerm211', true); fixture.detectChanges(); }); + it('should update FAYT search when user inputs a valid term', (done) => { + fixture.componentInstance.searchChange.subscribe(() => { + expect(fixture.componentInstance.liveSearchTerm).toBe('customSearchTerm'); + done(); + }); + fixture.detectChanges(); + fixture.componentInstance.searchTerm = 'customSearchTerm'; + fixture.detectChanges(); + }); + + it('should NOT update FAYT term when user inputs a search term less than 3 characters', (done) => { + fixture.componentInstance.searchChange.subscribe(() => { + expect(fixture.componentInstance.liveSearchTerm).toBe(''); + done(); + }); + fixture.detectChanges(); + fixture.componentInstance.searchTerm = 'cu'; + fixture.detectChanges(); + }); + + it('should still fire an event when user inputs a search term less than 3 characters', (done) => { + fixture.componentInstance.searchChange.subscribe((e) => { + expect(e.value).toBe('cu'); + expect(e.valid).toBe(false); + done(); + }); + fixture.detectChanges(); + fixture.componentInstance.searchTerm = 'cu'; + fixture.detectChanges(); + }); + describe('Component rendering', () => { it('should display a text input field by default', () => { diff --git a/ng2-components/ng2-alfresco-search/src/components/alfresco-search-control.component.ts b/ng2-components/ng2-alfresco-search/src/components/alfresco-search-control.component.ts index ca4658fabe..9377a9c72b 100644 --- a/ng2-components/ng2-alfresco-search/src/components/alfresco-search-control.component.ts +++ b/ng2-components/ng2-alfresco-search/src/components/alfresco-search-control.component.ts @@ -111,9 +111,9 @@ export class AlfrescoSearchControlComponent implements OnInit, OnDestroy { } private onSearchTermChange(value: string): void { - this.liveSearchTerm = value; - this.searchControl.setValue(value, true); this.searchValid = this.searchControl.valid; + this.liveSearchTerm = this.searchValid ? value : ''; + this.searchControl.setValue(value, true); this.searchChange.emit({ value: value, valid: this.searchValid