Skip to content

Commit 29f70c0

Browse files
committed
Add new tests for unhandled usecase
1 parent 6308b63 commit 29f70c0

File tree

3 files changed

+136
-3
lines changed

3 files changed

+136
-3
lines changed

packages/autocomplete-client/src/search/__tests__/fetchMeilisearchResults.test.ts

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
MOVIES,
55
meilisearchClient,
66
} from '../../../__tests__/test.utils'
7+
import { HighlightMetadata } from '../highlight'
78

89
type Movie = (typeof MOVIES)[number]
910

@@ -184,4 +185,136 @@ describe('fetchMeilisearchResults', () => {
184185
matchedWords: [],
185186
})
186187
})
188+
189+
describe('nested object and array highlighting', () => {
190+
interface Person {
191+
id: number
192+
name: string
193+
nicknames: string[]
194+
familyMembers: Array<{
195+
relationship: string
196+
name: string
197+
}>
198+
}
199+
200+
interface PersonHighlightResult {
201+
id: HighlightMetadata
202+
name: HighlightMetadata
203+
nicknames: HighlightMetadata[]
204+
familyMembers: Array<{
205+
relationship: HighlightMetadata
206+
name: HighlightMetadata
207+
}>
208+
}
209+
210+
const PERSON: Person = {
211+
id: 1,
212+
name: 'Joseph',
213+
nicknames: ['Joe', 'Joey'],
214+
familyMembers: [
215+
{
216+
relationship: 'mother',
217+
name: 'Susan',
218+
},
219+
{
220+
relationship: 'father',
221+
name: 'John',
222+
},
223+
],
224+
}
225+
const PEOPLE_INDEX = 'people_highlight_test'
226+
227+
beforeAll(async () => {
228+
await meilisearchClient.deleteIndex(PEOPLE_INDEX)
229+
const task = await meilisearchClient
230+
.index(PEOPLE_INDEX)
231+
.addDocuments([PERSON])
232+
await meilisearchClient.waitForTask(task.taskUid)
233+
})
234+
235+
afterAll(async () => {
236+
await meilisearchClient.deleteIndex(PEOPLE_INDEX)
237+
})
238+
239+
test('highlights in array values', async () => {
240+
const pre = '<em>'
241+
const post = '</em>'
242+
const results = await fetchMeilisearchResults<Person>({
243+
searchClient,
244+
queries: [
245+
{
246+
indexName: PEOPLE_INDEX,
247+
query: 'Joe',
248+
params: {
249+
highlightPreTag: pre,
250+
highlightPostTag: post,
251+
},
252+
},
253+
],
254+
})
255+
256+
const highlightResult = results[0].hits[0]
257+
._highlightResult as PersonHighlightResult
258+
expect(highlightResult.nicknames[0]).toEqual({
259+
value: `${pre}Joe${post}`,
260+
fullyHighlighted: true,
261+
matchLevel: 'full',
262+
matchedWords: ['Joe'],
263+
})
264+
})
265+
266+
test('highlights in nested objects within arrays', async () => {
267+
const pre = '<em>'
268+
const post = '</em>'
269+
const results = await fetchMeilisearchResults<Person>({
270+
searchClient,
271+
queries: [
272+
{
273+
indexName: PEOPLE_INDEX,
274+
query: 'Susan',
275+
params: {
276+
highlightPreTag: pre,
277+
highlightPostTag: post,
278+
},
279+
},
280+
],
281+
})
282+
283+
const highlightResult = results[0].hits[0]
284+
._highlightResult as PersonHighlightResult
285+
expect(highlightResult.familyMembers[0].name).toEqual({
286+
value: `${pre}Susan${post}`,
287+
fullyHighlighted: true,
288+
matchLevel: 'full',
289+
matchedWords: ['Susan'],
290+
})
291+
})
292+
293+
test('highlights multiple nested fields', async () => {
294+
const pre = '<em>'
295+
const post = '</em>'
296+
const results = await fetchMeilisearchResults<Person>({
297+
searchClient,
298+
queries: [
299+
{
300+
indexName: PEOPLE_INDEX,
301+
query: 'mother',
302+
params: {
303+
highlightPreTag: pre,
304+
highlightPostTag: post,
305+
},
306+
},
307+
],
308+
})
309+
310+
const highlightResult = results[0].hits[0]
311+
._highlightResult as PersonHighlightResult
312+
expect(highlightResult.familyMembers[0].relationship).toEqual({
313+
value: `${pre}mother${post}`,
314+
fullyHighlighted: true,
315+
matchLevel: 'full',
316+
matchedWords: ['mother'],
317+
})
318+
})
319+
})
187320
})

packages/autocomplete-client/src/search/fetchMeilisearchResults.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
HITS_PER_PAGE,
99
} from '../constants'
1010
import { SearchClient as MeilisearchSearchClient } from '../types/SearchClient'
11-
import { HighlightResult } from 'instantsearch.js/es/types/algoliasearch'
11+
import { FieldHighlight } from 'instantsearch.js/es/types/algoliasearch'
1212
import { calculateHighlightMetadata } from './highlight'
1313
import { mapOneOrMany } from '../utils'
1414

@@ -93,6 +93,6 @@ function buildHits<TRecord>(
9393
)
9494
),
9595
}
96-
}, {} as HighlightResult<TRecord>),
96+
}, {} as FieldHighlight<TRecord>),
9797
}))
9898
}

packages/autocomplete-client/src/search/highlight.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
interface HighlightMetadata {
1+
export interface HighlightMetadata {
22
value: string
33
fullyHighlighted: boolean
44
matchLevel: 'none' | 'partial' | 'full'

0 commit comments

Comments
 (0)