Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions spec/integ/matrix-client-methods.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,14 @@ describe("MatrixClient", function() {
describe("joinRoom", function() {
it("should no-op if you've already joined a room", function() {
const roomId = "!foo:bar";
const room = new Room(roomId, userId);
const room = new Room(roomId, client, userId);
client.fetchRoomEvent = () => Promise.resolve({});
room.addLiveEvents([
utils.mkMembership({
user: userId, room: roomId, mship: "join", event: true,
}),
]);
httpBackend.verifyNoOutstandingRequests();
store.storeRoom(room);
client.joinRoom(roomId);
httpBackend.verifyNoOutstandingRequests();
Expand Down Expand Up @@ -556,11 +558,14 @@ describe("MatrixClient", function() {
});

describe("partitionThreadedEvents", function() {
const room = new Room("!STrMRsukXHtqQdSeHa:matrix.org", client, userId);
let room;
beforeEach(() => {
room = new Room("!STrMRsukXHtqQdSeHa:matrix.org", client, userId);
});

it("returns empty arrays when given an empty arrays", function() {
const events = [];
const [timeline, threaded] = client.partitionThreadedEvents(room, events);
const [timeline, threaded] = room.partitionThreadedEvents(events);
expect(timeline).toEqual([]);
expect(threaded).toEqual([]);
});
Expand All @@ -580,7 +585,7 @@ describe("MatrixClient", function() {
// Vote has no threadId yet
expect(eventPollResponseReference.threadId).toBeFalsy();

const [timeline, threaded] = client.partitionThreadedEvents(room, events);
const [timeline, threaded] = room.partitionThreadedEvents(events);

expect(timeline).toEqual([
// The message that was sent in a thread is missing
Expand Down Expand Up @@ -613,7 +618,7 @@ describe("MatrixClient", function() {
eventReaction,
];

const [timeline, threaded] = client.partitionThreadedEvents(room, events);
const [timeline, threaded] = room.partitionThreadedEvents(events);

expect(timeline).toEqual([
eventPollStartThreadRoot,
Expand All @@ -640,7 +645,7 @@ describe("MatrixClient", function() {
eventMessageInThread,
];

const [timeline, threaded] = client.partitionThreadedEvents(room, events);
const [timeline, threaded] = room.partitionThreadedEvents(events);

expect(timeline).toEqual([
eventPollStartThreadRoot,
Expand All @@ -667,7 +672,7 @@ describe("MatrixClient", function() {
eventReaction,
];

const [timeline, threaded] = client.partitionThreadedEvents(room, events);
const [timeline, threaded] = room.partitionThreadedEvents(events);

expect(timeline).toEqual([
eventPollStartThreadRoot,
Expand Down Expand Up @@ -710,7 +715,7 @@ describe("MatrixClient", function() {
eventMember,
eventCreate,
];
const [timeline, threaded] = client.partitionThreadedEvents(room, events);
const [timeline, threaded] = room.partitionThreadedEvents(events);

expect(timeline).toEqual([
// The message that was sent in a thread is missing
Expand Down Expand Up @@ -749,7 +754,7 @@ describe("MatrixClient", function() {
threadedReactionRedaction,
];

const [timeline, threaded] = client.partitionThreadedEvents(room, events);
const [timeline, threaded] = room.partitionThreadedEvents(events);

expect(timeline).toEqual([
threadRootEvent,
Expand Down Expand Up @@ -778,7 +783,7 @@ describe("MatrixClient", function() {
replyToReply,
];

const [timeline, threaded] = client.partitionThreadedEvents(room, events);
const [timeline, threaded] = room.partitionThreadedEvents(events);

expect(timeline).toEqual([
threadRootEvent,
Expand All @@ -805,7 +810,7 @@ describe("MatrixClient", function() {
replyToThreadResponse,
];

const [timeline, threaded] = client.partitionThreadedEvents(room, events);
const [timeline, threaded] = room.partitionThreadedEvents(events);

expect(timeline).toEqual([
threadRootEvent,
Expand Down
18 changes: 14 additions & 4 deletions spec/test-utils/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { logger } from '../../src/logger';
import { IContent, IEvent, IUnsigned, MatrixEvent, MatrixEventEvent } from "../../src/models/event";
import { ClientEvent, EventType, MatrixClient } from "../../src";
import { SyncState } from "../../src/sync";
import { eventMapperFor } from "../../src/event-mapper";

/**
* Return a promise that is resolved when the client next emits a
Expand Down Expand Up @@ -79,6 +80,7 @@ interface IEventOpts {
redacts?: string;
}

let testEventIndex = 1; // counter for events, easier for comparison of randomly generated events
/**
* Create an Event.
* @param {Object} opts Values for the event.
Expand All @@ -88,9 +90,10 @@ interface IEventOpts {
* @param {string} opts.skey Optional. The state key (auto inserts empty string)
* @param {Object} opts.content The event.content
* @param {boolean} opts.event True to make a MatrixEvent.
* @param {MatrixClient} client If passed along with opts.event=true will be used to set up re-emitters.
* @return {Object} a JSON object representing this event.
*/
export function mkEvent(opts: IEventOpts): object | MatrixEvent {
export function mkEvent(opts: IEventOpts, client?: MatrixClient): object | MatrixEvent {
if (!opts.type || !opts.content) {
throw new Error("Missing .type or .content =>" + JSON.stringify(opts));
}
Expand All @@ -100,7 +103,8 @@ export function mkEvent(opts: IEventOpts): object | MatrixEvent {
sender: opts.sender || opts.user, // opts.user for backwards-compat
content: opts.content,
unsigned: opts.unsigned || {},
event_id: "$" + Math.random() + "-" + Math.random(),
event_id: "$" + testEventIndex++ + "-" + Math.random() + "-" + Math.random(),
txn_id: "~" + Math.random(),
redacts: opts.redacts,
};
if (opts.skey !== undefined) {
Expand All @@ -116,6 +120,11 @@ export function mkEvent(opts: IEventOpts): object | MatrixEvent {
].includes(opts.type)) {
event.state_key = "";
}

if (opts.event && client) {
return eventMapperFor(client, {})(event);
}

return opts.event ? new MatrixEvent(event) : event;
}

Expand Down Expand Up @@ -208,9 +217,10 @@ interface IMessageOpts {
* @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.
* @param {MatrixClient} client If passed along with opts.event=true will be used to set up re-emitters.
* @return {Object|MatrixEvent} The event
*/
export function mkMessage(opts: IMessageOpts): object | MatrixEvent {
export function mkMessage(opts: IMessageOpts, client?: MatrixClient): object | MatrixEvent {
const eventOpts: IEventOpts = {
...opts,
type: EventType.RoomMessage,
Expand All @@ -223,7 +233,7 @@ export function mkMessage(opts: IMessageOpts): object | MatrixEvent {
if (!eventOpts.content.body) {
eventOpts.content.body = "Random->" + Math.random();
}
return mkEvent(eventOpts);
return mkEvent(eventOpts, client);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/matrix-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ describe("MatrixClient", function() {

expect(rootEvent.isThreadRoot).toBe(true);

const [roomEvents, threadEvents] = client.partitionThreadedEvents(room, [rootEvent]);
const [roomEvents, threadEvents] = room.partitionThreadedEvents([rootEvent]);
expect(roomEvents).toHaveLength(1);
expect(threadEvents).toHaveLength(1);

Expand Down
Loading