|
1 | 1 | import { fetchMeilisearchResults } from '../fetchMeilisearchResults'
|
2 | 2 | import {
|
3 | 3 | searchClient,
|
4 |
| - dataset, |
| 4 | + MOVIES, |
5 | 5 | meilisearchClient,
|
6 | 6 | } from '../../../__tests__/test.utils'
|
7 | 7 |
|
| 8 | +type Movie = (typeof MOVIES)[number] |
| 9 | + |
| 10 | +const INDEX_NAME = 'movies_fetch-meilisearch-results-test' |
| 11 | +const FIRST_ITEM_ID = MOVIES[0].id |
| 12 | +const SECOND_ITEM_ID = MOVIES[1].id |
| 13 | + |
8 | 14 | beforeAll(async () => {
|
9 |
| - await meilisearchClient.deleteIndex('testUid') |
10 |
| - const task = await meilisearchClient.index('testUid').addDocuments(dataset) |
| 15 | + await meilisearchClient.deleteIndex(INDEX_NAME) |
| 16 | + const task = await meilisearchClient.index(INDEX_NAME).addDocuments(MOVIES) |
11 | 17 | await meilisearchClient.waitForTask(task.taskUid)
|
12 | 18 | })
|
13 | 19 |
|
14 | 20 | afterAll(async () => {
|
15 |
| - await meilisearchClient.deleteIndex('testUid') |
| 21 | + await meilisearchClient.deleteIndex(INDEX_NAME) |
16 | 22 | })
|
17 | 23 |
|
18 | 24 | describe('fetchMeilisearchResults', () => {
|
19 | 25 | test('with default options', async () => {
|
20 |
| - const results = await fetchMeilisearchResults<(typeof dataset)[0]>({ |
| 26 | + const results = await fetchMeilisearchResults<Movie>({ |
21 | 27 | searchClient,
|
22 | 28 | queries: [
|
23 | 29 | {
|
24 |
| - indexName: 'testUid', |
| 30 | + indexName: INDEX_NAME, |
25 | 31 | query: '',
|
26 | 32 | },
|
27 | 33 | ],
|
28 | 34 | })
|
29 | 35 |
|
30 |
| - expect(results[0].hits[0].id).toEqual(1) |
31 |
| - expect(results[0].hits[1].id).toEqual(2) |
| 36 | + expect(results[0].hits[0].id).toEqual(FIRST_ITEM_ID) |
| 37 | + expect(results[0].hits[1].id).toEqual(SECOND_ITEM_ID) |
32 | 38 | })
|
33 | 39 |
|
34 |
| - test('with custom search parameters', async () => { |
| 40 | + test('with custom pagination', async () => { |
35 | 41 | const results = await fetchMeilisearchResults({
|
36 | 42 | searchClient,
|
37 | 43 | queries: [
|
38 | 44 | {
|
39 |
| - indexName: 'testUid', |
40 |
| - query: 'Hit', |
| 45 | + indexName: INDEX_NAME, |
| 46 | + query: '', |
41 | 47 | params: {
|
42 | 48 | hitsPerPage: 1,
|
43 |
| - highlightPreTag: '<test>', |
44 |
| - highlightPostTag: '</test>', |
45 |
| - page: 1, |
| 49 | + page: 1, // pages start at 0 |
| 50 | + }, |
| 51 | + }, |
| 52 | + ], |
| 53 | + }) |
| 54 | + |
| 55 | + expect(results[0].hits[0].id).toEqual(SECOND_ITEM_ID) |
| 56 | + }) |
| 57 | + |
| 58 | + test('with custom highlight tags', async () => { |
| 59 | + const results = await fetchMeilisearchResults({ |
| 60 | + searchClient, |
| 61 | + queries: [ |
| 62 | + { |
| 63 | + indexName: INDEX_NAME, |
| 64 | + query: 'Ariel', |
| 65 | + params: { |
| 66 | + highlightPreTag: '<b>', |
| 67 | + highlightPostTag: '</b>', |
| 68 | + }, |
| 69 | + }, |
| 70 | + ], |
| 71 | + }) |
| 72 | + |
| 73 | + expect(results[0].hits[0]._highlightResult?.title?.value).toEqual( |
| 74 | + '<b>Ariel</b>' |
| 75 | + ) |
| 76 | + }) |
| 77 | + |
| 78 | + test('highlight results contain highlighting metadata', async () => { |
| 79 | + const results = await fetchMeilisearchResults({ |
| 80 | + searchClient, |
| 81 | + queries: [ |
| 82 | + { |
| 83 | + indexName: INDEX_NAME, |
| 84 | + query: 'Ariel', |
| 85 | + }, |
| 86 | + ], |
| 87 | + }) |
| 88 | + |
| 89 | + expect(results[0].hits[0]._highlightResult?.id?.fullyHighlighted).toEqual( |
| 90 | + false |
| 91 | + ) |
| 92 | + expect(results[0].hits[0]._highlightResult?.id?.matchLevel).toEqual('none') |
| 93 | + expect(results[0].hits[0]._highlightResult?.id?.matchedWords).toEqual([]) |
| 94 | + expect(results[0].hits[0]._highlightResult?.id?.value).toEqual(String(2)) |
| 95 | + }) |
| 96 | + |
| 97 | + test('highlight results contain fully highlighted match', async () => { |
| 98 | + const pre = '<em>' |
| 99 | + const post = '</em>' |
| 100 | + const results = await fetchMeilisearchResults({ |
| 101 | + searchClient, |
| 102 | + queries: [ |
| 103 | + { |
| 104 | + indexName: INDEX_NAME, |
| 105 | + query: 'Ariel', |
| 106 | + params: { |
| 107 | + highlightPreTag: pre, |
| 108 | + highlightPostTag: post, |
| 109 | + }, |
| 110 | + }, |
| 111 | + ], |
| 112 | + }) |
| 113 | + |
| 114 | + expect(results[0].hits[0]._highlightResult?.title).toEqual({ |
| 115 | + value: `${pre}Ariel${post}`, |
| 116 | + fullyHighlighted: true, |
| 117 | + matchLevel: 'full', |
| 118 | + matchedWords: ['Ariel'], |
| 119 | + }) |
| 120 | + }) |
| 121 | + |
| 122 | + test('highlight results contains full match but not fully highlighted', async () => { |
| 123 | + const pre = '<em>' |
| 124 | + const post = '</em>' |
| 125 | + const results = await fetchMeilisearchResults({ |
| 126 | + searchClient, |
| 127 | + queries: [ |
| 128 | + { |
| 129 | + indexName: INDEX_NAME, |
| 130 | + query: 'Star', |
| 131 | + params: { |
| 132 | + highlightPreTag: pre, |
| 133 | + highlightPostTag: post, |
46 | 134 | },
|
47 | 135 | },
|
48 | 136 | ],
|
49 | 137 | })
|
50 | 138 |
|
51 |
| - expect(results[0].hits[0].id).toEqual(2) |
52 |
| - expect(results[0].hits[0]._highlightResult).toEqual({ |
53 |
| - id: { value: '2' }, |
54 |
| - label: { value: '<test>Hit</test> 2' }, |
| 139 | + expect(results[0].hits[0]._highlightResult?.title).toEqual({ |
| 140 | + value: `${pre}Star${post} Wars`, |
| 141 | + fullyHighlighted: false, |
| 142 | + matchLevel: 'full', |
| 143 | + matchedWords: ['Star'], |
| 144 | + }) |
| 145 | + }) |
| 146 | + |
| 147 | + test('highlight results contain partially highlighted match', async () => { |
| 148 | + const pre = '<em>' |
| 149 | + const post = '</em>' |
| 150 | + const movie = MOVIES[0] |
| 151 | + const results = await fetchMeilisearchResults({ |
| 152 | + searchClient, |
| 153 | + queries: [ |
| 154 | + { |
| 155 | + indexName: INDEX_NAME, |
| 156 | + query: 'Tasto', // missing 'i' from 'Taisto' |
| 157 | + params: { |
| 158 | + highlightPreTag: pre, |
| 159 | + highlightPostTag: post, |
| 160 | + }, |
| 161 | + }, |
| 162 | + ], |
| 163 | + }) |
| 164 | + |
| 165 | + expect(results[0].hits[0]._highlightResult?.overview).toEqual({ |
| 166 | + // The first word of the overview is highlighted |
| 167 | + value: `${pre}Taist${post}` + (movie.overview as string).slice(5), |
| 168 | + fullyHighlighted: false, |
| 169 | + matchLevel: 'partial', |
| 170 | + matchedWords: ['Taist'], |
| 171 | + }) |
| 172 | + }) |
| 173 | + |
| 174 | + test('highlight results contain no match', async () => { |
| 175 | + const results = await fetchMeilisearchResults({ |
| 176 | + searchClient, |
| 177 | + queries: [{ indexName: INDEX_NAME, query: '' }], |
| 178 | + }) |
| 179 | + |
| 180 | + expect(results[0].hits[0]._highlightResult?.title).toEqual({ |
| 181 | + value: 'Ariel', |
| 182 | + fullyHighlighted: false, |
| 183 | + matchLevel: 'none', |
| 184 | + matchedWords: [], |
55 | 185 | })
|
56 | 186 | })
|
57 | 187 | })
|
0 commit comments