Prevent live search from being run too soon (#1402)

Refs #1356
This commit is contained in:
Will Abson
2017-01-06 11:00:14 +00:00
committed by Denys Vuika
parent 779d456c63
commit 8c0048ad7f
2 changed files with 34 additions and 2 deletions

View File

@@ -86,12 +86,44 @@ describe('AlfrescoSearchControlComponent', () => {
fixture.detectChanges(); fixture.detectChanges();
fixture.componentInstance.searchChange.subscribe(e => { fixture.componentInstance.searchChange.subscribe(e => {
expect(e.value).toBe('customSearchTerm211'); expect(e.value).toBe('customSearchTerm211');
expect(e.valid).toBe(true);
done(); done();
}); });
component.searchControl.setValue('customSearchTerm211', true); component.searchControl.setValue('customSearchTerm211', true);
fixture.detectChanges(); 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', () => { describe('Component rendering', () => {
it('should display a text input field by default', () => { it('should display a text input field by default', () => {

View File

@@ -111,9 +111,9 @@ export class AlfrescoSearchControlComponent implements OnInit, OnDestroy {
} }
private onSearchTermChange(value: string): void { private onSearchTermChange(value: string): void {
this.liveSearchTerm = value;
this.searchControl.setValue(value, true);
this.searchValid = this.searchControl.valid; this.searchValid = this.searchControl.valid;
this.liveSearchTerm = this.searchValid ? value : '';
this.searchControl.setValue(value, true);
this.searchChange.emit({ this.searchChange.emit({
value: value, value: value,
valid: this.searchValid valid: this.searchValid