1
1
import { Location } from '@reach/router' ;
2
2
import type { WindowLocation } from '@reach/router' ;
3
3
import type { SearchOptions } from 'instantsearch.js' ;
4
- import algoliasearch from 'algoliasearch/lite' ;
4
+ import algoliasearch , { type SearchClient } from 'algoliasearch/lite' ;
5
5
import React , { useEffect , useRef } from 'react' ;
6
6
import type { ReactNode } from 'react' ;
7
7
import { connect } from 'react-redux' ;
@@ -18,31 +18,32 @@ import {
18
18
updateSearchQuery
19
19
} from './redux' ;
20
20
21
+ const mockSearchClient = {
22
+ // When Algolia is not configured, the client will still render,
23
+ // the result query is returned to the search component as a mock
24
+ //(mainly for testing without relying on Playwright fuffill route as
25
+ // there is no request made without a key).
26
+ search : ( request : Array < { indexName : string ; params : SearchOptions } > ) => {
27
+ return Promise . resolve ( {
28
+ results : [
29
+ {
30
+ hits : [ ] ,
31
+ query : request [ 0 ] . params ?. query === 'test' ? 'test' : '' ,
32
+ params :
33
+ 'highlightPostTag=__%2Fais-highlight__&highlightPreTag=__ais-highlight__&hitsPerPage=5&query=sdefpuhsdfpiouhdsfgp' ,
34
+ index : 'news'
35
+ }
36
+ ]
37
+ } ) ;
38
+ }
39
+ // TODO: mock this in the tests.
40
+ } as unknown as SearchClient ;
41
+
21
42
// If a key is missing, searches will fail, but the client will still render.
22
43
const searchClient =
23
44
algoliaAppId && algoliaAPIKey
24
45
? algoliasearch ( algoliaAppId , algoliaAPIKey )
25
- : {
26
- // When Algolia is not configured, the client will still render,
27
- // the result query is returned to the search component as a mock
28
- //(mainly for testing without relying on Playwright fuffill route as
29
- // there is no request made without a key).
30
- search : (
31
- request : Array < { indexName : string ; params : SearchOptions } >
32
- ) => {
33
- return Promise . resolve ( {
34
- results : [
35
- {
36
- hits : [ ] ,
37
- query : request [ 0 ] . params ?. query === 'test' ? 'test' : '' ,
38
- params :
39
- 'highlightPostTag=__%2Fais-highlight__&highlightPreTag=__ais-highlight__&hitsPerPage=5&query=sdefpuhsdfpiouhdsfgp' ,
40
- index : 'news'
41
- }
42
- ]
43
- } ) ;
44
- }
45
- } ;
46
+ : mockSearchClient ;
46
47
47
48
const mapStateToProps = createSelector (
48
49
searchQuerySelector ,
0 commit comments