File size: 9,568 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import React from 'react';
import renderer from 'react-test-renderer';
import Enzyme, { shallow, mount } from 'enzyme';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
import SearchBox from '../SearchBox';

Enzyme.configure({ adapter: new Adapter() });

describe('SearchBox', () => {
  it('applies its default props', () => {
    const instance = renderer.create(<SearchBox refine={() => null} />);

    expect(instance.toJSON()).toMatchSnapshot();

    instance.unmount();
  });

  it('applies its default props with custom className', () => {
    const instance = renderer.create(
      <SearchBox className="MyCustomSearchBox" refine={() => null} />
    );

    expect(instance.toJSON()).toMatchSnapshot();

    instance.unmount();
  });

  it('applies its default props with custom inputId', () => {
    const inputId = 'search-box';
    const wrapper = mount(<SearchBox inputId={inputId} refine={() => null} />);

    const input = wrapper.find('input').getDOMNode();
    expect(input.getAttribute('id')).toEqual(inputId);
  });

  it('transfers the autoFocus prop to the underlying input element', () => {
    const instance = renderer.create(
      <SearchBox refine={() => null} autoFocus />
    );

    expect(instance.toJSON()).toMatchSnapshot();

    instance.unmount();
  });

  it('treats its query prop as its input value', () => {
    const instance = renderer.create(
      <SearchBox refine={() => null} currentRefinement="QUERY1" />
    );

    expect(instance.toJSON()).toMatchSnapshot();

    instance.update(
      <SearchBox refine={() => null} currentRefinement="QUERY2" />
    );

    expect(instance.toJSON()).toMatchSnapshot();

    instance.unmount();
  });

  it('lets you customize its theme', () => {
    const instance = renderer.create(
      <SearchBox
        refine={() => null}
        theme={{
          root: 'ROOT',
          wrapper: 'WRAPPER',
          input: 'INPUT',
          submit: 'SUBMIT',
          reset: 'RESET',
        }}
      />
    );

    expect(instance.toJSON()).toMatchSnapshot();

    instance.unmount();
  });

  it('lets you give custom components for reset and submit', () => {
    const instance = renderer.create(
      <SearchBox
        refine={() => null}
        submit={<span>🔍</span>}
        reset={
          <svg viewBox="200 198 108 122">
            <path d="M200.8 220l45 46.7-20 47.4 31.7-34 50.4 39.3-34.3-52.6 30.2-68.3-49.7 51.7" />
          </svg>
        }
      />
    );

    expect(instance.toJSON()).toMatchSnapshot();

    instance.unmount();
  });

  it('lets you customize its translations', () => {
    const instance = renderer.create(
      <SearchBox
        refine={() => null}
        translations={{
          resetTitle: 'RESET_TITLE',
          placeholder: 'PLACEHOLDER',
        }}
      />
    );

    expect(instance.toJSON()).toMatchSnapshot();

    instance.unmount();
  });

  it('treats query as a default value when searchAsYouType=false', () => {
    const wrapper = mount(
      <SearchBox
        refine={() => null}
        currentRefinement="QUERY1"
        searchAsYouType={false}
      />
    );

    expect(wrapper.find('input').props().value).toBe('QUERY1');

    wrapper.find('input').simulate('change', { target: { value: 'QUERY2' } });

    expect(wrapper.find('input').props().value).toBe('QUERY2');

    wrapper.unmount();
  });

  it('refines its value on change when searchAsYouType=true', () => {
    const refine = jest.fn();
    const wrapper = mount(<SearchBox searchAsYouType refine={refine} />);

    wrapper.find('input').simulate('change', { target: { value: 'hello' } });

    expect(refine.mock.calls).toHaveLength(1);
    expect(refine.mock.calls[0][0]).toBe('hello');

    wrapper.unmount();
  });

  it('only refines its query on submit when searchAsYouType=false', () => {
    const refine = jest.fn();
    const wrapper = mount(
      <SearchBox searchAsYouType={false} refine={refine} />
    );

    wrapper.find('input').simulate('change', { target: { value: 'hello' } });

    expect(refine.mock.calls).toHaveLength(0);

    wrapper.find('form').simulate('submit');

    expect(refine.mock.calls).toHaveLength(1);
    expect(refine.mock.calls[0][0]).toBe('hello');

    wrapper.unmount();
  });

  it('onSubmit behavior should be override if provided as props', () => {
    const onSubmit = jest.fn();
    const refine = jest.fn();
    const wrapper = mount(
      <SearchBox searchAsYouType={false} onSubmit={onSubmit} refine={refine} />
    );

    wrapper.find('form').simulate('submit');

    expect(onSubmit.mock.calls).toHaveLength(1);
    expect(refine.mock.calls).toHaveLength(0);

    wrapper.unmount();
  });

  it('focuses the input when one of the keys in focusShortcuts is pressed', () => {
    let input;

    const wrapper = mount(
      <SearchBox
        refine={() => null}
        focusShortcuts={['s', 84]}
        inputRef={(node) => {
          input = node;
        }}
      />
    );

    input.focus = jest.fn();

    const event1 = new KeyboardEvent('keydown', { keyCode: 82 });
    document.dispatchEvent(event1);
    expect(input.focus.mock.calls).toHaveLength(0);

    const event2 = new KeyboardEvent('keydown', { keyCode: 83 });
    document.dispatchEvent(event2);
    expect(input.focus.mock.calls).toHaveLength(1);

    const event3 = new KeyboardEvent('keydown', { keyCode: 84 });
    document.dispatchEvent(event3);
    expect(input.focus.mock.calls).toHaveLength(2);

    wrapper.unmount();
  });

  it('should accept `onXXX` events', () => {
    const onSubmit = jest.fn();
    const onReset = jest.fn();

    const inputEventsList = [
      'onChange',
      'onFocus',
      'onBlur',
      'onSelect',
      'onKeyDown',
      'onKeyPress',
    ];

    const inputProps = inputEventsList.reduce(
      (props, prop) => ({ ...props, [prop]: jest.fn() }),
      {}
    );

    const wrapper = mount(
      <SearchBox
        refine={() => null}
        onSubmit={onSubmit}
        onReset={onReset}
        {...inputProps}
      />
    );

    // simulate form events `onReset` && `onSubmit`
    wrapper.find('form').simulate('submit');
    expect(onSubmit).toHaveBeenCalled();

    wrapper.find('form').simulate('reset');
    expect(onReset).toHaveBeenCalled();

    // simulate input search events
    inputEventsList.forEach((eventName) => {
      wrapper
        .find('input')
        .simulate(eventName.replace(/^on/, '').toLowerCase());

      expect(inputProps[eventName]).toHaveBeenCalled();
    });

    wrapper.unmount();
  });

  it('should render the loader if showLoadingIndicator is true', () => {
    const instanceWithoutLoadingIndicator = renderer.create(
      <SearchBox refine={() => null} showLoadingIndicator />
    );

    expect(instanceWithoutLoadingIndicator.toJSON()).toMatchSnapshot();

    const instanceWithLoadingIndicator = renderer.create(
      <SearchBox refine={() => null} showLoadingIndicator isSearchStalled />
    );

    expect(instanceWithLoadingIndicator.toJSON()).toMatchSnapshot();

    instanceWithoutLoadingIndicator.unmount();
    instanceWithLoadingIndicator.unmount();
  });

  it('expect to clear the query when the form is reset with searchAsYouType=true', () => {
    const refine = jest.fn();

    const wrapper = shallow(
      <SearchBox refine={refine} searchAsYouType />
    ).dive();

    // Simulate the ref
    wrapper.instance().input = { focus: jest.fn() };

    wrapper.find('form').simulate('reset');

    expect(refine).toHaveBeenCalledWith('');
    expect(wrapper.instance().input.focus).toHaveBeenCalled();
  });

  it('expect to clear the query when the form is reset with searchAsYouType=false', () => {
    const refine = jest.fn();

    const wrapper = shallow(
      <SearchBox refine={refine} searchAsYouType={false} />
    ).dive();

    // Simulate the ref
    wrapper.instance().input = { focus: jest.fn() };

    // Simulate change event
    wrapper.setState({ query: 'Hello' });

    wrapper.find('form').simulate('reset');

    expect(refine).toHaveBeenCalledWith('');
    expect(wrapper.instance().input.focus).toHaveBeenCalled();
    expect(wrapper.state()).toEqual({ query: '' });
  });

  it('should point created refs to its input element', () => {
    const inputRef = React.createRef();
    const wrapper = mount(
      <SearchBox refine={() => null} inputRef={inputRef} />
    );

    const refSpy = jest.spyOn(inputRef.current, 'focus');

    // Trigger input.focus()
    wrapper.find('form').simulate('reset');

    expect(refSpy).toHaveBeenCalled();
    expect(inputRef.current.tagName).toEqual('INPUT');
  });

  it('should return a ref when given a callback as input ref', () => {
    let inputRef;
    const wrapper = mount(
      <SearchBox
        refine={() => null}
        inputRef={(ref) => {
          inputRef = ref;
        }}
      />
    );

    const refSpy = jest.spyOn(inputRef, 'focus');

    // Trigger input.focus()
    wrapper.find('form').simulate('reset');

    expect(refSpy).toHaveBeenCalled();
    expect(inputRef.tagName).toEqual('INPUT');
  });

  it('should point created refs to its form element', () => {
    const formRef = React.createRef();
    mount(<SearchBox refine={() => null} formRef={formRef} />);

    expect(formRef.current.tagName).toEqual('FORM');
  });

  it('should return a ref when given a callback as form ref', () => {
    let formRef;
    mount(
      <SearchBox
        refine={() => null}
        formRef={(ref) => {
          formRef = ref;
        }}
      />
    );

    expect(formRef.tagName).toEqual('FORM');
  });
});