Skip to content

Commit 4c97ad1

Browse files
committed
cypress-commands: fix clickUi5ListItemByText
1 parent 303f543 commit 4c97ad1

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

packages/cypress-commands/src/commands.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,17 @@ declare global {
6464
closeUi5PopupWithEsc(): Chainable<Element>;
6565

6666
/**
67-
* Click on a list item of the `List` component by text.
67+
* Click on a list item of the `ui5-list` component by text.
68+
*
69+
* __Note:__ Chaining this command to a `ui5-list` selector is recommended.
70+
*
6871
* @param {string} text The text of the list item that should be clicked.
69-
* @example cy.clickUi5ListItemByText("List Item")
72+
* @param options ClickOptions
73+
* @example
74+
* cy.get('[ui5-list]').clickUi5ListItemByText("List Item")
75+
* cy.clickUi5ListItemByText("List Item")
7076
*/
71-
clickUi5ListItemByText(text: string): Chainable<Element>;
77+
clickUi5ListItemByText(text: string, options: Partial<ClickOptions>): Chainable<Element>;
7278

7379
/**
7480
* Click on an `ui5-option` of the `ui5-select` component by text.
@@ -188,8 +194,17 @@ Cypress.Commands.add('closeUi5PopupWithEsc', () => {
188194
cy.get('body').type('{esc}', { force: true });
189195
});
190196

191-
Cypress.Commands.add('clickUi5ListItemByText', (text) => {
192-
cy.contains(text).find('li').click({ force: true });
197+
Cypress.Commands.add('clickUi5ListItemByText', { prevSubject: 'optional' }, (subject, text) => {
198+
cy.document().then((doc) => {
199+
const _subject = subject?.[0] || doc;
200+
const li = _subject.querySelector(`[text="${text}"]`);
201+
202+
if (li) {
203+
cy.wrap(li).click();
204+
} else {
205+
cy.wrap(_subject).contains(text).click();
206+
}
207+
});
193208
});
194209

195210
Cypress.Commands.add('clickUi5SelectOptionByText', { prevSubject: 'element' }, (subject, text, options = {}) => {

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,29 @@ describe('UI5 Web Components - Child Commands', () => {
124124

125125
it('click list item', () => {
126126
cy.mount(
127-
<List selectionMode="Multiple">
128-
<ListItemStandard data-testid="s" text="ListItemStandard" />
129-
<ListItemCustom data-testid="c">ListItemCustom</ListItemCustom>
130-
</List>
127+
<>
128+
<List selectionMode="Multiple" data-testid="list">
129+
<ListItemStandard data-testid="li1" text="ListItemStandard" />
130+
<ListItemStandard data-testid="li2">ListItemStandard2</ListItemStandard>
131+
<ListItemCustom data-testid="li3">ListItemCustom</ListItemCustom>
132+
</List>
133+
</>
131134
);
132-
cy.findByText('ListItemStandard').click();
133-
cy.findByTestId('c').click();
135+
cy.clickUi5ListItemByText('ListItemStandard');
136+
cy.clickUi5ListItemByText('ListItemStandard2');
137+
cy.clickUi5ListItemByText('ListItemCustom');
138+
139+
cy.findByTestId('li1').should('have.attr', 'selected', 'selected');
140+
cy.findByTestId('li2').should('have.attr', 'selected', 'selected');
141+
cy.findByTestId('li3').should('have.attr', 'selected', 'selected');
142+
143+
cy.get('[ui5-list]').clickUi5ListItemByText('ListItemStandard');
144+
cy.get('[ui5-list]').clickUi5ListItemByText('ListItemStandard2');
145+
cy.get('[ui5-list]').clickUi5ListItemByText('ListItemCustom');
146+
147+
cy.findByTestId('li1').should('not.have.attr', 'selected');
148+
cy.findByTestId('li2').should('not.have.attr', 'selected');
149+
cy.findByTestId('li3').should('not.have.attr', 'selected');
134150
});
135151

136152
// TODO figure out how to re-enable this test. Currently the popover directly closes after any interaction with the component.

0 commit comments

Comments
 (0)