DEV: Use async and sinon.resolves instead of rsvp (#18001)

…in tests
This commit is contained in:
Jarek Radosz 2022-08-20 11:20:55 +02:00 committed by GitHub
parent e3108ded11
commit dc5dc78309
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 38 additions and 65 deletions

View File

@ -9,7 +9,6 @@ import {
import { click, fillIn, visit } from "@ember/test-helpers"; import { click, fillIn, visit } from "@ember/test-helpers";
import Draft from "discourse/models/draft"; import Draft from "discourse/models/draft";
import I18n from "I18n"; import I18n from "I18n";
import { Promise } from "rsvp";
import selectKit from "discourse/tests/helpers/select-kit-helper"; import selectKit from "discourse/tests/helpers/select-kit-helper";
import sinon from "sinon"; import sinon from "sinon";
import { test } from "qunit"; import { test } from "qunit";
@ -117,9 +116,8 @@ acceptance("Composer Actions", function (needs) {
}); });
test("replying to post - reply_as_new_topic", async function (assert) { test("replying to post - reply_as_new_topic", async function (assert) {
sinon sinon.stub(Draft, "get").resolves({ draft: "", draft_sequence: 0 });
.stub(Draft, "get")
.returns(Promise.resolve({ draft: "", draft_sequence: 0 }));
const composerActions = selectKit(".composer-actions"); const composerActions = selectKit(".composer-actions");
const categoryChooser = selectKit(".title-wrapper .category-chooser"); const categoryChooser = selectKit(".title-wrapper .category-chooser");
const categoryChooserReplyArea = selectKit(".reply-area .category-chooser"); const categoryChooserReplyArea = selectKit(".reply-area .category-chooser");
@ -405,13 +403,11 @@ acceptance("Composer Actions", function (needs) {
}); });
function stubDraftResponse() { function stubDraftResponse() {
sinon.stub(Draft, "get").returns( sinon.stub(Draft, "get").resolves({
Promise.resolve({
draft: draft:
'{"reply":"dum de dum da ba.","action":"createTopic","title":"dum da ba dum dum","categoryId":null,"archetypeId":"regular","metaData":null,"composerTime":540879,"typingTime":3400}', '{"reply":"dum de dum da ba.","action":"createTopic","title":"dum da ba dum dum","categoryId":null,"archetypeId":"regular","metaData":null,"composerTime":540879,"typingTime":3400}',
draft_sequence: 0, draft_sequence: 0,
}) });
);
} }
acceptance("Composer Actions With New Topic Draft", function (needs) { acceptance("Composer Actions With New Topic Draft", function (needs) {

View File

@ -21,7 +21,6 @@ import {
import selectKit from "discourse/tests/helpers/select-kit-helper"; 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 { Promise } from "rsvp";
import sinon from "sinon"; import sinon from "sinon";
acceptance("Composer", function (needs) { acceptance("Composer", function (needs) {
@ -774,12 +773,10 @@ acceptance("Composer", function (needs) {
const longText = "a".repeat(256); const longText = "a".repeat(256);
sinon.stub(Draft, "get").returns( sinon.stub(Draft, "get").resolves({
Promise.resolve({
draft: null, draft: null,
draft_sequence: 0, draft_sequence: 0,
}) });
);
await click(".btn-primary.create.btn"); await click(".btn-primary.create.btn");
@ -816,13 +813,11 @@ acceptance("Composer", function (needs) {
test("Loading draft also replaces the recipients", async function (assert) { test("Loading draft also replaces the recipients", async function (assert) {
toggleCheckDraftPopup(true); toggleCheckDraftPopup(true);
sinon.stub(Draft, "get").returns( sinon.stub(Draft, "get").resolves({
Promise.resolve({
draft: draft:
'{"reply":"hello","action":"privateMessage","title":"hello","categoryId":null,"archetypeId":"private_message","metaData":null,"recipients":"codinghorror","composerTime":9159,"typingTime":2500}', '{"reply":"hello","action":"privateMessage","title":"hello","categoryId":null,"archetypeId":"private_message","metaData":null,"recipients":"codinghorror","composerTime":9159,"typingTime":2500}',
draft_sequence: 0, draft_sequence: 0,
}) });
);
await visit("/u/charlie"); await visit("/u/charlie");
await click("button.compose-pm"); await click("button.compose-pm");
@ -835,14 +830,12 @@ acceptance("Composer", function (needs) {
test("Loads tags and category from draft payload", async function (assert) { test("Loads tags and category from draft payload", async function (assert) {
updateCurrentUser({ has_topic_draft: true }); updateCurrentUser({ has_topic_draft: true });
sinon.stub(Draft, "get").returns( sinon.stub(Draft, "get").resolves({
Promise.resolve({
draft: draft:
'{"reply":"Hey there","action":"createTopic","title":"Draft topic","categoryId":2,"tags":["fun", "times"],"archetypeId":"regular","metaData":null,"composerTime":25269,"typingTime":8100}', '{"reply":"Hey there","action":"createTopic","title":"Draft topic","categoryId":2,"tags":["fun", "times"],"archetypeId":"regular","metaData":null,"composerTime":25269,"typingTime":8100}',
draft_sequence: 0, draft_sequence: 0,
draft_key: NEW_TOPIC_KEY, draft_key: NEW_TOPIC_KEY,
}) });
);
await visit("/latest"); await visit("/latest");
assert.strictEqual( assert.strictEqual(

View File

@ -66,7 +66,6 @@ module(
assert.ok(exists("li.unread")); assert.ok(exists("li.unread"));
this.item.notification.read = true; this.item.notification.read = true;
// await pauseTest();
await settled(); await settled();
assert.ok( assert.ok(

View File

@ -1,6 +1,5 @@
import EmberObject from "@ember/object"; import EmberObject from "@ember/object";
import { Placeholder } from "discourse/lib/posts-with-placeholders"; import { Placeholder } from "discourse/lib/posts-with-placeholders";
import { Promise } from "rsvp";
import Topic from "discourse/models/topic"; import Topic from "discourse/models/topic";
import User from "discourse/models/user"; import User from "discourse/models/user";
import { discourseModule } from "discourse/tests/helpers/qunit-helpers"; import { discourseModule } from "discourse/tests/helpers/qunit-helpers";
@ -70,9 +69,8 @@ discourseModule("Unit | Controller | topic", function (hooks) {
const model = Topic.create(); const model = Topic.create();
let destroyed = false; let destroyed = false;
let modalDisplayed = false; let modalDisplayed = false;
model.destroy = () => { model.destroy = async () => {
destroyed = true; destroyed = true;
return Promise.resolve();
}; };
const controller = this.getController("topic", { const controller = this.getController("topic", {
model, model,
@ -672,9 +670,8 @@ discourseModule("Unit | Controller | topic", function (hooks) {
post_number: 2, post_number: 2,
can_delete: true, can_delete: true,
reply_count: 3, reply_count: 3,
destroy: () => { destroy: async () => {
destroyed = true; destroyed = true;
return Promise.resolve();
}, },
}); });

View File

@ -36,7 +36,7 @@ module("Unit | Utility | preload-store", function (hooks) {
}); });
test("getAndRemove returns a promise that resolves to the result of the finder's promise", async function (assert) { test("getAndRemove returns a promise that resolves to the result of the finder's promise", async function (assert) {
const finder = () => Promise.resolve("hahahah"); const finder = async () => "hahahah";
const result = await PreloadStore.getAndRemove("joker", finder); const result = await PreloadStore.getAndRemove("joker", finder);
assert.strictEqual(result, "hahahah"); assert.strictEqual(result, "hahahah");

View File

@ -1,6 +1,5 @@
import UppyMediaOptimization from "discourse/lib/uppy-media-optimization-plugin"; import UppyMediaOptimization from "discourse/lib/uppy-media-optimization-plugin";
import { module, test } from "qunit"; import { module, test } from "qunit";
import { Promise } from "rsvp";
class FakeUppy { class FakeUppy {
constructor() { constructor() {
@ -75,9 +74,7 @@ module("Unit | Utility | UppyMediaOptimization Plugin", function () {
const fakeUppy = new FakeUppy(); const fakeUppy = new FakeUppy();
const plugin = new UppyMediaOptimization(fakeUppy, { const plugin = new UppyMediaOptimization(fakeUppy, {
runParallel: true, runParallel: true,
optimizeFn: () => { optimizeFn: async () => "new file state",
return Promise.resolve("new file state");
},
}); });
plugin.install(); plugin.install();
const done = assert.async(); const done = assert.async();
@ -96,10 +93,8 @@ module("Unit | Utility | UppyMediaOptimization Plugin", function () {
const fakeUppy = new FakeUppy(); const fakeUppy = new FakeUppy();
const plugin = new UppyMediaOptimization(fakeUppy, { const plugin = new UppyMediaOptimization(fakeUppy, {
runParallel: true, runParallel: true,
optimizeFn: () => { optimizeFn: async () => {
return new Promise(() => {
throw new Error("bad stuff"); throw new Error("bad stuff");
});
}, },
}); });
plugin.install(); plugin.install();
@ -119,9 +114,7 @@ module("Unit | Utility | UppyMediaOptimization Plugin", function () {
const fakeUppy = new FakeUppy(); const fakeUppy = new FakeUppy();
const plugin = new UppyMediaOptimization(fakeUppy, { const plugin = new UppyMediaOptimization(fakeUppy, {
runParallel: false, runParallel: false,
optimizeFn: () => { optimizeFn: async () => "new file state",
return Promise.resolve("new file state");
},
}); });
plugin.install(); plugin.install();
const done = assert.async(); const done = assert.async();
@ -151,9 +144,7 @@ module("Unit | Utility | UppyMediaOptimization Plugin", function () {
const fakeUppy = new FakeUppy(); const fakeUppy = new FakeUppy();
const plugin = new UppyMediaOptimization(fakeUppy, { const plugin = new UppyMediaOptimization(fakeUppy, {
runParallel: true, runParallel: true,
optimizeFn: () => { optimizeFn: async () => "new file state",
return Promise.resolve("new file state");
},
}); });
plugin.install(); plugin.install();
const done = assert.async(); const done = assert.async();

View File

@ -2,7 +2,6 @@ import { module, test } from "qunit";
import AppEvents from "discourse/services/app-events"; import AppEvents from "discourse/services/app-events";
import ArrayProxy from "@ember/array/proxy"; import ArrayProxy from "@ember/array/proxy";
import Post from "discourse/models/post"; import Post from "discourse/models/post";
import { Promise } from "rsvp";
import User from "discourse/models/user"; import User from "discourse/models/user";
import createStore from "discourse/tests/helpers/create-store"; import createStore from "discourse/tests/helpers/create-store";
import pretender, { response } from "discourse/tests/helpers/create-pretender"; import pretender, { response } from "discourse/tests/helpers/create-pretender";
@ -232,7 +231,7 @@ module("Unit | Model | post-stream", function () {
test("cancelFilter", function (assert) { test("cancelFilter", function (assert) {
const postStream = buildStream(1235); const postStream = buildStream(1235);
sinon.stub(postStream, "refresh").returns(Promise.resolve()); sinon.stub(postStream, "refresh").resolves();
postStream.set("filter", "summary"); postStream.set("filter", "summary");
postStream.cancelFilter(); postStream.cancelFilter();
@ -274,7 +273,7 @@ module("Unit | Model | post-stream", function () {
test("fillGapBefore", function (assert) { test("fillGapBefore", function (assert) {
const postStream = buildStream(1234, [60]); const postStream = buildStream(1234, [60]);
sinon.stub(postStream, "findPostsByIds").returns(Promise.resolve([])); sinon.stub(postStream, "findPostsByIds").resolves([]);
let post = postStream.store.createRecord("post", { let post = postStream.store.createRecord("post", {
id: 60, id: 60,
post_number: 60, post_number: 60,
@ -294,7 +293,7 @@ module("Unit | Model | post-stream", function () {
test("filterParticipant", function (assert) { test("filterParticipant", function (assert) {
const postStream = buildStream(1236); const postStream = buildStream(1236);
sinon.stub(postStream, "refresh").returns(Promise.resolve()); sinon.stub(postStream, "refresh").resolves();
assert.strictEqual( assert.strictEqual(
postStream.get("userFilters.length"), postStream.get("userFilters.length"),
@ -320,7 +319,7 @@ module("Unit | Model | post-stream", function () {
store.createRecord("post", { id: 2, post_number: 3 }) store.createRecord("post", { id: 2, post_number: 3 })
); );
sinon.stub(postStream, "refresh").returns(Promise.resolve()); sinon.stub(postStream, "refresh").resolves();
assert.strictEqual( assert.strictEqual(
postStream.get("filterRepliesToPostNumber"), postStream.get("filterRepliesToPostNumber"),
@ -351,7 +350,7 @@ module("Unit | Model | post-stream", function () {
store.createRecord("post", { id: 2, post_number: 3 }) store.createRecord("post", { id: 2, post_number: 3 })
); );
sinon.stub(postStream, "refresh").returns(Promise.resolve()); sinon.stub(postStream, "refresh").resolves();
assert.strictEqual( assert.strictEqual(
postStream.get("filterUpwardsPostID"), postStream.get("filterUpwardsPostID"),
@ -376,7 +375,7 @@ module("Unit | Model | post-stream", function () {
test("streamFilters", function (assert) { test("streamFilters", function (assert) {
const postStream = buildStream(1237); const postStream = buildStream(1237);
sinon.stub(postStream, "refresh").returns(Promise.resolve()); sinon.stub(postStream, "refresh").resolves();
assert.deepEqual( assert.deepEqual(
postStream.get("streamFilters"), postStream.get("streamFilters"),
@ -960,9 +959,7 @@ module("Unit | Model | post-stream", function () {
username: "ignoreduser", username: "ignoreduser",
}); });
let stub = sinon let stub = sinon.stub(postStream, "findPostsByIds").resolves([post2]);
.stub(postStream, "findPostsByIds")
.returns(Promise.resolve([post2]));
await postStream.triggerNewPostsInStream([101]); await postStream.triggerNewPostsInStream([101]);
assert.strictEqual( assert.strictEqual(
@ -977,7 +974,7 @@ module("Unit | Model | post-stream", function () {
); );
stub.restore(); stub.restore();
sinon.stub(postStream, "findPostsByIds").returns(Promise.resolve([post3])); sinon.stub(postStream, "findPostsByIds").resolves([post3]);
await postStream.triggerNewPostsInStream([102]); await postStream.triggerNewPostsInStream([102]);
assert.strictEqual( assert.strictEqual(