Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 22ddb2d

Browse files
committed
test: adding tests for (de-)serializing without markdown
1 parent 9e5e6a5 commit 22ddb2d

File tree

2 files changed

+148
-52
lines changed

2 files changed

+148
-52
lines changed

test/editor/deserialize-test.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,4 +331,78 @@ describe('editor/deserialize', function() {
331331
expect(parts).toMatchSnapshot();
332332
});
333333
});
334+
describe('plaintext messages', function() {
335+
it('turns html tags back into markdown', function() {
336+
const html = "<strong>bold</strong> and <em>emphasized</em> text <a href=\"http://example.com/\">this</a>!";
337+
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator(), { shouldEscape: false }));
338+
expect(parts.length).toBe(1);
339+
expect(parts[0]).toStrictEqual({
340+
type: "plain",
341+
text: "**bold** and _emphasized_ text [this](http://example.com/)!",
342+
});
343+
});
344+
it('keeps backticks unescaped', () => {
345+
const html = "this → ` is a backtick and here are 3 of them:\n```";
346+
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator(), { shouldEscape: false }));
347+
expect(parts.length).toBe(1);
348+
expect(parts[0]).toStrictEqual({
349+
type: "plain",
350+
text: "this → ` is a backtick and here are 3 of them:\n```",
351+
});
352+
});
353+
it('keeps backticks outside of code blocks', () => {
354+
const html = "some `backticks`";
355+
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator(), { shouldEscape: false }));
356+
expect(parts.length).toBe(1);
357+
expect(parts[0]).toStrictEqual({
358+
type: "plain",
359+
text: "some `backticks`",
360+
});
361+
});
362+
it('keeps backslashes', () => {
363+
const html = "C:\\My Documents";
364+
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator(), { shouldEscape: false }));
365+
expect(parts.length).toBe(1);
366+
expect(parts[0]).toStrictEqual({
367+
type: "plain",
368+
text: "C:\\My Documents",
369+
});
370+
});
371+
it('keeps asterisks', () => {
372+
const html = "*hello*";
373+
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator(), { shouldEscape: false }));
374+
expect(parts.length).toBe(1);
375+
expect(parts[0]).toStrictEqual({
376+
type: "plain",
377+
text: "*hello*",
378+
});
379+
});
380+
it('keeps underscores', () => {
381+
const html = "__emphasis__";
382+
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator(), { shouldEscape: false }));
383+
expect(parts.length).toBe(1);
384+
expect(parts[0]).toStrictEqual({
385+
type: "plain",
386+
text: "__emphasis__",
387+
});
388+
});
389+
it('keeps square brackets', () => {
390+
const html = "[not an actual link](https://example.org)";
391+
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator(), { shouldEscape: false }));
392+
expect(parts.length).toBe(1);
393+
expect(parts[0]).toStrictEqual({
394+
type: "plain",
395+
text: "[not an actual link](https://example.org)",
396+
});
397+
});
398+
it('escapes angle brackets', () => {
399+
const html = "> &lt;del&gt;no formatting here&lt;/del&gt;";
400+
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator(), { shouldEscape: false }));
401+
expect(parts.length).toBe(1);
402+
expect(parts[0]).toStrictEqual({
403+
type: "plain",
404+
text: "> <del>no formatting here</del>",
405+
});
406+
});
407+
});
334408
});

test/editor/serialize-test.ts

Lines changed: 74 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -19,58 +19,80 @@ import { htmlSerializeIfNeeded } from "../../src/editor/serialize";
1919
import { createPartCreator } from "./mock";
2020

2121
describe('editor/serialize', function() {
22-
it('user pill turns message into html', function() {
23-
const pc = createPartCreator();
24-
const model = new EditorModel([pc.userPill("Alice", "@alice:hs.tld")], pc);
25-
const html = htmlSerializeIfNeeded(model, {});
26-
expect(html).toBe("<a href=\"https://matrix.to/#/@alice:hs.tld\">Alice</a>");
22+
describe('with markdown', function() {
23+
it('user pill turns message into html', function() {
24+
const pc = createPartCreator();
25+
const model = new EditorModel([pc.userPill("Alice", "@alice:hs.tld")], pc);
26+
const html = htmlSerializeIfNeeded(model, {});
27+
expect(html).toBe("<a href=\"https://matrix.to/#/@alice:hs.tld\">Alice</a>");
28+
});
29+
it('room pill turns message into html', function() {
30+
const pc = createPartCreator();
31+
const model = new EditorModel([pc.roomPill("#room:hs.tld")], pc);
32+
const html = htmlSerializeIfNeeded(model, {});
33+
expect(html).toBe("<a href=\"https://matrix.to/#/#room:hs.tld\">#room:hs.tld</a>");
34+
});
35+
it('@room pill turns message into html', function() {
36+
const pc = createPartCreator();
37+
const model = new EditorModel([pc.atRoomPill("@room")], pc);
38+
const html = htmlSerializeIfNeeded(model, {});
39+
expect(html).toBeFalsy();
40+
});
41+
it('any markdown turns message into html', function() {
42+
const pc = createPartCreator();
43+
const model = new EditorModel([pc.plain("*hello* world")], pc);
44+
const html = htmlSerializeIfNeeded(model, {});
45+
expect(html).toBe("<em>hello</em> world");
46+
});
47+
it('displaynames ending in a backslash work', function() {
48+
const pc = createPartCreator();
49+
const model = new EditorModel([pc.userPill("Displayname\\", "@user:server")], pc);
50+
const html = htmlSerializeIfNeeded(model, {});
51+
expect(html).toBe("<a href=\"https://matrix.to/#/@user:server\">Displayname\\</a>");
52+
});
53+
it('displaynames containing an opening square bracket work', function() {
54+
const pc = createPartCreator();
55+
const model = new EditorModel([pc.userPill("Displayname[[", "@user:server")], pc);
56+
const html = htmlSerializeIfNeeded(model, {});
57+
expect(html).toBe("<a href=\"https://matrix.to/#/@user:server\">Displayname[[</a>");
58+
});
59+
it('displaynames containing a closing square bracket work', function() {
60+
const pc = createPartCreator();
61+
const model = new EditorModel([pc.userPill("Displayname]", "@user:server")], pc);
62+
const html = htmlSerializeIfNeeded(model, {});
63+
expect(html).toBe("<a href=\"https://matrix.to/#/@user:server\">Displayname]</a>");
64+
});
65+
it('escaped markdown should not retain backslashes', function() {
66+
const pc = createPartCreator();
67+
const model = new EditorModel([pc.plain('\\*hello\\* world')], pc);
68+
const html = htmlSerializeIfNeeded(model, {});
69+
expect(html).toBe('*hello* world');
70+
});
71+
it('escaped markdown should convert HTML entities', function() {
72+
const pc = createPartCreator();
73+
const model = new EditorModel([pc.plain('\\*hello\\* world < hey world!')], pc);
74+
const html = htmlSerializeIfNeeded(model, {});
75+
expect(html).toBe('*hello* world &lt; hey world!');
76+
});
2777
});
28-
it('room pill turns message into html', function() {
29-
const pc = createPartCreator();
30-
const model = new EditorModel([pc.roomPill("#room:hs.tld")], pc);
31-
const html = htmlSerializeIfNeeded(model, {});
32-
expect(html).toBe("<a href=\"https://matrix.to/#/#room:hs.tld\">#room:hs.tld</a>");
33-
});
34-
it('@room pill turns message into html', function() {
35-
const pc = createPartCreator();
36-
const model = new EditorModel([pc.atRoomPill("@room")], pc);
37-
const html = htmlSerializeIfNeeded(model, {});
38-
expect(html).toBeFalsy();
39-
});
40-
it('any markdown turns message into html', function() {
41-
const pc = createPartCreator();
42-
const model = new EditorModel([pc.plain("*hello* world")], pc);
43-
const html = htmlSerializeIfNeeded(model, {});
44-
expect(html).toBe("<em>hello</em> world");
45-
});
46-
it('displaynames ending in a backslash work', function() {
47-
const pc = createPartCreator();
48-
const model = new EditorModel([pc.userPill("Displayname\\", "@user:server")], pc);
49-
const html = htmlSerializeIfNeeded(model, {});
50-
expect(html).toBe("<a href=\"https://matrix.to/#/@user:server\">Displayname\\</a>");
51-
});
52-
it('displaynames containing an opening square bracket work', function() {
53-
const pc = createPartCreator();
54-
const model = new EditorModel([pc.userPill("Displayname[[", "@user:server")], pc);
55-
const html = htmlSerializeIfNeeded(model, {});
56-
expect(html).toBe("<a href=\"https://matrix.to/#/@user:server\">Displayname[[</a>");
57-
});
58-
it('displaynames containing a closing square bracket work', function() {
59-
const pc = createPartCreator();
60-
const model = new EditorModel([pc.userPill("Displayname]", "@user:server")], pc);
61-
const html = htmlSerializeIfNeeded(model, {});
62-
expect(html).toBe("<a href=\"https://matrix.to/#/@user:server\">Displayname]</a>");
63-
});
64-
it('escaped markdown should not retain backslashes', function() {
65-
const pc = createPartCreator();
66-
const model = new EditorModel([pc.plain('\\*hello\\* world')], pc);
67-
const html = htmlSerializeIfNeeded(model, {});
68-
expect(html).toBe('*hello* world');
69-
});
70-
it('escaped markdown should convert HTML entities', function() {
71-
const pc = createPartCreator();
72-
const model = new EditorModel([pc.plain('\\*hello\\* world < hey world!')], pc);
73-
const html = htmlSerializeIfNeeded(model, {});
74-
expect(html).toBe('*hello* world &lt; hey world!');
78+
describe('with plaintext', function() {
79+
it('markdown remains plaintext', function() {
80+
const pc = createPartCreator();
81+
const model = new EditorModel([pc.plain("*hello* world")], pc);
82+
const html = htmlSerializeIfNeeded(model, { useMarkdown: false });
83+
expect(html).toBe("*hello* world");
84+
});
85+
it('markdown should retain backslashes', function() {
86+
const pc = createPartCreator();
87+
const model = new EditorModel([pc.plain('\\*hello\\* world')], pc);
88+
const html = htmlSerializeIfNeeded(model, { useMarkdown: false });
89+
expect(html).toBe('\\*hello\\* world');
90+
});
91+
it('markdown should convert HTML entities', function() {
92+
const pc = createPartCreator();
93+
const model = new EditorModel([pc.plain('\\*hello\\* world < hey world!')], pc);
94+
const html = htmlSerializeIfNeeded(model, { useMarkdown: false });
95+
expect(html).toBe('\\*hello\\* world &lt; hey world!');
96+
});
7597
});
7698
});

0 commit comments

Comments
 (0)