Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 3bd8704

Browse files
committed
Add Cypress Testing Library
1 parent aec454d commit 3bd8704

File tree

6 files changed

+50
-12
lines changed

6 files changed

+50
-12
lines changed

cypress/e2e/create-room/create-room.spec.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import { HomeserverInstance } from "../../plugins/utils/homeserver";
2020
import Chainable = Cypress.Chainable;
2121

2222
function openCreateRoomDialog(): Chainable<JQuery<HTMLElement>> {
23-
cy.get('[aria-label="Add room"]').click();
24-
cy.get('.mx_ContextualMenu [aria-label="New room"]').click();
23+
cy.findByRole("button", { name: "Add room" }).click();
24+
cy.findByRole("menuitem", { name: "New room" }).click();
2525
return cy.get(".mx_CreateRoomDialog");
2626
}
2727

@@ -46,20 +46,23 @@ describe("Create Room", () => {
4646

4747
openCreateRoomDialog().within(() => {
4848
// Fill name & topic
49-
cy.get('[label="Name"]').type(name);
50-
cy.get('[label="Topic (optional)"]').type(topic);
49+
cy.findByRole("textbox", { name: "Name" }).type(name);
50+
cy.findByRole("textbox", { name: "Topic (optional)" }).type(topic);
5151
// Change room to public
52-
cy.get('[aria-label="Room visibility"]').click();
53-
cy.get("#mx_JoinRuleDropdown__public").click();
52+
cy.findByRole("button", { name: "Room visibility" }).click();
53+
cy.findByRole("option", { name: "Public room" }).click();
5454
// Fill room address
55-
cy.get('[label="Room address"]').type("test-room-1");
55+
cy.findByRole("textbox", { name: "Room address" }).type("test-room-1");
5656
// Submit
57-
cy.get(".mx_Dialog_primary").click();
57+
cy.findByRole("button", { name: "Create room" }).click();
5858
});
5959

6060
cy.url().should("contain", "/#/room/#test-room-1:localhost");
61-
cy.contains(".mx_RoomHeader_nametext", name);
62-
cy.contains(".mx_RoomHeader_topic", topic);
61+
62+
cy.get(".mx_RoomHeader").within(() => {
63+
cy.findByText(name);
64+
cy.findByText(topic);
65+
});
6366
});
6467

6568
it("should create a room with a long room name, which is displayed with ellipsis", () => {

cypress/support/e2e.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818

1919
import "@percy/cypress";
2020
import "cypress-real-events";
21+
import "@testing-library/cypress/add-commands";
2122

2223
import "./homeserver";
2324
import "./login";

cypress/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"target": "es2016",
44
"jsx": "react",
55
"lib": ["es2020", "dom", "dom.iterable"],
6-
"types": ["cypress", "cypress-axe", "@percy/cypress"],
6+
"types": ["cypress", "cypress-axe", "@percy/cypress", "@testing-library/cypress"],
77
"resolveJsonModule": true,
88
"esModuleInterop": true,
99
"moduleResolution": "node",

docs/cypress.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ API before logging the user in. You can make use of `cy.getBot(homeserver)` and
154154
We should probably end up with convenience APIs that wrap the homeserver creation, logging in and room
155155
creation that can be called to set up tests.
156156

157+
### Try to write tests from the users's perspective
158+
159+
Like for instance a user will not look for a button by querying a CSS selector. Instead you should work
160+
with roles / labels etc.. You can make use of `findBy…` queries provided by
161+
[Cypress Testing Library](https://github.com/testing-library/cypress-testing-library).
162+
157163
### Using matrix-js-sdk
158164

159165
Due to the way we run the Cypress tests in CI, at this time you can only use the matrix-js-sdk module

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
"@percy/cli": "^1.11.0",
144144
"@percy/cypress": "^3.1.2",
145145
"@sinonjs/fake-timers": "^9.1.2",
146+
"@testing-library/cypress": "^9.0.0",
146147
"@testing-library/jest-dom": "^5.16.5",
147148
"@testing-library/react": "^12.1.5",
148149
"@testing-library/user-event": "^14.4.3",

yarn.lock

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1138,7 +1138,7 @@
11381138
pirates "^4.0.5"
11391139
source-map-support "^0.5.16"
11401140

1141-
"@babel/runtime@^7.0.0", "@babel/runtime@^7.12.13", "@babel/runtime@^7.12.5", "@babel/runtime@^7.15.4", "@babel/runtime@^7.17.9", "@babel/runtime@^7.20.7", "@babel/runtime@^7.5.5", "@babel/runtime@^7.8.4", "@babel/runtime@^7.8.7", "@babel/runtime@^7.9.2":
1141+
"@babel/runtime@^7.0.0", "@babel/runtime@^7.12.13", "@babel/runtime@^7.12.5", "@babel/runtime@^7.14.6", "@babel/runtime@^7.15.4", "@babel/runtime@^7.17.9", "@babel/runtime@^7.20.7", "@babel/runtime@^7.5.5", "@babel/runtime@^7.8.4", "@babel/runtime@^7.8.7", "@babel/runtime@^7.9.2":
11421142
version "7.21.0"
11431143
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.21.0.tgz#5b55c9d394e5fcf304909a8b00c07dc217b56673"
11441144
integrity sha512-xwII0//EObnq89Ji5AKYQaRYiW/nZ3llSv29d49IuxPhKbtJoLP+9QUUZ4nVragQVtaVGeZrpB+ZtG/Pdy/POw==
@@ -2048,6 +2048,14 @@
20482048
dependencies:
20492049
"@sinonjs/commons" "^1.7.0"
20502050

2051+
"@testing-library/cypress@^9.0.0":
2052+
version "9.0.0"
2053+
resolved "https://registry.yarnpkg.com/@testing-library/cypress/-/cypress-9.0.0.tgz#3facad49c4654a99bbd138f83f33b415d2d6f097"
2054+
integrity sha512-c1XiCGeHGGTWn0LAU12sFUfoX3qfId5gcSE2yHode+vsyHDWraxDPALjVnHd4/Fa3j4KBcc5k++Ccy6A9qnkMA==
2055+
dependencies:
2056+
"@babel/runtime" "^7.14.6"
2057+
"@testing-library/dom" "^8.1.0"
2058+
20512059
"@testing-library/dom@^8.0.0":
20522060
version "8.19.0"
20532061
resolved "https://registry.yarnpkg.com/@testing-library/dom/-/dom-8.19.0.tgz#bd3f83c217ebac16694329e413d9ad5fdcfd785f"
@@ -2062,6 +2070,20 @@
20622070
lz-string "^1.4.4"
20632071
pretty-format "^27.0.2"
20642072

2073+
"@testing-library/dom@^8.1.0":
2074+
version "8.20.0"
2075+
resolved "https://registry.yarnpkg.com/@testing-library/dom/-/dom-8.20.0.tgz#914aa862cef0f5e89b98cc48e3445c4c921010f6"
2076+
integrity sha512-d9ULIT+a4EXLX3UU8FBjauG9NnsZHkHztXoIcTsOKoOw030fyjheN9svkTULjJxtYag9DZz5Jz5qkWZDPxTFwA==
2077+
dependencies:
2078+
"@babel/code-frame" "^7.10.4"
2079+
"@babel/runtime" "^7.12.5"
2080+
"@types/aria-query" "^5.0.1"
2081+
aria-query "^5.0.0"
2082+
chalk "^4.1.0"
2083+
dom-accessibility-api "^0.5.9"
2084+
lz-string "^1.4.4"
2085+
pretty-format "^27.0.2"
2086+
20652087
"@testing-library/jest-dom@^5.16.5":
20662088
version "5.16.5"
20672089
resolved "https://registry.yarnpkg.com/@testing-library/jest-dom/-/jest-dom-5.16.5.tgz#3912846af19a29b2dbf32a6ae9c31ef52580074e"
@@ -2109,6 +2131,11 @@
21092131
resolved "https://registry.yarnpkg.com/@types/aria-query/-/aria-query-4.2.2.tgz#ed4e0ad92306a704f9fb132a0cfcf77486dbe2bc"
21102132
integrity sha512-HnYpAE1Y6kRyKM/XkEuiRQhTHvkzMBurTHnpFLYLBGPIylZNPs9jJcuOOYWxPLJCSEtmZT0Y8rHDokKN7rRTig==
21112133

2134+
"@types/aria-query@^5.0.1":
2135+
version "5.0.1"
2136+
resolved "https://registry.yarnpkg.com/@types/aria-query/-/aria-query-5.0.1.tgz#3286741fb8f1e1580ac28784add4c7a1d49bdfbc"
2137+
integrity sha512-XTIieEY+gvJ39ChLcB4If5zHtPxt3Syj5rgZR+e1ctpmK8NjPf0zFqsz4JpLJT0xla9GFDKjy8Cpu331nrmE1Q==
2138+
21122139
"@types/babel__core@^7.1.14":
21132140
version "7.1.20"
21142141
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.20.tgz#e168cdd612c92a2d335029ed62ac94c95b362359"

0 commit comments

Comments
 (0)