Skip to content

Commit 5f36e46

Browse files
authored
feat(cypress-commands): add clickUi5SelectOptionByText & clickUi5SelectOption commands (#5150)
1 parent c1eafa5 commit 5f36e46

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

packages/cypress-commands/src/commands.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,23 @@ declare global {
6565
* @example cy.clickUi5ListItemByText("List Item")
6666
*/
6767
clickUi5ListItemByText(text: string): Chainable<Element>;
68+
69+
/**
70+
* Click on an `ui5-option` of the `ui5-select` component by text.
71+
* @param text text of the ui5-option that should be clicked
72+
* @example cy.get('[ui5-select]').clickUi5SelectOptionByText('Option2');
73+
*
74+
* __Note:__ The select popover must be visible, otherwise the `change` event is not fired.
75+
*/
76+
clickUi5SelectOptionByText(text: string, options?: Partial<ClickOptions>): Chainable<Element>;
77+
78+
/**
79+
* Click on chained `ui5-option`.
80+
* @example cy.get('[ui5-option]').clickUi5SelectOption();
81+
*
82+
* __Note:__ The select popover must be visible, otherwise the `change` event is not fired.
83+
*/
84+
clickUi5SelectOption(options?: Partial<ClickOptions>): Chainable<Element>;
6885
}
6986
}
7087
}
@@ -119,4 +136,20 @@ Cypress.Commands.add('clickUi5ListItemByText', (text) => {
119136
cy.contains(text).find('li').click({ force: true });
120137
});
121138

139+
Cypress.Commands.add('clickUi5SelectOptionByText', { prevSubject: 'element' }, (subject, text, options = {}) => {
140+
cy.wrap(subject).then(async ($select) => {
141+
// @ts-expect-error: cannot set $select to use SelectDomRef
142+
const domRef = await $select.get(0).getStaticAreaItemDomRef();
143+
cy.wrap(domRef).contains(text).click(options);
144+
});
145+
});
146+
147+
Cypress.Commands.add('clickUi5SelectOption', { prevSubject: 'element' }, (subject, options = {}) => {
148+
cy.wrap(subject).then(($option) => {
149+
// @ts-expect-error: cannot set $option to use OptionDomRef
150+
const domRef = $option.get(0).getDomRef();
151+
cy.wrap(domRef).click(options);
152+
});
153+
});
154+
122155
export {};

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ import {
1313
MultiComboBoxItem,
1414
ComboBox,
1515
ComboBoxItem,
16-
SuggestionItem
16+
SuggestionItem,
17+
Select,
18+
Option
1719
} from '@ui5/webcomponents-react';
1820
import '@ui5/webcomponents/dist/features/InputSuggestions.js';
1921

2022
describe('UI5 Web Components - Child Commands', () => {
2123
it('clickUi5Tab', () => {
2224
cy.mount(
23-
<TabContainer data-testId={'tabContainer'}>
25+
<TabContainer>
2426
<Tab data-testId="tab1" text={'Tab 1'} selected>
2527
Tab 2
2628
</Tab>
@@ -30,7 +32,7 @@ describe('UI5 Web Components - Child Commands', () => {
3032
</TabContainer>
3133
);
3234

33-
cy.findByTestId('tabContainer').findUi5TabByText('Tab 2').click();
35+
cy.get('[ui5-tabcontainer]').findUi5TabByText('Tab 2').click();
3436
cy.findByTestId('tab1').should('not.have.attr', 'selected');
3537
cy.findByTestId('tab2').should('have.attr', 'selected');
3638
});
@@ -154,4 +156,30 @@ describe('UI5 Web Components - Child Commands', () => {
154156
document.querySelector('ui5-static-area')?.remove();
155157
});
156158
});
159+
160+
it('click Option of Select', () => {
161+
const select = cy.spy().as('select');
162+
cy.mount(
163+
<Select onChange={select}>
164+
<Option>Test1</Option>
165+
<Option>Test2</Option>
166+
<Option>Test3</Option>
167+
<Option data-testid="4">Test4</Option>
168+
<Option data-testid="5">Test5</Option>
169+
</Select>
170+
);
171+
cy.get('[ui5-select]').click();
172+
cy.get('[ui5-select]').clickUi5SelectOptionByText('Test2');
173+
cy.get('@select').should('have.been.calledOnce');
174+
cy.get('[ui5-select]').clickUi5SelectOptionByText('Test3', { force: true });
175+
// the web component doesn't fire the event if the popover is not opened
176+
cy.get('@select').should('have.been.calledOnce');
177+
178+
cy.get('[ui5-select]').click();
179+
cy.findByTestId('5').clickUi5SelectOption();
180+
cy.get('@select').should('have.been.calledTwice');
181+
cy.findByTestId('4').clickUi5SelectOption({ force: true });
182+
// the web component doesn't fire the event if the popover is not opened
183+
cy.get('@select').should('have.been.calledTwice');
184+
});
157185
});

packages/cypress-commands/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"declarationDir": "./dist",
1010
"rootDir": "./src",
1111
"types": ["cypress"],
12-
"strict": true
12+
"strict": true,
13+
"skipLibCheck": true
1314
},
1415
"include": ["src"]
1516
}

0 commit comments

Comments
 (0)