DEV: Extract build-quote tests (#21217)

They are not related to pretty-text.
This commit is contained in:
Jarek Radosz 2023-04-24 11:19:28 +02:00 committed by GitHub
parent b0bc37fd96
commit 9e2a490f79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 100 additions and 86 deletions

View File

@ -0,0 +1,82 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import { getOwner } from "discourse-common/lib/get-owner";
import { buildQuote } from "discourse/lib/quote";
import PrettyText from "pretty-text/pretty-text";
module("Unit | Utility | build-quote", function (hooks) {
setupTest(hooks);
test("quotes", function (assert) {
const store = getOwner(this).lookup("service:store");
const post = store.createRecord("post", {
cooked: "<p><b>lorem</b> ipsum</p>",
username: "eviltrout",
post_number: 1,
topic_id: 2,
});
assert.strictEqual(
buildQuote(post, undefined),
"",
"empty string for undefined content"
);
assert.strictEqual(
buildQuote(post, null),
"",
"empty string for null content"
);
assert.strictEqual(
buildQuote(post, ""),
"",
"empty string for empty string content"
);
assert.strictEqual(
buildQuote(post, "lorem"),
'[quote="eviltrout, post:1, topic:2"]\nlorem\n[/quote]\n\n',
"correctly formats quotes"
);
assert.strictEqual(
buildQuote(post, " lorem \t "),
'[quote="eviltrout, post:1, topic:2"]\nlorem\n[/quote]\n\n',
"trims white spaces before & after the quoted contents"
);
assert.strictEqual(
buildQuote(post, "lorem ipsum", { full: true }),
'[quote="eviltrout, post:1, topic:2, full:true"]\nlorem ipsum\n[/quote]\n\n',
"marks quotes as full if the `full` option is passed"
);
assert.strictEqual(
buildQuote(post, "**lorem** ipsum"),
'[quote="eviltrout, post:1, topic:2"]\n**lorem** ipsum\n[/quote]\n\n',
"keeps BBCode formatting"
);
});
test("quoting a quote", function (assert) {
const store = getOwner(this).lookup("service:store");
const post = store.createRecord("post", {
cooked: new PrettyText().cook(
'[quote="sam, post:1, topic:1, full:true"]\nhello\n[/quote]\n*Test*'
),
username: "eviltrout",
post_number: 1,
topic_id: 2,
});
const quote = buildQuote(
post,
'[quote="sam, post:1, topic:1, full:true"]\nhello\n[/quote]'
);
assert.strictEqual(
quote,
'[quote="eviltrout, post:1, topic:2"]\n[quote="sam, post:1, topic:1, full:true"]\nhello\n[/quote]\n[/quote]\n\n',
"allows quoting a quote"
);
});
});

View File

@ -4,12 +4,10 @@ import {
deleteCachedInlineOnebox,
} from "pretty-text/inline-oneboxer";
import QUnit, { module, test } from "qunit";
import { buildQuote } from "discourse/lib/quote";
import { deepMerge } from "discourse-common/lib/object";
import { extractDataAttribute } from "pretty-text/engines/discourse-markdown-it";
import { registerEmoji } from "pretty-text/emoji";
import { IMAGE_VERSION as v } from "pretty-text/emoji/version";
import { getOwner } from "discourse-common/lib/get-owner";
const rawOpts = {
siteSettings: {
@ -440,6 +438,24 @@ eviltrout</p>
</aside>`,
"quote has group class"
);
assert.cooked(
"[quote]\ntest\n[/quote]",
'<aside class="quote no-group">\n<blockquote>\n<p>test</p>\n</blockquote>\n</aside>',
"it supports quotes without params"
);
assert.cooked(
"[quote]\n*test*\n[/quote]",
'<aside class="quote no-group">\n<blockquote>\n<p><em>test</em></p>\n</blockquote>\n</aside>',
"it doesn't insert a new line for italics"
);
assert.cooked(
"[quote=,script='a'><script>alert('test');//':a]\n[/quote]",
'<aside class="quote no-group">\n<blockquote></blockquote>\n</aside>',
"It will not create a script tag within an attribute"
);
});
test("Incomplete quotes", function (assert) {
@ -1273,90 +1289,6 @@ eviltrout</p>
);
});
test("quotes", function (assert) {
const store = getOwner(this).lookup("service:store");
const post = store.createRecord("post", {
cooked: "<p><b>lorem</b> ipsum</p>",
username: "eviltrout",
post_number: 1,
topic_id: 2,
});
function formatQuote(val, expected, text, opts) {
assert.strictEqual(buildQuote(post, val, opts), expected, text);
}
formatQuote(undefined, "", "empty string for undefined content");
formatQuote(null, "", "empty string for null content");
formatQuote("", "", "empty string for empty string content");
formatQuote(
"lorem",
'[quote="eviltrout, post:1, topic:2"]\nlorem\n[/quote]\n\n',
"correctly formats quotes"
);
formatQuote(
" lorem \t ",
'[quote="eviltrout, post:1, topic:2"]\nlorem\n[/quote]\n\n',
"trims white spaces before & after the quoted contents"
);
formatQuote(
"lorem ipsum",
'[quote="eviltrout, post:1, topic:2, full:true"]\nlorem ipsum\n[/quote]\n\n',
"marks quotes as full if the `full` option is passed",
{ full: true }
);
formatQuote(
"**lorem** ipsum",
'[quote="eviltrout, post:1, topic:2"]\n**lorem** ipsum\n[/quote]\n\n',
"keeps BBCode formatting"
);
assert.cooked(
"[quote]\ntest\n[/quote]",
'<aside class="quote no-group">\n<blockquote>\n<p>test</p>\n</blockquote>\n</aside>',
"it supports quotes without params"
);
assert.cooked(
"[quote]\n*test*\n[/quote]",
'<aside class="quote no-group">\n<blockquote>\n<p><em>test</em></p>\n</blockquote>\n</aside>',
"it doesn't insert a new line for italics"
);
assert.cooked(
"[quote=,script='a'><script>alert('test');//':a]\n[/quote]",
'<aside class="quote no-group">\n<blockquote></blockquote>\n</aside>',
"It will not create a script tag within an attribute"
);
});
test("quoting a quote", function (assert) {
const store = getOwner(this).lookup("service:store");
const post = store.createRecord("post", {
cooked: new PrettyText(defaultOpts).cook(
'[quote="sam, post:1, topic:1, full:true"]\nhello\n[/quote]\n*Test*'
),
username: "eviltrout",
post_number: 1,
topic_id: 2,
});
const quote = buildQuote(
post,
'[quote="sam, post:1, topic:1, full:true"]\nhello\n[/quote]'
);
assert.strictEqual(
quote,
'[quote="eviltrout, post:1, topic:2"]\n[quote="sam, post:1, topic:1, full:true"]\nhello\n[/quote]\n[/quote]\n\n',
"allows quoting a quote"
);
});
test("quote formatting", function (assert) {
assert.cooked(
'[quote="EvilTrout, post:123, topic:456, full:true"]\n[sam]\n[/quote]',