|
1 |
| -import { createInteractor, perform, focused } from '../index'; |
| 1 | +import { Interaction, InteractorConstructor, createInteractor, perform, focused } from '../index'; |
2 | 2 | import { isVisible } from 'element-is-visible';
|
| 3 | +import { FilterParams, ActionMethods } from '../specification'; |
3 | 4 |
|
4 |
| -export const CheckBox = createInteractor<HTMLInputElement>('check box')({ |
5 |
| - selector: 'input[type=checkbox]', |
6 |
| - locator: (element) => element.labels ? (Array.from(element.labels)[0]?.textContent || '') : '', |
7 |
| - filters: { |
8 |
| - title: (element) => element.title, |
9 |
| - id: (element) => element.id, |
10 |
| - valid: (element) => element.validity.valid, |
11 |
| - checked: (element) => element.checked, |
12 |
| - visible: { |
13 |
| - apply: (element) => isVisible(element) || (element.labels && Array.from(element.labels).some(isVisible)), |
14 |
| - default: true |
15 |
| - }, |
16 |
| - disabled: { |
17 |
| - apply: (element) => element.disabled, |
18 |
| - default: false |
19 |
| - }, |
20 |
| - focused |
| 5 | +const filters = { |
| 6 | + title: (element: HTMLInputElement) => element.title, |
| 7 | + id: (element: HTMLInputElement) => element.id, |
| 8 | + valid: (element: HTMLInputElement) => element.validity.valid, |
| 9 | + checked: (element: HTMLInputElement) => element.checked, |
| 10 | + visible: { |
| 11 | + apply: (element: HTMLInputElement) => isVisible(element) || (element.labels && Array.from(element.labels).some(isVisible)), |
| 12 | + default: true |
21 | 13 | },
|
22 |
| - actions: { |
23 |
| - click: perform((element) => { element.click(); }), |
24 |
| - check: perform((element) => { if(!element.checked) element.click(); }), |
25 |
| - uncheck: perform((element) => { if(element.checked) element.click(); }), |
26 |
| - toggle: perform((element) => { element.click(); }), |
| 14 | + disabled: { |
| 15 | + apply: (element: HTMLInputElement) => element.disabled, |
| 16 | + default: false |
27 | 17 | },
|
| 18 | + focused |
| 19 | +}; |
| 20 | + |
| 21 | +const actions = { |
| 22 | + click: perform((element: HTMLInputElement) => { element.click(); }), |
| 23 | + check: perform((element: HTMLInputElement) => { if(!element.checked) element.click(); }), |
| 24 | + uncheck: perform((element: HTMLInputElement) => { if(element.checked) element.click(); }), |
| 25 | + toggle: perform((element: HTMLInputElement) => { element.click(); }), |
| 26 | +}; |
| 27 | + |
| 28 | +type CheckboxConstructor = InteractorConstructor<HTMLInputElement, typeof filters, typeof actions>; |
| 29 | + |
| 30 | +export interface CheckboxFilters extends FilterParams<HTMLInputElement, typeof filters> { |
| 31 | + /** |
| 32 | + * Filter by title |
| 33 | + */ |
| 34 | + title?: string; |
| 35 | + /** |
| 36 | + * Filter by id |
| 37 | + */ |
| 38 | + id?: string; |
| 39 | + /** |
| 40 | + * Filter by visibility. See {@link isVisible}. |
| 41 | + */ |
| 42 | + valid?: boolean; |
| 43 | + /** |
| 44 | + * Filter by whether the checkbox is valid. |
| 45 | + */ |
| 46 | + checked?: boolean; |
| 47 | + /** |
| 48 | + * Filter by whether the checkbox is checked. |
| 49 | + */ |
| 50 | + visible?: boolean; |
| 51 | + /** |
| 52 | + * Filter by whether the checkbox is disabled. |
| 53 | + */ |
| 54 | + disabled?: boolean; |
| 55 | + /** |
| 56 | + * Filter by whether the checkbox is focused. |
| 57 | + */ |
| 58 | + focused?: boolean; |
| 59 | +} |
| 60 | + |
| 61 | +export interface CheckboxActions extends ActionMethods<HTMLInputElement, typeof actions> { |
| 62 | + /** |
| 63 | + * Click on the checkbox |
| 64 | + */ |
| 65 | + click(): Interaction<void>; |
| 66 | + /** |
| 67 | + * Check the checkbox if it is not checked |
| 68 | + */ |
| 69 | + check(): Interaction<void>; |
| 70 | + /** |
| 71 | + * Uncheck the checkbox if it is checked |
| 72 | + */ |
| 73 | + uncheck(): Interaction<void>; |
| 74 | + /** |
| 75 | + * Toggle the checkbox |
| 76 | + */ |
| 77 | + toggle(): Interaction<void>; |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Call this {@link InteractorConstructor} to initialize a checkbox interactor. |
| 82 | + * The checkbox interactor can be used to interact with checkboxes on the page and |
| 83 | + * to assert on their state. |
| 84 | + * |
| 85 | + * The checkbox is located by the text of its label. |
| 86 | + * |
| 87 | + * ### Example |
| 88 | + * |
| 89 | + * ``` typescript |
| 90 | + * await Checbox('Submit').click(); |
| 91 | + * await Checbox('Submit').is({ disabled: true }); |
| 92 | + * await Checbox({ id: 'submit-button', disabled: true }).exists(); |
| 93 | + * ``` |
| 94 | + * |
| 95 | + * ### See also |
| 96 | + * |
| 97 | + * - {@link ButtonFilters}: filters defined for this interactor |
| 98 | + * - {@link ButtonActions}: actions callable on instances of this interactor |
| 99 | + * - {@link InteractorConstructor}: how to create an interactor instance from this constructor |
| 100 | + * - {@link Interactor}: interface of instances of this interactor in addition to its actions |
| 101 | + * |
| 102 | + * @category Interactor |
| 103 | + */ |
| 104 | +export const CheckBox: CheckboxConstructor = createInteractor<HTMLInputElement>('check box')({ |
| 105 | + selector: 'input[type=checkbox]', |
| 106 | + locator: (element) => element.labels ? (Array.from(element.labels)[0]?.textContent || '') : '', |
| 107 | + filters, |
| 108 | + actions, |
28 | 109 | });
|
0 commit comments