mirror of
https://github.com/element-hq/element-web
synced 2024-11-29 12:58:53 +03:00
Add tests for createEditContent which mirror tests for createMessageContent. (#10331)
This commit is contained in:
parent
85e8d27697
commit
aca077cf2b
3 changed files with 173 additions and 21 deletions
|
@ -67,7 +67,8 @@ function getTextReplyFallback(mxEvent: MatrixEvent): string {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
function createEditContent(model: EditorModel, editedEvent: MatrixEvent): IContent {
|
// exported for tests
|
||||||
|
export function createEditContent(model: EditorModel, editedEvent: MatrixEvent): IContent {
|
||||||
const isEmote = containsEmote(model);
|
const isEmote = containsEmote(model);
|
||||||
if (isEmote) {
|
if (isEmote) {
|
||||||
model = stripEmoteCommand(model);
|
model = stripEmoteCommand(model);
|
||||||
|
@ -103,15 +104,16 @@ function createEditContent(model: EditorModel, editedEvent: MatrixEvent): IConte
|
||||||
contentBody.formatted_body = `${htmlPrefix} * ${formattedBody}`;
|
contentBody.formatted_body = `${htmlPrefix} * ${formattedBody}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const relation = {
|
return Object.assign(
|
||||||
|
{
|
||||||
"m.new_content": newContent,
|
"m.new_content": newContent,
|
||||||
"m.relates_to": {
|
"m.relates_to": {
|
||||||
rel_type: "m.replace",
|
rel_type: "m.replace",
|
||||||
event_id: editedEvent.getId(),
|
event_id: editedEvent.getId(),
|
||||||
},
|
},
|
||||||
};
|
},
|
||||||
|
contentBody,
|
||||||
return Object.assign(relation, contentBody);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IEditMessageComposerProps extends MatrixClientProps {
|
interface IEditMessageComposerProps extends MatrixClientProps {
|
||||||
|
|
150
test/components/views/rooms/EditMessageComposer-test.tsx
Normal file
150
test/components/views/rooms/EditMessageComposer-test.tsx
Normal file
|
@ -0,0 +1,150 @@
|
||||||
|
/*
|
||||||
|
Copyright 2023 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 { createEditContent } from "../../../../src/components/views/rooms/EditMessageComposer";
|
||||||
|
import EditorModel from "../../../../src/editor/model";
|
||||||
|
import { createPartCreator } from "../../../editor/mock";
|
||||||
|
import { mkEvent } from "../../../test-utils";
|
||||||
|
import DocumentOffset from "../../../../src/editor/offset";
|
||||||
|
|
||||||
|
describe("<EditMessageComposer/>", () => {
|
||||||
|
const editedEvent = mkEvent({
|
||||||
|
type: "m.room.message",
|
||||||
|
user: "@alice:test",
|
||||||
|
room: "!abc:test",
|
||||||
|
content: { body: "original message", msgtype: "m.text" },
|
||||||
|
event: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("createEditContent", () => {
|
||||||
|
it("sends plaintext messages correctly", () => {
|
||||||
|
const model = new EditorModel([], createPartCreator());
|
||||||
|
const documentOffset = new DocumentOffset(11, true);
|
||||||
|
model.update("hello world", "insertText", documentOffset);
|
||||||
|
|
||||||
|
const content = createEditContent(model, editedEvent);
|
||||||
|
|
||||||
|
expect(content).toEqual({
|
||||||
|
"body": " * hello world",
|
||||||
|
"msgtype": "m.text",
|
||||||
|
"m.new_content": {
|
||||||
|
body: "hello world",
|
||||||
|
msgtype: "m.text",
|
||||||
|
},
|
||||||
|
"m.relates_to": {
|
||||||
|
event_id: editedEvent.getId(),
|
||||||
|
rel_type: "m.replace",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("sends markdown messages correctly", () => {
|
||||||
|
const model = new EditorModel([], createPartCreator());
|
||||||
|
const documentOffset = new DocumentOffset(13, true);
|
||||||
|
model.update("hello *world*", "insertText", documentOffset);
|
||||||
|
|
||||||
|
const content = createEditContent(model, editedEvent);
|
||||||
|
|
||||||
|
expect(content).toEqual({
|
||||||
|
"body": " * hello *world*",
|
||||||
|
"msgtype": "m.text",
|
||||||
|
"format": "org.matrix.custom.html",
|
||||||
|
"formatted_body": " * hello <em>world</em>",
|
||||||
|
"m.new_content": {
|
||||||
|
body: "hello *world*",
|
||||||
|
msgtype: "m.text",
|
||||||
|
format: "org.matrix.custom.html",
|
||||||
|
formatted_body: "hello <em>world</em>",
|
||||||
|
},
|
||||||
|
"m.relates_to": {
|
||||||
|
event_id: editedEvent.getId(),
|
||||||
|
rel_type: "m.replace",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("strips /me from messages and marks them as m.emote accordingly", () => {
|
||||||
|
const model = new EditorModel([], createPartCreator());
|
||||||
|
const documentOffset = new DocumentOffset(22, true);
|
||||||
|
model.update("/me blinks __quickly__", "insertText", documentOffset);
|
||||||
|
|
||||||
|
const content = createEditContent(model, editedEvent);
|
||||||
|
|
||||||
|
expect(content).toEqual({
|
||||||
|
"body": " * blinks __quickly__",
|
||||||
|
"msgtype": "m.emote",
|
||||||
|
"format": "org.matrix.custom.html",
|
||||||
|
"formatted_body": " * blinks <strong>quickly</strong>",
|
||||||
|
"m.new_content": {
|
||||||
|
body: "blinks __quickly__",
|
||||||
|
msgtype: "m.emote",
|
||||||
|
format: "org.matrix.custom.html",
|
||||||
|
formatted_body: "blinks <strong>quickly</strong>",
|
||||||
|
},
|
||||||
|
"m.relates_to": {
|
||||||
|
event_id: editedEvent.getId(),
|
||||||
|
rel_type: "m.replace",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("allows emoting with non-text parts", () => {
|
||||||
|
const model = new EditorModel([], createPartCreator());
|
||||||
|
const documentOffset = new DocumentOffset(16, true);
|
||||||
|
model.update("/me ✨sparkles✨", "insertText", documentOffset);
|
||||||
|
expect(model.parts.length).toEqual(4); // Emoji count as non-text
|
||||||
|
|
||||||
|
const content = createEditContent(model, editedEvent);
|
||||||
|
|
||||||
|
expect(content).toEqual({
|
||||||
|
"body": " * ✨sparkles✨",
|
||||||
|
"msgtype": "m.emote",
|
||||||
|
"m.new_content": {
|
||||||
|
body: "✨sparkles✨",
|
||||||
|
msgtype: "m.emote",
|
||||||
|
},
|
||||||
|
"m.relates_to": {
|
||||||
|
event_id: editedEvent.getId(),
|
||||||
|
rel_type: "m.replace",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("allows sending double-slash escaped slash commands correctly", () => {
|
||||||
|
const model = new EditorModel([], createPartCreator());
|
||||||
|
const documentOffset = new DocumentOffset(32, true);
|
||||||
|
|
||||||
|
model.update("//dev/null is my favourite place", "insertText", documentOffset);
|
||||||
|
|
||||||
|
const content = createEditContent(model, editedEvent);
|
||||||
|
|
||||||
|
// TODO Edits do not properly strip the double slash used to skip
|
||||||
|
// command processing.
|
||||||
|
expect(content).toEqual({
|
||||||
|
"body": " * //dev/null is my favourite place",
|
||||||
|
"msgtype": "m.text",
|
||||||
|
"m.new_content": {
|
||||||
|
body: "//dev/null is my favourite place",
|
||||||
|
msgtype: "m.text",
|
||||||
|
},
|
||||||
|
"m.relates_to": {
|
||||||
|
event_id: editedEvent.getId(),
|
||||||
|
rel_type: "m.replace",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
|
@ -26,7 +26,7 @@ import SendMessageComposer, {
|
||||||
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
||||||
import RoomContext, { TimelineRenderingType } from "../../../../src/contexts/RoomContext";
|
import RoomContext, { TimelineRenderingType } from "../../../../src/contexts/RoomContext";
|
||||||
import EditorModel from "../../../../src/editor/model";
|
import EditorModel from "../../../../src/editor/model";
|
||||||
import { createPartCreator, createRenderer } from "../../../editor/mock";
|
import { createPartCreator } from "../../../editor/mock";
|
||||||
import { createTestClient, mkEvent, mkStubRoom } from "../../../test-utils";
|
import { createTestClient, mkEvent, mkStubRoom } from "../../../test-utils";
|
||||||
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
||||||
import defaultDispatcher from "../../../../src/dispatcher/dispatcher";
|
import defaultDispatcher from "../../../../src/dispatcher/dispatcher";
|
||||||
|
@ -85,7 +85,7 @@ describe("<SendMessageComposer/>", () => {
|
||||||
const permalinkCreator = jest.fn() as any;
|
const permalinkCreator = jest.fn() as any;
|
||||||
|
|
||||||
it("sends plaintext messages correctly", () => {
|
it("sends plaintext messages correctly", () => {
|
||||||
const model = new EditorModel([], createPartCreator(), createRenderer());
|
const model = new EditorModel([], createPartCreator());
|
||||||
const documentOffset = new DocumentOffset(11, true);
|
const documentOffset = new DocumentOffset(11, true);
|
||||||
model.update("hello world", "insertText", documentOffset);
|
model.update("hello world", "insertText", documentOffset);
|
||||||
|
|
||||||
|
@ -98,7 +98,7 @@ describe("<SendMessageComposer/>", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("sends markdown messages correctly", () => {
|
it("sends markdown messages correctly", () => {
|
||||||
const model = new EditorModel([], createPartCreator(), createRenderer());
|
const model = new EditorModel([], createPartCreator());
|
||||||
const documentOffset = new DocumentOffset(13, true);
|
const documentOffset = new DocumentOffset(13, true);
|
||||||
model.update("hello *world*", "insertText", documentOffset);
|
model.update("hello *world*", "insertText", documentOffset);
|
||||||
|
|
||||||
|
@ -113,7 +113,7 @@ describe("<SendMessageComposer/>", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("strips /me from messages and marks them as m.emote accordingly", () => {
|
it("strips /me from messages and marks them as m.emote accordingly", () => {
|
||||||
const model = new EditorModel([], createPartCreator(), createRenderer());
|
const model = new EditorModel([], createPartCreator());
|
||||||
const documentOffset = new DocumentOffset(22, true);
|
const documentOffset = new DocumentOffset(22, true);
|
||||||
model.update("/me blinks __quickly__", "insertText", documentOffset);
|
model.update("/me blinks __quickly__", "insertText", documentOffset);
|
||||||
|
|
||||||
|
@ -128,7 +128,7 @@ describe("<SendMessageComposer/>", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("allows emoting with non-text parts", () => {
|
it("allows emoting with non-text parts", () => {
|
||||||
const model = new EditorModel([], createPartCreator(), createRenderer());
|
const model = new EditorModel([], createPartCreator());
|
||||||
const documentOffset = new DocumentOffset(16, true);
|
const documentOffset = new DocumentOffset(16, true);
|
||||||
model.update("/me ✨sparkles✨", "insertText", documentOffset);
|
model.update("/me ✨sparkles✨", "insertText", documentOffset);
|
||||||
expect(model.parts.length).toEqual(4); // Emoji count as non-text
|
expect(model.parts.length).toEqual(4); // Emoji count as non-text
|
||||||
|
@ -142,7 +142,7 @@ describe("<SendMessageComposer/>", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("allows sending double-slash escaped slash commands correctly", () => {
|
it("allows sending double-slash escaped slash commands correctly", () => {
|
||||||
const model = new EditorModel([], createPartCreator(), createRenderer());
|
const model = new EditorModel([], createPartCreator());
|
||||||
const documentOffset = new DocumentOffset(32, true);
|
const documentOffset = new DocumentOffset(32, true);
|
||||||
|
|
||||||
model.update("//dev/null is my favourite place", "insertText", documentOffset);
|
model.update("//dev/null is my favourite place", "insertText", documentOffset);
|
||||||
|
@ -349,7 +349,7 @@ describe("<SendMessageComposer/>", () => {
|
||||||
|
|
||||||
describe("isQuickReaction", () => {
|
describe("isQuickReaction", () => {
|
||||||
it("correctly detects quick reaction", () => {
|
it("correctly detects quick reaction", () => {
|
||||||
const model = new EditorModel([], createPartCreator(), createRenderer());
|
const model = new EditorModel([], createPartCreator());
|
||||||
model.update("+😊", "insertText", new DocumentOffset(3, true));
|
model.update("+😊", "insertText", new DocumentOffset(3, true));
|
||||||
|
|
||||||
const isReaction = isQuickReaction(model);
|
const isReaction = isQuickReaction(model);
|
||||||
|
@ -358,7 +358,7 @@ describe("<SendMessageComposer/>", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("correctly detects quick reaction with space", () => {
|
it("correctly detects quick reaction with space", () => {
|
||||||
const model = new EditorModel([], createPartCreator(), createRenderer());
|
const model = new EditorModel([], createPartCreator());
|
||||||
model.update("+ 😊", "insertText", new DocumentOffset(4, true));
|
model.update("+ 😊", "insertText", new DocumentOffset(4, true));
|
||||||
|
|
||||||
const isReaction = isQuickReaction(model);
|
const isReaction = isQuickReaction(model);
|
||||||
|
@ -367,10 +367,10 @@ describe("<SendMessageComposer/>", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("correctly rejects quick reaction with extra text", () => {
|
it("correctly rejects quick reaction with extra text", () => {
|
||||||
const model = new EditorModel([], createPartCreator(), createRenderer());
|
const model = new EditorModel([], createPartCreator());
|
||||||
const model2 = new EditorModel([], createPartCreator(), createRenderer());
|
const model2 = new EditorModel([], createPartCreator());
|
||||||
const model3 = new EditorModel([], createPartCreator(), createRenderer());
|
const model3 = new EditorModel([], createPartCreator());
|
||||||
const model4 = new EditorModel([], createPartCreator(), createRenderer());
|
const model4 = new EditorModel([], createPartCreator());
|
||||||
model.update("+😊hello", "insertText", new DocumentOffset(8, true));
|
model.update("+😊hello", "insertText", new DocumentOffset(8, true));
|
||||||
model2.update(" +😊", "insertText", new DocumentOffset(4, true));
|
model2.update(" +😊", "insertText", new DocumentOffset(4, true));
|
||||||
model3.update("+ 😊😊", "insertText", new DocumentOffset(6, true));
|
model3.update("+ 😊😊", "insertText", new DocumentOffset(6, true));
|
||||||
|
|
Loading…
Reference in a new issue