File size: 2,627 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { render } from '@testing-library/react';
import React from 'react';
import {
  DynamicWidgets as CoreDynamicWidgets,
  InstantSearch,
} from 'react-instantsearch-core';
import DynamicWidgets from '../DynamicWidgets';

jest.mock('react-instantsearch-core', () => {
  const original = jest.requireActual('react-instantsearch-core');
  return {
    ...original,
    DynamicWidgets: jest.fn(() => null),
  };
});

const EMPTY_RESPONSE = {
  results: [
    {
      hits: [],
      nbHits: 0,
      page: 0,
      nbPages: 0,
      hitsPerPage: 20,
      exhaustiveNbHits: true,
      query: '',
      queryAfterRemoval: '',
      params:
        'highlightPreTag=%3Cais-highlight-0000000000%3E&highlightPostTag=%3C%2Fais-highlight-0000000000%3E&query=&facets=%5B%5D&tagFilters=',
      index: 'instant_search',
      processingTimeMS: 2,
    },
  ],
};

const createSearchClient = () => ({
  search: jest.fn(() => Promise.resolve(EMPTY_RESPONSE)),
  searchForFacetValues: jest.fn(() => Promise.resolve({})),
});

beforeEach(() => {
  jest.clearAllMocks();
});

describe('DynamicWidgets', () => {
  it('sets a className', () => {
    const searchClient = createSearchClient();

    const { container } = render(
      <InstantSearch searchClient={searchClient} indexName="test">
        <DynamicWidgets></DynamicWidgets>
      </InstantSearch>
    );

    expect(container).toMatchInlineSnapshot(`
      <div>
        <div
          class="ais-DynamicWidgets"
        />
      </div>
    `);
  });

  it('allows a custom className', () => {
    const searchClient = createSearchClient();

    const { container } = render(
      <InstantSearch searchClient={searchClient} indexName="test">
        <DynamicWidgets className="extra"></DynamicWidgets>
      </InstantSearch>
    );

    expect(container).toMatchInlineSnapshot(`
      <div>
        <div
          class="ais-DynamicWidgets extra"
        />
      </div>
    `);
  });

  it('forwards props to core widget', () => {
    const searchClient = createSearchClient();

    const children = [<div key={1}>testing</div>, <div key={2}>testing2</div>];

    const transformItems = () => {};

    render(
      <InstantSearch searchClient={searchClient} indexName="test">
        <DynamicWidgets
          className="extra"
          transformItems={transformItems}
          unknownOption
        >
          {children}
        </DynamicWidgets>
      </InstantSearch>
    );

    expect(CoreDynamicWidgets).toHaveBeenCalledTimes(1);
    expect(CoreDynamicWidgets).toHaveBeenCalledWith(
      { children, transformItems, unknownOption: true },
      {}
    );
  });
});