@@ -5,8 +5,29 @@ import {
55 adaptRequest ,
66 adaptNumericFilters ,
77 parseRange ,
8+ filterRegex ,
89} from "../src/adaptRequest" ;
910
11+ describe ( "filterRegex test" , ( ) => {
12+ it ( "filter regex should add string to object" , ( ) => {
13+ const itemsJsFacets = { } ;
14+
15+ const facet1 = "color:blue" ;
16+ const filter1 = filterRegex ( itemsJsFacets , facet1 ) ;
17+ const result1 = {
18+ color : [ "blue" ] ,
19+ } ;
20+ expect ( filter1 ) . toStrictEqual ( result1 ) ;
21+
22+ const facet2 = "color:red" ;
23+ const filter2 = filterRegex ( itemsJsFacets , facet2 ) ;
24+ const result2 = {
25+ color : [ "blue" , "red" ] ,
26+ } ;
27+ expect ( filter2 ) . toStrictEqual ( result2 ) ;
28+ } ) ;
29+ } ) ;
30+
1031describe ( "adaptPage tests" , ( ) => {
1132 it ( "adaptpage(x) should return x+1" , ( ) => {
1233 expect ( adaptPage ( 0 ) ) . toBe ( 1 ) ;
@@ -16,14 +37,15 @@ describe("adaptPage tests", () => {
1637} ) ;
1738
1839describe ( "adaptRequest tests" , ( ) => {
19- it ( "adaptRequest should convert request to ItemsJs request" , ( ) => {
40+ it ( "adaptRequest should convert (max parameters) request to ItemsJs request" , ( ) => {
2041 const instantsearchRequest : MultipleQueriesQuery = {
2142 indexName : "products" ,
2243 params : {
2344 query : "a" ,
2445 page : 2 ,
2546 hitsPerPage : 5 ,
2647 facets : [ "price" , "in_stock" ] ,
48+ facetFilters : [ "category:electronics" ] ,
2749 numericFilters : [ "price>=10" , "price<=100" ] ,
2850 } ,
2951 } ;
@@ -35,6 +57,26 @@ describe("adaptRequest tests", () => {
3557 expect ( itemsjsRequest . per_page ) . toBe ( 5 ) ;
3658 expect ( itemsjsRequest . aggregations ) . toMatchObject ( [ "price" , "in_stock" ] ) ;
3759 expect ( itemsjsRequest . filter ) . toBeDefined ( ) ; // Returns native javascript .filter() function
60+ expect ( itemsjsRequest . filters ) . toStrictEqual ( { category : [ "electronics" ] } ) ;
61+ expect ( itemsjsRequest . indexName ) . toBe ( "products" ) ;
62+ expect ( itemsjsRequest . sort ) . toBe ( "products" ) ;
63+ } ) ;
64+
65+ it ( "adaptRequest should convert (min parameters) request to ItemsJs request " , ( ) => {
66+ const instantsearchRequest : MultipleQueriesQuery = {
67+ indexName : "products" ,
68+ params : {
69+ query : "a" ,
70+ page : 2 ,
71+ hitsPerPage : 5 ,
72+ } ,
73+ } ;
74+
75+ const itemsjsRequest = adaptRequest ( instantsearchRequest ) ;
76+
77+ expect ( itemsjsRequest . query ) . toBe ( "a" ) ;
78+ expect ( itemsjsRequest . page ) . toBe ( 3 ) ;
79+ expect ( itemsjsRequest . per_page ) . toBe ( 5 ) ;
3880 expect ( itemsjsRequest . indexName ) . toBe ( "products" ) ;
3981 expect ( itemsjsRequest . sort ) . toBe ( "products" ) ;
4082 } ) ;
@@ -136,8 +178,8 @@ describe("regexInput tests group in three", () => {
136178 } ) ;
137179} ) ;
138180
139- describe ( "adaptFacets tests" , ( ) => {
140- it ( "adaptFacets should convert nested arrays to Itemsjs format" , ( ) => {
181+ describe ( "adaptFilters tests" , ( ) => {
182+ it ( "adaptFilters should convert nested arrays to Itemsjs format" , ( ) => {
141183 const instantsearchFacets = [
142184 [ "category:electronics" , "category:men's clothing" ] ,
143185 [ "color:blue" ] ,
@@ -152,7 +194,7 @@ describe("adaptFacets tests", () => {
152194 expect ( adaptedResult ) . toMatchObject ( itemsJsFacets ) ;
153195 } ) ;
154196
155- it ( "adaptFacets should convert a single array to Itemsjs format" , ( ) => {
197+ it ( "adaptFilters should convert a single array to Itemsjs format" , ( ) => {
156198 const instantsearchFacets = [
157199 "category:electronics" ,
158200 "category:men's clothing" ,
@@ -166,7 +208,7 @@ describe("adaptFacets tests", () => {
166208 expect ( adaptedResult ) . toMatchObject ( itemsJsFacets ) ;
167209 } ) ;
168210
169- it ( "adaptFacets should convert single and nested arrays to Itemsjs format" , ( ) => {
211+ it ( "adaptFilters should convert single and nested arrays to Itemsjs format" , ( ) => {
170212 const instantsearchFacets = [
171213 [ "category:electronics" , "category:men's clothing" ] ,
172214 "color:Blue" ,
@@ -181,4 +223,14 @@ describe("adaptFacets tests", () => {
181223 const adaptedResult = adaptFilters ( instantsearchFacets ) ;
182224 expect ( adaptedResult ) . toMatchObject ( itemsJsFacets ) ;
183225 } ) ;
226+
227+ it ( "adaptFilters should throw an error when no array is given" , ( ) => {
228+ const instantsearchFacets = "category:electronics" ;
229+
230+ expect ( ( ) => {
231+ adaptFilters ( instantsearchFacets ) ;
232+ } ) . toThrowError (
233+ new Error ( "request.params.facetFilters does not contain an array" )
234+ ) ;
235+ } ) ;
184236} ) ;
0 commit comments