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

Commit 0c083dc

Browse files
committed
Improve cypress support
Signed-off-by: Šimon Brandner <[email protected]>
1 parent de201f5 commit 0c083dc

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

cypress/global.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ import type {
2828
} from "matrix-js-sdk/src/matrix";
2929
import type { MatrixDispatcher } from "../src/dispatcher/dispatcher";
3030
import type PerformanceMonitor from "../src/performance";
31+
import type SettingsStore from "../src/settings/SettingsStore";
3132

3233
declare global {
3334
// eslint-disable-next-line @typescript-eslint/no-namespace
3435
namespace Cypress {
3536
interface ApplicationWindow {
37+
mxSettingsStore: typeof SettingsStore;
3638
mxMatrixClientPeg: {
3739
matrixClient?: MatrixClient;
3840
};

cypress/support/client.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ limitations under the License.
1616

1717
/// <reference types="cypress" />
1818

19-
import type { ICreateRoomOpts } from "matrix-js-sdk/src/@types/requests";
19+
import type { ICreateRoomOpts, ISendEventResponse } from "matrix-js-sdk/src/@types/requests";
2020
import type { MatrixClient } from "matrix-js-sdk/src/client";
2121
import type { Room } from "matrix-js-sdk/src/models/room";
22+
import type { IContent } from "matrix-js-sdk/src/models/event";
2223
import Chainable = Cypress.Chainable;
2324

2425
declare global {
@@ -53,6 +54,26 @@ declare global {
5354
* @param data The data to store.
5455
*/
5556
setAccountData(type: string, data: object): Chainable<{}>;
57+
/**
58+
* @param {string} roomId
59+
* @param {string} threadId
60+
* @param {string} eventType
61+
* @param {Object} content
62+
* @return {module:http-api.MatrixError} Rejects: with an error response.
63+
*/
64+
sendEvent(
65+
roomId: string,
66+
threadId: string | null,
67+
eventType: string,
68+
content: IContent
69+
): Chainable<ISendEventResponse>;
70+
/**
71+
* @param {string} name
72+
* @param {module:client.callback} callback Optional.
73+
* @return {Promise} Resolves: {} an empty object.
74+
* @return {module:http-api.MatrixError} Rejects: with an error response.
75+
*/
76+
setDisplayName(name: string): Chainable<{}>;
5677
}
5778
}
5879
}
@@ -103,3 +124,20 @@ Cypress.Commands.add("setAccountData", (type: string, data: object): Chainable<{
103124
return cli.setAccountData(type, data);
104125
});
105126
});
127+
128+
Cypress.Commands.add("sendEvent", (
129+
roomId: string,
130+
threadId: string | null,
131+
eventType: string,
132+
content: IContent,
133+
): Chainable<ISendEventResponse> => {
134+
return cy.getClient().then(async (cli: MatrixClient) => {
135+
return cli.sendEvent(roomId, threadId, eventType, content);
136+
});
137+
});
138+
139+
Cypress.Commands.add("setDisplayName", (name: string): Chainable<{}> => {
140+
return cy.getClient().then(async (cli: MatrixClient) => {
141+
return cli.setDisplayName(name);
142+
});
143+
});

cypress/support/settings.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@ limitations under the License.
1717
/// <reference types="cypress" />
1818

1919
import Chainable = Cypress.Chainable;
20+
import type { SettingLevel } from "../../src/settings/SettingLevel";
21+
import SettingsStore from "../../src/settings/SettingsStore";
2022

2123
declare global {
2224
// eslint-disable-next-line @typescript-eslint/no-namespace
2325
namespace Cypress {
2426
interface Chainable {
27+
/**
28+
* Returns the SettingsStore
29+
*/
30+
getSettingsStore(): Chainable<typeof SettingsStore | undefined>;
2531
/**
2632
* Open the top left user menu, returning a handle to the resulting context menu.
2733
*/
@@ -63,10 +69,59 @@ declare global {
6369
* @param name the name of the beta to leave.
6470
*/
6571
leaveBeta(name: string): Chainable<JQuery<HTMLElement>>;
72+
73+
/**
74+
* Sets the value for a setting. The room ID is optional if the
75+
* setting is not being set for a particular room, otherwise it
76+
* should be supplied. The value may be null to indicate that the
77+
* level should no longer have an override.
78+
* @param {string} settingName The name of the setting to change.
79+
* @param {String} roomId The room ID to change the value in, may be
80+
* null.
81+
* @param {SettingLevel} level The level to change the value at.
82+
* @param {*} value The new value of the setting, may be null.
83+
* @return {Promise} Resolves when the setting has been changed.
84+
*/
85+
setSettingValue(name: string, roomId: string, level: SettingLevel, value: any): Chainable<void>;
86+
87+
/**
88+
* Gets the value of a setting. The room ID is optional if the
89+
* setting is not to be applied to any particular room, otherwise it
90+
* should be supplied.
91+
* @param {string} settingName The name of the setting to read the
92+
* value of.
93+
* @param {String} roomId The room ID to read the setting value in,
94+
* may be null.
95+
* @param {boolean} excludeDefault True to disable using the default
96+
* value.
97+
* @return {*} The value, or null if not found
98+
*/
99+
getSettingValue<T>(name: string, roomId?: string): Chainable<T>;
66100
}
67101
}
68102
}
69103

104+
Cypress.Commands.add("getSettingsStore", (): Chainable<typeof SettingsStore> => {
105+
return cy.window({ log: false }).then(win => win.mxSettingsStore);
106+
});
107+
108+
Cypress.Commands.add("setSettingValue", (
109+
name: string,
110+
roomId: string,
111+
level: SettingLevel,
112+
value: any,
113+
): Chainable<void> => {
114+
return cy.getSettingsStore().then(async (store: typeof SettingsStore) => {
115+
return store.setValue(name, roomId, level, value);
116+
});
117+
});
118+
119+
Cypress.Commands.add("getSettingValue", <T = any>(name: string, roomId?: string): Chainable<T> => {
120+
return cy.getSettingsStore().then(<T>(store: typeof SettingsStore) => {
121+
return store.getValue<T>(name, roomId);
122+
});
123+
});
124+
70125
Cypress.Commands.add("openUserMenu", (): Chainable<JQuery<HTMLElement>> => {
71126
cy.get('[aria-label="User menu"]').click();
72127
return cy.get(".mx_ContextualMenu");

0 commit comments

Comments
 (0)