Refactor and document test helpers.

This commit is contained in:
lukebarnard 2017-01-18 11:53:17 +01:00
parent 5dd1512ff2
commit 78e2c787e0
2 changed files with 45 additions and 31 deletions

View file

@ -4,12 +4,15 @@ const ReactDOM = require("react-dom");
const ReactTestUtils = require('react-addons-test-utils'); const ReactTestUtils = require('react-addons-test-utils');
const sdk = require('matrix-react-sdk'); const sdk = require('matrix-react-sdk');
const MemberEventListSummary = sdk.getComponent('views.elements.MemberEventListSummary'); const MemberEventListSummary = sdk.getComponent('views.elements.MemberEventListSummary');
var jssdk = require('matrix-js-sdk');
var MatrixEvent = jssdk.MatrixEvent;
const testUtils = require('../../../test-utils'); const testUtils = require('../../../test-utils');
describe.only('MemberEventListSummary', function() { describe.only('MemberEventListSummary', function() {
let sandbox; let sandbox;
let parentDiv; let parentDiv;
// Generate dummy event tiles for use in simulating an expanded MELS
const generateTiles = (events) => { const generateTiles = (events) => {
return events.map((e) => { return events.map((e) => {
return ( return (
@ -20,42 +23,41 @@ describe.only('MemberEventListSummary', function() {
}); });
}; };
/**
* Generates a membership event with the target of the event set as a mocked RoomMember based
* on `parameters.userId`.
* @param {string} eventId the ID of the event.
* @param {object} parameters the parameters to use to create the event.
* @param {string} parameters.membership the membership to assign to `content.membership`
* @param {string} parameters.userId the state key and target userId of the event. If
* `parameters.senderId` is not specified, this is also used as the event sender.
* @param {string} parameters.prevMembership the membership to assign to
* `prev_content.membership`.
* @param {string} parameters.senderId the user ID of the sender of the event. Optional.
* Defaults to `parameters.userId`.
* @returns {MatrixEvent} the event created.
*/
const generateMembershipEvent = (eventId, parameters) => { const generateMembershipEvent = (eventId, parameters) => {
let membership = parameters.membership; let e = testUtils.mkMembership({
let userId = parameters.userId; event: true,
let prevMembership = parameters.prevMembership; user: parameters.senderId || parameters.userId,
let senderId = parameters.senderId; skey: parameters.userId,
return { mship: parameters.membership,
content: { prevMship: parameters.prevMembership,
membership: membership, target : {
}, name: parameters.userId.match(/@([^:]*):/)[1], // Use localpart as display name
target: { userId: parameters.userId,
name: userId.match(/@([^:]*):/)[1],
getAvatarUrl: () => { getAvatarUrl: () => {
return "avatar.jpeg"; return "avatar.jpeg";
}, },
userId: userId,
}, },
getId: () => { });
return eventId; // Override random event ID
}, e.event.event_id = eventId;
getContent: function() { return e;
return this.content;
},
getPrevContent: function() {
return {
membership: prevMembership ? prevMembership : this.content,
};
},
getSender: () => {
return senderId || userId;
},
getStateKey: () => {
return userId;
},
};
}; };
// Generate mock MatrixEvents from the array of parameters
const generateEvents = (parameters) => { const generateEvents = (parameters) => {
const res = []; const res = [];
for (let i = 0; i < parameters.length; i++) { for (let i = 0; i < parameters.length; i++) {
@ -64,6 +66,9 @@ describe.only('MemberEventListSummary', function() {
return res; return res;
}; };
// Generate the same sequence of `events` for `n` users, where each user ID
// is created by replacing the first "$" in userIdTemplate with `i` for
// `i = 0 .. n`.
const generateEventsForUsers = (userIdTemplate, n, events) => { const generateEventsForUsers = (userIdTemplate, n, events) => {
let eventsForUsers = []; let eventsForUsers = [];
let userId = ""; let userId = "";
@ -71,7 +76,6 @@ describe.only('MemberEventListSummary', function() {
userId = userIdTemplate.replace('$', i); userId = userIdTemplate.replace('$', i);
events.forEach((e) => { events.forEach((e) => {
e.userId = userId; e.userId = userId;
return e;
}); });
eventsForUsers = eventsForUsers.concat(generateEvents(events)); eventsForUsers = eventsForUsers.concat(generateEvents(events));
} }

View file

@ -108,6 +108,7 @@ export function mkEvent(opts) {
room_id: opts.room, room_id: opts.room,
sender: opts.user, sender: opts.user,
content: opts.content, content: opts.content,
prev_content: opts.prev_content,
event_id: "$" + Math.random() + "-" + Math.random(), event_id: "$" + Math.random() + "-" + Math.random(),
origin_server_ts: opts.ts, origin_server_ts: opts.ts,
}; };
@ -150,7 +151,9 @@ export function mkPresence(opts) {
* @param {Object} opts Values for the membership. * @param {Object} opts Values for the membership.
* @param {string} opts.room The room ID for the event. * @param {string} opts.room The room ID for the event.
* @param {string} opts.mship The content.membership for the event. * @param {string} opts.mship The content.membership for the event.
* @param {string} opts.prevMship The prev_content.membership for the event.
* @param {string} opts.user The user ID for the event. * @param {string} opts.user The user ID for the event.
* @param {RoomMember} opts.target The target of the event.
* @param {string} opts.skey The other user ID for the event if applicable * @param {string} opts.skey The other user ID for the event if applicable
* e.g. for invites/bans. * e.g. for invites/bans.
* @param {string} opts.name The content.displayname for the event. * @param {string} opts.name The content.displayname for the event.
@ -169,9 +172,16 @@ export function mkMembership(opts) {
opts.content = { opts.content = {
membership: opts.mship membership: opts.mship
}; };
if (opts.prevMship) {
opts.prev_content = { membership: opts.prevMship };
}
if (opts.name) { opts.content.displayname = opts.name; } if (opts.name) { opts.content.displayname = opts.name; }
if (opts.url) { opts.content.avatar_url = opts.url; } if (opts.url) { opts.content.avatar_url = opts.url; }
return mkEvent(opts); let e = mkEvent(opts);
if (opts.target) {
e.target = opts.target;
}
return e;
}; };
/** /**