Skip to content

Commit 03c9767

Browse files
committed
Start adding docs for other interactor
1 parent 82cf6e6 commit 03c9767

File tree

2 files changed

+106
-23
lines changed

2 files changed

+106
-23
lines changed

packages/interactor/src/definitions/button.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ export interface ButtonActions extends ActionMethods<ButtonElement, typeof actio
8585
* - {@link ButtonActions}: actions callable on instances of this interactor
8686
* - {@link InteractorConstructor}: how to create an interactor instance from this constructor
8787
* - {@link Interactor}: interface of instances of this interactor in addition to its actions
88+
*
89+
* @category Interactor
8890
*/
8991
export const Button: ButtonConstructor = createInteractor<ButtonElement>('button')({
9092
selector: 'button,input[type=button],input[type=submit],input[type=reset],input[type=image]',
Lines changed: 104 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,109 @@
1-
import { createInteractor, perform, focused } from '../index';
1+
import { Interaction, InteractorConstructor, createInteractor, perform, focused } from '../index';
22
import { isVisible } from 'element-is-visible';
3+
import { FilterParams, ActionMethods } from '../specification';
34

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
2113
},
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
2717
},
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,
28109
});

0 commit comments

Comments
 (0)