|
| 1 | +/* |
| 2 | + * Copyright 2025 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +import {act, within} from '@testing-library/react'; |
| 14 | +import {CheckboxGroupTesterOpts, UserOpts} from './types'; |
| 15 | +import {pressElement} from './events'; |
| 16 | + |
| 17 | +interface TriggerCheckboxOptions { |
| 18 | + /** |
| 19 | + * What interaction type to use when triggering a checkbox. Defaults to the interaction type set on the tester. |
| 20 | + */ |
| 21 | + interactionType?: UserOpts['interactionType'], |
| 22 | + /** |
| 23 | + * The index, text, or node of the checkbox to toggle selection for. |
| 24 | + */ |
| 25 | + checkbox: number | string | HTMLElement |
| 26 | +} |
| 27 | + |
| 28 | +export class CheckboxGroupTester { |
| 29 | + private user; |
| 30 | + private _interactionType: UserOpts['interactionType']; |
| 31 | + private _checkboxgroup: HTMLElement; |
| 32 | + |
| 33 | + |
| 34 | + constructor(opts: CheckboxGroupTesterOpts) { |
| 35 | + let {root, user, interactionType} = opts; |
| 36 | + this.user = user; |
| 37 | + this._interactionType = interactionType || 'mouse'; |
| 38 | + |
| 39 | + this._checkboxgroup = root; |
| 40 | + let checkboxgroup = within(root).queryAllByRole('group'); |
| 41 | + if (checkboxgroup.length > 0) { |
| 42 | + this._checkboxgroup = checkboxgroup[0]; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Set the interaction type used by the checkbox group tester. |
| 48 | + */ |
| 49 | + setInteractionType(type: UserOpts['interactionType']): void { |
| 50 | + this._interactionType = type; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Returns a checkbox matching the specified index or text content. |
| 55 | + */ |
| 56 | + findCheckbox(opts: {checkboxIndexOrText: number | string}): HTMLElement { |
| 57 | + let { |
| 58 | + checkboxIndexOrText |
| 59 | + } = opts; |
| 60 | + |
| 61 | + let checkbox; |
| 62 | + if (typeof checkboxIndexOrText === 'number') { |
| 63 | + checkbox = this.checkboxes[checkboxIndexOrText]; |
| 64 | + } else if (typeof checkboxIndexOrText === 'string') { |
| 65 | + let label = within(this.checkboxgroup).getByText(checkboxIndexOrText); |
| 66 | + |
| 67 | + // Label may wrap the checkbox, or the actual label may be a sibling span, or the checkbox div could have the label within it |
| 68 | + if (label) { |
| 69 | + checkbox = within(label).queryByRole('checkbox'); |
| 70 | + if (!checkbox) { |
| 71 | + let labelWrapper = label.closest('label'); |
| 72 | + if (labelWrapper) { |
| 73 | + checkbox = within(labelWrapper).queryByRole('checkbox'); |
| 74 | + } else { |
| 75 | + checkbox = label.closest('[role=checkbox]'); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return checkbox; |
| 82 | + } |
| 83 | + |
| 84 | + private async keyboardNavigateToCheckbox(opts: {checkbox: HTMLElement}) { |
| 85 | + let {checkbox} = opts; |
| 86 | + let checkboxes = this.checkboxes; |
| 87 | + checkboxes = checkboxes.filter(checkbox => !(checkbox.hasAttribute('disabled') || checkbox.getAttribute('aria-disabled') === 'true')); |
| 88 | + if (checkboxes.length === 0) { |
| 89 | + throw new Error('Checkbox group doesnt have any non-disabled checkboxes. Please double check your checkbox group.'); |
| 90 | + } |
| 91 | + |
| 92 | + let targetIndex = checkboxes.indexOf(checkbox); |
| 93 | + if (targetIndex === -1) { |
| 94 | + throw new Error('Checkbox provided is not in the checkbox group.'); |
| 95 | + } |
| 96 | + |
| 97 | + if (!this.checkboxgroup.contains(document.activeElement)) { |
| 98 | + act(() => checkboxes[0].focus()); |
| 99 | + } |
| 100 | + |
| 101 | + let currIndex = checkboxes.indexOf(document.activeElement as HTMLElement); |
| 102 | + if (currIndex === -1) { |
| 103 | + throw new Error('Active element is not in the checkbox group.'); |
| 104 | + } |
| 105 | + |
| 106 | + for (let i = 0; i < Math.abs(targetIndex - currIndex); i++) { |
| 107 | + await this.user.tab({shift: targetIndex < currIndex}); |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + /** |
| 112 | + * Toggles the specified checkbox. Defaults to using the interaction type set on the checkbox tester. |
| 113 | + */ |
| 114 | + async toggleCheckbox(opts: TriggerCheckboxOptions): Promise<void> { |
| 115 | + let { |
| 116 | + checkbox, |
| 117 | + interactionType = this._interactionType |
| 118 | + } = opts; |
| 119 | + |
| 120 | + if (typeof checkbox === 'string' || typeof checkbox === 'number') { |
| 121 | + checkbox = this.findCheckbox({checkboxIndexOrText: checkbox}); |
| 122 | + } |
| 123 | + |
| 124 | + if (!checkbox) { |
| 125 | + throw new Error('Target checkbox not found in the checkboxgroup.'); |
| 126 | + } else if (checkbox.hasAttribute('disabled')) { |
| 127 | + throw new Error('Target checkbox is disabled.'); |
| 128 | + } |
| 129 | + |
| 130 | + if (interactionType === 'keyboard') { |
| 131 | + await this.keyboardNavigateToCheckbox({checkbox}); |
| 132 | + await this.user.keyboard('[Space]'); |
| 133 | + } else { |
| 134 | + await pressElement(this.user, checkbox, interactionType); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Returns the checkboxgroup. |
| 140 | + */ |
| 141 | + get checkboxgroup(): HTMLElement { |
| 142 | + return this._checkboxgroup; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Returns the checkboxes. |
| 147 | + */ |
| 148 | + get checkboxes(): HTMLElement[] { |
| 149 | + return within(this.checkboxgroup).queryAllByRole('checkbox'); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Returns the currently selected checkboxes in the checkboxgroup if any. |
| 154 | + */ |
| 155 | + get selectedCheckboxes(): HTMLElement[] { |
| 156 | + return this.checkboxes.filter(checkbox => (checkbox as HTMLInputElement).checked || checkbox.getAttribute('aria-checked') === 'true'); |
| 157 | + } |
| 158 | +} |
0 commit comments