File size: 2,801 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import React, { createRef } from 'react';

import { InstantSearch } from '../../packages/react-instantsearch-hooks/src/components/InstantSearch';
import { IndexContext } from '../../packages/react-instantsearch-hooks/src/lib/IndexContext';
import { InstantSearchContext } from '../../packages/react-instantsearch-hooks/src/lib/InstantSearchContext';
import type { InstantSearchProps } from '../../packages/react-instantsearch-hooks/src';
import type { InstantSearch as InstantSearchType } from 'instantsearch.js';
import type { IndexWidget } from 'instantsearch.js/es/widgets/index/index';

export function createInstantSearchSpy() {
  const searchContext = createRef<InstantSearchType>();
  const indexContext = createRef<IndexWidget>();
  const isSpyAttachedRef = createRef<boolean>();

  function InstantSearchSpy({ children, ...props }: InstantSearchProps) {
    return (
      <InstantSearch {...props}>
        <InstantSearchContext.Consumer>
          {(searchContextValue) => {
            if (!isSpyAttachedRef.current) {
              searchContextValue!.start = jest.fn(
                searchContextValue!.start.bind(searchContextValue)
              );
              searchContextValue!.dispose = jest.fn(
                searchContextValue!.dispose.bind(searchContextValue)
              );
            }

            // @ts-ignore `React.RefObject` is typed as immutable
            searchContext.current = searchContextValue;

            return (
              <InstantSearchContext.Provider value={searchContext.current}>
                <IndexContext.Consumer>
                  {(indexContextValue) => {
                    if (!isSpyAttachedRef.current) {
                      indexContextValue!.addWidgets = jest.fn(
                        indexContextValue!.addWidgets.bind(indexContextValue)
                      );
                      indexContextValue!.removeWidgets = jest.fn(
                        indexContextValue!.removeWidgets.bind(indexContextValue)
                      );
                    }

                    // @ts-ignore `React.RefObject` is typed as immutable
                    indexContext.current = indexContextValue;
                    // @ts-ignore `React.RefObject` is typed as immutable
                    isSpyAttachedRef.current = true;

                    return (
                      <IndexContext.Provider value={indexContext.current}>
                        {children}
                      </IndexContext.Provider>
                    );
                  }}
                </IndexContext.Consumer>
              </InstantSearchContext.Provider>
            );
          }}
        </InstantSearchContext.Consumer>
      </InstantSearch>
    );
  }

  return {
    InstantSearchSpy,
    searchContext,
    indexContext,
  };
}