This repository was archived by the owner on Dec 30, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +43
-5
lines changed Expand file tree Collapse file tree 2 files changed +43
-5
lines changed Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change @@ -8,11 +8,13 @@ export const serialize = function(helper) {
8
8
9
9
const client = helper . getClient ( ) ;
10
10
11
+ const response = helper . lastResults ? helper . lastResults . _rawResults : null ;
12
+
11
13
const serialized = {
12
14
searchParameters : Object . assign ( { } , helper . state ) ,
13
15
appId : client . applicationID ,
14
16
apiKey : client . apiKey ,
15
- response : helper . lastResults . _rawResults ,
17
+ response,
16
18
} ;
17
19
18
20
return serialized ;
@@ -25,10 +27,13 @@ export const deserialize = function(data) {
25
27
data . searchParameters . index ,
26
28
data . searchParameters
27
29
) ;
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
+ }
32
37
33
38
return helper ;
34
39
} ;
You can’t perform that action at this time.
0 commit comments