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

This commit is contained in:
Jarek Radosz 2022-08-21 09:20:26 +02:00 committed by GitHub
parent 870ff48470
commit 545bfcaca7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 26 deletions

View File

@ -22,6 +22,7 @@ import selectKit from "discourse/tests/helpers/select-kit-helper";
import I18n from "I18n"; import I18n from "I18n";
import { test } from "qunit"; import { test } from "qunit";
import sinon from "sinon"; import sinon from "sinon";
import pretender, { response } from "discourse/tests/helpers/create-pretender";
acceptance("Composer", function (needs) { acceptance("Composer", function (needs) {
needs.user({ needs.user({
@ -400,34 +401,27 @@ acceptance("Composer", function (needs) {
test("Editing a post stages new content", async function (assert) { test("Editing a post stages new content", async function (assert) {
await visit("/t/internationalization-localization/280"); await visit("/t/internationalization-localization/280");
await click(".topic-post:nth-of-type(1) button.show-more-actions"); await click(".topic-post button.show-more-actions");
await click(".topic-post:nth-of-type(1) button.edit"); await click(".topic-post button.edit");
await fillIn(".d-editor-input", "will return empty json"); await fillIn(".d-editor-input", "will return empty json");
await fillIn("#reply-title", "This is the new text for the title"); await fillIn("#reply-title", "This is the new text for the title");
// when this promise resolves, the request had already started because pretender.put("/posts/:post_id", async () => {
// this promise will be resolved by the pretender // at this point, request is in flight, so post is staged
const promise = new Promise((resolve) => { assert.strictEqual(count(".topic-post.staged"), 1);
window.resolveLastPromise = resolve; assert.ok(query(".topic-post").classList.contains("staged"));
assert.strictEqual(
query(".topic-post.staged .cooked").innerText.trim(),
"will return empty json"
);
return response(200, {});
}); });
// click to trigger the save, but wait until the request starts await click("#reply-control button.create");
click("#reply-control button.create");
await promise;
// 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"
);
// finally, finish request and wait for last render
window.resolveLastPromise();
await visit("/t/internationalization-localization/280"); await visit("/t/internationalization-localization/280");
assert.strictEqual(count(".topic-post.staged"), 0); assert.strictEqual(count(".topic-post.staged"), 0);
}); });

View File

@ -1,7 +1,6 @@
import Pretender from "pretender"; import Pretender from "pretender";
import User from "discourse/models/user"; import User from "discourse/models/user";
import getURL from "discourse-common/lib/get-url"; import getURL from "discourse-common/lib/get-url";
import { Promise } from "rsvp";
export function parsePostData(query) { export function parsePostData(query) {
const result = {}; const result = {};
@ -506,14 +505,11 @@ export function applyDefaultHandlers(pretender) {
pretender.put("/posts/:post_id", async (request) => { pretender.put("/posts/:post_id", async (request) => {
const data = parsePostData(request.requestBody); const data = parsePostData(request.requestBody);
if (data.post.raw === "this will 409") { if (data.post.raw === "this will 409") {
return response(409, { errors: ["edit conflict"] }); 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.id = request.params.post_id;
data.post.version = 2; data.post.version = 2;
return response(200, data.post); return response(200, data.post);