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

Commit cf2add9

Browse files
committed
fix(helper-serializer): allow to serialize a helper that has no results
1 parent 8603c7a commit cf2add9

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

src/__tests__/helper-serializer.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import algoliaClient from 'algoliasearch';
2+
import algoliaHelper from 'algoliasearch-helper';
3+
import { serialize } from '../helper-serializer';
4+
5+
test('should be able to serialize a helper', () => {
6+
const client = algoliaClient('appId', 'apiKey');
7+
const helper = algoliaHelper(client);
8+
9+
helper.lastResults = new algoliaHelper.SearchResults(helper.state, [
10+
{
11+
nbHits: 666,
12+
},
13+
]);
14+
const serialized = serialize(helper);
15+
16+
expect(serialized.searchParameters).toEqual(expect.any(Object));
17+
expect(serialized.appId).toEqual('appId');
18+
expect(serialized.apiKey).toEqual('apiKey');
19+
expect(serialized.response).toEqual([
20+
{
21+
nbHits: 666,
22+
},
23+
]);
24+
});
25+
26+
test('should be able to serialize a helper that has done no query to Algolia yet', () => {
27+
const client = algoliaClient('appId', 'apiKey');
28+
const helper = algoliaHelper(client);
29+
30+
const serialized = serialize(helper);
31+
32+
expect(serialized.response).toBeNull();
33+
});

src/helper-serializer.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ export const serialize = function(helper) {
88

99
const client = helper.getClient();
1010

11+
const response = helper.lastResults ? helper.lastResults._rawResults : null;
12+
1113
const serialized = {
1214
searchParameters: Object.assign({}, helper.state),
1315
appId: client.applicationID,
1416
apiKey: client.apiKey,
15-
response: helper.lastResults._rawResults,
17+
response,
1618
};
1719

1820
return serialized;
@@ -25,10 +27,13 @@ export const deserialize = function(data) {
2527
data.searchParameters.index,
2628
data.searchParameters
2729
);
28-
helper.lastResults = new algoliaHelper.SearchResults(
29-
helper.state,
30-
data.response
31-
);
30+
31+
if (data.response) {
32+
helper.lastResults = new algoliaHelper.SearchResults(
33+
helper.state,
34+
data.response
35+
);
36+
}
3237

3338
return helper;
3439
};

0 commit comments

Comments
 (0)