Skip to content

Commit 3c25985

Browse files
authored
fix(cypress-commands): force "Enter" press for clickDropdownMenuItem (for ui5-select) (#7223)
1 parent 3970458 commit 3c25985

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

packages/cypress-commands/src/commands.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,29 +95,42 @@ declare global {
9595
clickUi5SelectOption(options?: Partial<ClickOptions>): Chainable<Element>;
9696

9797
/**
98-
* Click on an option of "select-like" components by text. Currently supported components are `ui5-select`, `ui5-combobox` and `ui5-multi-combobox`.
98+
* Click on an option of "select-like" components by text.
99+
* Currently supported components: `ui5-select`, `ui5-combobox` and `ui5-multi-combobox`.
99100
*
100101
* __Note:__ The popover must be visible, otherwise it can lead to unwanted side effects.
101102
*
102-
* __Note:__ Currently `Select` doesn't support `cy.click()` on `ui5-options` (or elements in the shadow root), because of this the option is now selected via "Enter" press.
103+
* __Note:__ `ui5-select` currently does not support `cy.click()` on `ui5-options` (or elements in the shadow root).
104+
* Instead, the `ui5-option` is selected via an "Enter" press (`.trigger`).
105+
* Therefore, for `ui5-select`, the `options` parameter only accepts `TriggerOptions`.
103106
*
104-
* @param text text of the item inside the popover that should be clicked
105-
* @param options Cypress.ClickOptions
106-
* @example cy.get('[ui5-select]').clickDropdownMenuItemByText('Option2');
107+
* @param text The text of the item inside the popover that should be clicked.
108+
* @param options Default:`Cypress.ClickOptions`. For `ui5-select`, use `Cypress.TriggerOptions` (you can use the generic type to adjust accordingly).
109+
*
110+
* @example
111+
* cy.get('[ui5-select]').clickDropdownMenuItemByText<Cypress.TriggerOptions>('Option2');
112+
* cy.get('[ui5-multi-combobox]').clickDropdownMenuItemByText('Option2');
107113
*
108114
*/
109-
clickDropdownMenuItemByText(text: string, options?: Partial<ClickOptions>): Chainable<Element>;
115+
clickDropdownMenuItemByText<T = ClickOptions>(text: string, options?: Partial<T>): Chainable<Element>;
110116

111117
/**
112-
* Click on a chained option of "select-like" components. Currently supported components are `ui5-option`, `ui5-mcb-item` and `ui5-cb-item` (since v1.24.3 of `@ui5/webcomponents`).
118+
* Click on a chained option of "select-like" components.
119+
* Currently supported components: `ui5-option`, `ui5-mcb-item` and `ui5-cb-item` (since v1.24.3 of `@ui5/webcomponents`).
113120
*
114121
* __Note:__ The popover must be visible, otherwise it can lead to unwanted side effects.
115122
*
116-
* __Note:__ Currently `Select` doesn't support `cy.click()` on `ui5-options` (or elements in the shadow root), because of this the option is now selected via "Enter" press.
123+
* __Note:__ `ui5-select` currently does not support `cy.click()` on `ui5-options` (or elements in the shadow root).
124+
* Instead, the `ui5-option` is selected via an "Enter" press (`.trigger`).
125+
* Therefore, for `ui5-option`, the `options` parameter only accepts `TriggerOptions`.
126+
*
127+
* @param options Default: `Cypress.ClickOptions`. For `ui5-option`, use `Cypress.TriggerOptions` (you can use the generic type to adjust accordingly).
117128
*
118-
* @example cy.get('[ui5-option]').clickDropdownMenuItem();
129+
* @example
130+
* cy.get('[ui5-option]').clickDropdownMenuItem<Cypress.TriggerOptions>();
131+
* cy.get('[ui5-mcb-item]').clickDropdownMenuItem();
119132
*/
120-
clickDropdownMenuItem(options?: Partial<ClickOptions>): Chainable<Element>;
133+
clickDropdownMenuItem<T = ClickOptions>(options?: Partial<T>): Chainable<Element>;
121134

122135
/**
123136
* Click on the open button in "select-like" components to open the popover. Currently supported components are `ui5-select`, `ui5-combobox` and `ui5-multi-combobox`.
@@ -201,7 +214,7 @@ Cypress.Commands.add('clickDropdownMenuItemByText', { prevSubject: 'element' },
201214
cy.wrap(subject).then(([$dropdown]) => {
202215
switch (true) {
203216
case $dropdown.hasAttribute('ui5-select'):
204-
cy.wrap($dropdown).contains(text).clickDropdownMenuItem(options);
217+
cy.wrap($dropdown).contains(text).clickDropdownMenuItem<Cypress.TypeOptions>(options);
205218
break;
206219
default:
207220
cy.wrap($dropdown).get(`[text="${text}"]`).clickDropdownMenuItem(options);
@@ -216,7 +229,14 @@ Cypress.Commands.add('clickDropdownMenuItem', { prevSubject: 'element' }, (subje
216229
cy.wrap(domRef).focus();
217230
if ($option.hasAttribute('ui5-option')) {
218231
//todo: check if this can be refactored to use `click()` again.
219-
cy.wrap(domRef).type('{enter}');
232+
cy.wrap(domRef).trigger('keydown', {
233+
force: true,
234+
...options,
235+
key: 'Enter',
236+
code: 'Enter',
237+
which: 13,
238+
keyCode: 13
239+
});
220240
} else {
221241
cy.wrap(domRef).click(options);
222242
}

packages/cypress-commands/test/UI5WebComponentsChild.cy.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ describe('UI5 Web Components - Child Commands', () => {
267267
];
268268

269269
components.forEach((component) => {
270-
it(component.key, () => {
270+
it(component.key!, () => {
271271
changeSpy = cy.spy().as('change');
272272
cy.mount(component);
273273
cy.get(`[${component.key}]`).openDropDownByClick();

0 commit comments

Comments
 (0)