2016-03-28 23:59:34 +02:00
|
|
|
"use strict";
|
|
|
|
|
2017-03-16 18:26:42 +01:00
|
|
|
import sinon from 'sinon';
|
|
|
|
import q from 'q';
|
|
|
|
import ReactTestUtils from 'react-addons-test-utils';
|
2016-04-08 15:50:04 +02:00
|
|
|
|
2017-03-16 18:26:42 +01:00
|
|
|
import peg from '../src/MatrixClientPeg.js';
|
|
|
|
import jssdk from 'matrix-js-sdk';
|
|
|
|
const MatrixEvent = jssdk.MatrixEvent;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper around window.requestAnimationFrame that returns a promise
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
function _waitForFrame() {
|
|
|
|
const def = q.defer();
|
|
|
|
window.requestAnimationFrame(() => {
|
|
|
|
def.resolve();
|
|
|
|
});
|
|
|
|
return def.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Waits a small number of animation frames for a component to appear
|
|
|
|
* in the DOM. Like findRenderedDOMComponentWithTag(), but allows
|
|
|
|
* for the element to appear a short time later, eg. if a promise needs
|
|
|
|
* to resolve first.
|
|
|
|
* @return a promise that resolves once the component appears, or rejects
|
|
|
|
* if it doesn't appear after a nominal number of animation frames.
|
|
|
|
*/
|
|
|
|
export function waitForRenderedDOMComponentWithTag(tree, tag, attempts) {
|
|
|
|
if (attempts === undefined) {
|
|
|
|
// Let's start by assuming we'll only need to wait a single frame, and
|
|
|
|
// we can try increasing this if necessary.
|
|
|
|
attempts = 1;
|
|
|
|
} else if (attempts == 0) {
|
|
|
|
return q.reject("Gave up waiting for component with tag: " + tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _waitForFrame().then(() => {
|
|
|
|
const result = ReactTestUtils.scryRenderedDOMComponentsWithTag(tree, tag);
|
|
|
|
if (result.length > 0) {
|
|
|
|
return result[0];
|
|
|
|
} else {
|
|
|
|
return waitForRenderedDOMComponentWithTag(tree, tag, attempts - 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-03-28 23:59:34 +02:00
|
|
|
|
2016-04-02 19:09:44 +02: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 14:37:42 +02:00
|
|
|
export function beforeEach(context) {
|
2016-04-02 19:09:44 +02:00
|
|
|
var desc = context.currentTest.fullTitle();
|
2017-01-31 23:40:53 +01:00
|
|
|
|
2016-04-02 19:09:44 +02:00
|
|
|
console.log();
|
2017-01-31 23:40:53 +01: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-02 19:09:44 +02:00
|
|
|
console.log(desc);
|
|
|
|
console.log(new Array(1 + desc.length).join("="));
|
|
|
|
};
|
|
|
|
|
2016-03-31 01:48:46 +02:00
|
|
|
|
2016-03-28 23:59:34 +02:00
|
|
|
/**
|
|
|
|
* Stub out the MatrixClient, and configure the MatrixClientPeg object to
|
|
|
|
* return it when get() is called.
|
2016-04-07 17:47:17 +02:00
|
|
|
*
|
2016-11-14 19:20:15 +01: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 17:47:17 +02:00
|
|
|
* @returns {sinon.Sandbox}; remember to call sandbox.restore afterwards.
|
2016-03-28 23:59:34 +02:00
|
|
|
*/
|
2016-09-09 14:37:42 +02:00
|
|
|
export function stubClient() {
|
2016-04-07 17:47:17 +02:00
|
|
|
var sandbox = sinon.sandbox.create();
|
|
|
|
|
2016-11-14 19:20:15 +01:00
|
|
|
var client = createTestClient();
|
|
|
|
|
|
|
|
// stub out the methods in MatrixClientPeg
|
|
|
|
//
|
|
|
|
// 'sandbox.restore()' doesn't work correctly on inherited methods,
|
|
|
|
// so we do this for each method
|
|
|
|
var methods = ['get', 'unset', 'replaceUsingCreds'];
|
|
|
|
for (var i = 0; i < methods.length; i++) {
|
|
|
|
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 15:50:04 +02:00
|
|
|
getHomeserverUrl: sinon.stub(),
|
|
|
|
getIdentityServerUrl: sinon.stub(),
|
|
|
|
|
|
|
|
getPushActionsForEvent: sinon.stub(),
|
2016-11-14 19:20:15 +01:00
|
|
|
getRoom: sinon.stub().returns(mkStubRoom()),
|
2016-06-17 13:20:26 +02:00
|
|
|
getRooms: sinon.stub().returns([]),
|
2016-04-08 15:50:04 +02:00
|
|
|
loginFlows: sinon.stub(),
|
|
|
|
on: sinon.stub(),
|
2016-04-21 14:41:25 +02:00
|
|
|
removeListener: sinon.stub(),
|
2016-09-12 18:19:58 +02:00
|
|
|
isRoomEncrypted: sinon.stub().returns(false),
|
2016-11-14 19:20:15 +01:00
|
|
|
peekInRoom: sinon.stub().returns(q(mkStubRoom())),
|
2016-04-08 15:50:04 +02:00
|
|
|
|
|
|
|
paginateEventTimeline: sinon.stub().returns(q()),
|
|
|
|
sendReadReceipt: sinon.stub().returns(q()),
|
2016-06-17 13:20:26 +02:00
|
|
|
getRoomIdForAlias: sinon.stub().returns(q()),
|
|
|
|
getProfileInfo: sinon.stub().returns(q({})),
|
2016-09-09 14:37:42 +02:00
|
|
|
getAccountData: (type) => {
|
|
|
|
return mkEvent({
|
|
|
|
type,
|
|
|
|
event: true,
|
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
setAccountData: sinon.stub(),
|
|
|
|
sendTyping: sinon.stub().returns(q({})),
|
2016-09-26 11:20:56 +02:00
|
|
|
sendTextMessage: () => q({}),
|
2016-09-09 14:37:42 +02:00
|
|
|
sendHtmlMessage: () => q({}),
|
2016-10-10 18:51:26 +02:00
|
|
|
getSyncState: () => "SYNCING",
|
2017-02-24 12:41:23 +01:00
|
|
|
generateClientSecret: () => "t35tcl1Ent5ECr3T",
|
2017-05-02 11:14:54 +02:00
|
|
|
isGuest: () => false,
|
2016-04-08 15:50:04 +02:00
|
|
|
};
|
2016-03-28 23:59:34 +02:00
|
|
|
}
|
|
|
|
|
2016-03-31 01:48:46 +02: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 14:37:42 +02:00
|
|
|
export function mkEvent(opts) {
|
2016-03-31 01:48:46 +02:00
|
|
|
if (!opts.type || !opts.content) {
|
|
|
|
throw new Error("Missing .type or .content =>" + JSON.stringify(opts));
|
|
|
|
}
|
|
|
|
var event = {
|
|
|
|
type: opts.type,
|
|
|
|
room_id: opts.room,
|
|
|
|
sender: opts.user,
|
|
|
|
content: opts.content,
|
2017-01-18 11:53:17 +01:00
|
|
|
prev_content: opts.prev_content,
|
2016-03-31 01:48:46 +02:00
|
|
|
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",
|
|
|
|
"com.example.state"].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
|
|
|
|
*/
|
2016-09-09 14:37:42 +02:00
|
|
|
export function mkPresence(opts) {
|
2016-03-31 01:48:46 +02:00
|
|
|
if (!opts.user) {
|
|
|
|
throw new Error("Missing user");
|
|
|
|
}
|
|
|
|
var 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 11:53:17 +01:00
|
|
|
* @param {string} opts.prevMship The prev_content.membership for the event.
|
2016-03-31 01:48:46 +02:00
|
|
|
* @param {string} opts.user The user ID for the event.
|
2017-01-18 11:53:17 +01:00
|
|
|
* @param {RoomMember} opts.target The target of the event.
|
2016-03-31 01:48:46 +02: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 14:37:42 +02:00
|
|
|
export function mkMembership(opts) {
|
2016-03-31 01:48:46 +02: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 = {
|
|
|
|
membership: opts.mship
|
|
|
|
};
|
2017-01-18 11:53:17 +01:00
|
|
|
if (opts.prevMship) {
|
|
|
|
opts.prev_content = { membership: opts.prevMship };
|
|
|
|
}
|
2016-03-31 01:48:46 +02:00
|
|
|
if (opts.name) { opts.content.displayname = opts.name; }
|
|
|
|
if (opts.url) { opts.content.avatar_url = opts.url; }
|
2017-01-18 11:53:17 +01:00
|
|
|
let e = mkEvent(opts);
|
|
|
|
if (opts.target) {
|
|
|
|
e.target = opts.target;
|
|
|
|
}
|
|
|
|
return e;
|
2016-03-31 01:48:46 +02: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 14:37:42 +02:00
|
|
|
export function mkMessage(opts) {
|
2016-03-31 01:48:46 +02: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",
|
|
|
|
body: opts.msg
|
|
|
|
};
|
2016-09-09 14:37:42 +02:00
|
|
|
return mkEvent(opts);
|
|
|
|
}
|
2016-06-17 13:20:26 +02:00
|
|
|
|
2016-09-09 14:37:42 +02:00
|
|
|
export function mkStubRoom(roomId = null) {
|
2016-10-10 18:51:26 +02:00
|
|
|
var stubTimeline = { getEvents: () => [] };
|
2016-06-17 13:20:26 +02:00
|
|
|
return {
|
2016-09-09 14:37:42 +02:00
|
|
|
roomId,
|
2016-06-17 13:20:26 +02:00
|
|
|
getReceiptsForEvent: sinon.stub().returns([]),
|
|
|
|
getMember: sinon.stub().returns({}),
|
|
|
|
getJoinedMembers: sinon.stub().returns([]),
|
2016-10-10 18:51:26 +02:00
|
|
|
getPendingEvents: () => [],
|
|
|
|
getLiveTimeline: () => stubTimeline,
|
|
|
|
getUnfilteredTimelineSet: () => null,
|
|
|
|
getAccountData: () => null,
|
|
|
|
hasMembershipState: () => null,
|
2016-06-17 13:20:26 +02:00
|
|
|
currentState: {
|
|
|
|
getStateEvents: sinon.stub(),
|
|
|
|
members: [],
|
|
|
|
},
|
|
|
|
};
|
2016-09-09 14:37:42 +02:00
|
|
|
}
|