element-web/test/test-utils.js

373 lines
13 KiB
JavaScript
Raw Normal View History

import React from 'react';
import EventEmitter from "events";
import ShallowRenderer from 'react-test-renderer/shallow';
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
import { JoinRule } from 'matrix-js-sdk/src/@types/partials';
2021-06-29 15:11:58 +03:00
import { MatrixClientPeg as peg } from '../src/MatrixClientPeg';
import dis from '../src/dispatcher/dispatcher';
2021-06-29 15:11:58 +03:00
import { makeType } from "../src/utils/TypeUtils";
import { ValidatedServerConfig } from "../src/utils/AutoDiscoveryUtils";
import MatrixClientContext from "../src/contexts/MatrixClientContext";
export function getRenderer() {
// Old: ReactTestUtils.createRenderer();
return new ShallowRenderer();
}
2016-03-29 00:59:34 +03:00
/**
* Stub out the MatrixClient, and configure the MatrixClientPeg object to
* return it when get() is called.
*
* TODO: once the components are updated to get their MatrixClients from
* the react context, we can get rid of this and just inject a test client
* via the context instead.
2016-03-29 00:59:34 +03:00
*/
export function stubClient() {
const client = createTestClient();
// stub out the methods in MatrixClientPeg
//
// 'sandbox.restore()' doesn't work correctly on inherited methods,
// so we do this for each method
const methods = ['get', 'unset', 'replaceUsingCreds'];
for (let i = 0; i < methods.length; i++) {
2019-12-16 14:12:48 +03:00
peg[methods[i]] = jest.spyOn(peg, methods[i]);
}
// MatrixClientPeg.get() is called a /lot/, so implement it with our own
// fast stub function rather than a sinon stub
peg.get = function() { return client; };
}
/**
* Create a stubbed-out MatrixClient
*
* @returns {object} MatrixClient stub
*/
export function createTestClient() {
const eventEmitter = new EventEmitter();
return {
2019-12-16 14:12:48 +03:00
getHomeserverUrl: jest.fn(),
getIdentityServerUrl: jest.fn(),
getDomain: jest.fn().mockReturnValue("matrix.rog"),
getUserId: jest.fn().mockReturnValue("@userId:matrix.rog"),
getUser: jest.fn().mockReturnValue({ on: jest.fn() }),
credentials: { userId: "@userId:matrix.rog" },
2019-12-16 14:12:48 +03:00
getPushActionsForEvent: jest.fn(),
getRoom: jest.fn().mockImplementation(mkStubRoom),
2019-12-16 14:12:48 +03:00
getRooms: jest.fn().mockReturnValue([]),
getVisibleRooms: jest.fn().mockReturnValue([]),
getGroups: jest.fn().mockReturnValue([]),
loginFlows: jest.fn(),
on: eventEmitter.on.bind(eventEmitter),
emit: eventEmitter.emit.bind(eventEmitter),
removeListener: eventEmitter.removeListener.bind(eventEmitter),
2019-12-16 14:12:48 +03:00
isRoomEncrypted: jest.fn().mockReturnValue(false),
peekInRoom: jest.fn().mockResolvedValue(mkStubRoom()),
2019-12-16 14:12:48 +03:00
paginateEventTimeline: jest.fn().mockResolvedValue(undefined),
sendReadReceipt: jest.fn().mockResolvedValue(undefined),
getRoomIdForAlias: jest.fn().mockResolvedValue(undefined),
getRoomDirectoryVisibility: jest.fn().mockResolvedValue(undefined),
getProfileInfo: jest.fn().mockResolvedValue({}),
getThirdpartyProtocols: jest.fn().mockResolvedValue({}),
getClientWellKnown: jest.fn().mockReturnValue(null),
supportsVoip: jest.fn().mockReturnValue(true),
getTurnServersExpiry: jest.fn().mockReturnValue(2^32),
getThirdpartyUser: jest.fn().mockResolvedValue([]),
getAccountData: (type) => {
return mkEvent({
type,
event: true,
content: {},
});
},
mxcUrlToHttp: (mxc) => `http://this.is.a.url/${mxc.substring(6)}`,
2019-12-16 14:12:48 +03:00
setAccountData: jest.fn(),
setRoomAccountData: jest.fn(),
2019-12-16 14:12:48 +03:00
sendTyping: jest.fn().mockResolvedValue({}),
sendMessage: () => jest.fn().mockResolvedValue({}),
sendStateEvent: jest.fn().mockResolvedValue(),
getSyncState: () => "SYNCING",
generateClientSecret: () => "t35tcl1Ent5ECr3T",
2017-05-02 12:14:54 +03:00
isGuest: () => false,
isCryptoEnabled: () => false,
getRoomHierarchy: jest.fn().mockReturnValue({
2021-04-23 16:45:22 +03:00
rooms: [],
}),
2021-04-22 01:45:21 +03:00
// Used by various internal bits we aren't concerned with (yet)
sessionStore: {
2021-04-22 01:45:21 +03:00
store: {
getItem: jest.fn(),
setItem: jest.fn(),
2021-04-22 01:45:21 +03:00
},
},
pushRules: {},
2021-05-18 15:46:47 +03:00
decryptEventIfNeeded: () => Promise.resolve(),
isUserIgnored: jest.fn().mockReturnValue(false),
2021-07-06 12:34:50 +03:00
getCapabilities: jest.fn().mockResolvedValue({}),
supportsExperimentalThreads: () => false,
getRoomUpgradeHistory: jest.fn().mockReturnValue([]),
getOpenIdToken: jest.fn().mockResolvedValue(),
registerWithIdentityServer: jest.fn().mockResolvedValue({}),
getIdentityAccount: jest.fn().mockResolvedValue({}),
getTerms: jest.fn().mockResolvedValueOnce(),
doesServerSupportUnstableFeature: jest.fn().mockResolvedValue(),
getPushRules: jest.fn().mockResolvedValue(),
getPushers: jest.fn().mockResolvedValue({ pushers: [] }),
getThreePids: jest.fn().mockResolvedValue({ threepids: [] }),
setPusher: jest.fn().mockResolvedValue(),
setPushRuleEnabled: jest.fn().mockResolvedValue(),
setPushRuleActions: jest.fn().mockResolvedValue(),
};
2016-03-29 00:59:34 +03:00
}
/**
* Create an Event.
* @param {Object} opts Values for the event.
* @param {string} opts.type The event.type
* @param {string} opts.room The event.room_id
* @param {string} opts.user The event.user_id
2021-04-22 16:45:13 +03:00
* @param {string=} opts.skey Optional. The state key (auto inserts empty string)
* @param {number=} opts.ts Optional. Timestamp for the event
* @param {Object} opts.content The event.content
* @param {boolean} opts.event True to make a MatrixEvent.
2021-08-10 09:55:11 +03:00
* @param {unsigned=} opts.unsigned
* @return {Object} a JSON object representing this event.
*/
export function mkEvent(opts) {
if (!opts.type || !opts.content) {
throw new Error("Missing .type or .content =>" + JSON.stringify(opts));
}
const event = {
type: opts.type,
room_id: opts.room,
sender: opts.user,
content: opts.content,
2017-01-18 13:53:17 +03:00
prev_content: opts.prev_content,
event_id: "$" + Math.random() + "-" + Math.random(),
origin_server_ts: opts.ts,
};
if (opts.skey) {
event.state_key = opts.skey;
} else if ([
"m.room.name", "m.room.topic", "m.room.create", "m.room.join_rules",
"m.room.power_levels", "m.room.topic", "m.room.history_visibility",
"m.room.encryption", "m.room.member", "com.example.state",
"m.room.guest_access",
].indexOf(opts.type) !== -1) {
event.state_key = "";
}
return opts.event ? new MatrixEvent(event) : event;
}
/**
* Create an m.presence event.
* @param {Object} opts Values for the presence.
* @return {Object|MatrixEvent} The event
*/
export function mkPresence(opts) {
if (!opts.user) {
throw new Error("Missing user");
}
const event = {
event_id: "$" + Math.random() + "-" + Math.random(),
type: "m.presence",
sender: opts.user,
content: {
avatar_url: opts.url,
displayname: opts.name,
last_active_ago: opts.ago,
presence: opts.presence || "offline",
},
};
return opts.event ? new MatrixEvent(event) : event;
}
/**
* Create an m.room.member event.
* @param {Object} opts Values for the membership.
* @param {string} opts.room The room ID for the event.
* @param {string} opts.mship The content.membership for the event.
2017-01-18 13:53:17 +03:00
* @param {string} opts.prevMship The prev_content.membership for the event.
2021-08-10 09:55:11 +03:00
* @param {number=} opts.ts Optional. Timestamp for the event
* @param {string} opts.user The user ID for the event.
2017-01-18 13:53:17 +03:00
* @param {RoomMember} opts.target The target of the event.
2021-08-10 09:55:11 +03:00
* @param {string=} opts.skey The other user ID for the event if applicable
* e.g. for invites/bans.
* @param {string} opts.name The content.displayname for the event.
2021-08-10 09:55:11 +03:00
* @param {string=} opts.url The content.avatar_url for the event.
* @param {boolean} opts.event True to make a MatrixEvent.
* @return {Object|MatrixEvent} The event
*/
export function mkMembership(opts) {
opts.type = "m.room.member";
if (!opts.skey) {
opts.skey = opts.user;
}
if (!opts.mship) {
throw new Error("Missing .mship => " + JSON.stringify(opts));
}
opts.content = {
membership: opts.mship,
};
2017-01-18 13:53:17 +03:00
if (opts.prevMship) {
opts.prev_content = { membership: opts.prevMship };
}
if (opts.name) { opts.content.displayname = opts.name; }
if (opts.url) { opts.content.avatar_url = opts.url; }
const e = mkEvent(opts);
2017-01-18 13:53:17 +03:00
if (opts.target) {
e.target = opts.target;
}
return e;
}
/**
* Create an m.room.message event.
* @param {Object} opts Values for the message
* @param {string} opts.room The room ID for the event.
* @param {string} opts.user The user ID for the event.
2021-08-03 12:06:21 +03:00
* @param {number} opts.ts The timestamp for the event.
* @param {boolean} opts.event True to make a MatrixEvent.
2021-08-03 12:06:21 +03:00
* @param {string=} opts.msg Optional. The content.body for the event.
* @return {Object|MatrixEvent} The event
*/
export function mkMessage(opts) {
opts.type = "m.room.message";
if (!opts.msg) {
opts.msg = "Random->" + Math.random();
}
if (!opts.room || !opts.user) {
throw new Error("Missing .room or .user from", opts);
}
opts.content = {
msgtype: "m.text",
body: opts.msg,
};
return mkEvent(opts);
}
2016-06-17 14:20:26 +03:00
export function mkStubRoom(roomId = null, name, client) {
const stubTimeline = { getEvents: () => [] };
2016-06-17 14:20:26 +03:00
return {
roomId,
2019-12-16 14:12:48 +03:00
getReceiptsForEvent: jest.fn().mockReturnValue([]),
getMember: jest.fn().mockReturnValue({
userId: '@member:domain.bla',
name: 'Member',
rawDisplayName: 'Member',
roomId: roomId,
getAvatarUrl: () => 'mxc://avatar.url/image.png',
getMxcAvatarUrl: () => 'mxc://avatar.url/image.png',
}),
2019-12-16 14:12:48 +03:00
getMembersWithMembership: jest.fn().mockReturnValue([]),
getJoinedMembers: jest.fn().mockReturnValue([]),
getJoinedMemberCount: jest.fn().mockReturnValue(1),
2021-05-19 14:34:27 +03:00
getMembers: jest.fn().mockReturnValue([]),
getPendingEvents: () => [],
getLiveTimeline: () => stubTimeline,
getUnfilteredTimelineSet: () => null,
findEventById: () => null,
getAccountData: () => null,
hasMembershipState: () => null,
getVersion: () => '1',
2018-08-17 17:15:53 +03:00
shouldUpgradeToVersion: () => null,
2021-04-23 14:19:08 +03:00
getMyMembership: jest.fn().mockReturnValue("join"),
2019-12-16 14:12:48 +03:00
maySendMessage: jest.fn().mockReturnValue(true),
2016-06-17 14:20:26 +03:00
currentState: {
2019-12-16 14:12:48 +03:00
getStateEvents: jest.fn(),
2021-05-19 12:31:05 +03:00
getMember: jest.fn(),
2019-12-16 14:12:48 +03:00
mayClientSendStateEvent: jest.fn().mockReturnValue(true),
maySendStateEvent: jest.fn().mockReturnValue(true),
maySendEvent: jest.fn().mockReturnValue(true),
2016-06-17 14:20:26 +03:00
members: [],
getJoinRule: jest.fn().mockReturnValue(JoinRule.Invite),
on: jest.fn(),
2016-06-17 14:20:26 +03:00
},
2021-04-23 14:19:08 +03:00
tags: {},
2019-12-16 14:12:48 +03:00
setBlacklistUnverifiedDevices: jest.fn(),
on: jest.fn(),
off: jest.fn(),
2019-12-16 14:12:48 +03:00
removeListener: jest.fn(),
2020-11-05 19:27:41 +03:00
getDMInviter: jest.fn(),
name,
getAvatarUrl: () => 'mxc://avatar.url/room.png',
getMxcAvatarUrl: () => 'mxc://avatar.url/room.png',
2021-04-22 16:45:13 +03:00
isSpaceRoom: jest.fn(() => false),
2021-04-23 14:19:08 +03:00
getUnreadNotificationCount: jest.fn(() => 0),
getEventReadUpTo: jest.fn(() => null),
getCanonicalAlias: jest.fn(),
getAltAliases: jest.fn().mockReturnValue([]),
2021-04-23 14:19:08 +03:00
timeline: [],
2021-07-06 12:44:09 +03:00
getJoinRule: jest.fn().mockReturnValue("invite"),
client,
2016-06-17 14:20:26 +03:00
};
}
2019-05-03 08:46:43 +03:00
export function mkServerConfig(hsUrl, isUrl) {
return makeType(ValidatedServerConfig, {
hsUrl,
hsName: "TEST_ENVIRONMENT",
hsNameIsDifferent: false, // yes, we lie
isUrl,
});
}
export function getDispatchForStore(store) {
// Mock the dispatcher by gut-wrenching. Stores can only __emitChange whilst a
// dispatcher `_isDispatching` is true.
return (payload) => {
dis._isDispatching = true;
dis._callbacks[store._dispatchToken](payload);
dis._isDispatching = false;
};
}
export function wrapInMatrixClientContext(WrappedComponent) {
class Wrapper extends React.Component {
constructor(props) {
super(props);
this._matrixClient = peg.get();
}
render() {
return <MatrixClientContext.Provider value={this._matrixClient}>
<WrappedComponent ref={this.props.wrappedRef} {...this.props} />
</MatrixClientContext.Provider>;
}
}
return Wrapper;
}
2018-05-02 13:19:01 +03:00
/**
* Call fn before calling componentDidUpdate on a react component instance, inst.
* @param {React.Component} inst an instance of a React component.
2019-12-16 14:12:48 +03:00
* @param {number} updates Number of updates to wait for. (Defaults to 1.)
2018-05-02 13:19:01 +03:00
* @returns {Promise} promise that resolves when componentDidUpdate is called on
* given component instance.
*/
export function waitForUpdate(inst, updates = 1) {
2018-05-02 13:19:01 +03:00
return new Promise((resolve, reject) => {
const cdu = inst.componentDidUpdate;
console.log(`Waiting for ${updates} update(s)`);
2018-05-02 13:19:01 +03:00
inst.componentDidUpdate = (prevProps, prevState, snapshot) => {
updates--;
console.log(`Got update, ${updates} remaining`);
2018-05-02 13:19:01 +03:00
if (updates == 0) {
inst.componentDidUpdate = cdu;
resolve();
}
2018-05-02 13:19:01 +03:00
if (cdu) cdu(prevProps, prevState, snapshot);
2018-05-02 13:19:01 +03:00
};
});
}