Test for different types of events

This commit is contained in:
Jaiwanth 2021-08-10 12:25:11 +05:30
parent 900accd823
commit ecf0aba97c
3 changed files with 104 additions and 18 deletions

View file

@ -303,13 +303,17 @@ export default class HTMLExporter extends Exporter {
return eventTileMarkup; return eventTileMarkup;
} }
protected createModifiedEvent(text: string, mxEv: MatrixEvent) { protected createModifiedEvent(text: string, mxEv: MatrixEvent, italic=true) {
const modifiedContent = { const modifiedContent = {
msgtype: "m.text", msgtype: "m.text",
body: `*${text}*`, body: `${text}`,
format: "org.matrix.custom.html", format: "org.matrix.custom.html",
formatted_body: `<em>${text}</em>`, formatted_body: `${text}`,
}; };
if (italic) {
modifiedContent.formatted_body = '<em>' + modifiedContent.formatted_body + '</em>';
modifiedContent.body = '*' + modifiedContent.body + '*';
}
const modifiedEvent = new MatrixEvent(); const modifiedEvent = new MatrixEvent();
modifiedEvent.event = mxEv.event; modifiedEvent.event = mxEv.event;
modifiedEvent.sender = mxEv.sender; modifiedEvent.sender = mxEv.sender;
@ -356,7 +360,10 @@ export default class HTMLExporter extends Exporter {
} catch (e) { } catch (e) {
// TODO: Handle callEvent errors // TODO: Handle callEvent errors
console.error(e); console.error(e);
eventTile = await this.getEventTileMarkup(this.createModifiedEvent(textForEvent(mxEv), mxEv), joined); eventTile = await this.getEventTileMarkup(
this.createModifiedEvent(textForEvent(mxEv), mxEv, false),
joined,
);
} }
return eventTile; return eventTile;

View file

@ -113,6 +113,7 @@ export function createTestClient() {
* @param {number=} opts.ts Optional. Timestamp for the event * @param {number=} opts.ts Optional. Timestamp for the event
* @param {Object} opts.content The event.content * @param {Object} opts.content The event.content
* @param {boolean} opts.event True to make a MatrixEvent. * @param {boolean} opts.event True to make a MatrixEvent.
* @param {unsigned=} opts.unsigned
* @return {Object} a JSON object representing this event. * @return {Object} a JSON object representing this event.
*/ */
export function mkEvent(opts) { export function mkEvent(opts) {
@ -167,12 +168,13 @@ export function mkPresence(opts) {
* @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.prevMship The prev_content.membership for the event.
* @param {number=} opts.ts Optional. Timestamp 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 {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.
* @param {string} opts.url The content.avatar_url for the event. * @param {string=} opts.url The content.avatar_url for the event.
* @param {boolean} opts.event True to make a MatrixEvent. * @param {boolean} opts.event True to make a MatrixEvent.
* @return {Object|MatrixEvent} The event * @return {Object|MatrixEvent} The event
*/ */

View file

@ -67,24 +67,102 @@ describe('export', function() {
}, },
]; ];
const events: MatrixEvent[] = mkEvents();
const room = createRoom();
function createRoom() { function createRoom() {
const room = new Room(generateRoomId(), null, client.getUserId()); const room = new Room(generateRoomId(), null, client.getUserId());
return room; return room;
} }
const mockRoom = createRoom();
function mkEvents() { function mkEvents() {
const events = []; const matrixEvents = [];
const ts0 = Date.now(); const ts0 = Date.now();
for (let i = 0; i < 10; i++) { let i: number;
events.push(TestUtilsMatrix.mkMessage({ // plain text
for (i = 0; i < 10; i++) {
matrixEvents.push(TestUtilsMatrix.mkMessage({
event: true, room: "!room:id", user: "@user:id", event: true, room: "!room:id", user: "@user:id",
ts: ts0 + i * 1000, ts: ts0 + i * 1000,
})); }));
} }
return events; // reply events
for (i = 0; i < 10; i++) {
matrixEvents.push(TestUtilsMatrix.mkEvent({
"content": {
"body": "> <@me:here> Hi\n\nTest",
"format": "org.matrix.custom.html",
"m.relates_to": {
"m.in_reply_to": {
"event_id": "$" + Math.random() + "-" + Math.random(),
},
},
"msgtype": "m.text",
},
"user": "@me:here",
"type": "m.room.message",
"room": mockRoom.roomId,
"event": true,
}));
} }
// membership events
for (i = 0; i < 10; i++) {
matrixEvents.push(TestUtilsMatrix.mkMembership({
event: true, room: "!room:id", user: "@user:id",
target: {
userId: "@user:id",
name: "Bob",
getAvatarUrl: () => {
return "avatar.jpeg";
},
getMxcAvatarUrl: () => 'mxc://avatar.url/image.png',
},
ts: ts0 + i*1000,
mship: 'join',
prevMship: 'join',
name: 'A user',
}));
}
// emote
matrixEvents.push(TestUtilsMatrix.mkEvent({
"content": {
"body": "waves",
"msgtype": "m.emote",
},
"user": "@me:here",
"type": "m.room.message",
"room": mockRoom.roomId,
"event": true,
}));
// redacted events
for (i = 0; i < 10; i++) {
matrixEvents.push(new MatrixEvent({
type: "m.room.message",
sender: MY_USER_ID,
content: {},
unsigned: {
"age": 72,
"transaction_id": "m1212121212.23",
"redacted_because": {
"content": {},
"origin_server_ts": ts0 + i*1000,
"redacts": "$9999999999999999999999999999999999999999998",
"sender": "@me:here",
"type": "m.room.redaction",
"unsigned": {
"age": 94,
"transaction_id": "m1111111111.1",
},
"event_id": "$9999999999999999999999999999999999999999998",
"room_id": mockRoom.roomId,
},
},
event_id: "$9999999999999999999999999999999999999999999",
room_id: mockRoom.roomId,
}));
}
return matrixEvents;
}
const events: MatrixEvent[] = mkEvents();
it('checks if the export format is valid', function() { it('checks if the export format is valid', function() {
expect(textForFormat('HTML')).toBeTruthy(); expect(textForFormat('HTML')).toBeTruthy();
@ -101,7 +179,7 @@ describe('export', function() {
it('checks if the export options are valid', function() { it('checks if the export options are valid', function() {
for (const exportOption of invalidExportOptions) { for (const exportOption of invalidExportOptions) {
try { try {
new PlainTextExporter(room, ExportTypes.BEGINNING, exportOption, null); new PlainTextExporter(mockRoom, ExportTypes.BEGINNING, exportOption, null);
throw new Error("Expected to throw an error"); throw new Error("Expected to throw an error");
} catch (e) { } catch (e) {
expect(e.message).toBe("Invalid export options"); expect(e.message).toBe("Invalid export options");
@ -110,7 +188,7 @@ describe('export', function() {
}); });
it('tests the file extension splitter', function() { it('tests the file extension splitter', function() {
const exporter = new PlainTextExporter(room, ExportTypes.BEGINNING, mockExportOptions, null); const exporter = new PlainTextExporter(mockRoom, ExportTypes.BEGINNING, mockExportOptions, null);
const fileNameWithExtensions = { const fileNameWithExtensions = {
"": ["", ""], "": ["", ""],
"name": ["name", ""], "name": ["name", ""],
@ -147,15 +225,14 @@ describe('export', function() {
"expectedText": "<@me:here \"This\"> Reply", "expectedText": "<@me:here \"This\"> Reply",
}, },
]; ];
const exporter = new PlainTextExporter(room, ExportTypes.BEGINNING, mockExportOptions, null); const exporter = new PlainTextExporter(mockRoom, ExportTypes.BEGINNING, mockExportOptions, null);
for (const content of eventContents) { for (const content of eventContents) {
expect(exporter.textForReplyEvent(content)).toBe(content.expectedText); expect(exporter.textForReplyEvent(content)).toBe(content.expectedText);
} }
}); });
it('checks if the render to string works for eventTile', function() { it("checks if the render to string doesn't throw any error for different kinds of strings", function() {
// Todo: Generate different event types const exporter = new HTMLExporter(mockRoom, ExportTypes.BEGINNING, mockExportOptions, null);
const exporter = new HTMLExporter(room, ExportTypes.BEGINNING, mockExportOptions, null);
for (const event of events) { for (const event of events) {
expect(renderToString(exporter.getEventTile(event, false))).toBeTruthy(); expect(renderToString(exporter.getEventTile(event, false))).toBeTruthy();
} }