Skip to content

Commit 3780643

Browse files
committed
Search service suggestion tests
1 parent 9c021a9 commit 3780643

2 files changed

Lines changed: 51 additions & 8 deletions

File tree

src/app/shared/search/search.service.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,45 @@ describe('SearchService', () => {
146146
});
147147
});
148148

149+
describe('getSuggestionsFor', () => {
150+
let remoteDataMocks: Record<string, RemoteData<any>>;
151+
let dictionary = 'supermodels';
152+
let query = 'strawburster';
153+
154+
beforeEach(() => {
155+
remoteDataMocks = {
156+
RequestPending: new RemoteData(undefined, msToLive, remoteDataTimestamp, RequestEntryState.RequestPending, undefined, undefined, undefined),
157+
ResponsePending: new RemoteData(undefined, msToLive, remoteDataTimestamp, RequestEntryState.ResponsePending, undefined, undefined, undefined),
158+
Success: new RemoteData(remoteDataTimestamp, msToLive, remoteDataTimestamp, RequestEntryState.Success, undefined,
159+
{ suggest: { [dictionary]: { [query]: ['strawburster', 'lesser', 'known', 'people'] } } },
160+
200),
161+
SuccessStale: new RemoteData(remoteDataTimestamp, msToLive, remoteDataTimestamp, RequestEntryState.SuccessStale, undefined, new SearchObjects(), 200),
162+
};
163+
});
164+
165+
it('should call getEndpoint on the halService with the suggest endpoint', () => {
166+
spyOn(halService, 'getEndpoint').and.callThrough();
167+
168+
service.getSuggestionsFor(dictionary, query).subscribe();
169+
170+
expect(halService.getEndpoint).toHaveBeenCalledWith('discover');
171+
});
172+
173+
it('should send out the request on the request service', () => {
174+
service.getSuggestionsFor(dictionary, query).subscribe();
175+
176+
expect(requestService.send).toHaveBeenCalled();
177+
});
178+
179+
it('should call buildFromUUID on the request service', () => {
180+
spyOn(remoteDataBuildService, 'buildFromRequestUUID').and.callThrough();
181+
182+
service.getSuggestionsFor(dictionary, query).subscribe();
183+
184+
expect(remoteDataBuildService.buildFromRequestUUID).toHaveBeenCalled();
185+
});
186+
});
187+
149188
describe('search', () => {
150189
let remoteDataMocks: Record<string, RemoteData<SearchObjects<any>>>;
151190

src/app/shared/search/search.service.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export class SearchService {
147147
}
148148
}
149149

150-
getSuggestEndpoint(searchOptions?: PaginatedSearchOptions): Observable<string> {
150+
getDiscoverEndpoint(searchOptions?: PaginatedSearchOptions): Observable<string> {
151151
// TODO: HAL link to 'suggest' endpoint does not exist yet, need to fix this
152152
return this.halService.getEndpoint('discover').pipe(
153153
map((url: string) => {
@@ -359,7 +359,7 @@ export class SearchService {
359359
* @returns serialised JSON of Solr term suggestions
360360
*/
361361
getSuggestionsFor(query: string, dictionary: string): Observable<SuggestionEntry[]> {
362-
return this.getSuggestEndpoint().pipe(
362+
return this.getDiscoverEndpoint().pipe(
363363
take(1),
364364
switchMap((baseUrl: string) => {
365365
const href = new URLCombiner(baseUrl, 'suggest').toString();
@@ -375,12 +375,16 @@ export class SearchService {
375375
this.requestService.send(request, false);
376376
return this.rdb.buildFromRequestUUID(request.uuid).pipe(
377377
getFirstSucceededRemoteDataPayload(),
378-
map((data: any) => data.suggest[dictionary][query].suggestions
379-
?.map(((suggestion: any) => new SuggestionEntry(
380-
new DOMParser().parseFromString(suggestion.term, 'text/html')
381-
.body.textContent,
382-
this.sanitizer.bypassSecurityTrustHtml(suggestion.term),
383-
suggestion.weight)))));
378+
map((data: any) => {
379+
if (data.suggest && data.suggest[dictionary] && data.suggest[dictionary][query]?.suggestions) {
380+
return data.suggest[dictionary][query].suggestions
381+
.map(((suggestion: any) => new SuggestionEntry(
382+
new DOMParser().parseFromString(suggestion.term, 'text/html')
383+
.body.textContent,
384+
this.sanitizer.bypassSecurityTrustHtml(suggestion.term),
385+
suggestion.weight)));
386+
}
387+
}));
384388
}));
385389
}
386390

0 commit comments

Comments
 (0)