2021-07-26 19:41:39 +03:00
|
|
|
import './skinned-sdk';
|
|
|
|
|
|
|
|
import { MatrixEvent } from "matrix-js-sdk";
|
2021-10-23 01:23:32 +03:00
|
|
|
import renderer from 'react-test-renderer';
|
|
|
|
|
2022-01-19 03:58:31 +03:00
|
|
|
import { getSenderName, textForEvent } from "../src/TextForEvent";
|
2021-07-26 19:41:39 +03:00
|
|
|
import SettingsStore from "../src/settings/SettingsStore";
|
|
|
|
import { SettingLevel } from "../src/settings/SettingLevel";
|
|
|
|
|
|
|
|
function mockPinnedEvent(
|
|
|
|
pinnedMessageIds?: string[],
|
|
|
|
prevPinnedMessageIds?: string[],
|
|
|
|
): MatrixEvent {
|
|
|
|
return new MatrixEvent({
|
|
|
|
type: "m.room.pinned_events",
|
|
|
|
state_key: "",
|
|
|
|
sender: "@foo:example.com",
|
|
|
|
content: {
|
|
|
|
pinned: pinnedMessageIds,
|
|
|
|
},
|
|
|
|
prev_content: {
|
|
|
|
pinned: prevPinnedMessageIds,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-11 13:40:33 +03:00
|
|
|
// Helper function that renders a component to a plain text string.
|
|
|
|
// Once snapshots are introduced in tests, this function will no longer be necessary,
|
|
|
|
// and should be replaced with snapshots.
|
|
|
|
function renderComponent(component): string {
|
|
|
|
const serializeObject = (object): string => {
|
|
|
|
if (typeof object === 'string') {
|
|
|
|
return object === ' ' ? '' : object;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(object) && object.length === 1 && typeof object[0] === 'string') {
|
|
|
|
return object[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object['type'] !== undefined && typeof object['children'] !== undefined) {
|
|
|
|
return serializeObject(object.children);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(object)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return object.map(child => {
|
|
|
|
return serializeObject(child);
|
|
|
|
}).join('');
|
|
|
|
};
|
|
|
|
|
|
|
|
return serializeObject(component.toJSON());
|
|
|
|
}
|
|
|
|
|
2021-08-10 18:06:33 +03:00
|
|
|
describe('TextForEvent', () => {
|
2022-01-19 03:58:31 +03:00
|
|
|
describe("getSenderName()", () => {
|
|
|
|
it("Prefers sender.name", () => {
|
|
|
|
expect(getSenderName({ sender: { name: "Alice" } } as MatrixEvent)).toBe("Alice");
|
|
|
|
});
|
|
|
|
it("Handles missing sender", () => {
|
|
|
|
expect(getSenderName({ getSender: () => "Alice" } as MatrixEvent)).toBe("Alice");
|
|
|
|
});
|
|
|
|
it("Handles missing sender and get sender", () => {
|
|
|
|
expect(getSenderName({ getSender: () => undefined } as MatrixEvent)).toBe("Someone");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-08-10 18:06:33 +03:00
|
|
|
describe("TextForPinnedEvent", () => {
|
|
|
|
SettingsStore.setValue("feature_pinning", null, SettingLevel.DEVICE, true);
|
|
|
|
|
|
|
|
it("mentions message when a single message was pinned, with no previously pinned messages", () => {
|
|
|
|
const event = mockPinnedEvent(['message-1']);
|
2021-08-11 13:40:33 +03:00
|
|
|
const plainText = textForEvent(event);
|
2021-08-10 18:06:33 +03:00
|
|
|
const component = renderer.create(textForEvent(event, true));
|
2021-08-11 13:40:33 +03:00
|
|
|
|
|
|
|
const expectedText = "@foo:example.com pinned a message to this room. See all pinned messages.";
|
|
|
|
expect(plainText).toBe(expectedText);
|
|
|
|
expect(renderComponent(component)).toBe(expectedText);
|
2021-08-10 18:06:33 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("mentions message when a single message was pinned, with multiple previously pinned messages", () => {
|
2021-08-11 16:47:49 +03:00
|
|
|
const event = mockPinnedEvent(['message-1', 'message-2', 'message-3'], ['message-1', 'message-2']);
|
2021-08-11 13:40:33 +03:00
|
|
|
const plainText = textForEvent(event);
|
2021-08-10 18:06:33 +03:00
|
|
|
const component = renderer.create(textForEvent(event, true));
|
2021-08-11 13:40:33 +03:00
|
|
|
|
|
|
|
const expectedText = "@foo:example.com pinned a message to this room. See all pinned messages.";
|
|
|
|
expect(plainText).toBe(expectedText);
|
|
|
|
expect(renderComponent(component)).toBe(expectedText);
|
2021-08-10 18:06:33 +03:00
|
|
|
});
|
|
|
|
|
2021-08-11 16:51:36 +03:00
|
|
|
it("mentions message when a single message was unpinned, with a single message previously pinned", () => {
|
|
|
|
const event = mockPinnedEvent([], ['message-1']);
|
2021-08-11 13:40:33 +03:00
|
|
|
const plainText = textForEvent(event);
|
2021-08-10 18:06:33 +03:00
|
|
|
const component = renderer.create(textForEvent(event, true));
|
2021-08-11 13:40:33 +03:00
|
|
|
|
2021-08-11 16:51:36 +03:00
|
|
|
const expectedText = "@foo:example.com unpinned a message from this room. See all pinned messages.";
|
2021-08-11 13:40:33 +03:00
|
|
|
expect(plainText).toBe(expectedText);
|
|
|
|
expect(renderComponent(component)).toBe(expectedText);
|
2021-08-10 18:06:33 +03:00
|
|
|
});
|
|
|
|
|
2021-08-11 16:51:36 +03:00
|
|
|
it("mentions message when a single message was unpinned, with multiple previously pinned messages", () => {
|
|
|
|
const event = mockPinnedEvent(['message-2'], ['message-1', 'message-2']);
|
2021-08-11 13:40:33 +03:00
|
|
|
const plainText = textForEvent(event);
|
2021-08-10 18:06:33 +03:00
|
|
|
const component = renderer.create(textForEvent(event, true));
|
2021-08-11 13:40:33 +03:00
|
|
|
|
|
|
|
const expectedText = "@foo:example.com unpinned a message from this room. See all pinned messages.";
|
|
|
|
expect(plainText).toBe(expectedText);
|
|
|
|
expect(renderComponent(component)).toBe(expectedText);
|
2021-08-10 18:06:33 +03:00
|
|
|
});
|
|
|
|
|
2021-08-11 16:51:36 +03:00
|
|
|
it("shows generic text when multiple messages were pinned", () => {
|
|
|
|
const event = mockPinnedEvent(['message-1', 'message-2', 'message-3'], ['message-1']);
|
2021-08-11 13:40:33 +03:00
|
|
|
const plainText = textForEvent(event);
|
2021-08-10 18:06:33 +03:00
|
|
|
const component = renderer.create(textForEvent(event, true));
|
2021-08-11 13:40:33 +03:00
|
|
|
|
2021-08-11 16:51:36 +03:00
|
|
|
const expectedText = "@foo:example.com changed the pinned messages for the room.";
|
2021-08-11 13:40:33 +03:00
|
|
|
expect(plainText).toBe(expectedText);
|
|
|
|
expect(renderComponent(component)).toBe(expectedText);
|
2021-08-10 18:06:33 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("shows generic text when multiple messages were unpinned", () => {
|
|
|
|
const event = mockPinnedEvent(['message-3'], ['message-1', 'message-2', 'message-3']);
|
2021-08-11 13:40:33 +03:00
|
|
|
const plainText = textForEvent(event);
|
2021-08-10 18:06:33 +03:00
|
|
|
const component = renderer.create(textForEvent(event, true));
|
2021-08-11 13:40:33 +03:00
|
|
|
|
|
|
|
const expectedText = "@foo:example.com changed the pinned messages for the room.";
|
|
|
|
expect(plainText).toBe(expectedText);
|
|
|
|
expect(renderComponent(component)).toBe(expectedText);
|
2021-08-10 18:06:33 +03:00
|
|
|
});
|
2021-08-11 16:56:59 +03:00
|
|
|
|
|
|
|
it("shows generic text when one message was pinned, and another unpinned", () => {
|
|
|
|
const event = mockPinnedEvent(['message-2'], ['message-1']);
|
|
|
|
const plainText = textForEvent(event);
|
|
|
|
const component = renderer.create(textForEvent(event, true));
|
|
|
|
|
|
|
|
const expectedText = "@foo:example.com changed the pinned messages for the room.";
|
|
|
|
expect(plainText).toBe(expectedText);
|
|
|
|
expect(renderComponent(component)).toBe(expectedText);
|
|
|
|
});
|
2021-07-26 19:41:39 +03:00
|
|
|
});
|
|
|
|
});
|