-
Notifications
You must be signed in to change notification settings - Fork 552
Expand file tree
/
Copy pathAutocompleteSearch.test.tsx
More file actions
106 lines (96 loc) · 3.28 KB
/
AutocompleteSearch.test.tsx
File metadata and controls
106 lines (96 loc) · 3.28 KB
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
/**
* @jest-environment @instantsearch/testutils/jest-environment-jsdom.ts
*/
/** @jsx createElement */
import { render } from '@testing-library/preact';
import userEvent from '@testing-library/user-event';
import { createElement, createRef, Fragment } from 'preact';
import { createAutocompleteDetachedFormContainerComponent } from '../AutocompleteDetachedFormContainer';
import { createAutocompleteSearchComponent } from '../AutocompleteSearch';
const AutocompleteSearch = createAutocompleteSearchComponent({
createElement,
Fragment,
});
const AutocompleteDetachedFormContainer =
createAutocompleteDetachedFormContainerComponent({
createElement,
Fragment,
});
describe('AutocompleteSearch', () => {
test('renders a back button and closes on click in detached mode', () => {
const onSubmit = jest.fn();
const inputRef = createRef<HTMLInputElement>();
const { container, getByTitle } = render(
<AutocompleteSearch
inputProps={{
id: 'detached-search',
ref: inputRef,
onInput: jest.fn(),
}}
onClear={jest.fn()}
query="iphone"
isSearchStalled={false}
onSubmit={onSubmit}
isDetached={true}
submitTitle="Close"
/>
);
userEvent.click(getByTitle('Close'));
expect(onSubmit).toHaveBeenCalledTimes(1);
// A dedicated back button is rendered – not the submit button
expect(
container.querySelector('.ais-AutocompleteBackButton')
).not.toBeNull();
expect(container.querySelector('.ais-AutocompleteBackIcon')).not.toBeNull();
// Submit button is hidden (label hidden), back button is separate
expect(
container.querySelector<HTMLLabelElement>('.ais-AutocompleteLabel')
?.hidden
).toBe(true);
expect(
container.querySelector('.ais-AutocompleteSubmitButton')
).not.toBeNull();
});
test('renders the submit button in non-detached mode', () => {
const inputRef = createRef<HTMLInputElement>();
const { container, getByTitle } = render(
<AutocompleteSearch
inputProps={{
id: 'default-search',
ref: inputRef,
onInput: jest.fn(),
}}
onClear={jest.fn()}
query=""
isSearchStalled={false}
/>
);
expect(getByTitle('Submit').getAttribute('type')).toBe('submit');
expect(
container.querySelector('.ais-AutocompleteSubmitIcon')
).not.toBeNull();
expect(container.querySelector('.ais-AutocompleteBackButton')).toBeNull();
expect(container.querySelector('.ais-AutocompleteBackIcon')).toBeNull();
});
});
describe('AutocompleteDetachedFormContainer', () => {
test('keeps backwards-compatible props without rendering a cancel button', () => {
const { container, queryByText } = render(
<AutocompleteDetachedFormContainer
onCancel={jest.fn()}
translations={{
detachedCancelButtonText: 'Cancel',
detachedSearchButtonTitle: 'Search',
detachedClearButtonTitle: 'Clear',
}}
>
<div>Search form</div>
</AutocompleteDetachedFormContainer>
);
expect(queryByText('Search form')).not.toBeNull();
expect(queryByText('Cancel')).toBeNull();
expect(
container.querySelector('.ais-AutocompleteDetachedCancelButton')
).toBeNull();
});
});