Skip to content

Commit 53dd3e1

Browse files
committed
Add tests on placeholder search
1 parent 952de5e commit 53dd3e1

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed

src/adapter/search-request-adapter/__tests__/search-params.tests.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,29 @@ test('Adapt SearchContext with no finite pagination and pagination total hits lo
183183

184184
expect(searchParams.limit).toBe(4)
185185
})
186+
187+
test('Adapt SearchContext placeholderSearch set to false', () => {
188+
const searchParams = adaptSearchParams({
189+
indexUid: 'test',
190+
query: '',
191+
pagination: { paginationTotalHits: 4, page: 0, hitsPerPage: 6 },
192+
defaultFacetDistribution: {},
193+
finitePagination: false,
194+
placeholderSearch: false,
195+
})
196+
197+
expect(searchParams.limit).toBe(0)
198+
})
199+
200+
test('Adapt SearchContext placeholderSearch set to false', () => {
201+
const searchParams = adaptSearchParams({
202+
indexUid: 'test',
203+
query: '',
204+
pagination: { paginationTotalHits: 200, page: 0, hitsPerPage: 6 },
205+
defaultFacetDistribution: {},
206+
finitePagination: false,
207+
placeholderSearch: true,
208+
})
209+
210+
expect(searchParams.limit).toBe(7)
211+
})

src/adapter/search-request-adapter/search-resolver.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ import {
77
} from '../../types'
88
import { addMissingFacets, extractFacets } from './filters'
99

10+
const emptySearch: MeiliSearchResponse<Record<string, any>> = {
11+
hits: [],
12+
query: '',
13+
facetsDistribution: {},
14+
limit: 0,
15+
offset: 0,
16+
exhaustiveNbHits: false,
17+
nbHits: 0,
18+
processingTimeMs: 0,
19+
}
20+
1021
/**
1122
* @param {ResponseCacher} cache
1223
*/
@@ -23,6 +34,14 @@ export function SearchResolver(cache: SearchCacheInterface) {
2334
searchParams: MeiliSearchParams,
2435
client: MeiliSearch
2536
): Promise<MeiliSearchResponse<Record<string, any>>> {
37+
const { placeholderSearch, query } = searchContext
38+
39+
// query can be: empty string, undefined or null
40+
// all of them are falsy's
41+
if (!placeholderSearch && !query) {
42+
return emptySearch
43+
}
44+
2645
const { pagination } = searchContext
2746

2847
// In case we are in a `finitePagination`, only one big request is made

src/contexts/search-context.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ export function createSearchContext(
2626
hitsPerPage: instantSearchParams?.hitsPerPage,
2727
page: instantSearchParams?.page,
2828
})
29+
2930
const searchContext: SearchContext = {
3031
...options,
3132
...instantSearchParams,
3233
sort: sortByArray.join(':') || '',
3334
indexUid,
3435
pagination,
3536
defaultFacetDistribution,
36-
placeholderSearch: !options.placeholderSearch, // true by default
37+
placeholderSearch: options.placeholderSearch !== false, // true by default
3738
keepZeroFacets: !!options.keepZeroFacets, // false by default
3839
finitePagination: !!options.finitePagination, // false by default
3940
}

tests/placeholder-search.tests.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { instantMeiliSearch } from '../src'
2+
import {
3+
searchClient,
4+
dataset,
5+
Movies,
6+
meilisearchClient,
7+
} from './assets/utils'
8+
9+
describe('Pagination browser test', () => {
10+
beforeAll(async () => {
11+
const deleteTask = await meilisearchClient.deleteIndex('movies')
12+
await meilisearchClient.waitForTask(deleteTask.uid)
13+
await meilisearchClient
14+
.index('movies')
15+
.updateFilterableAttributes(['genres'])
16+
const documentsTask = await meilisearchClient
17+
.index('movies')
18+
.addDocuments(dataset)
19+
await meilisearchClient.index('movies').waitForTask(documentsTask.uid)
20+
})
21+
22+
test('Test placeholdersearch set to false', async () => {
23+
const customClient = instantMeiliSearch(
24+
'http://localhost:7700',
25+
'masterKey',
26+
{
27+
paginationTotalHits: 5,
28+
placeholderSearch: true,
29+
}
30+
)
31+
const response = await customClient.search<Movies>([
32+
{
33+
indexName: 'movies',
34+
},
35+
])
36+
const hits = response.results[0].hits
37+
expect(hits.length).toBe(5)
38+
})
39+
40+
test('Test placeholdersearch set to true', async () => {
41+
const customClient = instantMeiliSearch(
42+
'http://localhost:7700',
43+
'masterKey',
44+
{
45+
paginationTotalHits: 5,
46+
placeholderSearch: false,
47+
}
48+
)
49+
const response = await customClient.search<Movies>([
50+
{
51+
indexName: 'movies',
52+
},
53+
])
54+
const hits = response.results[0].hits
55+
expect(hits.length).toBe(0)
56+
})
57+
})

0 commit comments

Comments
 (0)