File size: 10,416 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
364
365
366
367
import { act, render, waitFor, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';

import { InstantSearchHooksTestWrapper } from '../../../../../test/utils';
import { SearchBox } from '../SearchBox';

import type { UiState } from 'instantsearch.js';

describe('SearchBox', () => {
  test('renders with default props', async () => {
    const { container } = render(
      <InstantSearchHooksTestWrapper>
        <SearchBox />
      </InstantSearchHooksTestWrapper>
    );

    await waitFor(() => {
      expect(container.querySelector('.ais-SearchBox')).toMatchInlineSnapshot(`
        <div
          class="ais-SearchBox"
        >
          <form
            action=""
            class="ais-SearchBox-form"
            novalidate=""
          >
            <input
              autocapitalize="off"
              autocomplete="off"
              autocorrect="off"
              class="ais-SearchBox-input"
              maxlength="512"
              spellcheck="false"
              type="search"
              value=""
            />
            <button
              class="ais-SearchBox-submit"
              title="Submit the search query"
              type="submit"
            >
              <svg
                class="ais-SearchBox-submitIcon"
                height="10"
                viewBox="0 0 40 40"
                width="10"
              >
                <path
                  d="M26.804 29.01c-2.832 2.34-6.465 3.746-10.426 3.746C7.333 32.756 0 25.424 0 16.378 0 7.333 7.333 0 16.378 0c9.046 0 16.378 7.333 16.378 16.378 0 3.96-1.406 7.594-3.746 10.426l10.534 10.534c.607.607.61 1.59-.004 2.202-.61.61-1.597.61-2.202.004L26.804 29.01zm-10.426.627c7.323 0 13.26-5.936 13.26-13.26 0-7.32-5.937-13.257-13.26-13.257C9.056 3.12 3.12 9.056 3.12 16.378c0 7.323 5.936 13.26 13.258 13.26z"
                />
              </svg>
            </button>
            <button
              class="ais-SearchBox-reset"
              hidden=""
              title="Clear the search query"
              type="reset"
            >
              <svg
                class="ais-SearchBox-resetIcon"
                height="10"
                viewBox="0 0 20 20"
                width="10"
              >
                <path
                  d="M8.114 10L.944 2.83 0 1.885 1.886 0l.943.943L10 8.113l7.17-7.17.944-.943L20 1.886l-.943.943-7.17 7.17 7.17 7.17.943.944L18.114 20l-.943-.943-7.17-7.17-7.17 7.17-.944.943L0 18.114l.943-.943L8.113 10z"
                />
              </svg>
            </button>
            <span
              class="ais-SearchBox-loadingIndicator"
              hidden=""
            >
              <svg
                class="ais-SearchBox-loadingIcon"
                height="16"
                stroke="#444"
                viewBox="0 0 38 38"
                width="16"
              >
                <g
                  fill="none"
                  fill-rule="evenodd"
                >
                  <g
                    stroke-width="2"
                    transform="translate(1 1)"
                  >
                    <circle
                      cx="18"
                      cy="18"
                      r="18"
                      stroke-opacity=".5"
                    />
                    <path
                      d="M36 18c0-9.94-8.06-18-18-18"
                    >
                      <animatetransform
                        attributeName="transform"
                        dur="1s"
                        from="0 18 18"
                        repeatCount="indefinite"
                        to="360 18 18"
                        type="rotate"
                      />
                    </path>
                  </g>
                </g>
              </svg>
            </span>
          </form>
        </div>
      `);
    });

    expect(within(container).getByRole('searchbox')).not.toHaveFocus();
  });

  test('forwards placeholder prop', async () => {
    const { container } = render(
      <InstantSearchHooksTestWrapper>
        <SearchBox placeholder="Placeholder" />
      </InstantSearchHooksTestWrapper>
    );

    await waitFor(() => {
      expect(container.querySelector('.ais-SearchBox-input')).toHaveAttribute(
        'placeholder',
        'Placeholder'
      );
    });
  });

  test('forwards `autoFocus` prop', async () => {
    const { container } = render(
      <InstantSearchHooksTestWrapper>
        <SearchBox autoFocus />
      </InstantSearchHooksTestWrapper>
    );

    await waitFor(() => {
      expect(within(container).getByRole('searchbox')).toHaveFocus();
    });
  });

  test('sets query on change event', async () => {
    let lastUiState: UiState = {};

    const { container } = render(
      <InstantSearchHooksTestWrapper
        initialUiState={{
          indexName: {
            query: 'something',
          },
        }}
        onStateChange={({ uiState }) => {
          lastUiState = uiState;
        }}
      >
        <SearchBox />
      </InstantSearchHooksTestWrapper>
    );

    const input = container.querySelector<HTMLInputElement>(
      '.ais-SearchBox-input'
    )!;
    input.focus();

    await waitFor(() => {
      expect(input).toHaveValue('something');
    });

    ' else'.split('').forEach((letter) => {
      act(() => {
        userEvent.type(input, letter);
      });
    });

    await waitFor(() => {
      expect(lastUiState.indexName.query).toEqual('something else');
      expect(input).toHaveValue('something else');
    });
  });

  test('resets query on reset event', async () => {
    let lastUiState: UiState = {};

    const { container } = render(
      <InstantSearchHooksTestWrapper
        initialUiState={{
          indexName: {
            query: 'something',
          },
        }}
        onStateChange={({ uiState }) => {
          lastUiState = uiState;
        }}
      >
        <SearchBox />
      </InstantSearchHooksTestWrapper>
    );

    const resetButton = container.querySelector<HTMLButtonElement>(
      '.ais-SearchBox-reset'
    )!;
    const input = container.querySelector<HTMLInputElement>(
      '.ais-SearchBox-input'
    )!;

    await waitFor(() => {
      expect(input.value).toEqual('something');
    });

    act(() => {
      userEvent.click(resetButton);
    });

    await waitFor(() => {
      expect(lastUiState.indexName.query).toEqual(undefined);
    });
  });

  test('restore InstantSearch query when out of sync on input is blurred', async () => {
    let localSetUiState;

    const { container } = render(
      <InstantSearchHooksTestWrapper
        onStateChange={({ setUiState }) => {
          localSetUiState = setUiState;
        }}
      >
        <SearchBox />
      </InstantSearchHooksTestWrapper>
    );
    const input = container.querySelector<HTMLInputElement>(
      '.ais-SearchBox-input'
    )!;
    input.focus();

    act(() => {
      // Trigger a change to get the `setUiState` reference
      userEvent.type(input, 'q');
    });

    act(() => {
      input.blur();
      localSetUiState({ indexName: { query: 'new query' } });
    });

    await waitFor(() => {
      expect(input).toHaveValue('new query');
    });
  });

  test('forwards custom class names and `div` props to the root element', () => {
    const { container } = render(
      <InstantSearchHooksTestWrapper>
        <SearchBox
          className="MySearchBox"
          classNames={{ root: 'ROOT' }}
          title="Some custom title"
        />
      </InstantSearchHooksTestWrapper>
    );

    const root = container.firstChild;
    expect(root).toHaveClass('MySearchBox', 'ROOT');
    expect(root).toHaveAttribute('title', 'Some custom title');
  });

  test('does not refine query on type when searchAsYouType is false', () => {
    let lastUiState: UiState = {};

    const { container } = render(
      <InstantSearchHooksTestWrapper
        onStateChange={({ uiState }) => {
          lastUiState = uiState;
        }}
      >
        <SearchBox searchAsYouType={false} />
      </InstantSearchHooksTestWrapper>
    );

    const input = container.querySelector<HTMLInputElement>(
      '.ais-SearchBox-input'
    )!;
    input.focus();

    userEvent.type(input, 'iPhone');

    expect(input).toHaveValue('iPhone');
    expect(lastUiState.indexName?.query).toBe(undefined);
  });

  test('refines on submit when searchAsYouType is false, even if custom onSubmit is provided', () => {
    const onSubmit = jest.fn();
    let lastUiState: UiState = {};

    const { container } = render(
      <InstantSearchHooksTestWrapper
        onStateChange={({ uiState }) => {
          lastUiState = uiState;
        }}
      >
        <SearchBox searchAsYouType={false} onSubmit={onSubmit} />
      </InstantSearchHooksTestWrapper>
    );

    const input = container.querySelector<HTMLInputElement>(
      '.ais-SearchBox-input'
    )!;
    input.focus();

    userEvent.type(input, 'iPhone{Enter}');

    expect(lastUiState.indexName.query).toBe('iPhone');
    expect(onSubmit).toHaveBeenCalledTimes(1);
  });

  test('refines on reset when searchAsYouType is set', () => {
    let lastUiState: UiState = {};

    const { container } = render(
      <InstantSearchHooksTestWrapper
        onStateChange={({ uiState }) => {
          lastUiState = uiState;
        }}
      >
        <SearchBox searchAsYouType={false} />
      </InstantSearchHooksTestWrapper>
    );

    const input = container.querySelector<HTMLInputElement>(
      '.ais-SearchBox-input'
    )!;
    input.focus();
    userEvent.type(input, 'iPhone{Enter}');
    expect(lastUiState.indexName.query).toBe('iPhone');

    const resetButton = container.querySelector<HTMLButtonElement>(
      '.ais-SearchBox-reset'
    )!;
    userEvent.click(resetButton);
    expect(lastUiState.indexName.query).toBe(undefined);
  });

  test('renders with translations', () => {
    const { getByRole } = render(
      <InstantSearchHooksTestWrapper>
        <SearchBox
          translations={{
            resetButtonTitle: 'Reset',
            submitButtonTitle: 'Submit',
          }}
        />
      </InstantSearchHooksTestWrapper>
    );

    expect(getByRole('button', { name: 'Submit' })).toBeInTheDocument();

    userEvent.type(getByRole('searchbox'), 'nothing');

    expect(getByRole('button', { name: 'Reset' })).toBeInTheDocument();
  });
});