Add tests to message.ts
This commit is contained in:
parent
67aab08759
commit
667e8ef10f
2 changed files with 160 additions and 7 deletions
|
@ -23,7 +23,6 @@ import { PosthogAnalytics } from "../../../../PosthogAnalytics";
|
||||||
import SettingsStore from "../../../../settings/SettingsStore";
|
import SettingsStore from "../../../../settings/SettingsStore";
|
||||||
import { decorateStartSendingTime, sendRoundTripMetric } from "../../../../sendTimePerformanceMetrics";
|
import { decorateStartSendingTime, sendRoundTripMetric } from "../../../../sendTimePerformanceMetrics";
|
||||||
import { attachRelation } from "../SendMessageComposer";
|
import { attachRelation } from "../SendMessageComposer";
|
||||||
import { addReplyToMessageContent } from "../../../../utils/Reply";
|
|
||||||
import { RoomPermalinkCreator } from "../../../../utils/permalinks/Permalinks";
|
import { RoomPermalinkCreator } from "../../../../utils/permalinks/Permalinks";
|
||||||
import { doMaybeLocalRoomAction } from "../../../../utils/local-room";
|
import { doMaybeLocalRoomAction } from "../../../../utils/local-room";
|
||||||
import { CHAT_EFFECTS } from "../../../../effects";
|
import { CHAT_EFFECTS } from "../../../../effects";
|
||||||
|
@ -33,7 +32,7 @@ import dis from '../../../../dispatcher/dispatcher';
|
||||||
|
|
||||||
interface SendMessageParams {
|
interface SendMessageParams {
|
||||||
mxClient: MatrixClient;
|
mxClient: MatrixClient;
|
||||||
relation: IEventRelation;
|
relation?: IEventRelation;
|
||||||
replyToEvent?: MatrixEvent;
|
replyToEvent?: MatrixEvent;
|
||||||
roomContext: IRoomState;
|
roomContext: IRoomState;
|
||||||
permalinkCreator: RoomPermalinkCreator;
|
permalinkCreator: RoomPermalinkCreator;
|
||||||
|
@ -81,12 +80,14 @@ export function createMessageContent(
|
||||||
}
|
}
|
||||||
|
|
||||||
attachRelation(content, relation);
|
attachRelation(content, relation);
|
||||||
if (replyToEvent) {
|
|
||||||
|
// TODO reply
|
||||||
|
/*if (replyToEvent) {
|
||||||
addReplyToMessageContent(content, replyToEvent, {
|
addReplyToMessageContent(content, replyToEvent, {
|
||||||
permalinkCreator,
|
permalinkCreator,
|
||||||
includeLegacyFallback: includeReplyLegacyFallback,
|
includeLegacyFallback: includeReplyLegacyFallback,
|
||||||
});
|
});
|
||||||
}
|
}*/
|
||||||
|
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
@ -106,7 +107,7 @@ export function sendMessage(
|
||||||
inThread: relation?.rel_type === THREAD_RELATION_TYPE.name,
|
inThread: relation?.rel_type === THREAD_RELATION_TYPE.name,
|
||||||
};
|
};
|
||||||
if (posthogEvent.inThread) {
|
if (posthogEvent.inThread) {
|
||||||
const threadRoot = room.findEventById(relation.event_id);
|
const threadRoot = room.findEventById(relation?.event_id);
|
||||||
posthogEvent.startsThread = threadRoot?.getThread()?.events.length === 1;
|
posthogEvent.startsThread = threadRoot?.getThread()?.events.length === 1;
|
||||||
}
|
}
|
||||||
PosthogAnalytics.instance.trackEvent<ComposerEvent>(posthogEvent);
|
PosthogAnalytics.instance.trackEvent<ComposerEvent>(posthogEvent);
|
||||||
|
@ -144,7 +145,9 @@ export function sendMessage(
|
||||||
(actualRoomId: string) => mxClient.sendMessage(actualRoomId, threadId, content),
|
(actualRoomId: string) => mxClient.sendMessage(actualRoomId, threadId, content),
|
||||||
mxClient,
|
mxClient,
|
||||||
);
|
);
|
||||||
if (replyToEvent) {
|
|
||||||
|
// TODO reply
|
||||||
|
/*if (replyToEvent) {
|
||||||
// Clear reply_to_event as we put the message into the queue
|
// Clear reply_to_event as we put the message into the queue
|
||||||
// if the send fails, retry will handle resending.
|
// if the send fails, retry will handle resending.
|
||||||
dis.dispatch({
|
dis.dispatch({
|
||||||
|
@ -152,7 +155,7 @@ export function sendMessage(
|
||||||
event: null,
|
event: null,
|
||||||
context: roomContext.timelineRenderingType,
|
context: roomContext.timelineRenderingType,
|
||||||
});
|
});
|
||||||
}
|
}*/
|
||||||
dis.dispatch({ action: "message_sent" });
|
dis.dispatch({ action: "message_sent" });
|
||||||
CHAT_EFFECTS.forEach((effect) => {
|
CHAT_EFFECTS.forEach((effect) => {
|
||||||
if (containsEmoji(content, effect.emojis)) {
|
if (containsEmoji(content, effect.emojis)) {
|
||||||
|
@ -180,4 +183,6 @@ export function sendMessage(
|
||||||
timelineRenderingType: roomContext.timelineRenderingType,
|
timelineRenderingType: roomContext.timelineRenderingType,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return prom;
|
||||||
}
|
}
|
||||||
|
|
148
test/components/views/rooms/wysisyg_composer/message-test.ts
Normal file
148
test/components/views/rooms/wysisyg_composer/message-test.ts
Normal file
|
@ -0,0 +1,148 @@
|
||||||
|
/*
|
||||||
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { IRoomState } from "../../../../../src/components/structures/RoomView";
|
||||||
|
import { createMessageContent, sendMessage } from "../../../../../src/components/views/rooms/wysiwyg_composer/message";
|
||||||
|
import { TimelineRenderingType } from "../../../../../src/contexts/RoomContext";
|
||||||
|
import { Layout } from "../../../../../src/settings/enums/Layout";
|
||||||
|
import { createTestClient, mkEvent, mkStubRoom } from "../../../../test-utils";
|
||||||
|
import defaultDispatcher from "../../../../../src/dispatcher/dispatcher";
|
||||||
|
import SettingsStore from "../../../../../src/settings/SettingsStore";
|
||||||
|
import { SettingLevel } from "../../../../../src/settings/SettingLevel";
|
||||||
|
|
||||||
|
describe('message', () => {
|
||||||
|
const permalinkCreator = jest.fn() as any;
|
||||||
|
const message = '<i><b>hello</b> world</i>';
|
||||||
|
const mockEvent = mkEvent({
|
||||||
|
type: "m.room.message",
|
||||||
|
room: 'myfakeroom',
|
||||||
|
user: 'myfakeuser',
|
||||||
|
content: { "msgtype": "m.text", "body": "Replying to this" },
|
||||||
|
event: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
jest.resetAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('createMessageContent', () => {
|
||||||
|
it("Should create html message", () => {
|
||||||
|
// When
|
||||||
|
const content = createMessageContent(message, { permalinkCreator });
|
||||||
|
|
||||||
|
// Then
|
||||||
|
expect(content).toEqual({
|
||||||
|
body: message,
|
||||||
|
format: "org.matrix.custom.html",
|
||||||
|
formatted_body: message,
|
||||||
|
msgtype: "m.text",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('sendMessage', () => {
|
||||||
|
const mockClient = createTestClient();
|
||||||
|
const mockRoom = mkStubRoom('myfakeroom', 'myfakeroom', mockClient) as any;
|
||||||
|
mockRoom.findEventById = jest.fn(eventId => {
|
||||||
|
return eventId === mockEvent.getId() ? mockEvent : null;
|
||||||
|
});
|
||||||
|
|
||||||
|
const defaultRoomContext: IRoomState = {
|
||||||
|
room: mockRoom,
|
||||||
|
roomLoading: true,
|
||||||
|
peekLoading: false,
|
||||||
|
shouldPeek: true,
|
||||||
|
membersLoaded: false,
|
||||||
|
numUnreadMessages: 0,
|
||||||
|
canPeek: false,
|
||||||
|
showApps: false,
|
||||||
|
isPeeking: false,
|
||||||
|
showRightPanel: true,
|
||||||
|
joining: false,
|
||||||
|
atEndOfLiveTimeline: true,
|
||||||
|
showTopUnreadMessagesBar: false,
|
||||||
|
statusBarVisible: false,
|
||||||
|
canReact: false,
|
||||||
|
canSendMessages: false,
|
||||||
|
canSendVoiceBroadcasts: false,
|
||||||
|
layout: Layout.Group,
|
||||||
|
lowBandwidth: false,
|
||||||
|
alwaysShowTimestamps: false,
|
||||||
|
showTwelveHourTimestamps: false,
|
||||||
|
readMarkerInViewThresholdMs: 3000,
|
||||||
|
readMarkerOutOfViewThresholdMs: 30000,
|
||||||
|
showHiddenEvents: false,
|
||||||
|
showReadReceipts: true,
|
||||||
|
showRedactions: true,
|
||||||
|
showJoinLeaves: true,
|
||||||
|
showAvatarChanges: true,
|
||||||
|
showDisplaynameChanges: true,
|
||||||
|
matrixClientIsReady: false,
|
||||||
|
timelineRenderingType: TimelineRenderingType.Room,
|
||||||
|
liveTimeline: undefined,
|
||||||
|
canSelfRedact: false,
|
||||||
|
resizing: false,
|
||||||
|
narrow: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
const spyDispatcher = jest.spyOn(defaultDispatcher, "dispatch");
|
||||||
|
|
||||||
|
it('Should not send empty html message', async () => {
|
||||||
|
// When
|
||||||
|
await sendMessage(message, { roomContext: defaultRoomContext, mxClient: mockClient, permalinkCreator });
|
||||||
|
|
||||||
|
// Then
|
||||||
|
const expectedContent = {
|
||||||
|
"body": "<i><b>hello</b> world</i>",
|
||||||
|
"format": "org.matrix.custom.html",
|
||||||
|
"formatted_body": "<i><b>hello</b> world</i>",
|
||||||
|
"msgtype": "m.text",
|
||||||
|
};
|
||||||
|
expect(mockClient.sendMessage).toBeCalledWith('myfakeroom', null, expectedContent);
|
||||||
|
expect(spyDispatcher).toBeCalledWith({ action: 'message_sent' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should send html message', async () => {
|
||||||
|
// When
|
||||||
|
await sendMessage('', { roomContext: defaultRoomContext, mxClient: mockClient, permalinkCreator });
|
||||||
|
|
||||||
|
// Then
|
||||||
|
expect(mockClient.sendMessage).toBeCalledTimes(0);
|
||||||
|
expect(spyDispatcher).toBeCalledTimes(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should scroll to bottom after sending a html message', async () => {
|
||||||
|
// When
|
||||||
|
SettingsStore.setValue("scrollToBottomOnMessageSent", null, SettingLevel.DEVICE, true);
|
||||||
|
await sendMessage(message, { roomContext: defaultRoomContext, mxClient: mockClient, permalinkCreator });
|
||||||
|
|
||||||
|
// Then
|
||||||
|
expect(spyDispatcher).toBeCalledWith(
|
||||||
|
{ action: 'scroll_to_bottom', timelineRenderingType: defaultRoomContext.timelineRenderingType },
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should handle emojis', async () => {
|
||||||
|
// When
|
||||||
|
await sendMessage('🎉', { roomContext: defaultRoomContext, mxClient: mockClient, permalinkCreator });
|
||||||
|
|
||||||
|
// Then
|
||||||
|
expect(spyDispatcher).toBeCalledWith(
|
||||||
|
{ action: 'effects.confetti' },
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue