Skip to content

Commit de86f23

Browse files
committed
Small copy updates, Cypress testing docs
1 parent 834a043 commit de86f23

File tree

1 file changed

+29
-50
lines changed

1 file changed

+29
-50
lines changed

docs/pages/teamshares/recipes/cypress.md

+29-50
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,18 @@ meta:
55

66
# Shoelace & Cypress
77

8-
> Tips for testing Shoelace components with Cypress
8+
> Testing Shoelace components with Cypress
99
10-
## TL;DR
10+
## Cypress Commands Cheat Sheet
1111

12-
For the most part, you can use Shoelace components the same way you'd use their HTML equivalents, since they emit many of the same events (`click`, `focus`, etc). But like all web components, Shoelace components encapsulate their internal parts within the [shadow dom](https://css-tricks.com/styling-in-the-shadow-dom-with-css-shadow-parts/).
13-
14-
This means that the internals of Shoelace components aren't available directly on the DOM (via `document.querySelector`, etc.), but have to be queried via the [`Element.shadowRoot` property](https://developer.mozilla.org/en-US/docs/Web/API/Element/shadowRoot).
15-
16-
Cypress provides a convenience method for accessing the shadow DOM via the [`.shadow()` method.](https://docs.cypress.io/api/commands/shadow).
17-
18-
For example, to find the anchor tag within a link button:
19-
20-
```js
21-
cy.get('sl-button[href]').shadow().find('a');
22-
```
23-
24-
To click on a specific button:
25-
26-
```js
27-
cy.get('[data-test-id="some_sl_button"]').click();
28-
```
29-
30-
Also see below for more on testing Shoelace design system components with Cypress:
31-
32-
- [Cypress Commands Cheat Sheet](#cypress-commands-cheat-sheet)
33-
- [Generic Cypress Commands](#generic-commands)
34-
- [Custom Cypress Commands](#custom-commands)
35-
- [Form Components with Cypress Documentation](#form-components-with-cypress-documentation)
36-
37-
### Cypress Commands Cheat Sheet
38-
39-
<div class="cy-table-desc">This is a quick reference guide to common test actions for form components.</div>
12+
<div class="cy-table-desc">A quick reference guide to common test actions for form components.</div>
4013

4114
| Component | Test action | Cypress command |
4215
| --------------------- | --------------------------------- | ---------------------------------------------------------------------- |
4316
| **Checkbox** | <span>check</span> | [`cy.slCheckboxCheck()`](#cy_slcheckboxcheck) |
4417
| | <span>uncheck</span> | [`cy.slCheckboxUncheck()`](#cy_slcheckboxuncheck) |
45-
| | <span>is checked?</span> | [`cy.get().should("have.prop", "checked", true)`](#shouldhave_prop) |
46-
| | <span>is not checked?</span> | [`cy.get().should("have.prop", "checked", false)`](#shouldhave_prop) |
18+
| | <span>checked?</span> | [`cy.get().should("have.prop", "checked", true)`](#shouldhave_prop) |
19+
| | <span>not checked?</span> | [`cy.get().should("have.prop", "checked", false)`](#shouldhave_prop) |
4720
| **Checkbox Group** | <span>value?</span> | [`cy.slCheckboxGroupValue()`](#cy_slcheckboxgroupvalue) |
4821
| | <span>not value?</span> | [`cy.get().should("not.have.value", "value")`](#shouldhave_value) |
4922
| **Input** | <span>focus</span> | [`cy.slFocus()`](#cy_slfocus) |
@@ -63,7 +36,7 @@ Also see below for more on testing Shoelace design system components with Cypres
6336

6437
## Generic Commands
6538

66-
The following are generic Cypress commands that can be used for testing Shoelace design system components:
39+
Generic Cypress commands that can be used for testing Shoelace design system components:
6740

6841
### `click()`
6942

@@ -73,10 +46,6 @@ Use to **click** an element like [sl-button](/components/button) or radio item i
7346
cy.get(`[data-test-id="item-test-1"]`).click();
7447
```
7548

76-
:::tip
77-
What other elements does this work for?
78-
:::
79-
8049
### `should('have.prop')`
8150

8251
Use on boolean elements like [sl-checkbox](/components/checkbox/#with-cypress) and [sl-switch](/components/switch/#with-cypress) to verify whether the input is checked:
@@ -106,13 +75,9 @@ Can also use to verify that an input does **NOT** have a certain value:
10675
cy.get(`[data-test-id="checkbox-group-test"]`).should('not.have.value', 'option-1');
10776
```
10877

109-
:::tip
110-
Does this also work for Checkbox Group? Select?
111-
:::
112-
11378
## Custom Commands
11479

115-
The following are custom Cypress commands created for testing Shoelace design system components:
80+
Custom Cypress commands created for testing Shoelace design system components:
11681

11782
### `cy.slCheckboxCheck()`
11883

@@ -162,10 +127,6 @@ Use to **focus** on elements like [sl-input](/components/input/#with-cypress) an
162127
cy.slFocus(`[data-test-id="input-test"]`);
163128
```
164129

165-
:::tip
166-
What other elements does this work for?
167-
:::
168-
169130
### `cy.slClear()`
170131

171132
Use to **clear** [sl-input](/components/input/#with-cypress):
@@ -174,10 +135,6 @@ Use to **clear** [sl-input](/components/input/#with-cypress):
174135
cy.slClear(`[data-test-id="input-test"]`);
175136
```
176137

177-
:::tip
178-
What other elements does this work for?
179-
:::
180-
181138
### `cy.slTextAreaClear()`
182139

183140
Use to **clear** [sl-textarea](/components/textarea/#with-cypress):
@@ -220,6 +177,28 @@ You can find additional documentation for testing specific components with Cypre
220177
- [sl-switch](/components/switch/#testing)
221178
- [sl-textarea](/components/textarea/#testing)
222179

180+
## Writing Custom Cypress Commands
181+
182+
For the most part, you can use Shoelace components the same way you'd use their HTML equivalents, since they emit many of the same events (`click`, `focus`, etc).
183+
184+
But like all web components, Shoelace components encapsulate their internal parts within the [shadow dom](https://css-tricks.com/styling-in-the-shadow-dom-with-css-shadow-parts/).
185+
186+
This means that the internals of Shoelace components aren't available directly on the DOM (via `document.querySelector`, etc.), but have to be queried via the [`Element.shadowRoot` property](https://developer.mozilla.org/en-US/docs/Web/API/Element/shadowRoot).
187+
188+
Cypress provides a convenience method for accessing the shadow DOM via the [`.shadow()` method.](https://docs.cypress.io/api/commands/shadow).
189+
190+
For example, to find the anchor tag within a link button:
191+
192+
```js
193+
cy.get('sl-button[href]').shadow().find('a');
194+
```
195+
196+
To click on a specific button:
197+
198+
```js
199+
cy.get('[data-test-id="some_sl_button"]').click();
200+
```
201+
223202
## Questions and Feedback
224203

225204
Commands not working as expected? Need a specific command for testing but not seeing it here?

0 commit comments

Comments
 (0)