-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathSearchInputAndResults.test.tsx
More file actions
155 lines (127 loc) · 4.58 KB
/
Copy pathSearchInputAndResults.test.tsx
File metadata and controls
155 lines (127 loc) · 4.58 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
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
import fetchMock from '@fetch-mock/jest';
import userEvent from '@testing-library/user-event';
import SearchInputAndResults from '../../components/Search/SearchInputAndResults';
import getTestData from '../utils/fixtures';
import { screen, render } from '../utils/test-utils';
describe('SearchInputAndResults - Auto-select revision hash', () => {
const mockOnToggle = jest.fn();
const { testData } = getTestData();
const fullHash = testData[7].revision;
const partialHash = fullHash.substring(0, 12);
beforeEach(() => {
mockOnToggle.mockClear();
});
it('should auto-select when pasting a full hash for base revision', async () => {
fetchMock.get(
`glob:https://treeherder.mozilla.org/api/project/try/push/*`,
{ results: testData },
);
const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime });
render(
<SearchInputAndResults
compact={false}
inputPlaceholder='Search base'
displayedRevisions={[]}
searchType='base'
repository='try'
onSearchResultsToggle={mockOnToggle}
listItemComponent='radio'
/>,
);
const input = screen.getByRole('textbox');
await user.type(input, fullHash);
// Assert auto-select happened
expect(mockOnToggle).toHaveBeenCalledWith(testData[7]);
expect(input).toHaveValue(''); // Input cleared
});
it('should auto-select when pasting a partial hash (>= 7 chars) for base revision', async () => {
fetchMock.get(
`glob:https://treeherder.mozilla.org/api/project/try/push/*`,
{ results: testData },
);
const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime });
render(
<SearchInputAndResults
compact={false}
inputPlaceholder='Search base'
displayedRevisions={[]}
searchType='base'
repository='try'
onSearchResultsToggle={mockOnToggle}
listItemComponent='radio'
/>,
);
const input = screen.getByRole('textbox');
await user.type(input, partialHash);
// Assert auto-select happened
expect(mockOnToggle).toHaveBeenCalledWith(testData[7]);
expect(input).toHaveValue(''); // Input cleared
});
it('should auto-select when pasting a revision hash for new revision (not already selected)', async () => {
fetchMock.get(
`glob:https://treeherder.mozilla.org/api/project/try/push/*`,
{ results: testData },
);
const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime });
render(
<SearchInputAndResults
compact={false}
inputPlaceholder='Search new'
displayedRevisions={[]} // Not selected yet
searchType='new'
repository='try'
onSearchResultsToggle={mockOnToggle}
listItemComponent='checkbox'
/>,
);
const input = screen.getByRole('textbox');
await user.type(input, fullHash);
expect(mockOnToggle).toHaveBeenCalledWith(testData[7]);
expect(input).toHaveValue('');
});
it('should not auto-select if revision hash is already selected for new revision', async () => {
fetchMock.get(
`glob:https://treeherder.mozilla.org/api/project/try/push/*`,
{ results: testData },
);
const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime });
render(
<SearchInputAndResults
compact={false}
inputPlaceholder='Search new'
displayedRevisions={[testData[7]]} // Already selected
searchType='new'
repository='try'
onSearchResultsToggle={mockOnToggle}
listItemComponent='checkbox'
/>,
);
const input = screen.getByRole('textbox');
await user.type(input, fullHash);
expect(mockOnToggle).not.toHaveBeenCalled(); // No toggle
expect(input).toHaveValue(''); // Still cleared
});
it('should show dropdown for non-hash search terms (no auto-select)', async () => {
fetchMock.get(
`glob:https://treeherder.mozilla.org/api/project/try/push/*`,
{ results: testData },
);
const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime });
render(
<SearchInputAndResults
compact={false}
inputPlaceholder='Search base'
displayedRevisions={[]}
searchType='base'
repository='try'
onSearchResultsToggle={mockOnToggle}
listItemComponent='radio'
/>,
);
const input = screen.getByRole('textbox');
await user.type(input, 'coconut'); // Not a hex hash
expect(mockOnToggle).not.toHaveBeenCalled();
// Dropdown should appear with results
await screen.findByText(/no arms left/); // From testData
});
});