|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import createWrapper, { |
| 5 | + AutosuggestWrapper, |
| 6 | + FormFieldWrapper, |
| 7 | + MultiselectWrapper, |
| 8 | + SelectWrapper, |
| 9 | +} from '../../../lib/components/test-utils/dom'; |
| 10 | + |
| 11 | +import itemStyles from '../../../lib/components/internal/components/selectable-item/styles.css.js'; |
| 12 | +import selectableStyles from '../../../lib/components/internal/components/selectable-item/styles.selectors.js'; |
| 13 | +import propertyFilterStyles from '../../../lib/components/property-filter/styles.selectors.js'; |
| 14 | +import selectStyles from '../../../lib/components/select/parts/styles.selectors.js'; |
| 15 | + |
| 16 | +export function createExtendedWrapper() { |
| 17 | + const wrapper = createWrapper().findPropertyFilter()!; |
| 18 | + function findEditorDropdown() { |
| 19 | + return wrapper |
| 20 | + .findTokens() |
| 21 | + .map(w => w.findEditorDropdown()) |
| 22 | + .filter(Boolean)[0]!; |
| 23 | + } |
| 24 | + function findControl(fieldIndex: number, fieldType: 'property' | 'operator' | 'value') { |
| 25 | + const dropdown = findEditorDropdown(); |
| 26 | + const field = { |
| 27 | + property: dropdown.findPropertyField(fieldIndex), |
| 28 | + operator: dropdown.findOperatorField(fieldIndex), |
| 29 | + value: dropdown.findValueField(fieldIndex), |
| 30 | + }[fieldType]; |
| 31 | + return field.findControl()!; |
| 32 | + } |
| 33 | + |
| 34 | + return { |
| 35 | + input: { |
| 36 | + focus() { |
| 37 | + wrapper.focus(); |
| 38 | + }, |
| 39 | + value(text: string) { |
| 40 | + wrapper.setInputValue(text); |
| 41 | + }, |
| 42 | + date(text: string) { |
| 43 | + wrapper.findDropdown().findOpenDropdown()!.findDateInput()!.setInputValue(text); |
| 44 | + }, |
| 45 | + keys(...codes: number[]) { |
| 46 | + codes.forEach(code => wrapper.findNativeInput().keydown(code)); |
| 47 | + }, |
| 48 | + submit() { |
| 49 | + wrapper.findPropertySubmitButton()!.click(); |
| 50 | + }, |
| 51 | + cancel() { |
| 52 | + wrapper.findPropertyCancelButton()!.click(); |
| 53 | + }, |
| 54 | + selectByValue(value: null | string) { |
| 55 | + wrapper.selectSuggestionByValue(value as any); |
| 56 | + }, |
| 57 | + selectAll() { |
| 58 | + wrapper |
| 59 | + .findDropdown() |
| 60 | + .findOpenDropdown()! |
| 61 | + .findByClassName(selectableStyles['selectable-item'])! |
| 62 | + .fireEvent(new MouseEvent('mouseup', { bubbles: true })); |
| 63 | + }, |
| 64 | + dropdown() { |
| 65 | + return !!wrapper.findDropdown().findOpenDropdown(); |
| 66 | + }, |
| 67 | + status() { |
| 68 | + return wrapper.findDropdown().findFooterRegion()?.getElement().textContent ?? ''; |
| 69 | + }, |
| 70 | + options() { |
| 71 | + const enteredTextOption = wrapper.findEnteredTextOption()?.getElement().textContent; |
| 72 | + const otherOptions = printOptions(wrapper); |
| 73 | + return [enteredTextOption, ...otherOptions].filter(Boolean); |
| 74 | + }, |
| 75 | + }, |
| 76 | + tokens: { |
| 77 | + token(index = 1) { |
| 78 | + const token = wrapper.findTokens()[index - 1]; |
| 79 | + return { |
| 80 | + nested(index = 1) { |
| 81 | + const nested = token.findGroupTokens()[index - 1]; |
| 82 | + return { |
| 83 | + operation(value: 'and' | 'or') { |
| 84 | + nested.findTokenOperation()!.openDropdown(); |
| 85 | + nested.findTokenOperation()?.selectOptionByValue(value); |
| 86 | + }, |
| 87 | + }; |
| 88 | + }, |
| 89 | + operation(value: 'and' | 'or') { |
| 90 | + token.findTokenOperation()!.openDropdown(); |
| 91 | + token.findTokenOperation()?.selectOptionByValue(value); |
| 92 | + }, |
| 93 | + }; |
| 94 | + }, |
| 95 | + list() { |
| 96 | + return wrapper.findTokens().map(w => { |
| 97 | + const operation = w.findTokenOperation()?.find('button')?.getElement().textContent; |
| 98 | + const value = w.findLabel()?.getElement().textContent; |
| 99 | + const nested = w |
| 100 | + .findGroupTokens() |
| 101 | + .map(w => { |
| 102 | + const operation = w.findTokenOperation()?.find('button')?.getElement().textContent; |
| 103 | + const value = w.findLabel()?.getElement().textContent; |
| 104 | + return [operation, value].filter(Boolean).join(' '); |
| 105 | + }) |
| 106 | + .join(' '); |
| 107 | + return [operation, value, nested].filter(Boolean).join(' '); |
| 108 | + }); |
| 109 | + }, |
| 110 | + }, |
| 111 | + editor: { |
| 112 | + dropdown() { |
| 113 | + return !!findEditorDropdown(); |
| 114 | + }, |
| 115 | + open(index: number) { |
| 116 | + const token = wrapper.findTokens()[index - 1]; |
| 117 | + const target = token.findEditButton() ?? token.findLabel(); |
| 118 | + target.click(); |
| 119 | + }, |
| 120 | + property(fieldIndex = 1) { |
| 121 | + const select = findControl(fieldIndex, 'property').findSelect()!; |
| 122 | + return { |
| 123 | + open() { |
| 124 | + dropdownOpen(select); |
| 125 | + }, |
| 126 | + value(option: null | string) { |
| 127 | + dropdownOpen(select); |
| 128 | + select.selectOptionByValue(option as any); |
| 129 | + }, |
| 130 | + options() { |
| 131 | + dropdownOpen(select); |
| 132 | + return printOptions(select); |
| 133 | + }, |
| 134 | + }; |
| 135 | + }, |
| 136 | + operator(fieldIndex = 1) { |
| 137 | + const select = findControl(fieldIndex, 'operator').findSelect()!; |
| 138 | + return { |
| 139 | + open() { |
| 140 | + dropdownOpen(select); |
| 141 | + }, |
| 142 | + value(option: null | string) { |
| 143 | + dropdownOpen(select); |
| 144 | + select.selectOptionByValue(option as any); |
| 145 | + }, |
| 146 | + options() { |
| 147 | + dropdownOpen(select); |
| 148 | + return printOptions(select); |
| 149 | + }, |
| 150 | + }; |
| 151 | + }, |
| 152 | + value(fieldIndex = 1) { |
| 153 | + return { |
| 154 | + autosuggest() { |
| 155 | + const autosuggest = findControl(fieldIndex, 'value').findAutosuggest()!; |
| 156 | + return { |
| 157 | + focus() { |
| 158 | + autosuggest.focus(); |
| 159 | + }, |
| 160 | + option(value: string) { |
| 161 | + autosuggest.focus(); |
| 162 | + autosuggest.selectSuggestionByValue(value); |
| 163 | + }, |
| 164 | + input(text: string) { |
| 165 | + autosuggest.setInputValue(text); |
| 166 | + }, |
| 167 | + options() { |
| 168 | + return printOptions(autosuggest); |
| 169 | + }, |
| 170 | + }; |
| 171 | + }, |
| 172 | + multiselect() { |
| 173 | + const multiselect = findControl(fieldIndex, 'value').findMultiselect()!; |
| 174 | + return { |
| 175 | + open() { |
| 176 | + dropdownOpen(multiselect); |
| 177 | + }, |
| 178 | + filter(text: string) { |
| 179 | + dropdownOpen(multiselect); |
| 180 | + multiselect.findFilteringInput()!.setInputValue(text); |
| 181 | + }, |
| 182 | + value(options: (null | string)[]) { |
| 183 | + dropdownOpen(multiselect); |
| 184 | + options.forEach(option => multiselect.selectOptionByValue(option as any)); |
| 185 | + }, |
| 186 | + options() { |
| 187 | + dropdownOpen(multiselect); |
| 188 | + return printOptions(multiselect); |
| 189 | + }, |
| 190 | + }; |
| 191 | + }, |
| 192 | + dateInput() { |
| 193 | + const dateInput = findControl(fieldIndex, 'value').findDateInput()!; |
| 194 | + return { |
| 195 | + value(value: string) { |
| 196 | + dateInput.setInputValue(value); |
| 197 | + }, |
| 198 | + }; |
| 199 | + }, |
| 200 | + }; |
| 201 | + }, |
| 202 | + remove(rowIndex: number) { |
| 203 | + findEditorDropdown().findTokenRemoveActions(rowIndex)!.openDropdown(); |
| 204 | + findEditorDropdown().findTokenRemoveActions(rowIndex)!.findItemById('remove')!.click(); |
| 205 | + }, |
| 206 | + addFilter() { |
| 207 | + findEditorDropdown().findTokenAddActions()!.findMainAction()!.click(); |
| 208 | + }, |
| 209 | + submit() { |
| 210 | + findEditorDropdown().findSubmitButton()!.click(); |
| 211 | + }, |
| 212 | + cancel() { |
| 213 | + findEditorDropdown().findCancelButton()!.click(); |
| 214 | + }, |
| 215 | + form() { |
| 216 | + const dropdown = findEditorDropdown(); |
| 217 | + const rows: string[] = []; |
| 218 | + for (let index = 1; index <= 10; index++) { |
| 219 | + const property = printField(dropdown.findPropertyField(index), 'property'); |
| 220 | + const operator = printField(dropdown.findOperatorField(index), 'operator'); |
| 221 | + const value = printField(dropdown.findValueField(index), 'value'); |
| 222 | + if (property || operator || value) { |
| 223 | + rows.push([property, operator, value].join(' ')); |
| 224 | + } else { |
| 225 | + break; |
| 226 | + } |
| 227 | + } |
| 228 | + return rows; |
| 229 | + }, |
| 230 | + }, |
| 231 | + }; |
| 232 | +} |
| 233 | + |
| 234 | +function dropdownOpen(wrapper: SelectWrapper | MultiselectWrapper) { |
| 235 | + if (!wrapper.findDropdown().findOpenDropdown()) { |
| 236 | + wrapper.openDropdown(); |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +function printOptions(wrapper: AutosuggestWrapper | SelectWrapper | MultiselectWrapper) { |
| 241 | + const options = wrapper |
| 242 | + .findDropdown() |
| 243 | + .findOptions() |
| 244 | + .flatMap(w => { |
| 245 | + const selectOptions = w.findAllByClassName(itemStyles['option-content']); |
| 246 | + const options = selectOptions.length > 0 ? selectOptions : [w]; |
| 247 | + return options.map(w => w.getElement().textContent); |
| 248 | + }); |
| 249 | + return options.filter(Boolean); |
| 250 | +} |
| 251 | + |
| 252 | +function printField(wrapper: null | FormFieldWrapper, type: 'property' | 'operator' | 'value') { |
| 253 | + if (!wrapper) { |
| 254 | + return null; |
| 255 | + } |
| 256 | + const headers = createWrapper().findAllByClassName(propertyFilterStyles['token-editor-grid-header']); |
| 257 | + const header = headers[['property', 'operator', 'value'].indexOf(type)]; |
| 258 | + const formFieldLabel = wrapper.findLabel()?.getElement().textContent ?? header?.getElement().textContent; |
| 259 | + const autosuggest = wrapper.findControl()!.findAutosuggest(); |
| 260 | + const dateInput = wrapper.findControl()?.findDateInput(); |
| 261 | + if (autosuggest || dateInput) { |
| 262 | + const input = autosuggest?.findNativeInput() ?? dateInput?.findNativeInput(); |
| 263 | + const value = input!.getElement().value; |
| 264 | + return `${formFieldLabel}[${value}]`; |
| 265 | + } |
| 266 | + const select = wrapper.findControl()!.findSelect(); |
| 267 | + if (select) { |
| 268 | + const value = select.getElement().textContent; |
| 269 | + return `${formFieldLabel}[${value}]`; |
| 270 | + } |
| 271 | + const multiselect = wrapper.findControl()!.findMultiselect(); |
| 272 | + if (multiselect) { |
| 273 | + const tokens = multiselect.findAllByClassName(selectStyles['inline-token']); |
| 274 | + const value = tokens.map(w => w.getElement().textContent).join(', '); |
| 275 | + return `${formFieldLabel}[${value}]`; |
| 276 | + } |
| 277 | + return `${formFieldLabel}[]`; |
| 278 | +} |
0 commit comments