Skip to content

Commit

Permalink
Added test to verify if suggestion params given in options are passed…
Browse files Browse the repository at this point in the history
… in the suggestion query
  • Loading branch information
wosullivan committed Sep 22, 2020
1 parent 48da9f0 commit 0b16517
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
1 change: 0 additions & 1 deletion pages/top_queries.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
Coveo.init(document.body, {
TopQueries: {
suggestionQueryParams: {
organizationId: 'my_org_id',
count: 3,
searchHub: 'default',
},
Expand Down
4 changes: 2 additions & 2 deletions src/components/TopQueries/TopQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ export class TopQueries extends Component {
this.updateTopQueries();
}

public async updateTopQueries(suggestionQueryParams?: IQuerySuggestRequest): Promise<void> {
public async updateTopQueries(suggestionQueryParams: IQuerySuggestRequest = this.options.suggestionQueryParams): Promise<void> {
this.show();

let suggestions: IQuerySuggestResponse;
try {
suggestions = await this.queryController.getEndpoint().getQuerySuggest(suggestionQueryParams ?? this.options.suggestionQueryParams);
suggestions = await this.queryController.getEndpoint().getQuerySuggest(suggestionQueryParams);
} catch (err) {
console.error(`Failed to fetch query suggestions: ${err}`);
this.hide();
Expand Down
19 changes: 17 additions & 2 deletions tests/components/TopQueries/TopQueries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IQuerySuggestResponse, NoopAnalyticsClient } from 'coveo-search-ui';
import { Mock } from 'coveo-search-ui-tests';
import { IBasicComponentSetup } from 'coveo-search-ui-tests/MockEnvironment';
import { createSandbox, SinonSandbox } from 'sinon';
import { ITopQueriesOptions } from '../../../src/components/TopQueries/TopQueries';
import { TopQueries } from '../../../src/Index';

describe('TopQueries', () => {
Expand Down Expand Up @@ -53,10 +54,10 @@ describe('TopQueries', () => {
sandbox.restore();
});

async function basicTopQueriesSetup(querySuggestionResponse: any) {
async function basicTopQueriesSetup(querySuggestionResponse: any, options: ITopQueriesOptions = {}) {
const topQueries = Mock.advancedComponentSetup<TopQueries>(
TopQueries,
new Mock.AdvancedComponentSetupOptions(null, {}, (env) => {
new Mock.AdvancedComponentSetupOptions(null, options, (env) => {
env.searchInterface.usageAnalytics = new NoopAnalyticsClient();
return env;
})
Expand Down Expand Up @@ -159,4 +160,18 @@ describe('TopQueries', () => {
expect(suggestStub.called).toBe(true);
expect(executeQuerySpy.called).toBe(true);
});

it('Should call getQuerySuggest with paramers given in options', async () => {
const options: ITopQueriesOptions = {
suggestionQueryParams: {
q: '',
count: 13,
},
};

const { suggestStub } = await basicTopQueriesSetup(SUGGESTION, options);

expect(suggestStub.called).toBe(true);
expect(suggestStub.args[0][0]).toBe(options.suggestionQueryParams);
});
});

0 comments on commit 0b16517

Please sign in to comment.