From 9e2a490f7982cbc5d1374b290713c9f9818e718a Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Mon, 24 Apr 2023 11:19:28 +0200 Subject: [PATCH] DEV: Extract build-quote tests (#21217) They are not related to pretty-text. --- .../tests/unit/lib/build-quote-test.js | 82 ++++++++++++++ .../tests/unit/lib/pretty-text-test.js | 104 +++--------------- 2 files changed, 100 insertions(+), 86 deletions(-) create mode 100644 app/assets/javascripts/discourse/tests/unit/lib/build-quote-test.js diff --git a/app/assets/javascripts/discourse/tests/unit/lib/build-quote-test.js b/app/assets/javascripts/discourse/tests/unit/lib/build-quote-test.js new file mode 100644 index 00000000000..fc742da9cdd --- /dev/null +++ b/app/assets/javascripts/discourse/tests/unit/lib/build-quote-test.js @@ -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: "

lorem ipsum

", + 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" + ); + }); +}); diff --git a/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js b/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js index 085a971f700..5886c320874 100644 --- a/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js +++ b/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js @@ -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

`, "quote has group class" ); + + assert.cooked( + "[quote]\ntest\n[/quote]", + '', + "it supports quotes without params" + ); + + assert.cooked( + "[quote]\n*test*\n[/quote]", + '', + "it doesn't insert a new line for italics" + ); + + assert.cooked( + "[quote=,script='a'>