Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Facets tests #400

Merged
merged 4 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"cleanup": "shx rm -rf dist/",
"test:watch": "yarn test --watch",
"test": "jest",
"test": "jest --runInBand",
"test:all": "yarn test && yarn test:env && yarn test:playgrounds && yarn test:e2e",
"test:env": "yarn build && yarn test:env:browser && yarn test:env:nodejs && yarn test:env:esm && yarn test:env:ts",
"test:env:browser": "yarn --cwd tests/env/express && yarn --cwd tests/env/express test",
Expand Down
1 change: 0 additions & 1 deletion src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export function instantMeiliSearch(
hitsPerPage: hitsPerPage === undefined ? 20 : hitsPerPage, // 20 is the MeiliSearch's default limit value. `hitsPerPage` can be changed with `InsantSearch.configure`.
page: page || 0, // default page is 0 if none is provided
}

// Transform IS params to MeiliSearch params
const msSearchParams = transformToMeiliSearchParams(
instantSearchParams,
Expand Down
27 changes: 18 additions & 9 deletions src/types/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import * as MStypes from 'meilisearch'
import * as IStypes from './instantsearch-types'

export type ISSearchParams = IStypes.SearchRequestParameters &
MStypes.SearchParams<any>
export type FacetFilter = Array<string | string[]>

export type IMSearchParams = Omit<IStypes.SearchParameters, 'facetFilters'> & {
query?: string
facetFilters?: FacetFilter | FacetFilter[]
}

export type IMSearchRequest = {
indexName: string
params: IMSearchParams
}

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

export type IMResponse = {
facets?: Record<string, object | undefined>
facets?: Record<string, Record<string, number> | undefined>
exhaustiveFacetsCount?: boolean
exhaustiveNbHits: boolean
nbPages?: number
Expand Down Expand Up @@ -54,7 +63,7 @@ export type SnippetsParams = {

export type CreateHighlighResult = (
highLightParams: HighLightParams & FormattedHit
) => { formattedHit: any } & ISSearchParams
) => { formattedHit: any } & IMSearchParams

export type ReplaceHighlightTags = (
value: string,
Expand All @@ -64,7 +73,7 @@ export type ReplaceHighlightTags = (

export type CreateSnippetResult = (
snippetsParams: HighLightParams & SnippetsParams & FormattedHit
) => { formattedHit: any } & ISSearchParams
) => { formattedHit: any } & IMSearchParams

export type SnippetValue = (
value: string,
Expand All @@ -74,20 +83,20 @@ export type SnippetValue = (
) => string

export type TransformToMeiliSearchParams = (
instantSearchParams: ISSearchParams,
instantSearchParams: IMSearchParams,
instantMeiliSearchContext: InstantMeiliSearchContext
) => Record<string, any>

export type TransformToISResponse = (
indexUid: string,
meiliSearchResponse: MStypes.SearchResponse<any, any>,
instantSearchParams: ISSearchParams,
instantSearchParams: IMSearchParams,
instantMeiliSearchContext: InstantMeiliSearchContext
) => { results: SearchResponse[] }

export type TransformToISHitsm = (
meiliSearchHits: Array<Record<string, any>>,
instantSearchParams: ISSearchParams,
instantSearchParams: IMSearchParams,
instantMeiliSearchContext: InstantMeiliSearchContext
) => ISHits[]

Expand All @@ -104,7 +113,7 @@ export type PaginateHits = (
export type InstantMeiliSearchInstance = {
MeiliSearchClient: MStypes.MeiliSearch
search: (
requests: IStypes.SearchRequest[]
requests: IMSearchRequest[]
) => Promise<{ results: SearchResponse[] }>
}

Expand Down
106 changes: 106 additions & 0 deletions tests/facets.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { searchClient, dataset } from './assets/utils'

describe('Instant MeiliSearch Browser test', () => {
beforeAll(async () => {
try {
await searchClient.MeiliSearchClient.deleteIndex('movies')
} catch (e) {
// movies does not exist
}
bidoubiwa marked this conversation as resolved.
Show resolved Hide resolved
await searchClient.MeiliSearchClient.index(
'movies'
).updateAttributesForFaceting(['genres'])
const moviesUpdate = await searchClient.MeiliSearchClient.index(
'movies'
).addDocuments(dataset)
await searchClient.MeiliSearchClient.index('movies').waitForPendingUpdate(
moviesUpdate.updateId
)
})

test('Test one facet on facetsFilters without a query', async () => {
const response = await searchClient.search([
{
indexName: 'movies',
params: {
query: '',
facetFilters: ['genres:Adventure'],
},
},
])
const hits = response.results[0].hits
expect(hits.length).toEqual(1)
expect(hits[0].title).toEqual('Star Wars')
})

test('Test one facet on facetsFilters with a query', async () => {
const response = await searchClient.search([
{
indexName: 'movies',
params: {
query: 'four',
facetFilters: ['genres:Crime'],
},
},
])
const hits = response.results[0].hits
expect(hits.length).toEqual(2)
expect(hits[0].title).toEqual('Four Rooms')
})

test('Test multiple on facetsFilters without a query', async () => {
const response = await searchClient.search([
{
indexName: 'movies',
params: {
query: '',
facetFilters: ['genres:Comedy', 'genres:Crime'],
},
},
])
const hits = response.results[0].hits
expect(hits.length).toEqual(2)
expect(hits[0].title).toEqual('Ariel')
})

test('Test multiple on facetsFilters with a query', async () => {
const response = await searchClient.search([
{
indexName: 'movies',
params: {
query: 'ar',
facetFilters: ['genres:Comedy', 'genres:Crime'],
},
},
])
const hits = response.results[0].hits
expect(hits.length).toEqual(2)
expect(hits[0].title).toEqual('Ariel')
})

test('Test multiple nested on facetsFilters without a query', async () => {
const params = {
indexName: 'movies',
params: {
query: 'night',
facetFilters: [['genres:action', 'genres:Thriller'], 'genres:crime'],
},
}
const response = await searchClient.search([params])
const hits = response.results[0].hits
expect(hits[0].title).toEqual('Judgment Night')
})

test('Test multiple nested on facetsFilters with a query', async () => {
const params = {
indexName: 'movies',
params: {
query: '',
facetFilters: [['genres:action', 'genres:Thriller'], 'genres:crime'],
},
}
const response = await searchClient.search([params])
const hits = response.results[0].hits
expect(hits[0].title).toEqual('Judgment Night')
})
})
59 changes: 59 additions & 0 deletions tests/facetsDistribution.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { searchClient, dataset } from './assets/utils'

describe('Instant MeiliSearch Browser test', () => {
beforeAll(async () => {
try {
await searchClient.MeiliSearchClient.deleteIndex('movies')
} catch (e) {
// movies does not exist
}
await searchClient.MeiliSearchClient.index(
'movies'
).updateAttributesForFaceting(['genres'])
const moviesUpdate = await searchClient.MeiliSearchClient.index(
'movies'
).addDocuments(dataset)
await searchClient.MeiliSearchClient.index('movies').waitForPendingUpdate(
moviesUpdate.updateId
)
})

test('Test empty array on facetsDistribution', async () => {
const response = await searchClient.search([
{
indexName: 'movies',
params: {
query: '',
facets: [],
},
},
])
expect(response.results[0].facets?.genres).toEqual(undefined)
})

test('Test one facet on facetsDistribution', async () => {
const response = await searchClient.search([
{
indexName: 'movies',
params: {
query: '',
facets: ['genres'],
},
},
])
expect(response.results[0].facets?.genres?.Action).toEqual(2)
})

test('Test non-existent facets on facetsDistribution', async () => {
const response = await searchClient.search([
{
indexName: 'movies',
params: {
query: '',
facets: ['genres', 'notKnown'],
},
},
])
expect(response.results[0].facets?.genres?.Action).toEqual(2)
})
})