Skip to content

A platform for community discussion. Free, open, simple.

DEV: Simplify "Editing a post stages new content" test (#18007)

Jarek Radosz545bfcaca7a1
diff --git a/app/assets/javascripts/discourse/tests/acceptance/composer-test.js b/app/assets/javascripts/discourse/tests/acceptance/composer-test.js
index d454739d8cb..7659597d4da 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/composer-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/composer-test.js
@@ -22,6 +22,7 @@ import selectKit from "discourse/tests/helpers/select-kit-helper";
import I18n from "I18n";
import { test } from "qunit";
import sinon from "sinon";
+import pretender, { response } from "discourse/tests/helpers/create-pretender";
acceptance("Composer", function (needs) {
needs.user({
@@ -400,34 +401,27 @@ acceptance("Composer", function (needs) {
test("Editing a post stages new content", async function (assert) {
await visit("/t/internationalization-localization/280");
- await click(".topic-post:nth-of-type(1) button.show-more-actions");
- await click(".topic-post:nth-of-type(1) button.edit");
+ await click(".topic-post button.show-more-actions");
+ await click(".topic-post button.edit");
await fillIn(".d-editor-input", "will return empty json");
await fillIn("#reply-title", "This is the new text for the title");
- // when this promise resolves, the request had already started because
- // this promise will be resolved by the pretender
- const promise = new Promise((resolve) => {
- window.resolveLastPromise = resolve;
- });
+ pretender.put("/posts/:post_id", async () => {
+ // at this point, request is in flight, so post is staged
+ assert.strictEqual(count(".topic-post.staged"), 1);
+ assert.ok(query(".topic-post").classList.contains("staged"));
+ assert.strictEqual(
+ query(".topic-post.staged .cooked").innerText.trim(),
+ "will return empty json"
+ );
- // click to trigger the save, but wait until the request starts
- click("#reply-control button.create");
- await promise;
+ return response(200, {});
+ });
- // at this point, request is in flight, so post is staged
- assert.strictEqual(count(".topic-post.staged"), 1);
- assert.ok(query(".topic-post:nth-of-type(1)").className.includes("staged"));
- assert.strictEqual(
- query(".topic-post.staged .cooked").innerText.trim(),
- "will return empty json"
- );
+ await click("#reply-control button.create");
- // finally, finish request and wait for last render
- window.resolveLastPromise();
await visit("/t/internationalization-localization/280");
-
assert.strictEqual(count(".topic-post.staged"), 0);
});
diff --git a/app/assets/javascripts/discourse/tests/helpers/create-pretender.js b/app/assets/javascripts/discourse/tests/helpers/create-pretender.js
index 40317640ee5..608a718b403 100644
--- a/app/assets/javascripts/discourse/tests/helpers/create-pretender.js
+++ b/app/assets/javascripts/discourse/tests/helpers/create-pretender.js
@@ -1,7 +1,6 @@
import Pretender from "pretender";
import User from "discourse/models/user";
import getURL from "discourse-common/lib/get-url";
-import { Promise } from "rsvp";
export function parsePostData(query) {
const result = {};
@@ -506,14 +505,11 @@ export function applyDefaultHandlers(pretender) {
pretender.put("/posts/:post_id", async (request) => {
const data = parsePostData(request.requestBody);
+
if (data.post.raw === "this will 409") {
return response(409, { errors: ["edit conflict"] });
- } else if (data.post.raw === "will return empty json") {
- window.resolveLastPromise();
- return new Promise((resolve) => {
- window.resolveLastPromise = resolve;
- }).then(() => response(200, {}));
}
+
data.post.id = request.params.post_id;
data.post.version = 2;
return response(200, data.post);