eslint-plugin-cypress 4.2.0 → 4.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +26 -0
- package/README.md +1 -0
- package/circle.yml +82 -9
- package/docs/rules/no-chained-get.md +19 -0
- package/eslint.config.mjs +1 -0
- package/legacy.js +1 -0
- package/lib/flat.js +1 -0
- package/lib/rules/no-chained-get.js +87 -0
- package/package.json +6 -6
- package/test-project/README.md +48 -0
- package/test-project/cypress/e2e/1-getting-started/todo.cy.js +143 -0
- package/test-project/cypress/e2e/2-advanced-examples/actions.cy.js +321 -0
- package/test-project/cypress/e2e/2-advanced-examples/aliasing.cy.js +39 -0
- package/test-project/cypress/e2e/2-advanced-examples/assertions.cy.js +176 -0
- package/test-project/cypress/e2e/2-advanced-examples/connectors.cy.js +98 -0
- package/test-project/cypress/e2e/2-advanced-examples/cookies.cy.js +118 -0
- package/test-project/cypress/e2e/2-advanced-examples/cypress_api.cy.js +185 -0
- package/test-project/cypress/e2e/2-advanced-examples/files.cy.js +85 -0
- package/test-project/cypress/e2e/2-advanced-examples/location.cy.js +32 -0
- package/test-project/cypress/e2e/2-advanced-examples/misc.cy.js +90 -0
- package/test-project/cypress/e2e/2-advanced-examples/navigation.cy.js +55 -0
- package/test-project/cypress/e2e/2-advanced-examples/network_requests.cy.js +163 -0
- package/test-project/cypress/e2e/2-advanced-examples/querying.cy.js +114 -0
- package/test-project/cypress/e2e/2-advanced-examples/spies_stubs_clocks.cy.js +204 -0
- package/test-project/cypress/e2e/2-advanced-examples/storage.cy.js +117 -0
- package/test-project/cypress/e2e/2-advanced-examples/traversal.cy.js +121 -0
- package/test-project/cypress/e2e/2-advanced-examples/utilities.cy.js +107 -0
- package/test-project/cypress/e2e/2-advanced-examples/viewport.cy.js +58 -0
- package/test-project/cypress/e2e/2-advanced-examples/waiting.cy.js +30 -0
- package/test-project/cypress/e2e/2-advanced-examples/window.cy.js +22 -0
- package/test-project/cypress/fixtures/example.json +5 -0
- package/test-project/cypress/support/commands.js +25 -0
- package/test-project/cypress/support/e2e.js +17 -0
- package/test-project/cypress.config.js +9 -0
- package/test-project/eslint-configs/eslint.default.mjs +9 -0
- package/test-project/eslint-configs/eslint.globals.mjs +4 -0
- package/test-project/eslint-configs/eslint.recommended.mjs +9 -0
- package/test-project/eslint.config.mjs +9 -0
- package/test-project/package.json +9 -0
- package/tests/lib/rules/no-chained-get.js +31 -0
package/CONTRIBUTING.md
CHANGED
|
@@ -48,3 +48,29 @@ This plugin uses the ESLint [eslint-doc-generator](https://www.npmjs.com/package
|
|
|
48
48
|
|
|
49
49
|
* The directory [tests-legacy](https://github.com/cypress-io/eslint-plugin-cypress/tree/master/tests-legacy) contains tests which are compatible with the legacy [ESLint v8 RuleTester](https://eslint.org/docs/v8.x/integrate/nodejs-api#ruletester) utility. It is not expected to add new rules to this set of tests.
|
|
50
50
|
* The directory [tests](https://github.com/cypress-io/eslint-plugin-cypress/tree/master/tests) is for tests compatible with the current [ESLint RuleTester](https://eslint.org/docs/latest/integrate/nodejs-api#ruletester).
|
|
51
|
+
|
|
52
|
+
## Merging pull requests
|
|
53
|
+
|
|
54
|
+
This information is for Cypress.io Members or Collaborators who merge pull requests:
|
|
55
|
+
|
|
56
|
+
1. When merging a pull request, use the [Squash and merge](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/configuring-pull-request-merges/about-merge-methods-on-github#squashing-your-merge-commits) option to squash all commits into one.
|
|
57
|
+
1. Make sure the commit subject and body follow [semantic commit convention](https://semantic-release.gitbook.io/semantic-release/#commit-message-format), for instance:
|
|
58
|
+
|
|
59
|
+
```text
|
|
60
|
+
feat: added new parameter
|
|
61
|
+
fix: fixed a bug
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
If you need to bump the major version, mark it as breaking change in the body of the commit's message like:
|
|
65
|
+
|
|
66
|
+
```text
|
|
67
|
+
fix: upgrade dependency X
|
|
68
|
+
|
|
69
|
+
BREAKING CHANGE: requires minimum Node.js 20 to run
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
1. New versions of this module will be released automatically by the CI pipeline when any PR with a triggering commit message is merged to the `master` branch: see the `release` job of [circle.yml](circle.yml).
|
|
73
|
+
This will create a new [GitHub release](https://github.com/cypress-io/eslint-plugin-cypress/releases) and publish it to [eslint-plugin-cypress](https://www.npmjs.com/package/eslint-plugin-cypress) on the [npm registry](https://docs.npmjs.com/about-the-public-npm-registry).
|
|
74
|
+
1. The module's CI is configured to use the [default Angular release rules](https://github.com/semantic-release/commit-analyzer/blob/master/lib/default-release-rules.js).
|
|
75
|
+
This means that only `feat:`, `fix:` and `perf:` trigger a new release.
|
|
76
|
+
Other Angular commit types listed in the [Angular commit message guidelines](https://github.com/angular/angular/blob/main/contributing-docs/commit-message-guidelines.md) can be used for documentation purposes, however they are ignored by the currently configured release process.
|
package/README.md
CHANGED
|
@@ -50,6 +50,7 @@ These rules enforce some of the [best practices recommended for using Cypress](h
|
|
|
50
50
|
| [no-assigning-return-values](docs/rules/no-assigning-return-values.md) | disallow assigning return values of `cy` calls | ✅ |
|
|
51
51
|
| [no-async-before](docs/rules/no-async-before.md) | disallow using `async`/`await` in Cypress `before` methods | |
|
|
52
52
|
| [no-async-tests](docs/rules/no-async-tests.md) | disallow using `async`/`await` in Cypress test cases | ✅ |
|
|
53
|
+
| [no-chained-get](docs/rules/no-chained-get.md) | disallow chain of `cy.get()` calls | |
|
|
53
54
|
| [no-debug](docs/rules/no-debug.md) | disallow using `cy.debug()` calls | |
|
|
54
55
|
| [no-force](docs/rules/no-force.md) | disallow using `force: true` with action commands | |
|
|
55
56
|
| [no-pause](docs/rules/no-pause.md) | disallow using `cy.pause()` calls | |
|
package/circle.yml
CHANGED
|
@@ -1,17 +1,37 @@
|
|
|
1
1
|
version: 2.1
|
|
2
2
|
|
|
3
|
+
executors:
|
|
4
|
+
docker-executor:
|
|
5
|
+
docker:
|
|
6
|
+
- image: cimg/node:22.14.0
|
|
7
|
+
resource_class: medium
|
|
8
|
+
|
|
3
9
|
workflows:
|
|
4
|
-
version: 2.1
|
|
5
10
|
main:
|
|
6
11
|
jobs:
|
|
7
12
|
- lint
|
|
8
13
|
- test-v8
|
|
9
14
|
- test-v9
|
|
15
|
+
|
|
16
|
+
- build-test-project
|
|
17
|
+
- test-test-project:
|
|
18
|
+
matrix:
|
|
19
|
+
parameters:
|
|
20
|
+
eslint-version: ['9']
|
|
21
|
+
config-file: [
|
|
22
|
+
# configurations correspond to examples in README
|
|
23
|
+
'default',
|
|
24
|
+
'recommended',
|
|
25
|
+
'globals',
|
|
26
|
+
]
|
|
27
|
+
requires:
|
|
28
|
+
- build-test-project
|
|
10
29
|
- release:
|
|
11
30
|
requires:
|
|
12
31
|
- lint
|
|
13
32
|
- test-v8
|
|
14
33
|
- test-v9
|
|
34
|
+
- test-test-project
|
|
15
35
|
filters:
|
|
16
36
|
branches:
|
|
17
37
|
only:
|
|
@@ -19,8 +39,7 @@ workflows:
|
|
|
19
39
|
|
|
20
40
|
jobs:
|
|
21
41
|
lint:
|
|
22
|
-
docker
|
|
23
|
-
- image: cimg/node:22.11.0
|
|
42
|
+
executor: docker-executor
|
|
24
43
|
steps:
|
|
25
44
|
- checkout
|
|
26
45
|
- run:
|
|
@@ -34,8 +53,7 @@ jobs:
|
|
|
34
53
|
command: npm run lint
|
|
35
54
|
|
|
36
55
|
test-v8:
|
|
37
|
-
docker
|
|
38
|
-
- image: cimg/node:22.11.0
|
|
56
|
+
executor: docker-executor
|
|
39
57
|
steps:
|
|
40
58
|
- checkout
|
|
41
59
|
- run:
|
|
@@ -52,8 +70,7 @@ jobs:
|
|
|
52
70
|
command: npm run test:legacy
|
|
53
71
|
|
|
54
72
|
test-v9:
|
|
55
|
-
docker
|
|
56
|
-
- image: cimg/node:22.11.0
|
|
73
|
+
executor: docker-executor
|
|
57
74
|
steps:
|
|
58
75
|
- checkout
|
|
59
76
|
- run:
|
|
@@ -69,9 +86,65 @@ jobs:
|
|
|
69
86
|
name: Test ESLint 9
|
|
70
87
|
command: npm test
|
|
71
88
|
|
|
89
|
+
build-test-project:
|
|
90
|
+
executor: docker-executor
|
|
91
|
+
steps:
|
|
92
|
+
- checkout
|
|
93
|
+
- run:
|
|
94
|
+
name: Install dependencies
|
|
95
|
+
command: npm ci
|
|
96
|
+
- run:
|
|
97
|
+
name: Build tarball
|
|
98
|
+
command: npm pack
|
|
99
|
+
- run:
|
|
100
|
+
name: Get version
|
|
101
|
+
command: |
|
|
102
|
+
echo "PLUGIN_VERSION=$(jq -r '.version' package.json)" >> $BASH_ENV
|
|
103
|
+
cp $BASH_ENV bash.env
|
|
104
|
+
- persist_to_workspace:
|
|
105
|
+
root: .
|
|
106
|
+
paths:
|
|
107
|
+
- eslint-plugin-cypress-*.tgz
|
|
108
|
+
- bash.env
|
|
109
|
+
|
|
110
|
+
test-test-project:
|
|
111
|
+
description: Run ESLint with different configurations
|
|
112
|
+
parameters:
|
|
113
|
+
eslint-version:
|
|
114
|
+
description: Version of ESLint to use
|
|
115
|
+
default: 'latest'
|
|
116
|
+
type: string
|
|
117
|
+
config-file:
|
|
118
|
+
description: Configuration file
|
|
119
|
+
default: 'default'
|
|
120
|
+
type: string
|
|
121
|
+
executor: docker-executor
|
|
122
|
+
working_directory: ./test-project
|
|
123
|
+
steps:
|
|
124
|
+
- checkout:
|
|
125
|
+
path: ../
|
|
126
|
+
- attach_workspace:
|
|
127
|
+
at: .
|
|
128
|
+
- run:
|
|
129
|
+
name: Get plugin version
|
|
130
|
+
command: |
|
|
131
|
+
cat bash.env >> $BASH_ENV
|
|
132
|
+
- run:
|
|
133
|
+
name: Install dependencies
|
|
134
|
+
command: |
|
|
135
|
+
npm install eslint@<< parameters.eslint-version>> ./eslint-plugin-cypress-$PLUGIN_VERSION.tgz -D
|
|
136
|
+
- run:
|
|
137
|
+
name: Display ESLint version
|
|
138
|
+
command: |
|
|
139
|
+
npx eslint --version
|
|
140
|
+
- run: echo Testing a << parameters.config-file >> configuration
|
|
141
|
+
- run:
|
|
142
|
+
name: Lint with example configuration
|
|
143
|
+
command: |
|
|
144
|
+
npx eslint --config ./eslint-configs/eslint.<< parameters.config-file >>.mjs .
|
|
145
|
+
|
|
72
146
|
release:
|
|
73
|
-
docker
|
|
74
|
-
- image: cimg/node:22.11.0
|
|
147
|
+
executor: docker-executor
|
|
75
148
|
steps:
|
|
76
149
|
- checkout
|
|
77
150
|
- run:
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Disallow chain of `cy.get()` calls (`cypress/no-chained-get`)
|
|
2
|
+
|
|
3
|
+
<!-- end auto-generated rule header -->
|
|
4
|
+
This rule disallows the usage of chained `.get()` calls as `cy.get()` always starts its search from the cy.root element.
|
|
5
|
+
|
|
6
|
+
## Rule Details
|
|
7
|
+
|
|
8
|
+
Examples of **incorrect** code for this rule:
|
|
9
|
+
|
|
10
|
+
```js
|
|
11
|
+
cy.get('parent').get('child')
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Examples of **correct** code for this rule:
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
cy.get('parent')
|
|
18
|
+
.find('child')
|
|
19
|
+
```
|
package/eslint.config.mjs
CHANGED
package/legacy.js
CHANGED
|
@@ -13,6 +13,7 @@ module.exports = {
|
|
|
13
13
|
'no-pause': require('./lib/rules/no-pause'),
|
|
14
14
|
'no-debug': require('./lib/rules/no-debug'),
|
|
15
15
|
'no-xpath': require('./lib/rules/no-xpath'),
|
|
16
|
+
'no-chained-get': require('./lib/rules/no-chained-get'),
|
|
16
17
|
},
|
|
17
18
|
configs: {
|
|
18
19
|
recommended: require('./lib/config/recommended'),
|
package/lib/flat.js
CHANGED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
//------------------------------------------------------------------------------
|
|
4
|
+
// Rule Definition
|
|
5
|
+
//------------------------------------------------------------------------------
|
|
6
|
+
|
|
7
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
|
8
|
+
module.exports = {
|
|
9
|
+
meta: {
|
|
10
|
+
type: 'problem',
|
|
11
|
+
docs: {
|
|
12
|
+
description: 'disallow chain of `cy.get()` calls',
|
|
13
|
+
recommended: false,
|
|
14
|
+
url: 'https://github.com/cypress-io/eslint-plugin-cypress/blob/master/docs/rules/no-chained-get.md',
|
|
15
|
+
},
|
|
16
|
+
fixable: null,
|
|
17
|
+
schema: [],
|
|
18
|
+
messages: {
|
|
19
|
+
unexpected: 'Avoid chaining multiple cy.get() calls',
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
create(context) {
|
|
24
|
+
const isRootCypress = (node) => {
|
|
25
|
+
if (
|
|
26
|
+
node.type !== 'CallExpression' ||
|
|
27
|
+
node.callee.type !== 'MemberExpression'
|
|
28
|
+
) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (
|
|
33
|
+
node.callee.object.type === 'Identifier' &&
|
|
34
|
+
node.callee.object.name === 'cy'
|
|
35
|
+
) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return isRootCypress(node.callee.object);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const hasChainedGet = (node) => {
|
|
43
|
+
// Check if this node is a get() call
|
|
44
|
+
const isGetCall =
|
|
45
|
+
node.callee &&
|
|
46
|
+
node.callee.type === 'MemberExpression' &&
|
|
47
|
+
node.callee.property &&
|
|
48
|
+
node.callee.property.type === 'Identifier' &&
|
|
49
|
+
node.callee.property.name === 'get';
|
|
50
|
+
|
|
51
|
+
if (!isGetCall) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const obj = node.callee.object;
|
|
56
|
+
|
|
57
|
+
if (obj.type === 'CallExpression') {
|
|
58
|
+
const objCallee = obj.callee;
|
|
59
|
+
|
|
60
|
+
if (
|
|
61
|
+
objCallee &&
|
|
62
|
+
objCallee.type === 'MemberExpression' &&
|
|
63
|
+
objCallee.property &&
|
|
64
|
+
objCallee.property.type === 'Identifier' &&
|
|
65
|
+
objCallee.property.name === 'get'
|
|
66
|
+
) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return hasChainedGet(obj);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return false;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
CallExpression(node) {
|
|
78
|
+
if (isRootCypress(node) && hasChainedGet(node)) {
|
|
79
|
+
context.report({
|
|
80
|
+
node,
|
|
81
|
+
messageId: 'unexpected',
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-cypress",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.0",
|
|
4
4
|
"description": "An ESLint plugin for projects using Cypress",
|
|
5
5
|
"main": "legacy.js",
|
|
6
6
|
"exports": {
|
|
@@ -26,15 +26,15 @@
|
|
|
26
26
|
"eslint": ">=9"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"globals": "^15.
|
|
29
|
+
"globals": "^15.15.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"eslint": "^9.
|
|
33
|
-
"eslint-plugin-eslint-plugin": "^6.
|
|
32
|
+
"eslint": "^9.24.0",
|
|
33
|
+
"eslint-plugin-eslint-plugin": "^6.4.0",
|
|
34
34
|
"eslint-plugin-mocha": "^10.5.0",
|
|
35
|
-
"husky": "^9.1.
|
|
35
|
+
"husky": "^9.1.7",
|
|
36
36
|
"jest": "^29.7.0",
|
|
37
|
-
"semantic-release": "24.2.
|
|
37
|
+
"semantic-release": "24.2.3"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
40
|
"lint": "eslint \"*.js\" \"**/**/*.js\"",
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Test-project
|
|
2
|
+
|
|
3
|
+
This test project was generated via the Cypress App.
|
|
4
|
+
|
|
5
|
+
This project can be updated to the latest Cypress default scaffolded E2E test specs by carrying out the following steps in the directory `/test-project`:
|
|
6
|
+
|
|
7
|
+
```shell
|
|
8
|
+
rm -rf cypress cypress.config.js
|
|
9
|
+
npm install cypress@latest --no-package-lock
|
|
10
|
+
npx cypress open
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
- Select "Continue" for "What's New in Cypress" if displayed
|
|
14
|
+
- Select "E2E Testing"
|
|
15
|
+
- Select "Continue" in "Configuration files"
|
|
16
|
+
- Select "Electron" browser
|
|
17
|
+
- Select "Start E2E Testing in Electron"
|
|
18
|
+
- Select "Scaffold example specs"
|
|
19
|
+
- Close all Cypress windows
|
|
20
|
+
|
|
21
|
+
Test that scaffolded specs run:
|
|
22
|
+
|
|
23
|
+
```shell
|
|
24
|
+
npm test
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Remove Cypress from `package.json`:
|
|
28
|
+
|
|
29
|
+
```shell
|
|
30
|
+
npm uninstall cypress --no-package-lock
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Tests
|
|
34
|
+
|
|
35
|
+
Tests are run via [circle.yml](../circle.yml).
|
|
36
|
+
|
|
37
|
+
To test the project locally:
|
|
38
|
+
|
|
39
|
+
```shell
|
|
40
|
+
cd test-project
|
|
41
|
+
npm install eslint@latest eslint-plugin-cypress@latest cypress@latest -D
|
|
42
|
+
npx cypress run
|
|
43
|
+
npx eslint
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Do not commit the changes from installing the above dependencies.
|
|
47
|
+
The CircleCI pipeline dynamically installs the latest `eslint*` dependencies.
|
|
48
|
+
Cypress is not needed for the CircleCI tests.
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/// <reference types="cypress" />
|
|
2
|
+
|
|
3
|
+
// Welcome to Cypress!
|
|
4
|
+
//
|
|
5
|
+
// This spec file contains a variety of sample tests
|
|
6
|
+
// for a todo list app that are designed to demonstrate
|
|
7
|
+
// the power of writing tests in Cypress.
|
|
8
|
+
//
|
|
9
|
+
// To learn more about how Cypress works and
|
|
10
|
+
// what makes it such an awesome testing tool,
|
|
11
|
+
// please read our getting started guide:
|
|
12
|
+
// https://on.cypress.io/introduction-to-cypress
|
|
13
|
+
|
|
14
|
+
describe('example to-do app', () => {
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
// Cypress starts out with a blank slate for each test
|
|
17
|
+
// so we must tell it to visit our website with the `cy.visit()` command.
|
|
18
|
+
// Since we want to visit the same URL at the start of all our tests,
|
|
19
|
+
// we include it in our beforeEach function so that it runs before each test
|
|
20
|
+
cy.visit('https://example.cypress.io/todo')
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('displays two todo items by default', () => {
|
|
24
|
+
// We use the `cy.get()` command to get all elements that match the selector.
|
|
25
|
+
// Then, we use `should` to assert that there are two matched items,
|
|
26
|
+
// which are the two default items.
|
|
27
|
+
cy.get('.todo-list li').should('have.length', 2)
|
|
28
|
+
|
|
29
|
+
// We can go even further and check that the default todos each contain
|
|
30
|
+
// the correct text. We use the `first` and `last` functions
|
|
31
|
+
// to get just the first and last matched elements individually,
|
|
32
|
+
// and then perform an assertion with `should`.
|
|
33
|
+
cy.get('.todo-list li').first().should('have.text', 'Pay electric bill')
|
|
34
|
+
cy.get('.todo-list li').last().should('have.text', 'Walk the dog')
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('can add new todo items', () => {
|
|
38
|
+
// We'll store our item text in a variable so we can reuse it
|
|
39
|
+
const newItem = 'Feed the cat'
|
|
40
|
+
|
|
41
|
+
// Let's get the input element and use the `type` command to
|
|
42
|
+
// input our new list item. After typing the content of our item,
|
|
43
|
+
// we need to type the enter key as well in order to submit the input.
|
|
44
|
+
// This input has a data-test attribute so we'll use that to select the
|
|
45
|
+
// element in accordance with best practices:
|
|
46
|
+
// https://on.cypress.io/selecting-elements
|
|
47
|
+
cy.get('[data-test=new-todo]').type(`${newItem}{enter}`)
|
|
48
|
+
|
|
49
|
+
// Now that we've typed our new item, let's check that it actually was added to the list.
|
|
50
|
+
// Since it's the newest item, it should exist as the last element in the list.
|
|
51
|
+
// In addition, with the two default items, we should have a total of 3 elements in the list.
|
|
52
|
+
// Since assertions yield the element that was asserted on,
|
|
53
|
+
// we can chain both of these assertions together into a single statement.
|
|
54
|
+
cy.get('.todo-list li')
|
|
55
|
+
.should('have.length', 3)
|
|
56
|
+
.last()
|
|
57
|
+
.should('have.text', newItem)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('can check off an item as completed', () => {
|
|
61
|
+
// In addition to using the `get` command to get an element by selector,
|
|
62
|
+
// we can also use the `contains` command to get an element by its contents.
|
|
63
|
+
// However, this will yield the <label>, which is lowest-level element that contains the text.
|
|
64
|
+
// In order to check the item, we'll find the <input> element for this <label>
|
|
65
|
+
// by traversing up the dom to the parent element. From there, we can `find`
|
|
66
|
+
// the child checkbox <input> element and use the `check` command to check it.
|
|
67
|
+
cy.contains('Pay electric bill')
|
|
68
|
+
.parent()
|
|
69
|
+
.find('input[type=checkbox]')
|
|
70
|
+
.check()
|
|
71
|
+
|
|
72
|
+
// Now that we've checked the button, we can go ahead and make sure
|
|
73
|
+
// that the list element is now marked as completed.
|
|
74
|
+
// Again we'll use `contains` to find the <label> element and then use the `parents` command
|
|
75
|
+
// to traverse multiple levels up the dom until we find the corresponding <li> element.
|
|
76
|
+
// Once we get that element, we can assert that it has the completed class.
|
|
77
|
+
cy.contains('Pay electric bill')
|
|
78
|
+
.parents('li')
|
|
79
|
+
.should('have.class', 'completed')
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
context('with a checked task', () => {
|
|
83
|
+
beforeEach(() => {
|
|
84
|
+
// We'll take the command we used above to check off an element
|
|
85
|
+
// Since we want to perform multiple tests that start with checking
|
|
86
|
+
// one element, we put it in the beforeEach hook
|
|
87
|
+
// so that it runs at the start of every test.
|
|
88
|
+
cy.contains('Pay electric bill')
|
|
89
|
+
.parent()
|
|
90
|
+
.find('input[type=checkbox]')
|
|
91
|
+
.check()
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
it('can filter for uncompleted tasks', () => {
|
|
95
|
+
// We'll click on the "active" button in order to
|
|
96
|
+
// display only incomplete items
|
|
97
|
+
cy.contains('Active').click()
|
|
98
|
+
|
|
99
|
+
// After filtering, we can assert that there is only the one
|
|
100
|
+
// incomplete item in the list.
|
|
101
|
+
cy.get('.todo-list li')
|
|
102
|
+
.should('have.length', 1)
|
|
103
|
+
.first()
|
|
104
|
+
.should('have.text', 'Walk the dog')
|
|
105
|
+
|
|
106
|
+
// For good measure, let's also assert that the task we checked off
|
|
107
|
+
// does not exist on the page.
|
|
108
|
+
cy.contains('Pay electric bill').should('not.exist')
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
it('can filter for completed tasks', () => {
|
|
112
|
+
// We can perform similar steps as the test above to ensure
|
|
113
|
+
// that only completed tasks are shown
|
|
114
|
+
cy.contains('Completed').click()
|
|
115
|
+
|
|
116
|
+
cy.get('.todo-list li')
|
|
117
|
+
.should('have.length', 1)
|
|
118
|
+
.first()
|
|
119
|
+
.should('have.text', 'Pay electric bill')
|
|
120
|
+
|
|
121
|
+
cy.contains('Walk the dog').should('not.exist')
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
it('can delete all completed tasks', () => {
|
|
125
|
+
// First, let's click the "Clear completed" button
|
|
126
|
+
// `contains` is actually serving two purposes here.
|
|
127
|
+
// First, it's ensuring that the button exists within the dom.
|
|
128
|
+
// This button only appears when at least one task is checked
|
|
129
|
+
// so this command is implicitly verifying that it does exist.
|
|
130
|
+
// Second, it selects the button so we can click it.
|
|
131
|
+
cy.contains('Clear completed').click()
|
|
132
|
+
|
|
133
|
+
// Then we can make sure that there is only one element
|
|
134
|
+
// in the list and our element does not exist
|
|
135
|
+
cy.get('.todo-list li')
|
|
136
|
+
.should('have.length', 1)
|
|
137
|
+
.should('not.have.text', 'Pay electric bill')
|
|
138
|
+
|
|
139
|
+
// Finally, make sure that the clear button no longer exists.
|
|
140
|
+
cy.contains('Clear completed').should('not.exist')
|
|
141
|
+
})
|
|
142
|
+
})
|
|
143
|
+
})
|