Skip to content

Commit 23cf7ae

Browse files
bors[bot]bidoubiwa
andauthored
Merge #400
400: Facets tests r=bidoubiwa a=bidoubiwa Co-authored-by: Charlotte Vermandel <[email protected]>
2 parents 0c443eb + 4ec543f commit 23cf7ae

File tree

5 files changed

+184
-11
lines changed

5 files changed

+184
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"cleanup": "shx rm -rf dist/",
88
"test:watch": "yarn test --watch",
9-
"test": "jest",
9+
"test": "jest --runInBand",
1010
"test:all": "yarn test && yarn test:env && yarn test:playgrounds && yarn test:e2e",
1111
"test:env": "yarn build && yarn test:env:browser && yarn test:env:nodejs && yarn test:env:esm && yarn test:env:ts",
1212
"test:env:browser": "yarn --cwd tests/env/express && yarn --cwd tests/env/express test",

src/client/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export function instantMeiliSearch(
3232
hitsPerPage: hitsPerPage === undefined ? 20 : hitsPerPage, // 20 is the MeiliSearch's default limit value. `hitsPerPage` can be changed with `InsantSearch.configure`.
3333
page: page || 0, // default page is 0 if none is provided
3434
}
35-
3635
// Transform IS params to MeiliSearch params
3736
const msSearchParams = transformToMeiliSearchParams(
3837
instantSearchParams,

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,
@@ -69,7 +78,7 @@ export type MergeFiltersAndNumericFilters = (
6978

7079
export type CreateSnippetResult = (
7180
snippetsParams: HighLightParams & SnippetsParams & FormattedHit
72-
) => { formattedHit: any } & ISSearchParams
81+
) => { formattedHit: any } & IMSearchParams
7382

7483
export type SnippetValue = (
7584
value: string,
@@ -79,20 +88,20 @@ export type SnippetValue = (
7988
) => string
8089

8190
export type TransformToMeiliSearchParams = (
82-
instantSearchParams: ISSearchParams,
91+
instantSearchParams: IMSearchParams,
8392
instantMeiliSearchContext: InstantMeiliSearchContext
8493
) => Record<string, any>
8594

8695
export type TransformToISResponse = (
8796
indexUid: string,
8897
meiliSearchResponse: MStypes.SearchResponse<any, any>,
89-
instantSearchParams: ISSearchParams,
98+
instantSearchParams: IMSearchParams,
9099
instantMeiliSearchContext: InstantMeiliSearchContext
91100
) => { results: SearchResponse[] }
92101

93102
export type TransformToISHitsm = (
94103
meiliSearchHits: Array<Record<string, any>>,
95-
instantSearchParams: ISSearchParams,
104+
instantSearchParams: IMSearchParams,
96105
instantMeiliSearchContext: InstantMeiliSearchContext
97106
) => ISHits[]
98107

@@ -109,7 +118,7 @@ export type PaginateHits = (
109118
export type InstantMeiliSearchInstance = {
110119
MeiliSearchClient: MStypes.MeiliSearch
111120
search: (
112-
requests: IStypes.SearchRequest[]
121+
requests: IMSearchRequest[]
113122
) => Promise<{ results: SearchResponse[] }>
114123
}
115124

tests/facets.tests.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 one facet on facetsFilters without a query', async () => {
22+
const response = await searchClient.search([
23+
{
24+
indexName: 'movies',
25+
params: {
26+
query: '',
27+
facetFilters: ['genres:Adventure'],
28+
},
29+
},
30+
])
31+
const hits = response.results[0].hits
32+
expect(hits.length).toEqual(1)
33+
expect(hits[0].title).toEqual('Star Wars')
34+
})
35+
36+
test('Test one facet on facetsFilters with a query', async () => {
37+
const response = await searchClient.search([
38+
{
39+
indexName: 'movies',
40+
params: {
41+
query: 'four',
42+
facetFilters: ['genres:Crime'],
43+
},
44+
},
45+
])
46+
const hits = response.results[0].hits
47+
expect(hits.length).toEqual(2)
48+
expect(hits[0].title).toEqual('Four Rooms')
49+
})
50+
51+
test('Test multiple on facetsFilters without a query', async () => {
52+
const response = await searchClient.search([
53+
{
54+
indexName: 'movies',
55+
params: {
56+
query: '',
57+
facetFilters: ['genres:Comedy', 'genres:Crime'],
58+
},
59+
},
60+
])
61+
const hits = response.results[0].hits
62+
expect(hits.length).toEqual(2)
63+
expect(hits[0].title).toEqual('Ariel')
64+
})
65+
66+
test('Test multiple on facetsFilters with a query', async () => {
67+
const response = await searchClient.search([
68+
{
69+
indexName: 'movies',
70+
params: {
71+
query: 'ar',
72+
facetFilters: ['genres:Comedy', 'genres:Crime'],
73+
},
74+
},
75+
])
76+
const hits = response.results[0].hits
77+
expect(hits.length).toEqual(2)
78+
expect(hits[0].title).toEqual('Ariel')
79+
})
80+
81+
test('Test multiple nested on facetsFilters without a query', async () => {
82+
const params = {
83+
indexName: 'movies',
84+
params: {
85+
query: 'night',
86+
facetFilters: [['genres:action', 'genres:Thriller'], 'genres:crime'],
87+
},
88+
}
89+
const response = await searchClient.search([params])
90+
const hits = response.results[0].hits
91+
expect(hits[0].title).toEqual('Judgment Night')
92+
})
93+
94+
test('Test multiple nested on facetsFilters with a query', async () => {
95+
const params = {
96+
indexName: 'movies',
97+
params: {
98+
query: '',
99+
facetFilters: [['genres:action', 'genres:Thriller'], 'genres:crime'],
100+
},
101+
}
102+
const response = await searchClient.search([params])
103+
const hits = response.results[0].hits
104+
expect(hits[0].title).toEqual('Judgment Night')
105+
})
106+
})

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)