Skip to content

Commit b04a707

Browse files
committed
Add tests on facets
1 parent fd017b0 commit b04a707

File tree

4 files changed

+92
-48
lines changed

4 files changed

+92
-48
lines changed

src/client/index.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ export function instantMeiliSearch(
2121
indexName: indexUid,
2222
} = isSearchRequest
2323

24-
// console.log({
25-
// params: instantSearchParams,
26-
// indexName: indexUid,
27-
// })
28-
2924
const { paginationTotalHits, primaryKey, placeholderSearch } = options
3025
const { page, hitsPerPage } = instantSearchParams
3126
const client = this.MeiliSearchClient
@@ -37,9 +32,6 @@ export function instantMeiliSearch(
3732
hitsPerPage: hitsPerPage === undefined ? 20 : hitsPerPage, // 20 is the MeiliSearch's default limit value. `hitsPerPage` can be changed with `InsantSearch.configure`.
3833
page: page || 0, // default page is 0 if none is provided
3934
}
40-
41-
// console.log({instantSearchParams, context})
42-
4335
// Transform IS params to MeiliSearch params
4436
const msSearchParams = transformToMeiliSearchParams(
4537
instantSearchParams,
@@ -58,8 +50,6 @@ export function instantMeiliSearch(
5850
instantSearchParams,
5951
context
6052
)
61-
// console.log({ ISresponse })
62-
6353
return ISresponse
6454
} catch (e) {
6555
console.error(e)

src/types/types.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import * as MStypes from 'meilisearch'
22
import * as IStypes from './instantsearch-types'
33

4-
export type ISSearchParams = IStypes.SearchRequestParameters &
5-
MStypes.SearchParams<any>
4+
export type FacetFilter = Array<string | string[]>
5+
6+
export type IMSearchParams = Omit<IStypes.SearchParameters, 'facetFilters'> & {
7+
query?: string
8+
facetFilters?: FacetFilter | FacetFilter[]
9+
}
10+
11+
export type IMSearchRequest = {
12+
indexName: string
13+
params: IMSearchParams
14+
}
615

716
export type InstantMeiliSearchOptions = {
817
paginationTotalHits?: number
@@ -20,7 +29,7 @@ export type ISHits<T = Record<string, any>> = T & {
2029
}
2130

2231
export type IMResponse = {
23-
facets?: Record<string, object | undefined>
32+
facets?: Record<string, Record<string, number> | undefined>
2433
exhaustiveFacetsCount?: boolean
2534
exhaustiveNbHits: boolean
2635
nbPages?: number
@@ -54,7 +63,7 @@ export type SnippetsParams = {
5463

5564
export type CreateHighlighResult = (
5665
highLightParams: HighLightParams & FormattedHit
57-
) => { formattedHit: any } & ISSearchParams
66+
) => { formattedHit: any } & IMSearchParams
5867

5968
export type ReplaceHighlightTags = (
6069
value: string,
@@ -64,7 +73,7 @@ export type ReplaceHighlightTags = (
6473

6574
export type CreateSnippetResult = (
6675
snippetsParams: HighLightParams & SnippetsParams & FormattedHit
67-
) => { formattedHit: any } & ISSearchParams
76+
) => { formattedHit: any } & IMSearchParams
6877

6978
export type SnippetValue = (
7079
value: string,
@@ -74,20 +83,20 @@ export type SnippetValue = (
7483
) => string
7584

7685
export type TransformToMeiliSearchParams = (
77-
instantSearchParams: ISSearchParams,
86+
instantSearchParams: IMSearchParams,
7887
instantMeiliSearchContext: InstantMeiliSearchContext
7988
) => Record<string, any>
8089

8190
export type TransformToISResponse = (
8291
indexUid: string,
8392
meiliSearchResponse: MStypes.SearchResponse<any, any>,
84-
instantSearchParams: ISSearchParams,
93+
instantSearchParams: IMSearchParams,
8594
instantMeiliSearchContext: InstantMeiliSearchContext
8695
) => { results: SearchResponse[] }
8796

8897
export type TransformToISHitsm = (
8998
meiliSearchHits: Array<Record<string, any>>,
90-
instantSearchParams: ISSearchParams,
99+
instantSearchParams: IMSearchParams,
91100
instantMeiliSearchContext: InstantMeiliSearchContext
92101
) => ISHits[]
93102

@@ -104,7 +113,7 @@ export type PaginateHits = (
104113
export type InstantMeiliSearchInstance = {
105114
MeiliSearchClient: MStypes.MeiliSearch
106115
search: (
107-
requests: IStypes.SearchRequest[]
116+
requests: IMSearchRequest[]
108117
) => Promise<{ results: SearchResponse[] }>
109118
}
110119

tests/facets.tests.ts

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,13 @@ describe('Instant MeiliSearch Browser test', () => {
2424
indexName: 'movies',
2525
params: {
2626
query: '',
27-
facetFilters: ['genres:action'],
28-
},
29-
},
30-
])
31-
const hits = response.results[0].hits
32-
expect(hits.length).toEqual(2)
33-
})
34-
35-
test('Test multiple on facetsFilters', async () => {
36-
const response = await searchClient.search([
37-
{
38-
indexName: 'movies',
39-
params: {
40-
query: '',
41-
facetFilters: ['genres:action', 'genres:Thriller', 'genres:crime'],
27+
facetFilters: ['genres:Adventure'],
4228
},
4329
},
4430
])
4531
const hits = response.results[0].hits
4632
expect(hits.length).toEqual(1)
33+
expect(hits[0].title).toEqual('Star Wars')
4734
})
4835

4936
test('Test multiple on facetsFilters', async () => {
@@ -52,26 +39,25 @@ describe('Instant MeiliSearch Browser test', () => {
5239
indexName: 'movies',
5340
params: {
5441
query: '',
55-
facetFilters: ['genres:action', 'genres:Thriller', 'genres:crime'],
42+
facetFilters: ['genres:Comedy', 'genres:Crime'],
5643
},
5744
},
5845
])
5946
const hits = response.results[0].hits
60-
expect(hits.length).toEqual(1)
47+
expect(hits.length).toEqual(2)
48+
expect(hits[0].title).toEqual('Ariel')
6149
})
6250

63-
test('Test one facet on facetsDistribution', async () => {
64-
const response = await searchClient.search([
65-
{
66-
indexName: 'movies',
67-
params: {
68-
query: '',
69-
facets: ['genres'],
70-
},
51+
test('Test multiple nested on facetsFilters', async () => {
52+
const params = {
53+
indexName: 'movies',
54+
params: {
55+
query: '',
56+
facetFilters: [['genres:action', 'genres:Thriller'], 'genres:crime'],
7157
},
72-
])
73-
console.log(response.results[0])
74-
// console.log(response.results[0].hits)
75-
// expect(response.results[0].facets.genres).toEqual(1)
58+
}
59+
const response = await searchClient.search([params])
60+
const hits = response.results[0].hits
61+
expect(hits[0].title).toEqual('Judgment Night')
7662
})
7763
})

tests/facetsDistribution.tests.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { searchClient, dataset } from './assets/utils'
2+
3+
describe('Instant MeiliSearch Browser test', () => {
4+
beforeAll(async () => {
5+
try {
6+
await searchClient.MeiliSearchClient.deleteIndex('movies')
7+
} catch (e) {
8+
// movies does not exist
9+
}
10+
await searchClient.MeiliSearchClient.index(
11+
'movies'
12+
).updateAttributesForFaceting(['genres'])
13+
const moviesUpdate = await searchClient.MeiliSearchClient.index(
14+
'movies'
15+
).addDocuments(dataset)
16+
await searchClient.MeiliSearchClient.index('movies').waitForPendingUpdate(
17+
moviesUpdate.updateId
18+
)
19+
})
20+
21+
test('Test empty array on facetsDistribution', async () => {
22+
const response = await searchClient.search([
23+
{
24+
indexName: 'movies',
25+
params: {
26+
query: '',
27+
facets: [],
28+
},
29+
},
30+
])
31+
expect(response.results[0].facets?.genres).toEqual(undefined)
32+
})
33+
34+
test('Test one facet on facetsDistribution', async () => {
35+
const response = await searchClient.search([
36+
{
37+
indexName: 'movies',
38+
params: {
39+
query: '',
40+
facets: ['genres'],
41+
},
42+
},
43+
])
44+
expect(response.results[0].facets?.genres?.Action).toEqual(2)
45+
})
46+
47+
test('Test non-existent facets on facetsDistribution', async () => {
48+
const response = await searchClient.search([
49+
{
50+
indexName: 'movies',
51+
params: {
52+
query: '',
53+
facets: ['genres', 'notKnown'],
54+
},
55+
},
56+
])
57+
expect(response.results[0].facets?.genres?.Action).toEqual(2)
58+
})
59+
})

0 commit comments

Comments
 (0)