Skip to content

Commit 05223ff

Browse files
authored
Merge pull request #11 from swiftype/handling-missing-facets
Fixed reference app error
2 parents 3615ad5 + 63b7e88 commit 05223ff

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

src/app-search/AppSearchDriver.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export default class AppSearchDriver {
160160
this._setState({
161161
current,
162162
error: "",
163-
facets: resultList.info.facets,
163+
facets: resultList.info.facets || {},
164164
filters,
165165
requestId: resultList.info.meta.request_id,
166166
results: resultList.results,

src/app-search/AppSearchDriver.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ const resultList = {
1515
results: [{}, {}]
1616
};
1717

18+
const resultListWithoutFacets = {
19+
info: {
20+
meta: {
21+
page: {
22+
total_results: 1000
23+
},
24+
request_id: "12345"
25+
}
26+
},
27+
results: [{}, {}]
28+
};
29+
1830
const params = {
1931
engineName: "some-engine",
2032
hostIdentifier: "host-XXXX",
@@ -31,6 +43,11 @@ beforeAll(() => {
3143
SwiftypeAppSearch.createClient.mockReturnValue(mockClient);
3244
});
3345

46+
beforeEach(() => {
47+
mockClient.search = jest.fn().mockReturnValue({ then: cb => cb(resultList) });
48+
mockClient.click = jest.fn().mockReturnValue({ then: () => {} });
49+
});
50+
3451
it("can be initialized", () => {
3552
const driver = new AppSearchDriver(params);
3653
expect(driver).toBeInstanceOf(AppSearchDriver);
@@ -62,6 +79,30 @@ it("will use initial state if provided", () => {
6279
});
6380
});
6481

82+
it("will default facets to {} in state if facets is missing from the response", () => {
83+
const initialState = {
84+
searchTerm: "test"
85+
};
86+
87+
mockClient.search = jest
88+
.fn()
89+
.mockReturnValue({ then: cb => cb(resultListWithoutFacets) });
90+
91+
const driver = new AppSearchDriver({
92+
...params,
93+
initialState
94+
});
95+
const stateAfterCreation = driver.getState();
96+
97+
expect(stateAfterCreation).toEqual({
98+
...DEFAULT_STATE,
99+
...initialState,
100+
requestId: "12345",
101+
results: [{}, {}],
102+
totalResults: 1000
103+
});
104+
});
105+
65106
it("will trigger a search if searchTerm or filters are provided in initial state", () => {
66107
const initialState = {
67108
filters: [{ initial: "value" }],

src/config/config-helper.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,11 @@ export function buildSearchOptionsFromConfig() {
153153
return acc;
154154
}, undefined);
155155

156-
return {
157-
facets,
158-
result_fields: resultFields,
159-
search_fields: searchFields
160-
};
156+
const searchOptions = {};
157+
if (facets) searchOptions.facets = facets;
158+
searchOptions.result_fields = resultFields;
159+
searchOptions.search_fields = searchFields;
160+
return searchOptions;
161161
}
162162

163163
export function buildSortOptionsFromConfig() {

0 commit comments

Comments
 (0)