@patternfly/react-templates 1.1.0-prerelease.19 → 1.1.0-prerelease.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,408 @@
1
+ import * as React from 'react';
2
+ import { render, screen, waitForElementToBeRemoved } from '@testing-library/react';
3
+ import userEvent from '@testing-library/user-event';
4
+ import { TypeaheadSelect } from '../TypeaheadSelect';
5
+ import styles from '@patternfly/react-styles/css/components/Menu/menu';
6
+
7
+ test('renders typeahead select with options', async () => {
8
+ const initialOptions = [
9
+ { content: 'Option 1', value: 'option1' },
10
+ { content: 'Option 2', value: 'option2' },
11
+ { content: 'Option 3', value: 'option3' }
12
+ ];
13
+
14
+ const user = userEvent.setup();
15
+
16
+ render(<TypeaheadSelect initialOptions={initialOptions} />);
17
+
18
+ const toggle = screen.getByRole('button', { name: 'Typeahead menu toggle' });
19
+
20
+ await user.click(toggle);
21
+
22
+ const option1 = screen.getByText('Option 1');
23
+ const option2 = screen.getByText('Option 2');
24
+ const option3 = screen.getByText('Option 3');
25
+
26
+ expect(option1).toBeInTheDocument();
27
+ expect(option2).toBeInTheDocument();
28
+ expect(option3).toBeInTheDocument();
29
+ });
30
+
31
+ test('selects options when clicked', async () => {
32
+ const initialOptions = [
33
+ { content: 'Option 1', value: 'option1' },
34
+ { content: 'Option 2', value: 'option2' },
35
+ { content: 'Option 3', value: 'option3' }
36
+ ];
37
+
38
+ const user = userEvent.setup();
39
+
40
+ render(<TypeaheadSelect initialOptions={initialOptions} />);
41
+
42
+ const toggle = screen.getByRole('button', { name: 'Typeahead menu toggle' });
43
+
44
+ await user.click(toggle);
45
+
46
+ const option1 = screen.getByRole('option', { name: 'Option 1' });
47
+
48
+ expect(option1).not.toHaveClass(styles.modifiers.selected);
49
+
50
+ await user.click(option1);
51
+
52
+ expect(option1).toHaveClass(styles.modifiers.selected);
53
+ });
54
+
55
+ test('calls the onSelect callback with the selected value when an option is selected', async () => {
56
+ const initialOptions = [
57
+ { content: 'Option 1', value: 'option1' },
58
+ { content: 'Option 2', value: 'option2' },
59
+ { content: 'Option 3', value: 'option3' }
60
+ ];
61
+
62
+ const user = userEvent.setup();
63
+ const onSelectMock = jest.fn();
64
+
65
+ render(<TypeaheadSelect initialOptions={initialOptions} onSelect={onSelectMock} />);
66
+
67
+ const toggle = screen.getByRole('button', { name: 'Typeahead menu toggle' });
68
+
69
+ await user.click(toggle);
70
+
71
+ expect(onSelectMock).not.toHaveBeenCalled();
72
+
73
+ const option1 = screen.getByRole('option', { name: 'Option 1' });
74
+
75
+ await user.click(option1);
76
+
77
+ expect(onSelectMock).toHaveBeenCalledTimes(1);
78
+ expect(onSelectMock).toHaveBeenCalledWith(expect.anything(), 'option1');
79
+ });
80
+
81
+ test('deselects options when clear button is clicked', async () => {
82
+ const initialOptions = [
83
+ { content: 'Option 1', value: 'option1' },
84
+ { content: 'Option 2', value: 'option2' },
85
+ { content: 'Option 3', value: 'option3' }
86
+ ];
87
+
88
+ const user = userEvent.setup();
89
+
90
+ render(<TypeaheadSelect initialOptions={initialOptions} />);
91
+
92
+ const toggle = screen.getByRole('button', { name: 'Typeahead menu toggle' });
93
+
94
+ await user.click(toggle);
95
+
96
+ const option1 = screen.getByRole('option', { name: 'Option 1' });
97
+ await user.click(option1);
98
+ expect(option1).toHaveClass(styles.modifiers.selected);
99
+
100
+ const input = screen.getByRole('combobox');
101
+ expect(input).toHaveValue('Option 1');
102
+
103
+ const clearButton = screen.getByRole('button', { name: 'Clear input value' });
104
+ await user.click(clearButton);
105
+ expect(input).toHaveValue('');
106
+
107
+ await user.click(toggle);
108
+ expect(screen.getByRole('option', { name: 'Option 1' })).not.toHaveClass(styles.modifiers.selected);
109
+ });
110
+
111
+ test('toggles the select menu when the toggle button is clicked', async () => {
112
+ const initialOptions = [
113
+ { content: 'Option 1', value: 'option1' },
114
+ { content: 'Option 2', value: 'option2' },
115
+ { content: 'Option 3', value: 'option3' }
116
+ ];
117
+
118
+ const user = userEvent.setup();
119
+
120
+ render(<TypeaheadSelect initialOptions={initialOptions} />);
121
+
122
+ const toggle = screen.getByRole('button', { name: 'Typeahead menu toggle' });
123
+
124
+ await user.click(toggle);
125
+
126
+ const menu = screen.getByRole('listbox');
127
+ expect(menu).toBeInTheDocument();
128
+
129
+ await user.click(toggle);
130
+
131
+ await waitForElementToBeRemoved(menu);
132
+
133
+ expect(screen.queryByRole('listbox')).not.toBeInTheDocument();
134
+ });
135
+
136
+ test('calls the onToggle callback when the toggle is clicked', async () => {
137
+ const initialOptions = [
138
+ { content: 'Option 1', value: 'option1' },
139
+ { content: 'Option 2', value: 'option2' },
140
+ { content: 'Option 3', value: 'option3' }
141
+ ];
142
+
143
+ const user = userEvent.setup();
144
+ const onToggleMock = jest.fn();
145
+
146
+ render(<TypeaheadSelect initialOptions={initialOptions} onToggle={onToggleMock} />);
147
+
148
+ const toggle = screen.getByRole('button', { name: 'Typeahead menu toggle' });
149
+
150
+ expect(onToggleMock).not.toHaveBeenCalled();
151
+
152
+ await user.click(toggle);
153
+
154
+ expect(onToggleMock).toHaveBeenCalledTimes(1);
155
+ expect(onToggleMock).toHaveBeenCalledWith(true);
156
+
157
+ await user.click(toggle);
158
+
159
+ expect(onToggleMock).toHaveBeenCalledTimes(2);
160
+ expect(onToggleMock).toHaveBeenCalledWith(false);
161
+ });
162
+
163
+ test('Passes toggleWidth', () => {
164
+ const initialOptions = [
165
+ { content: 'Option 1', value: 'option1' },
166
+ { content: 'Option 2', value: 'option2' },
167
+ { content: 'Option 3', value: 'option3' }
168
+ ];
169
+
170
+ render(<TypeaheadSelect initialOptions={initialOptions} toggleWidth="500px" />);
171
+
172
+ const toggle = screen.getByRole('button', { name: 'Typeahead menu toggle' });
173
+ expect(toggle.parentElement).toHaveAttribute('style', 'width: 500px;');
174
+ });
175
+
176
+ test('Passes additional toggleProps', () => {
177
+ const initialOptions = [
178
+ { content: 'Option 1', value: 'option1' },
179
+ { content: 'Option 2', value: 'option2' },
180
+ { content: 'Option 3', value: 'option3' }
181
+ ];
182
+
183
+ render(<TypeaheadSelect initialOptions={initialOptions} toggleProps={{ id: 'toggle' }} />);
184
+
185
+ const toggle = screen.getByRole('button', { name: 'Typeahead menu toggle' });
186
+ expect(toggle.parentElement).toHaveAttribute('id', 'toggle');
187
+ });
188
+
189
+ test('passes custom placeholder text', async () => {
190
+ const initialOptions = [
191
+ { content: 'Option 1', value: 'option1' },
192
+ { content: 'Option 2', value: 'option2' },
193
+ { content: 'Option 3', value: 'option3' }
194
+ ];
195
+
196
+ render(<TypeaheadSelect initialOptions={initialOptions} placeholder="custom" />);
197
+
198
+ const input = screen.getByRole('combobox');
199
+
200
+ expect(input).toHaveAttribute('placeholder', 'custom');
201
+ });
202
+
203
+ test('displays noOptionsFoundMessage when filter returns no results', async () => {
204
+ const initialOptions = [
205
+ { content: 'Option 1', value: 'option1' },
206
+ { content: 'Option 2', value: 'option2' },
207
+ { content: 'Option 3', value: 'option3' }
208
+ ];
209
+ const user = userEvent.setup();
210
+
211
+ render(<TypeaheadSelect initialOptions={initialOptions} />);
212
+
213
+ const toggle = screen.getByRole('button', { name: 'Typeahead menu toggle' });
214
+ const input = screen.getByRole('combobox');
215
+
216
+ expect(input).toHaveAttribute('placeholder', 'Select an option');
217
+
218
+ await user.click(toggle);
219
+
220
+ const option1 = screen.getByRole('option', { name: 'Option 1' });
221
+ const option2 = screen.getByRole('option', { name: 'Option 2' });
222
+ const option3 = screen.getByRole('option', { name: 'Option 3' });
223
+
224
+ expect(option1).toBeInTheDocument();
225
+ expect(option2).toBeInTheDocument();
226
+ expect(option3).toBeInTheDocument();
227
+
228
+ await user.type(input, '9');
229
+ expect(input).toHaveValue('9');
230
+
231
+ expect(option1).not.toBeInTheDocument();
232
+ expect(option2).not.toBeInTheDocument();
233
+ expect(option3).not.toBeInTheDocument();
234
+ expect(screen.getByRole('option', { name: 'No results found for "9"' })).toBeInTheDocument();
235
+ });
236
+
237
+ test('displays custom noOptionsFoundMessage', async () => {
238
+ const initialOptions = [
239
+ { content: 'Option 1', value: 'option1' },
240
+ { content: 'Option 2', value: 'option2' },
241
+ { content: 'Option 3', value: 'option3' }
242
+ ];
243
+ const user = userEvent.setup();
244
+
245
+ render(<TypeaheadSelect initialOptions={initialOptions} noOptionsFoundMessage="custom" />);
246
+
247
+ const toggle = screen.getByRole('button', { name: 'Typeahead menu toggle' });
248
+ const input = screen.getByRole('combobox');
249
+
250
+ expect(input).toHaveAttribute('placeholder', 'Select an option');
251
+
252
+ await user.click(toggle);
253
+
254
+ const option1 = screen.getByRole('option', { name: 'Option 1' });
255
+ const option2 = screen.getByRole('option', { name: 'Option 2' });
256
+ const option3 = screen.getByRole('option', { name: 'Option 3' });
257
+
258
+ expect(option1).toBeInTheDocument();
259
+ expect(option2).toBeInTheDocument();
260
+ expect(option3).toBeInTheDocument();
261
+
262
+ await user.type(input, '9');
263
+ expect(input).toHaveValue('9');
264
+
265
+ expect(option1).not.toBeInTheDocument();
266
+ expect(option2).not.toBeInTheDocument();
267
+ expect(option3).not.toBeInTheDocument();
268
+ expect(screen.getByRole('option', { name: 'custom' })).toBeInTheDocument();
269
+ });
270
+
271
+ test('disables the select when isDisabled prop is true', async () => {
272
+ const initialOptions = [
273
+ { content: 'Option 1', value: 'option1' },
274
+ { content: 'Option 2', value: 'option2' },
275
+ { content: 'Option 3', value: 'option3' }
276
+ ];
277
+
278
+ const user = userEvent.setup();
279
+
280
+ render(<TypeaheadSelect initialOptions={initialOptions} isDisabled={true} />);
281
+
282
+ const toggle = screen.getByRole('button', { name: 'Typeahead menu toggle' });
283
+
284
+ expect(toggle.parentElement).toHaveClass(styles.modifiers.disabled);
285
+
286
+ await user.click(toggle);
287
+
288
+ expect(screen.queryByRole('menu')).not.toBeInTheDocument();
289
+ });
290
+
291
+ test('passes other SelectOption props to the SelectOption component', async () => {
292
+ const initialOptions = [{ content: 'Option 1', value: 'option1', isDisabled: true }];
293
+
294
+ const user = userEvent.setup();
295
+
296
+ render(<TypeaheadSelect initialOptions={initialOptions} />);
297
+
298
+ const toggle = screen.getByRole('button', { name: 'Typeahead menu toggle' });
299
+
300
+ await user.click(toggle);
301
+
302
+ const option1 = screen.getByRole('option', { name: 'Option 1' });
303
+ expect(option1).toBeDisabled();
304
+ });
305
+
306
+ test('displays the option in input when option is selected', async () => {
307
+ const initialOptions = [
308
+ { content: 'Option 1', value: 'option1' },
309
+ { content: 'Option 2', value: 'option2' },
310
+ { content: 'Option 3', value: 'option3' }
311
+ ];
312
+
313
+ const user = userEvent.setup();
314
+
315
+ render(<TypeaheadSelect initialOptions={initialOptions} />);
316
+
317
+ const toggle = screen.getByRole('button', { name: 'Typeahead menu toggle' });
318
+ const input = screen.getByRole('combobox');
319
+
320
+ expect(input).toHaveAttribute('placeholder', 'Select an option');
321
+
322
+ await user.click(toggle);
323
+
324
+ const option1 = screen.getByRole('option', { name: 'Option 1' });
325
+
326
+ await user.click(option1);
327
+
328
+ expect(input).toHaveValue('Option 1');
329
+
330
+ const clearButton = screen.getByRole('button', { name: 'Clear input value' });
331
+ await user.click(clearButton);
332
+
333
+ expect(input).toHaveValue('');
334
+ });
335
+
336
+ test('typing in input filters options', async () => {
337
+ const initialOptions = [
338
+ { content: 'Option 1', value: 'option1' },
339
+ { content: 'Option 2', value: 'option2' },
340
+ { content: 'Option 3', value: 'option3' }
341
+ ];
342
+
343
+ const user = userEvent.setup();
344
+
345
+ render(<TypeaheadSelect initialOptions={initialOptions} />);
346
+
347
+ const toggle = screen.getByRole('button', { name: 'Typeahead menu toggle' });
348
+ const input = screen.getByRole('combobox');
349
+
350
+ expect(input).toHaveAttribute('placeholder', 'Select an option');
351
+
352
+ await user.click(toggle);
353
+
354
+ const option1 = screen.getByRole('option', { name: 'Option 1' });
355
+ const option2 = screen.getByRole('option', { name: 'Option 2' });
356
+ const option3 = screen.getByRole('option', { name: 'Option 3' });
357
+
358
+ expect(option1).toBeInTheDocument();
359
+ expect(option2).toBeInTheDocument();
360
+ expect(option3).toBeInTheDocument();
361
+
362
+ await user.type(input, '1');
363
+ expect(input).toHaveValue('1');
364
+
365
+ expect(option1).toBeInTheDocument();
366
+ expect(option2).not.toBeInTheDocument();
367
+ expect(option3).not.toBeInTheDocument();
368
+ });
369
+
370
+ test('typing in input triggers onInputChange callback', async () => {
371
+ const initialOptions = [
372
+ { content: 'Option 1', value: 'option1' },
373
+ { content: 'Option 2', value: 'option2' },
374
+ { content: 'Option 3', value: 'option3' }
375
+ ];
376
+
377
+ const user = userEvent.setup();
378
+ const onInputChangeMock = jest.fn();
379
+
380
+ render(<TypeaheadSelect initialOptions={initialOptions} onInputChange={onInputChangeMock} />);
381
+
382
+ const input = screen.getByRole('combobox');
383
+
384
+ expect(input).toHaveAttribute('placeholder', 'Select an option');
385
+
386
+ await user.type(input, '1');
387
+
388
+ expect(input).toHaveValue('1');
389
+ expect(onInputChangeMock).toHaveBeenCalledTimes(1);
390
+ expect(onInputChangeMock).toHaveBeenCalledWith('1');
391
+ });
392
+
393
+ test('Matches snapshot', async () => {
394
+ const initialOptions = [
395
+ { content: 'Option 1', value: 'option1' },
396
+ { content: 'Option 2', value: 'option2' },
397
+ { content: 'Option 3', value: 'option3' }
398
+ ];
399
+
400
+ const user = userEvent.setup();
401
+
402
+ const { asFragment } = render(<TypeaheadSelect initialOptions={initialOptions} />);
403
+
404
+ const toggle = screen.getByRole('button', { name: 'Typeahead menu toggle' });
405
+ await user.click(toggle);
406
+
407
+ expect(asFragment()).toMatchSnapshot();
408
+ });
@@ -5,7 +5,7 @@ exports[`Matches snapshot 1`] = `
5
5
  <button
6
6
  aria-expanded="true"
7
7
  class="pf-v5-c-menu-toggle pf-m-expanded"
8
- data-ouia-component-id="OUIA-Generated-MenuToggle-14"
8
+ data-ouia-component-id="OUIA-Generated-MenuToggle-13"
9
9
  data-ouia-component-type="PF5/MenuToggle"
10
10
  data-ouia-safe="true"
11
11
  style="width: 200px;"
@@ -40,7 +40,7 @@ exports[`Matches snapshot 1`] = `
40
40
  </button>
41
41
  <div
42
42
  class="pf-v5-c-menu"
43
- data-ouia-component-id="OUIA-Generated-Select-25"
43
+ data-ouia-component-id="OUIA-Generated-Select-24"
44
44
  data-ouia-component-type="PF5/Select"
45
45
  data-ouia-safe="true"
46
46
  data-popper-escaped="true"
@@ -0,0 +1,180 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`Matches snapshot 1`] = `
4
+ <DocumentFragment>
5
+ <div
6
+ class="pf-v5-c-menu-toggle pf-m-expanded pf-m-full-width pf-m-typeahead"
7
+ >
8
+ <div
9
+ class="pf-v5-c-text-input-group pf-m-plain"
10
+ >
11
+ <div
12
+ autocomplete="off"
13
+ class="pf-v5-c-text-input-group__main"
14
+ >
15
+ <span
16
+ class="pf-v5-c-text-input-group__text"
17
+ >
18
+ <input
19
+ aria-controls="select-typeahead-listbox"
20
+ aria-expanded="true"
21
+ aria-label="Type to filter"
22
+ class="pf-v5-c-text-input-group__text-input"
23
+ placeholder="Select an option"
24
+ role="combobox"
25
+ type="text"
26
+ value=""
27
+ />
28
+ </span>
29
+ </div>
30
+ <div
31
+ class="pf-v5-c-text-input-group__utilities"
32
+ style="display: none;"
33
+ >
34
+ <button
35
+ aria-disabled="false"
36
+ aria-label="Clear input value"
37
+ class="pf-v5-c-button pf-m-plain"
38
+ data-ouia-component-id="OUIA-Generated-Button-plain-17"
39
+ data-ouia-component-type="PF5/Button"
40
+ data-ouia-safe="true"
41
+ type="button"
42
+ >
43
+ <svg
44
+ aria-hidden="true"
45
+ class="pf-v5-svg"
46
+ fill="currentColor"
47
+ height="1em"
48
+ role="img"
49
+ viewBox="0 0 352 512"
50
+ width="1em"
51
+ >
52
+ <path
53
+ d="M242.72 256l100.07-100.07c12.28-12.28 12.28-32.19 0-44.48l-22.24-22.24c-12.28-12.28-32.19-12.28-44.48 0L176 189.28 75.93 89.21c-12.28-12.28-32.19-12.28-44.48 0L9.21 111.45c-12.28 12.28-12.28 32.19 0 44.48L109.28 256 9.21 356.07c-12.28 12.28-12.28 32.19 0 44.48l22.24 22.24c12.28 12.28 32.2 12.28 44.48 0L176 322.72l100.07 100.07c12.28 12.28 32.2 12.28 44.48 0l22.24-22.24c12.28-12.28 12.28-32.19 0-44.48L242.72 256z"
54
+ />
55
+ </svg>
56
+ </button>
57
+ </div>
58
+ </div>
59
+ <button
60
+ aria-expanded="true"
61
+ aria-label="Typeahead menu toggle"
62
+ class="pf-v5-c-menu-toggle__button"
63
+ data-ouia-component-id="OUIA-Generated-MenuToggle-typeahead-17"
64
+ data-ouia-component-type="PF5/MenuToggle"
65
+ data-ouia-safe="true"
66
+ tabindex="-1"
67
+ type="button"
68
+ >
69
+ <span
70
+ class="pf-v5-c-menu-toggle__controls"
71
+ >
72
+ <span
73
+ class="pf-v5-c-menu-toggle__toggle-icon"
74
+ >
75
+ <svg
76
+ aria-hidden="true"
77
+ class="pf-v5-svg"
78
+ fill="currentColor"
79
+ height="1em"
80
+ role="img"
81
+ viewBox="0 0 320 512"
82
+ width="1em"
83
+ >
84
+ <path
85
+ d="M31.3 192h257.3c17.8 0 26.7 21.5 14.1 34.1L174.1 354.8c-7.8 7.8-20.5 7.8-28.3 0L17.2 226.1C4.6 213.5 13.5 192 31.3 192z"
86
+ />
87
+ </svg>
88
+ </span>
89
+ </span>
90
+ </button>
91
+ </div>
92
+ <div
93
+ class="pf-v5-c-menu"
94
+ data-ouia-component-id="OUIA-Generated-Select-51"
95
+ data-ouia-component-type="PF5/Select"
96
+ data-ouia-safe="true"
97
+ data-popper-escaped="true"
98
+ data-popper-placement="bottom-start"
99
+ data-popper-reference-hidden="true"
100
+ style="position: absolute; left: 0px; top: 0px; z-index: 9999; opacity: 1; transition: opacity 0ms cubic-bezier(.54, 1.5, .38, 1.11); min-width: 0px; transform: translate(0px, 0px);"
101
+ >
102
+ <div
103
+ class="pf-v5-c-menu__content"
104
+ >
105
+ <ul
106
+ aria-multiselectable="false"
107
+ class="pf-v5-c-menu__list"
108
+ role="listbox"
109
+ >
110
+ <li
111
+ class="pf-v5-c-menu__list-item"
112
+ role="none"
113
+ >
114
+ <button
115
+ aria-selected="false"
116
+ class="pf-v5-c-menu__item"
117
+ role="option"
118
+ tabindex="0"
119
+ type="button"
120
+ >
121
+ <span
122
+ class="pf-v5-c-menu__item-main"
123
+ >
124
+ <span
125
+ class="pf-v5-c-menu__item-text"
126
+ >
127
+ Option 1
128
+ </span>
129
+ </span>
130
+ </button>
131
+ </li>
132
+ <li
133
+ class="pf-v5-c-menu__list-item"
134
+ role="none"
135
+ >
136
+ <button
137
+ aria-selected="false"
138
+ class="pf-v5-c-menu__item"
139
+ role="option"
140
+ tabindex="-1"
141
+ type="button"
142
+ >
143
+ <span
144
+ class="pf-v5-c-menu__item-main"
145
+ >
146
+ <span
147
+ class="pf-v5-c-menu__item-text"
148
+ >
149
+ Option 2
150
+ </span>
151
+ </span>
152
+ </button>
153
+ </li>
154
+ <li
155
+ class="pf-v5-c-menu__list-item"
156
+ role="none"
157
+ >
158
+ <button
159
+ aria-selected="false"
160
+ class="pf-v5-c-menu__item"
161
+ role="option"
162
+ tabindex="-1"
163
+ type="button"
164
+ >
165
+ <span
166
+ class="pf-v5-c-menu__item-main"
167
+ >
168
+ <span
169
+ class="pf-v5-c-menu__item-text"
170
+ >
171
+ Option 3
172
+ </span>
173
+ </span>
174
+ </button>
175
+ </li>
176
+ </ul>
177
+ </div>
178
+ </div>
179
+ </DocumentFragment>
180
+ `;