diff --git a/src/__tests__/store.js b/src/__tests__/store.js
index 08452b269..f9b453872 100644
--- a/src/__tests__/store.js
+++ b/src/__tests__/store.js
@@ -9,6 +9,8 @@ import {
createFromAlgoliaCredentials,
createFromAlgoliaClient,
Store,
+ HIGHLIGHT_PRE_TAG,
+ HIGHLIGHT_POST_TAG,
} from '../store';
const createStore = () => {
@@ -76,11 +78,11 @@ describe('Store', () => {
);
});
- test('should use "em" as default highlighting tag', () => {
+ test('should default highlighting tags should be undefined', () => {
const store = createStore();
- expect(store.highlightPreTag).toEqual('');
- expect(store.highlightPostTag).toEqual('');
+ expect(store.highlightPreTag).toEqual(undefined);
+ expect(store.highlightPostTag).toEqual(undefined);
});
test('can retrieve index name', () => {
@@ -208,17 +210,6 @@ describe('Store', () => {
expect(store.queryParameters).toHaveProperty('page', 4);
});
- test('should reset page when query parameters are changed', () => {
- const store = createStore();
- store.page = 2;
- store.queryParameters = {
- distinct: 1,
- attributesToRetrieve: ['objectID'],
- };
-
- expect(store.page).toEqual(1);
- });
-
test('should allow page to be changed by updating query parameters', () => {
const store = createStore();
store.queryParameters = {
@@ -262,7 +253,9 @@ describe('Store', () => {
};
// Make sure distinct parameter is there.
- expect(store.queryParameters).toHaveProperty('distinct');
+ expect(store.queryParameters).toEqual(
+ expect.objectContaining({ distinct: 1 })
+ );
store.queryParameters = {
page: 3,
@@ -271,7 +264,9 @@ describe('Store', () => {
};
// Make sure distinct parameter is gone when overridden with undefined.
- expect(store.queryParameters).not.toHaveProperty('distinct');
+ expect(store.queryParameters).toEqual(
+ expect.objectContaining({ distinct: undefined })
+ );
store.queryParameters = {
page: 3,
@@ -281,30 +276,52 @@ describe('Store', () => {
store.addFacet('price');
// Make sure distinct parameter is gone when overridden with null.
- expect(store.queryParameters).not.toHaveProperty('distinct');
+ expect(store.queryParameters).toEqual(
+ expect.objectContaining({ distinct: undefined })
+ );
});
- test('should allow to retrieve all the search parameters', () => {
+ test('should accept new search parameters', () => {
const store = createStore();
- const searchParameters = Object.assign({}, store._helper.getState(), {
- page: 1,
- });
- expect(store.searchParameters).toEqual(searchParameters);
+ const newSearchParameters = { distinct: true };
+
+ store.searchParameters = newSearchParameters;
+
+ expect(store.searchParameters).toEqual(
+ expect.objectContaining(newSearchParameters)
+ );
});
- test('should accept new search parameters', () => {
+ test('highlighting tags set via query parameters should not leak to the helper', () => {
const store = createStore();
- const searchParameters = store._helper.getState();
- const newSearchParameters = Object.assign({}, searchParameters, {
- distinct: true,
- page: 1,
- });
+ const newSearchParameters = {
+ highlightPreTag: '',
+ highlightPostTag: '',
+ };
store.searchParameters = newSearchParameters;
- expect(store.searchParameters).toEqual(newSearchParameters);
+ expect(store._helper.state).toEqual(
+ expect.objectContaining({
+ highlightPreTag: HIGHLIGHT_PRE_TAG,
+ highlightPostTag: HIGHLIGHT_POST_TAG,
+ })
+ );
+ });
+
+ test('highlighting tags should not be local ones in query parameters', () => {
+ const store = createStore();
+ store.highlightPreTag = '';
+ store.highlightPostTag = '';
+
+ expect(store.searchParameters).toEqual(
+ expect.objectContaining({
+ highlightPreTag: '',
+ highlightPostTag: '',
+ })
+ );
});
test('page search parameter should start at 1', () => {
@@ -324,6 +341,17 @@ describe('Store', () => {
expect(store._helper.getPage()).toEqual(4);
});
+ test('search parameters should not contain internal highlighting tags', () => {
+ const store = createStore();
+
+ expect(store.searchParameters).toEqual(
+ expect.objectContaining({
+ highlightPreTag: undefined,
+ highlightPostTag: undefined,
+ })
+ );
+ });
+
test('should allow to fetch sanitized results', () => {
const store = createStore();
const response = {
diff --git a/src/store.js b/src/store.js
index 83bef2744..5e5666d7c 100644
--- a/src/store.js
+++ b/src/store.js
@@ -12,8 +12,8 @@ export const FACET_AND = 'and';
export const FACET_OR = 'or';
export const FACET_TREE = 'tree';
-const HIGHLIGHT_PRE_TAG = '__ais-highlight__';
-const HIGHLIGHT_POST_TAG = '__/ais-highlight__';
+export const HIGHLIGHT_PRE_TAG = '__ais-highlight__';
+export const HIGHLIGHT_POST_TAG = '__/ais-highlight__';
export const createFromAlgoliaCredentials = (appID, apiKey) => {
const client = algolia(appID, apiKey);
@@ -50,8 +50,8 @@ export class Store {
// without trigger multiple queries.
this._stoppedCounter = 1;
- this._highlightPreTag = '';
- this._highlightPostTag = '';
+ this._highlightPreTag = undefined;
+ this._highlightPostTag = undefined;
this._cacheEnabled = true;
@@ -325,41 +325,47 @@ export class Store {
}
set queryParameters(parameters) {
- const params = Object.assign({}, parameters);
- this.stop();
- for (const parameter in params) {
- if (params[parameter] === null) {
- params[parameter] = undefined;
- }
- this._helper.setQueryParameter(parameter, params[parameter]);
- }
-
- // Make sure page starts at 1.
- if ('page' in params) {
- this.page = params.page;
- delete params.page;
- }
- this.start();
- this.refresh();
+ this.searchParameters = parameters;
}
get queryParameters() {
- const parameters = this._helper.state.getQueryParams();
- parameters.page = this.page;
-
- return parameters;
+ return this.searchParameters;
}
get searchParameters() {
- return Object.assign({}, this._helper.state, { page: this.page });
+ return Object.assign({}, this._helper.state, {
+ page: this.page,
+ highlightPreTag: this.highlightPreTag,
+ highlightPostTag: this.highlightPostTag,
+ });
}
set searchParameters(searchParameters) {
const params = Object.assign({}, searchParameters);
+ const paramKeys = Object.keys(params);
+ paramKeys.forEach(key => {
+ if (params[key] === null) {
+ params[key] = undefined;
+ }
+ });
+
if (params.page !== undefined) {
params.page = params.page - 1;
}
- const newSearchParameters = algoliaHelper.SearchParameters.make(params);
+
+ if ('highlightPreTag' in params) {
+ this.highlightPreTag = params.highlightPreTag;
+ delete params.highlightPreTag;
+ }
+
+ if ('highlightPostTag' in params) {
+ this.highlightPostTag = params.highlightPostTag;
+ delete params.highlightPostTag;
+ }
+
+ const newSearchParameters = algoliaHelper.SearchParameters.make(
+ Object.assign({}, this._helper.state, params)
+ );
this._helper.setState(newSearchParameters);
}