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

Commit

Permalink
fix(helper-serializer): allow to serialize a helper that has no results
Browse files Browse the repository at this point in the history
  • Loading branch information
rayrutjes committed Aug 8, 2017
1 parent 8603c7a commit cf2add9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
33 changes: 33 additions & 0 deletions src/__tests__/helper-serializer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import algoliaClient from 'algoliasearch';
import algoliaHelper from 'algoliasearch-helper';
import { serialize } from '../helper-serializer';

test('should be able to serialize a helper', () => {
const client = algoliaClient('appId', 'apiKey');
const helper = algoliaHelper(client);

helper.lastResults = new algoliaHelper.SearchResults(helper.state, [
{
nbHits: 666,
},
]);
const serialized = serialize(helper);

expect(serialized.searchParameters).toEqual(expect.any(Object));
expect(serialized.appId).toEqual('appId');
expect(serialized.apiKey).toEqual('apiKey');
expect(serialized.response).toEqual([
{
nbHits: 666,
},
]);
});

test('should be able to serialize a helper that has done no query to Algolia yet', () => {
const client = algoliaClient('appId', 'apiKey');
const helper = algoliaHelper(client);

const serialized = serialize(helper);

expect(serialized.response).toBeNull();
});
15 changes: 10 additions & 5 deletions src/helper-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ export const serialize = function(helper) {

const client = helper.getClient();

const response = helper.lastResults ? helper.lastResults._rawResults : null;

const serialized = {
searchParameters: Object.assign({}, helper.state),
appId: client.applicationID,
apiKey: client.apiKey,
response: helper.lastResults._rawResults,
response,
};

return serialized;
Expand All @@ -25,10 +27,13 @@ export const deserialize = function(data) {
data.searchParameters.index,
data.searchParameters
);
helper.lastResults = new algoliaHelper.SearchResults(
helper.state,
data.response
);

if (data.response) {
helper.lastResults = new algoliaHelper.SearchResults(
helper.state,
data.response
);
}

return helper;
};

0 comments on commit cf2add9

Please sign in to comment.