Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
feat(store): improve query parameters merge strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
rayrutjes committed Aug 9, 2017
1 parent d52268e commit 0472627
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 55 deletions.
86 changes: 57 additions & 29 deletions src/__tests__/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
createFromAlgoliaCredentials,
createFromAlgoliaClient,
Store,
HIGHLIGHT_PRE_TAG,
HIGHLIGHT_POST_TAG,
} from '../store';

const createStore = () => {
Expand Down Expand Up @@ -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('<em>');
expect(store.highlightPostTag).toEqual('</em>');
expect(store.highlightPreTag).toEqual(undefined);
expect(store.highlightPostTag).toEqual(undefined);
});

test('can retrieve index name', () => {
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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: '<mark>',
highlightPostTag: '</mark>',
};

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 = '<mark>';
store.highlightPostTag = '</mark>';

expect(store.searchParameters).toEqual(
expect.objectContaining({
highlightPreTag: '<mark>',
highlightPostTag: '</mark>',
})
);
});

test('page search parameter should start at 1', () => {
Expand All @@ -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 = {
Expand Down
58 changes: 32 additions & 26 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -50,8 +50,8 @@ export class Store {
// without trigger multiple queries.
this._stoppedCounter = 1;

this._highlightPreTag = '<em>';
this._highlightPostTag = '</em>';
this._highlightPreTag = undefined;
this._highlightPostTag = undefined;

this._cacheEnabled = true;

Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 0472627

Please sign in to comment.