2016-03-29 05:59:34 +08:00
|
|
|
"use strict";
|
|
|
|
|
2017-03-17 01:26:42 +08:00
|
|
|
import sinon from 'sinon';
|
2017-07-12 20:58:14 +08:00
|
|
|
import Promise from 'bluebird';
|
2018-02-07 01:50:53 +08:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-05-24 23:56:13 +08:00
|
|
|
import peg from '../src/MatrixClientPeg';
|
|
|
|
import dis from '../src/dispatcher';
|
2017-03-17 01:26:42 +08:00
|
|
|
import jssdk from 'matrix-js-sdk';
|
|
|
|
const MatrixEvent = jssdk.MatrixEvent;
|
|
|
|
|
2016-04-03 01:09:44 +08:00
|
|
|
/**
|
|
|
|
* Perform common actions before each test case, e.g. printing the test case
|
|
|
|
* name to stdout.
|
|
|
|
* @param {Mocha.Context} context The test context
|
|
|
|
*/
|
2016-09-09 20:37:42 +08:00
|
|
|
export function beforeEach(context) {
|
2017-10-12 00:56:17 +08:00
|
|
|
const desc = context.currentTest.fullTitle();
|
2017-02-01 06:40:53 +08:00
|
|
|
|
2016-04-03 01:09:44 +08:00
|
|
|
console.log();
|
2017-02-01 06:40:53 +08:00
|
|
|
|
|
|
|
// this puts a mark in the chrome devtools timeline, which can help
|
|
|
|
// figure out what's been going on.
|
|
|
|
if (console.timeStamp) {
|
|
|
|
console.timeStamp(desc);
|
|
|
|
}
|
|
|
|
|
2016-04-03 01:09:44 +08:00
|
|
|
console.log(desc);
|
|
|
|
console.log(new Array(1 + desc.length).join("="));
|
2017-10-12 00:56:17 +08:00
|
|
|
}
|
2016-04-03 01:09:44 +08:00
|
|
|
|
2016-03-31 07:48:46 +08:00
|
|
|
|
2016-03-29 05:59:34 +08:00
|
|
|
/**
|
|
|
|
* Stub out the MatrixClient, and configure the MatrixClientPeg object to
|
|
|
|
* return it when get() is called.
|
2016-04-07 23:47:17 +08:00
|
|
|
*
|
2016-11-15 02:20:15 +08:00
|
|
|
* 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-04-07 23:47:17 +08:00
|
|
|
* @returns {sinon.Sandbox}; remember to call sandbox.restore afterwards.
|
2016-03-29 05:59:34 +08:00
|
|
|
*/
|
2016-09-09 20:37:42 +08:00
|
|
|
export function stubClient() {
|
2017-10-12 00:56:17 +08:00
|
|
|
const sandbox = sinon.sandbox.create();
|
2016-04-07 23:47:17 +08:00
|
|
|
|
2017-10-12 00:56:17 +08:00
|
|
|
const client = createTestClient();
|
2016-11-15 02:20:15 +08:00
|
|
|
|
|
|
|
// stub out the methods in MatrixClientPeg
|
|
|
|
//
|
|
|
|
// 'sandbox.restore()' doesn't work correctly on inherited methods,
|
|
|
|
// so we do this for each method
|
2017-10-12 00:56:17 +08:00
|
|
|
const methods = ['get', 'unset', 'replaceUsingCreds'];
|
|
|
|
for (let i = 0; i < methods.length; i++) {
|
2016-11-15 02:20:15 +08:00
|
|
|
sandbox.stub(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; };
|
|
|
|
return sandbox;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a stubbed-out MatrixClient
|
|
|
|
*
|
|
|
|
* @returns {object} MatrixClient stub
|
|
|
|
*/
|
|
|
|
export function createTestClient() {
|
|
|
|
return {
|
2016-04-08 21:50:04 +08:00
|
|
|
getHomeserverUrl: sinon.stub(),
|
|
|
|
getIdentityServerUrl: sinon.stub(),
|
|
|
|
|
|
|
|
getPushActionsForEvent: sinon.stub(),
|
2016-11-15 02:20:15 +08:00
|
|
|
getRoom: sinon.stub().returns(mkStubRoom()),
|
2016-06-17 19:20:26 +08:00
|
|
|
getRooms: sinon.stub().returns([]),
|
2016-04-08 21:50:04 +08:00
|
|
|
loginFlows: sinon.stub(),
|
|
|
|
on: sinon.stub(),
|
2016-04-21 20:41:25 +08:00
|
|
|
removeListener: sinon.stub(),
|
2016-09-13 00:19:58 +08:00
|
|
|
isRoomEncrypted: sinon.stub().returns(false),
|
2017-07-12 21:02:00 +08:00
|
|
|
peekInRoom: sinon.stub().returns(Promise.resolve(mkStubRoom())),
|
2016-04-08 21:50:04 +08:00
|
|
|
|
2017-07-12 21:02:00 +08:00
|
|
|
paginateEventTimeline: sinon.stub().returns(Promise.resolve()),
|
|
|
|
sendReadReceipt: sinon.stub().returns(Promise.resolve()),
|
|
|
|
getRoomIdForAlias: sinon.stub().returns(Promise.resolve()),
|
|
|
|
getProfileInfo: sinon.stub().returns(Promise.resolve({})),
|
2016-09-09 20:37:42 +08:00
|
|
|
getAccountData: (type) => {
|
|
|
|
return mkEvent({
|
|
|
|
type,
|
|
|
|
event: true,
|
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
setAccountData: sinon.stub(),
|
2017-07-12 21:02:00 +08:00
|
|
|
sendTyping: sinon.stub().returns(Promise.resolve({})),
|
|
|
|
sendTextMessage: () => Promise.resolve({}),
|
|
|
|
sendHtmlMessage: () => Promise.resolve({}),
|
2016-10-11 00:51:26 +08:00
|
|
|
getSyncState: () => "SYNCING",
|
2017-02-24 19:41:23 +08:00
|
|
|
generateClientSecret: () => "t35tcl1Ent5ECr3T",
|
2017-05-02 17:14:54 +08:00
|
|
|
isGuest: () => false,
|
2016-04-08 21:50:04 +08:00
|
|
|
};
|
2016-03-29 05:59:34 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 16:46:17 +08:00
|
|
|
export function createTestRtsClient(teamMap, sidMap) {
|
|
|
|
return {
|
|
|
|
getTeamsConfig() {
|
2017-07-12 21:02:00 +08:00
|
|
|
return Promise.resolve(Object.keys(teamMap).map((token) => teamMap[token]));
|
2017-05-17 16:46:17 +08:00
|
|
|
},
|
|
|
|
trackReferral(referrer, emailSid, clientSecret) {
|
2017-07-12 21:02:00 +08:00
|
|
|
return Promise.resolve({team_token: sidMap[emailSid]});
|
2017-05-17 16:46:17 +08:00
|
|
|
},
|
|
|
|
getTeam(teamToken) {
|
2017-07-12 21:02:00 +08:00
|
|
|
return Promise.resolve(teamMap[teamToken]);
|
2017-05-17 16:46:17 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-03-31 07:48:46 +08: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
|
|
|
|
* @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.
|
|
|
|
* @return {Object} a JSON object representing this event.
|
|
|
|
*/
|
2016-09-09 20:37:42 +08:00
|
|
|
export function mkEvent(opts) {
|
2016-03-31 07:48:46 +08:00
|
|
|
if (!opts.type || !opts.content) {
|
|
|
|
throw new Error("Missing .type or .content =>" + JSON.stringify(opts));
|
|
|
|
}
|
2017-10-12 00:56:17 +08:00
|
|
|
const event = {
|
2016-03-31 07:48:46 +08:00
|
|
|
type: opts.type,
|
|
|
|
room_id: opts.room,
|
|
|
|
sender: opts.user,
|
|
|
|
content: opts.content,
|
2017-01-18 18:53:17 +08:00
|
|
|
prev_content: opts.prev_content,
|
2016-03-31 07:48:46 +08:00
|
|
|
event_id: "$" + Math.random() + "-" + Math.random(),
|
|
|
|
origin_server_ts: opts.ts,
|
|
|
|
};
|
|
|
|
if (opts.skey) {
|
|
|
|
event.state_key = opts.skey;
|
2017-10-12 00:56:17 +08:00
|
|
|
} else if (["m.room.name", "m.room.topic", "m.room.create", "m.room.join_rules",
|
2016-03-31 07:48:46 +08:00
|
|
|
"m.room.power_levels", "m.room.topic",
|
|
|
|
"com.example.state"].indexOf(opts.type) !== -1) {
|
|
|
|
event.state_key = "";
|
|
|
|
}
|
|
|
|
return opts.event ? new MatrixEvent(event) : event;
|
2017-10-12 00:56:17 +08:00
|
|
|
}
|
2016-03-31 07:48:46 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an m.presence event.
|
|
|
|
* @param {Object} opts Values for the presence.
|
|
|
|
* @return {Object|MatrixEvent} The event
|
|
|
|
*/
|
2016-09-09 20:37:42 +08:00
|
|
|
export function mkPresence(opts) {
|
2016-03-31 07:48:46 +08:00
|
|
|
if (!opts.user) {
|
|
|
|
throw new Error("Missing user");
|
|
|
|
}
|
2017-10-12 00:56:17 +08:00
|
|
|
const event = {
|
2016-03-31 07:48:46 +08:00
|
|
|
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,
|
2017-10-12 00:56:17 +08:00
|
|
|
presence: opts.presence || "offline",
|
|
|
|
},
|
2016-03-31 07:48:46 +08:00
|
|
|
};
|
|
|
|
return opts.event ? new MatrixEvent(event) : event;
|
2017-10-12 00:56:17 +08:00
|
|
|
}
|
2016-03-31 07:48:46 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 18:53:17 +08:00
|
|
|
* @param {string} opts.prevMship The prev_content.membership for the event.
|
2016-03-31 07:48:46 +08:00
|
|
|
* @param {string} opts.user The user ID for the event.
|
2017-01-18 18:53:17 +08:00
|
|
|
* @param {RoomMember} opts.target The target of the event.
|
2016-03-31 07:48:46 +08: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.
|
|
|
|
* @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
|
|
|
|
*/
|
2016-09-09 20:37:42 +08:00
|
|
|
export function mkMembership(opts) {
|
2016-03-31 07:48:46 +08:00
|
|
|
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 = {
|
2017-10-12 00:56:17 +08:00
|
|
|
membership: opts.mship,
|
2016-03-31 07:48:46 +08:00
|
|
|
};
|
2017-01-18 18:53:17 +08:00
|
|
|
if (opts.prevMship) {
|
|
|
|
opts.prev_content = { membership: opts.prevMship };
|
|
|
|
}
|
2016-03-31 07:48:46 +08:00
|
|
|
if (opts.name) { opts.content.displayname = opts.name; }
|
|
|
|
if (opts.url) { opts.content.avatar_url = opts.url; }
|
2017-10-12 00:56:17 +08:00
|
|
|
const e = mkEvent(opts);
|
2017-01-18 18:53:17 +08:00
|
|
|
if (opts.target) {
|
|
|
|
e.target = opts.target;
|
|
|
|
}
|
|
|
|
return e;
|
2017-10-12 00:56:17 +08:00
|
|
|
}
|
2016-03-31 07:48:46 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* @param {string} opts.msg Optional. The content.body for the event.
|
|
|
|
* @param {boolean} opts.event True to make a MatrixEvent.
|
|
|
|
* @return {Object|MatrixEvent} The event
|
|
|
|
*/
|
2016-09-09 20:37:42 +08:00
|
|
|
export function mkMessage(opts) {
|
2016-03-31 07:48:46 +08:00
|
|
|
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",
|
2017-10-12 00:56:17 +08:00
|
|
|
body: opts.msg,
|
2016-03-31 07:48:46 +08:00
|
|
|
};
|
2016-09-09 20:37:42 +08:00
|
|
|
return mkEvent(opts);
|
|
|
|
}
|
2016-06-17 19:20:26 +08:00
|
|
|
|
2016-09-09 20:37:42 +08:00
|
|
|
export function mkStubRoom(roomId = null) {
|
2017-10-12 00:56:17 +08:00
|
|
|
const stubTimeline = { getEvents: () => [] };
|
2016-06-17 19:20:26 +08:00
|
|
|
return {
|
2016-09-09 20:37:42 +08:00
|
|
|
roomId,
|
2016-06-17 19:20:26 +08:00
|
|
|
getReceiptsForEvent: sinon.stub().returns([]),
|
2017-07-24 21:42:20 +08:00
|
|
|
getMember: sinon.stub().returns({
|
|
|
|
userId: '@member:domain.bla',
|
|
|
|
name: 'Member',
|
|
|
|
roomId: roomId,
|
|
|
|
getAvatarUrl: () => 'mxc://avatar.url/image.png',
|
|
|
|
}),
|
2016-06-17 19:20:26 +08:00
|
|
|
getJoinedMembers: sinon.stub().returns([]),
|
2016-10-11 00:51:26 +08:00
|
|
|
getPendingEvents: () => [],
|
|
|
|
getLiveTimeline: () => stubTimeline,
|
|
|
|
getUnfilteredTimelineSet: () => null,
|
|
|
|
getAccountData: () => null,
|
|
|
|
hasMembershipState: () => null,
|
2016-06-17 19:20:26 +08:00
|
|
|
currentState: {
|
|
|
|
getStateEvents: sinon.stub(),
|
|
|
|
members: [],
|
|
|
|
},
|
|
|
|
};
|
2016-09-09 20:37:42 +08:00
|
|
|
}
|
2017-05-24 23:56:13 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
}
|
2018-02-07 01:50:53 +08:00
|
|
|
|
|
|
|
export function wrapInMatrixClientContext(WrappedComponent) {
|
|
|
|
class Wrapper extends React.Component {
|
|
|
|
static childContextTypes = {
|
|
|
|
matrixClient: PropTypes.object,
|
|
|
|
}
|
|
|
|
|
|
|
|
getChildContext() {
|
|
|
|
return {
|
|
|
|
matrixClient: this._matrixClient,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount() {
|
|
|
|
this._matrixClient = peg.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return <WrappedComponent {...this.props} />;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Wrapper;
|
|
|
|
}
|