11import { dataset , meilisearchClient , HOST , API_KEY } from './assets/utils'
22import { instantMeiliSearch } from '../src'
33
4- describe ( 'First facet distribution' , ( ) => {
4+ describe ( 'Default facet distribution' , ( ) => {
55 beforeAll ( async ( ) => {
66 const deleteTask = await meilisearchClient . deleteIndex ( 'movies' )
77 await meilisearchClient . waitForTask ( deleteTask . taskUid )
@@ -14,6 +14,10 @@ describe('First facet distribution', () => {
1414 await meilisearchClient . index ( 'movies' ) . waitForTask ( documentsTask . taskUid )
1515 } )
1616
17+ // Without facets
18+ // Without keepZeroFacets
19+ // With placeholderSearch
20+ // With empty query
1721 test ( 'creation of facet distribution without facets' , async ( ) => {
1822 const searchClient = instantMeiliSearch ( HOST , API_KEY )
1923 const response = await searchClient . search ( [
@@ -28,9 +32,13 @@ describe('First facet distribution', () => {
2832 expect ( response . results [ 0 ] . facets ) . toEqual ( { } )
2933 } )
3034
35+ // Without facets
36+ // With keepZeroFacets
37+ // With placeholderSearch
38+ // With empty query
3139 test ( 'creation of facet distribution without facets and with keepZeroFacets to true' , async ( ) => {
3240 const searchClient = instantMeiliSearch ( HOST , API_KEY , {
33- keepZeroFacets : false ,
41+ keepZeroFacets : true ,
3442 } )
3543 const response = await searchClient . search ( [
3644 {
@@ -44,6 +52,58 @@ describe('First facet distribution', () => {
4452 expect ( response . results [ 0 ] . facets ) . toEqual ( { } )
4553 } )
4654
55+ // With facets
56+ // without keepZeroFacets
57+ // without placeholderSearch
58+ // With multiple searchs
59+ test ( 'creation of facet distribution with facets, with keepZeroFacets to false and placeholdersearch to false' , async ( ) => {
60+ const searchClient = instantMeiliSearch ( HOST , API_KEY , {
61+ keepZeroFacets : false ,
62+ placeholderSearch : false , // only test false since `true` is default value
63+ } )
64+ const response = await searchClient . search ( [
65+ {
66+ indexName : 'movies' ,
67+ params : {
68+ facets : [ 'genres' ] ,
69+ query : '' ,
70+ } ,
71+ } ,
72+ ] )
73+ expect ( response . results [ 0 ] . facets ) . toEqual ( {
74+ genres : {
75+ Action : 3 ,
76+ Adventure : 1 ,
77+ Animation : 1 ,
78+ Comedy : 2 ,
79+ Crime : 4 ,
80+ Drama : 1 ,
81+ 'Science Fiction' : 2 ,
82+ Thriller : 1 ,
83+ } ,
84+ } )
85+
86+ // Ensure cached default facet distribution between searchClient is correct
87+ const response2 = await searchClient . search ( [
88+ {
89+ indexName : 'movies' ,
90+ params : {
91+ facets : [ 'genres' ] ,
92+ query : 'no results' ,
93+ } ,
94+ } ,
95+ ] )
96+
97+ // No `0` are showcased as `keepZeroFacets` is set to false
98+ expect ( response2 . results [ 0 ] . facets ) . toEqual ( {
99+ genres : { } ,
100+ } )
101+ } )
102+
103+ // With facets
104+ // without keepZeroFacets
105+ // with placeholderSearch
106+ // With empty query
47107 test ( 'creation of facet distribution with facets' , async ( ) => {
48108 const searchClient = instantMeiliSearch ( HOST , API_KEY )
49109 const response = await searchClient . search ( [
@@ -67,8 +127,27 @@ describe('First facet distribution', () => {
67127 Thriller : 1 ,
68128 } ,
69129 } )
130+
131+ const response2 = await searchClient . search ( [
132+ {
133+ indexName : 'movies' ,
134+ params : {
135+ facets : [ 'genres' ] ,
136+ query : 'no results' ,
137+ } ,
138+ } ,
139+ ] )
140+
141+ // No `0` are showcased as `keepZeroFacets` is set to false
142+ expect ( response2 . results [ 0 ] . facets ) . toEqual ( {
143+ genres : { } ,
144+ } )
70145 } )
71146
147+ // With facets
148+ // with keepZeroFacets
149+ // with placeholderSearch
150+ // With multiple search (empty and no results expected)
72151 test ( 'creation of facet distribution with facets and keepZeroFacets to true' , async ( ) => {
73152 const searchClient = instantMeiliSearch ( HOST , API_KEY , {
74153 keepZeroFacets : true ,
@@ -119,6 +198,68 @@ describe('First facet distribution', () => {
119198 } )
120199 } )
121200
201+ // With facets
202+ // with keepZeroFacets
203+ // without placeholderSearch
204+ // With multiple search (empty and no results expected)
205+ test ( 'creation of facet distribution with facets, keepZeroFacets to true and placeholderSearch to false' , async ( ) => {
206+ const searchClient = instantMeiliSearch ( HOST , API_KEY , {
207+ keepZeroFacets : true ,
208+ placeholderSearch : false ,
209+ } )
210+ const response = await searchClient . search ( [
211+ {
212+ indexName : 'movies' ,
213+ params : {
214+ facets : [ 'genres' ] ,
215+ query : '' ,
216+ } ,
217+ } ,
218+ ] )
219+
220+ expect ( response . results [ 0 ] . facets ) . toEqual ( {
221+ genres : {
222+ Action : 3 ,
223+ Adventure : 1 ,
224+ Animation : 1 ,
225+ Comedy : 2 ,
226+ Crime : 4 ,
227+ Drama : 1 ,
228+ 'Science Fiction' : 2 ,
229+ Thriller : 1 ,
230+ } ,
231+ } )
232+ expect ( response . results [ 0 ] . hits . length ) . toEqual ( 0 )
233+
234+ const response2 = await searchClient . search ( [
235+ {
236+ indexName : 'movies' ,
237+ params : {
238+ facets : [ 'genres' ] ,
239+ query : 'no results' ,
240+ } ,
241+ } ,
242+ ] )
243+
244+ expect ( response2 . results [ 0 ] . facets ) . toEqual ( {
245+ genres : {
246+ Action : 0 ,
247+ Adventure : 0 ,
248+ Animation : 0 ,
249+ Comedy : 0 ,
250+ Crime : 0 ,
251+ Drama : 0 ,
252+ 'Science Fiction' : 0 ,
253+ Thriller : 0 ,
254+ } ,
255+ } )
256+ } )
257+
258+ // With facets
259+ // with keepZeroFacets
260+ // with placeholderSearch
261+ // Without multiple search
262+ // Without query expecting no results
122263 test ( 'creation of facet distribution with facets, keepZeroFacets to true, and query' , async ( ) => {
123264 const searchClient = instantMeiliSearch ( HOST , API_KEY , {
124265 keepZeroFacets : true ,
0 commit comments