Skip to content

Commit

Permalink
Merge #408
Browse files Browse the repository at this point in the history
408: Add snippet/highlight/pagination integration tests r=bidoubiwa a=bidoubiwa

see #389 

Unit tests have also been added see: 
#387


Co-authored-by: Charlotte Vermandel <charlottevermandel@gmail.com>
  • Loading branch information
bors[bot] and bidoubiwa authored May 25, 2021
2 parents af194db + ed06520 commit 300b7bb
Show file tree
Hide file tree
Showing 6 changed files with 502 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ export type IMResponse = {
nbPages?: number
}

export type SearchResponse = IStypes.SearchResponse & IMResponse
export type SearchResponse = Omit<IStypes.SearchResponse, 'hits'> &
IStypes.SearchResponse &
IMResponse & {
hits: ISHits[]
}

export type InstantMeiliSearchContext = {
page: number
Expand Down
16 changes: 16 additions & 0 deletions tests/assets/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ const dataset = [
poster: 'https://image.tmdb.org/t/p/w500/6FfCtAuVAW8XJjZ7eWeLibRLWTw.jpg',
release_date: 233366400,
},
{
id: 30,
title: 'Magnetic Rose',
overview: '',
genres: ['Animation', 'Science Fiction'],
poster: 'https://image.tmdb.org/t/p/w500/gSuHDeWemA1menrwfMRChnSmMVN.jpg',
release_date: 819676800,
},
{
id: 24,
title: 'Kill Bill: Vol. 1',
overview: null,
genres: ['Action', 'Crime'],
poster: 'https://image.tmdb.org/t/p/w500/v7TaX8kXMXs5yFFGR41guUDNcnB.jpg',
release_date: 1065744000,
},
]

export type Movies = {
Expand Down
4 changes: 2 additions & 2 deletions tests/facetsDistribution.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('Instant MeiliSearch Browser test', () => {
},
},
])
expect(response.results[0].facets?.genres?.Action).toEqual(2)
expect(response.results[0].facets?.genres?.Action).toEqual(3)
})

test('Test non-existent facets on facetsDistribution', async () => {
Expand All @@ -54,6 +54,6 @@ describe('Instant MeiliSearch Browser test', () => {
},
},
])
expect(response.results[0].facets?.genres?.Action).toEqual(2)
expect(response.results[0].facets?.genres?.Action).toEqual(3)
})
})
197 changes: 197 additions & 0 deletions tests/highlight.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
import { searchClient, dataset } from './assets/utils'

describe('Highlight 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 one attributesToHighlight on placeholder', async () => {
const response = await searchClient.search([
{
indexName: 'movies',
params: {
query: '',
attributesToHighlight: ['name'],
},
},
])
const resKeys = Object.keys(response.results[0].hits[0]._highlightResult)
expect(resKeys).toEqual(expect.arrayContaining(Object.keys(dataset[0])))
})

test('Test no attributesToHighlight on placeholder', async () => {
const response = await searchClient.search([
{
indexName: 'movies',
params: {
query: '',
attributesToHighlight: [],
},
},
])
const resKeys = Object.keys(response.results[0].hits[0]._highlightResult)
expect(resKeys).toEqual(expect.arrayContaining(Object.keys(dataset[0])))
})

test('Test one attributesToHighlight on specific query', async () => {
const response = await searchClient.search([
{
indexName: 'movies',
params: {
query: 'Ar',
attributesToHighlight: ['title'],
},
},
])
//
const highlightedResults = response.results[0].hits[0]._highlightResult
const resKeys = Object.keys(highlightedResults)
expect(resKeys).toEqual(expect.arrayContaining(Object.keys(dataset[0])))
expect(highlightedResults.title?.value).toEqual(
'__ais-highlight__Ar__/ais-highlight__iel'
)
})

test('Test two attributesToHighlight on specific query', async () => {
const response = await searchClient.search([
{
indexName: 'movies',
params: {
query: 'S',
attributesToHighlight: ['title', 'overview'],
},
},
])

const highlightedResults = response.results[0].hits[0]._highlightResult
const resKeys = Object.keys(highlightedResults)
expect(resKeys).toEqual(expect.arrayContaining(Object.keys(dataset[0])))
expect(highlightedResults.title?.value).toEqual(
'__ais-highlight__S__/ais-highlight__tar Wars'
)
expect(highlightedResults.overview?.value).toEqual(
expect.stringMatching('__ais-highlight__S__/ais-highlight__kywalker')
)
})

test('Test two attributesToHighlight on specific query with empty string value', async () => {
const response = await searchClient.search([
{
indexName: 'movies',
params: {
query: 'Magnetic',
attributesToHighlight: ['title', 'overview'],
},
},
])

const highlightedResults = response.results[0].hits[0]._highlightResult
const resKeys = Object.keys(highlightedResults)
expect(resKeys).toEqual(expect.arrayContaining(Object.keys(dataset[0])))
expect(highlightedResults.title?.value).toEqual(
'__ais-highlight__Magnetic__/ais-highlight__ Rose'
)
expect(highlightedResults.overview?.value).toEqual(
expect.not.stringMatching('__ais-highlight__Magnetic__/ais-highlight__')
)
})

test('Test two attributesToHighlight on specific query with null value', async () => {
const response = await searchClient.search([
{
indexName: 'movies',
params: {
query: 'Magnetic',
attributesToHighlight: ['title', 'overview'],
},
},
])
const highlightedResults = response.results[0].hits[0]._highlightResult
const resKeys = Object.keys(highlightedResults)
expect(resKeys).toEqual(expect.arrayContaining(Object.keys(dataset[0])))
expect(highlightedResults.title?.value).toEqual(
'__ais-highlight__Magnetic__/ais-highlight__ Rose'
)
expect(highlightedResults.overview?.value).toEqual(
expect.not.stringMatching('__ais-highlight__Magnetic__/ais-highlight__')
)
})

test('Test two attributesToHighlight on specific query with null value', async () => {
const response = await searchClient.search([
{
indexName: 'movies',
params: {
query: 'Magnetic',
attributesToHighlight: ['title', 'overview'],
},
},
])
const highlightedResults = response.results[0].hits[0]._highlightResult
const resKeys = Object.keys(highlightedResults)
expect(resKeys).toEqual(expect.arrayContaining(Object.keys(dataset[0])))
expect(highlightedResults.title?.value).toEqual(
'__ais-highlight__Magnetic__/ais-highlight__ Rose'
)
expect(highlightedResults.overview?.value).toEqual(
expect.not.stringMatching('__ais-highlight__Magnetic__/ais-highlight__')
)
})

test('Test two attributesToHighlight on wild card', async () => {
const response = await searchClient.search([
{
indexName: 'movies',
params: {
query: 'S',
attributesToHighlight: ['title', 'overview'],
},
},
])

const highlightedResults = response.results[0].hits[0]._highlightResult
const resKeys = Object.keys(highlightedResults)
expect(resKeys).toEqual(expect.arrayContaining(Object.keys(dataset[0])))
expect(highlightedResults.title?.value).toEqual(
'__ais-highlight__S__/ais-highlight__tar Wars'
)
expect(highlightedResults.overview?.value).toEqual(
expect.stringMatching('__ais-highlight__S__/ais-highlight__kywalker')
)
})

test('Test two attributesToHighlight with different tags', async () => {
const response = await searchClient.search([
{
indexName: 'movies',
params: {
query: 'S',
attributesToHighlight: ['title', 'overview'],
highlightPreTag: '<p>',
highlightPostTag: '</p>',
},
},
])

const highlightedResults = response.results[0].hits[0]._highlightResult
const resKeys = Object.keys(highlightedResults)
expect(resKeys).toEqual(expect.arrayContaining(Object.keys(dataset[0])))
expect(highlightedResults.title?.value).toEqual('<p>S</p>tar Wars')
expect(highlightedResults.overview?.value).toEqual(
expect.stringMatching('<p>S</p>olo')
)
})
})
99 changes: 99 additions & 0 deletions tests/pagination.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { searchClient, dataset } from './assets/utils'

describe('Pagination 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 1 hitsPerPage', async () => {
const response = await searchClient.search([
{
indexName: 'movies',
params: {
query: '',
hitsPerPage: 1,
},
},
])
const hits = response.results[0].hits
expect(hits.length).toBe(1)
expect(hits[0].title).toBe('Ariel')
})

test('Test 1 hitsPerPage w/ page 0 ', async () => {
const response = await searchClient.search([
{
indexName: 'movies',
params: {
query: '',
hitsPerPage: 1,
page: 0,
},
},
])
const hits = response.results[0].hits
expect(hits.length).toBe(1)
expect(hits[0].title).toBe('Ariel')
})

test('Test 1 hitsPerPage w/ page 1 ', async () => {
const response = await searchClient.search([
{
indexName: 'movies',
params: {
query: '',
hitsPerPage: 1,
page: 1,
},
},
])
const hits = response.results[0].hits
expect(hits.length).toBe(1)
expect(hits[0].title).toBe('Four Rooms')
})

test('Test 100 hitsPerPage w/ page 1 ', async () => {
const response = await searchClient.search([
{
indexName: 'movies',
params: {
query: '',
hitsPerPage: 100,
page: 1,
},
},
])
const hits = response.results[0].hits
expect(hits.length).toBe(0)
expect(hits).toEqual([])
})

test('Test 0 hitsPerPage w/ page 0 ', async () => {
const response = await searchClient.search([
{
indexName: 'movies',
params: {
query: '',
hitsPerPage: 0,
page: 0,
},
},
])
const hits = response.results[0].hits
expect(hits.length).toBe(0)
expect(hits).toEqual([])
})
})
Loading

0 comments on commit 300b7bb

Please sign in to comment.