Skip to content

Commit 1cee7e0

Browse files
test(algolia): test Algolia presets
1 parent 0ca8a2a commit 1cee7e0

File tree

5 files changed

+327
-18
lines changed

5 files changed

+327
-18
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module.exports = {
22
extends: ['algolia', 'algolia/jest', 'algolia/react', 'algolia/typescript'],
33
globals: {
44
__DEV__: false,
5+
__VERSION__: false,
56
},
67
settings: {
78
react: {

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ module.exports = {
1010
],
1111
globals: {
1212
__DEV__: true,
13+
__VERSION__: 'version-test',
1314
},
1415
};
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import {
2+
highlightAlgoliaHit,
3+
reverseHighlightAlgoliaHit,
4+
snippetAlgoliaHit,
5+
} from '../formatting';
6+
7+
describe('highlight', () => {
8+
describe('highlightAlgoliaHit', () => {
9+
test('returns the highlighted value of the hit', () => {
10+
expect(
11+
highlightAlgoliaHit({
12+
attribute: 'title',
13+
hit: {
14+
_highlightResult: {
15+
title: {
16+
value: '<mark>He</mark>llo t<mark>he</mark>re',
17+
},
18+
},
19+
},
20+
})
21+
).toEqual('<mark>He</mark>llo t<mark>he</mark>re');
22+
});
23+
24+
test('allows custom tags', () => {
25+
expect(
26+
highlightAlgoliaHit({
27+
highlightPreTag: '<em>',
28+
highlightPostTag: '</em>',
29+
attribute: 'title',
30+
hit: {
31+
_highlightResult: {
32+
title: {
33+
value: '<em>He</em>llo t<em>he</em>re',
34+
},
35+
},
36+
},
37+
})
38+
).toEqual('<em>He</em>llo t<em>he</em>re');
39+
});
40+
});
41+
42+
describe('reverseHighlightAlgoliaHit', () => {
43+
test('returns the reverse-highlighted value of the hit', () => {
44+
expect(
45+
reverseHighlightAlgoliaHit({
46+
attribute: 'title',
47+
hit: {
48+
_highlightResult: {
49+
title: {
50+
value: '<mark>He</mark>llo t<mark>he</mark>re',
51+
},
52+
},
53+
},
54+
})
55+
).toEqual('He<mark>llo t</mark>he<mark>re</mark>');
56+
});
57+
58+
test('allows custom tags', () => {
59+
expect(
60+
reverseHighlightAlgoliaHit({
61+
highlightPreTag: '<em>',
62+
highlightPostTag: '</em>',
63+
attribute: 'title',
64+
hit: {
65+
_highlightResult: {
66+
title: {
67+
value: '<em>He</em>llo t<em>he</em>re',
68+
},
69+
},
70+
},
71+
})
72+
).toEqual('He<em>llo t</em>he<em>re</em>');
73+
});
74+
75+
test('returns the non-highlighted value when every part matches', () => {
76+
expect(
77+
reverseHighlightAlgoliaHit({
78+
attribute: 'title',
79+
hit: { _highlightResult: { title: { value: 'Hello' } } },
80+
})
81+
).toEqual('Hello');
82+
});
83+
});
84+
85+
describe('snippetAlgoliaHit', () => {
86+
test('returns the highlighted snippet value of the hit', () => {
87+
expect(
88+
snippetAlgoliaHit({
89+
attribute: 'title',
90+
hit: {
91+
_snippetResult: {
92+
title: {
93+
value: '<mark>He</mark>llo t<mark>he</mark>re',
94+
},
95+
},
96+
},
97+
})
98+
).toEqual('<mark>He</mark>llo t<mark>he</mark>re');
99+
});
100+
101+
test('allows custom tags', () => {
102+
expect(
103+
snippetAlgoliaHit({
104+
highlightPreTag: '<em>',
105+
highlightPostTag: '</em>',
106+
attribute: 'title',
107+
hit: {
108+
_snippetResult: {
109+
title: {
110+
value: '<em>He</em>llo t<em>he</em>re',
111+
},
112+
},
113+
},
114+
})
115+
).toEqual('<em>He</em>llo t<em>he</em>re');
116+
});
117+
});
118+
});
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
import { getAlgoliaResults, getAlgoliaHits, Client } from '../results';
2+
3+
function createSearchClient() {
4+
return {
5+
search: jest.fn(() =>
6+
Promise.resolve({
7+
results: [
8+
{ hits: [{ label: 'Hit 1' }] },
9+
{ hits: [{ label: 'Hit 2' }] },
10+
],
11+
})
12+
),
13+
};
14+
}
15+
16+
describe('getAlgoliaResults', () => {
17+
test('with default options', async () => {
18+
const searchClient = createSearchClient();
19+
20+
const results = await getAlgoliaResults({
21+
searchClient,
22+
queries: [
23+
{
24+
indexName: 'indexName',
25+
query: 'query',
26+
},
27+
],
28+
});
29+
30+
expect(searchClient.search).toHaveBeenCalledTimes(1);
31+
expect(searchClient.search).toHaveBeenCalledWith([
32+
{
33+
indexName: 'indexName',
34+
query: 'query',
35+
params: {
36+
hitsPerPage: 5,
37+
highlightPreTag: '<mark>',
38+
highlightPostTag: '</mark>',
39+
},
40+
},
41+
]);
42+
expect(results).toEqual([
43+
{ hits: [{ label: 'Hit 1' }] },
44+
{ hits: [{ label: 'Hit 2' }] },
45+
]);
46+
});
47+
48+
test('with custom search parameters', async () => {
49+
const searchClient = createSearchClient();
50+
51+
const results = await getAlgoliaResults({
52+
searchClient,
53+
queries: [
54+
{
55+
indexName: 'indexName',
56+
query: 'query',
57+
params: {
58+
hitsPerPage: 10,
59+
highlightPreTag: '<em>',
60+
highlightPostTag: '</em>',
61+
page: 2,
62+
},
63+
},
64+
],
65+
});
66+
67+
expect(searchClient.search).toHaveBeenCalledTimes(1);
68+
expect(searchClient.search).toHaveBeenCalledWith([
69+
{
70+
indexName: 'indexName',
71+
query: 'query',
72+
params: {
73+
hitsPerPage: 10,
74+
highlightPreTag: '<em>',
75+
highlightPostTag: '</em>',
76+
page: 2,
77+
},
78+
},
79+
]);
80+
expect(results).toEqual([
81+
{ hits: [{ label: 'Hit 1' }] },
82+
{ hits: [{ label: 'Hit 2' }] },
83+
]);
84+
});
85+
86+
test('attaches Algolia agent', async () => {
87+
const searchClient = createSearchClient();
88+
(searchClient as Client).addAlgoliaAgent = jest.fn();
89+
90+
await getAlgoliaResults({
91+
searchClient,
92+
queries: [
93+
{
94+
indexName: 'indexName',
95+
query: 'query',
96+
params: {
97+
hitsPerPage: 10,
98+
highlightPreTag: '<em>',
99+
highlightPostTag: '</em>',
100+
page: 2,
101+
},
102+
},
103+
],
104+
});
105+
106+
expect((searchClient as Client).addAlgoliaAgent).toHaveBeenCalledTimes(1);
107+
expect((searchClient as Client).addAlgoliaAgent).toHaveBeenCalledWith(
108+
'Autocomplete.js (version-test)'
109+
);
110+
});
111+
});
112+
113+
describe('getAlgoliaHits', () => {
114+
test('with default options', async () => {
115+
const searchClient = createSearchClient();
116+
117+
const hits = await getAlgoliaHits({
118+
searchClient,
119+
queries: [
120+
{
121+
indexName: 'indexName',
122+
query: 'query',
123+
},
124+
],
125+
});
126+
127+
expect(searchClient.search).toHaveBeenCalledTimes(1);
128+
expect(searchClient.search).toHaveBeenCalledWith([
129+
{
130+
indexName: 'indexName',
131+
query: 'query',
132+
params: {
133+
hitsPerPage: 5,
134+
highlightPreTag: '<mark>',
135+
highlightPostTag: '</mark>',
136+
},
137+
},
138+
]);
139+
expect(hits).toEqual([{ label: 'Hit 1' }, { label: 'Hit 2' }]);
140+
});
141+
142+
test('with custom search parameters', async () => {
143+
const searchClient = createSearchClient();
144+
145+
const hits = await getAlgoliaHits({
146+
searchClient,
147+
queries: [
148+
{
149+
indexName: 'indexName',
150+
query: 'query',
151+
params: {
152+
hitsPerPage: 10,
153+
highlightPreTag: '<em>',
154+
highlightPostTag: '</em>',
155+
page: 2,
156+
},
157+
},
158+
],
159+
});
160+
161+
expect(searchClient.search).toHaveBeenCalledTimes(1);
162+
expect(searchClient.search).toHaveBeenCalledWith([
163+
{
164+
indexName: 'indexName',
165+
query: 'query',
166+
params: {
167+
hitsPerPage: 10,
168+
highlightPreTag: '<em>',
169+
highlightPostTag: '</em>',
170+
page: 2,
171+
},
172+
},
173+
]);
174+
expect(hits).toEqual([{ label: 'Hit 1' }, { label: 'Hit 2' }]);
175+
});
176+
177+
test('attaches Algolia agent', async () => {
178+
const searchClient = createSearchClient();
179+
(searchClient as Client).addAlgoliaAgent = jest.fn();
180+
181+
await getAlgoliaHits({
182+
searchClient,
183+
queries: [
184+
{
185+
indexName: 'indexName',
186+
query: 'query',
187+
params: {
188+
hitsPerPage: 10,
189+
highlightPreTag: '<em>',
190+
highlightPostTag: '</em>',
191+
page: 2,
192+
},
193+
},
194+
],
195+
});
196+
197+
expect((searchClient as Client).addAlgoliaAgent).toHaveBeenCalledTimes(1);
198+
expect((searchClient as Client).addAlgoliaAgent).toHaveBeenCalledWith(
199+
'Autocomplete.js (version-test)'
200+
);
201+
});
202+
});

packages/autocomplete-presets/src/results.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { flatten } from './utils';
22

33
type SearchClient = any;
4-
type Client = any;
4+
export type Client = any;
55
type SearchResponse = any;
66
type QueryParameters = any;
77

@@ -16,20 +16,11 @@ interface GetAlgoliaSourceParams {
1616
queries: SearchParameters[];
1717
}
1818

19-
export function getAlgoliaSource({
20-
searchClient,
21-
queries,
22-
}: GetAlgoliaSourceParams) {
19+
function getAlgoliaSource({ searchClient, queries }: GetAlgoliaSourceParams) {
2320
if (typeof (searchClient as Client).addAlgoliaAgent === 'function') {
24-
if (__DEV__) {
25-
(searchClient as Client).addAlgoliaAgent(
26-
`autocomplete.js (${__VERSION__}-development)`
27-
);
28-
} else {
29-
(searchClient as Client).addAlgoliaAgent(
30-
`autocomplete.js (${__VERSION__})`
31-
);
32-
}
21+
(searchClient as Client).addAlgoliaAgent(
22+
`Autocomplete.js (${__VERSION__})`
23+
);
3324
}
3425

3526
return searchClient.search(
@@ -66,10 +57,6 @@ export function getAlgoliaHits({
6657
return getAlgoliaSource({ searchClient, queries }).then(response => {
6758
const results = response.results;
6859

69-
if (!results) {
70-
return [];
71-
}
72-
7360
// @TODO: should `getAlgoliaHits` flatten the hits?
7461
return flatten(results.map(result => result.hits));
7562
});

0 commit comments

Comments
 (0)