feat(aio): tell user when no search results

closes #16294
This commit is contained in:
Ward Bell 2017-04-30 05:44:19 -07:00 committed by Matias Niemelä
parent ff32c53099
commit efaf502e95
3 changed files with 35 additions and 5 deletions

View File

@ -1,4 +1,4 @@
<div class="search-results" *ngIf="(searchAreas | async)?.length > 0" > <div class="search-results" *ngIf="hasAreas | async" >
<h2 class="visually-hidden">Search Results</h2> <h2 class="visually-hidden">Search Results</h2>
<div class="search-area" *ngFor="let area of searchAreas | async"> <div class="search-area" *ngFor="let area of searchAreas | async">
<h3>{{area.name}}</h3> <h3>{{area.name}}</h3>
@ -9,4 +9,6 @@
</ul> </ul>
</div> </div>
</div> </div>
<div class="search-results" *ngIf="notFound" >
<p>No results found.</p>
</div>

View File

@ -13,6 +13,9 @@ describe('SearchResultsComponent', () => {
let searchResults: Subject<SearchResults>; let searchResults: Subject<SearchResults>;
let currentAreas: SearchArea[]; let currentAreas: SearchArea[];
/** Get all text from component element */
function getText() { return fixture.debugElement.nativeElement.innerText; }
beforeEach(async(() => { beforeEach(async(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
declarations: [ SearchResultsComponent ], declarations: [ SearchResultsComponent ],
@ -138,7 +141,30 @@ describe('SearchResultsComponent', () => {
fixture.detectChanges(); fixture.detectChanges();
component.hideResults(); component.hideResults();
fixture.detectChanges(); fixture.detectChanges();
expect(fixture.debugElement.queryAll(By.css('a'))).toEqual([]); expect(getText()).toBe('');
});
});
describe('when no query results', () => {
it('should display "not found" message', () => {
searchResults.next({ query: 'something', results: [] });
fixture.detectChanges();
expect(getText()).toContain('No results');
});
it('should not display "not found" message after hideResults()', () => {
searchResults.next({ query: 'something', results: [] });
fixture.detectChanges();
component.hideResults();
fixture.detectChanges();
expect(getText()).toBe('');
});
it('should not display "not found" message when query is empty', () => {
searchResults.next({ query: '', results: [] });
fixture.detectChanges();
expect(getText()).toBe('');
}); });
}); });
}); });

View File

@ -20,7 +20,7 @@ export class SearchResultsComponent implements OnInit {
readonly defaultArea = 'other'; readonly defaultArea = 'other';
showResults = false; notFound = false;
@Output() @Output()
resultSelected = new EventEmitter<SearchResult>(); resultSelected = new EventEmitter<SearchResult>();
@ -29,6 +29,7 @@ export class SearchResultsComponent implements OnInit {
* A mapping of the search results grouped into areas * A mapping of the search results grouped into areas
*/ */
searchAreas = new ReplaySubject<SearchArea[]>(1); searchAreas = new ReplaySubject<SearchArea[]>(1);
hasAreas = this.searchAreas.map(areas => areas.length > 0);
constructor(private searchService: SearchService) {} constructor(private searchService: SearchService) {}
@ -50,11 +51,12 @@ export class SearchResultsComponent implements OnInit {
hideResults() { hideResults() {
this.searchAreas.next([]); this.searchAreas.next([]);
this.notFound = false;
} }
// Map the search results into groups by area // Map the search results into groups by area
private processSearchResults(search: SearchResults) { private processSearchResults(search: SearchResults) {
this.showResults = true; this.notFound = search.query.trim() && search.results.length === 0;
const searchAreaMap = {}; const searchAreaMap = {};
search.results.forEach(result => { search.results.forEach(result => {
if (!result.title) { return; } // bad data; should fix if (!result.title) { return; } // bad data; should fix