DEV: Consistently call setupTest(hooks) in unit tests (#23610)

This commit is contained in:
Jarek Radosz 2023-09-25 12:43:41 +02:00 committed by GitHub
parent 92839dc722
commit 837cec6c3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
38 changed files with 165 additions and 47 deletions

View File

@ -1,5 +1,6 @@
import { buildResolver, setResolverOption } from "discourse-common/resolver";
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import { buildResolver, setResolverOption } from "discourse-common/resolver";
import { registerTemporaryModule } from "discourse/tests/helpers/temporary-module-helper";
import DiscourseTemplateMap from "discourse-common/lib/discourse-template-map";
@ -20,6 +21,8 @@ function setTemplates(templateModuleNames) {
const DiscourseResolver = buildResolver("discourse");
module("Unit | Ember | resolver", function (hooks) {
setupTest(hooks);
hooks.beforeEach(function () {
DiscourseTemplateMap.setModuleNames(Object.keys(requirejs.entries));
resolver = DiscourseResolver.create({

View File

@ -1,7 +1,10 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import AllowLister from "pretty-text/allow-lister";
module("Unit | Utility | allowLister", function () {
module("Unit | Utility | allowLister", function (hooks) {
setupTest(hooks);
test("allowLister", function (assert) {
const allowLister = new AllowLister();

View File

@ -1,8 +1,10 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import autocomplete from "discourse/lib/autocomplete";
import { compile } from "handlebars";
module("Unit | Utility | autocomplete", function (hooks) {
setupTest(hooks);
let elements = [];
function textArea(value) {

View File

@ -1,8 +1,11 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import { fakeTime } from "discourse/tests/helpers/qunit-helpers";
import { formattedReminderTime } from "discourse/lib/bookmark";
module("Unit | Utility | bookmark", function (hooks) {
setupTest(hooks);
hooks.beforeEach(function () {
this.clock = fakeTime("2020-04-11 08:00:00", "Australia/Brisbane");
});

View File

@ -1,4 +1,5 @@
import { fixture, logIn } from "discourse/tests/helpers/qunit-helpers";
import { setupTest } from "ember-qunit";
import { module, skip, test } from "qunit";
import ClickTrack from "discourse/lib/click-track";
import DiscourseURL from "discourse/lib/url";
@ -14,6 +15,8 @@ function generateClickEventOn(selector) {
}
module("Unit | Utility | click-track", function (hooks) {
setupTest(hooks);
hooks.beforeEach(function () {
logIn();

View File

@ -1,7 +1,10 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import Columns from "discourse/lib/columns";
module("Unit | Columns", function (hooks) {
setupTest(hooks);
hooks.afterEach(function () {
document.getElementById("qunit-fixture").innerHTML = "";
});

View File

@ -1,3 +1,5 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import deprecated, {
withSilencedDeprecations,
withSilencedDeprecationsAsync,
@ -7,10 +9,11 @@ import {
enableRaiseOnDeprecation,
} from "discourse/tests/helpers/raise-on-deprecation";
import DeprecationCounter from "discourse/tests/helpers/deprecation-counter";
import { module, test } from "qunit";
import Sinon from "sinon";
module("Unit | Utility | deprecated", function (hooks) {
setupTest(hooks);
hooks.beforeEach(function () {
disableRaiseOnDeprecation();
this.warnStub = Sinon.stub(console, "warn");
@ -20,7 +23,7 @@ module("Unit | Utility | deprecated", function (hooks) {
);
});
hooks.afterEach(() => {
hooks.afterEach(function () {
enableRaiseOnDeprecation();
});

View File

@ -1,4 +1,5 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import {
downloadGoogle,
formatDates,
@ -7,6 +8,8 @@ import {
import sinon from "sinon";
module("Unit | Utility | download-calendar", function (hooks) {
setupTest(hooks);
hooks.beforeEach(function () {
let win = { focus: function () {} };
sinon.stub(window, "open").returns(win);

View File

@ -1,3 +1,5 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import getURL, {
getAbsoluteURL,
getURLWithCDN,
@ -7,9 +9,10 @@ import getURL, {
setupURL,
withoutPrefix,
} from "discourse-common/lib/get-url";
import { module, test } from "qunit";
module("Unit | Utility | get-url", function () {
module("Unit | Utility | get-url", function (hooks) {
setupTest(hooks);
test("isAbsoluteURL", function (assert) {
setupURL(null, "https://example.com", "/forum");
assert.ok(isAbsoluteURL("https://example.com/test/thing"));

View File

@ -1,8 +1,11 @@
import highlightSearch, { CLASS_NAME } from "discourse/lib/highlight-search";
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import highlightSearch, { CLASS_NAME } from "discourse/lib/highlight-search";
import { fixture } from "discourse/tests/helpers/qunit-helpers";
module("Unit | Utility | highlight-search", function () {
module("Unit | Utility | highlight-search", function (hooks) {
setupTest(hooks);
test("highlighting text", function (assert) {
fixture().innerHTML = `
<p>This is some text to highlight</p>

View File

@ -1,5 +1,6 @@
import highlightSyntax from "discourse/lib/highlight-syntax";
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import highlightSyntax from "discourse/lib/highlight-syntax";
import { fixture } from "discourse/tests/helpers/qunit-helpers";
let siteSettings = { autohighlight_all_code: true },
@ -7,7 +8,9 @@ let siteSettings = { autohighlight_all_code: true },
highlightJsPath: "/assets/highlightjs/highlight-test-bundle.min.js",
};
module("Unit | Utility | highlight-syntax", function () {
module("Unit | Utility | highlight-syntax", function (hooks) {
setupTest(hooks);
test("highlighting code", async function (assert) {
fixture().innerHTML = `
<pre>

View File

@ -1,8 +1,11 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import I18n from "I18n";
import { withSilencedDeprecations } from "discourse-common/lib/deprecated";
module("Unit | Utility | i18n", function (hooks) {
setupTest(hooks);
hooks.beforeEach(function () {
this._locale = I18n.locale;
this._fallbackLocale = I18n.fallbackLocale;

View File

@ -1,11 +1,14 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import {
convertIconClass,
iconHTML,
iconNode,
} from "discourse-common/lib/icon-library";
import { module, test } from "qunit";
module("Unit | Utility | icon-library", function () {
module("Unit | Utility | icon-library", function (hooks) {
setupTest(hooks);
test("return icon markup", function (assert) {
assert.ok(iconHTML("bars").includes('use href="#bars"'));

View File

@ -1,7 +1,10 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import KeyValueStore from "discourse/lib/key-value-store";
module("Unit | Utility | key-value-store", function () {
module("Unit | Utility | key-value-store", function (hooks) {
setupTest(hooks);
test("is able to get the result back from the store", function (assert) {
const store = new KeyValueStore("example");
store.set({ key: "bob", value: "uncle" });

View File

@ -1,9 +1,12 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import DiscourseURL from "discourse/lib/url";
import KeyboardShortcuts from "discourse/lib/keyboard-shortcuts";
import sinon from "sinon";
module("Unit | Utility | keyboard-shortcuts", function (hooks) {
setupTest(hooks);
hooks.beforeEach(function () {
sinon.stub(DiscourseURL, "routeTo");
});

View File

@ -1,15 +1,17 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import {
LIGHTBOX_IMAGE_FIXTURES,
generateImageUploaderMarkup,
generateLightboxMarkup,
} from "discourse/tests/helpers/lightbox-helpers";
import { module, test } from "qunit";
import { SELECTORS } from "discourse/lib/lightbox/constants";
import domFromString from "discourse-common/lib/dom-from-string";
import { processHTML } from "discourse/lib/lightbox/process-html";
module("Unit | lib | Experimental lightbox | processHTML()", function () {
module("Unit | lib | Experimental lightbox | processHTML()", function (hooks) {
setupTest(hooks);
const wrap = domFromString(generateLightboxMarkup())[0];
const imageUploaderWrap = domFromString(generateImageUploaderMarkup())[0];
const selector = SELECTORS.DEFAULT_ITEM_SELECTOR;

View File

@ -1,8 +1,11 @@
import LinkLookup from "discourse/lib/link-lookup";
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import LinkLookup from "discourse/lib/link-lookup";
import { getOwner } from "discourse-common/lib/get-owner";
module("Unit | Utility | link-lookup", function (hooks) {
setupTest(hooks);
hooks.beforeEach(function () {
const store = getOwner(this).lookup("service:store");
this.post = store.createRecord("post");

View File

@ -1,12 +1,15 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import {
fetchUnseenMentions,
linkSeenMentions,
} from "discourse/lib/link-mentions";
import { module, test } from "qunit";
import pretender, { response } from "discourse/tests/helpers/create-pretender";
import domFromString from "discourse-common/lib/dom-from-string";
module("Unit | Utility | link-mentions", function () {
module("Unit | Utility | link-mentions", function (hooks) {
setupTest(hooks);
test("linkSeenMentions replaces users and groups", async function (assert) {
pretender.get("/composer/mentions", () =>
response({

View File

@ -1,8 +1,11 @@
import { cacheBuster } from "discourse/lib/load-script";
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import { cacheBuster } from "discourse/lib/load-script";
import { PUBLIC_JS_VERSIONS as jsVersions } from "discourse/lib/public-js-versions";
module("Unit | Utility | load-script", function () {
module("Unit | Utility | load-script", function (hooks) {
setupTest(hooks);
test("works when a value is not present", function (assert) {
assert.strictEqual(
cacheBuster("/javascripts/my-script.js"),

View File

@ -1,5 +1,6 @@
import { failedCache, localCache } from "pretty-text/oneboxer-cache";
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import { failedCache, localCache } from "pretty-text/oneboxer-cache";
import { ajax } from "discourse/lib/ajax";
import { load } from "pretty-text/oneboxer";
@ -14,7 +15,9 @@ function loadOnebox(element) {
});
}
module("Unit | Utility | oneboxer", function () {
module("Unit | Utility | oneboxer", function (hooks) {
setupTest(hooks);
test("load - failed onebox", async function (assert) {
let element = document.createElement("A");
element.setAttribute("href", "http://somebadurl.com");

View File

@ -1,7 +1,10 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import { parseBBCodeTag } from "pretty-text/engines/discourse-markdown/bbcode-block";
module("Unit | Utility | parseBBCodeTag", function () {
module("Unit | Utility | parseBBCodeTag", function (hooks) {
setupTest(hooks);
test("block with multiple quoted attributes", function (assert) {
const parsed = parseBBCodeTag('[test one="foo" two="bar bar"]', 0, 30);

View File

@ -1,8 +1,11 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import PreloadStore from "discourse/lib/preload-store";
import { Promise } from "rsvp";
module("Unit | Utility | preload-store", function (hooks) {
setupTest(hooks);
hooks.beforeEach(function () {
PreloadStore.store("bane", "evil");
});

View File

@ -1,9 +1,10 @@
import QUnit, { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import PrettyText, { buildOptions } from "pretty-text/pretty-text";
import {
applyCachedInlineOnebox,
deleteCachedInlineOnebox,
} from "pretty-text/inline-oneboxer";
import QUnit, { module, test } from "qunit";
import { deepMerge } from "discourse-common/lib/object";
import { extractDataAttribute } from "pretty-text/engines/discourse-markdown-it";
import { registerEmoji } from "pretty-text/emoji";
@ -53,7 +54,9 @@ QUnit.assert.cookedPara = function (input, expected, message) {
QUnit.assert.cooked(input, `<p>${expected}</p>`, message);
};
module("Unit | Utility | pretty-text", function () {
module("Unit | Utility | pretty-text", function (hooks) {
setupTest(hooks);
test("buildOptions", function (assert) {
assert.ok(
buildOptions({ siteSettings: { enable_emoji: true } }).discourse.features

View File

@ -1,9 +1,12 @@
import PrettyText, { buildOptions } from "pretty-text/pretty-text";
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import PrettyText, { buildOptions } from "pretty-text/pretty-text";
import { hrefAllowed, sanitize } from "pretty-text/sanitizer";
import AllowLister from "pretty-text/allow-lister";
module("Unit | Utility | sanitizer", function () {
module("Unit | Utility | sanitizer", function (hooks) {
setupTest(hooks);
test("sanitize", function (assert) {
const pt = new PrettyText(
buildOptions({

View File

@ -1,11 +1,14 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import {
searchContextDescription,
translateResults,
} from "discourse/lib/search";
import I18n from "I18n";
module("Unit | Utility | search", function () {
module("Unit | Utility | search", function (hooks) {
setupTest(hooks);
test("unescapesEmojisInBlurbs", async function (assert) {
const source = {
posts: [

View File

@ -1,7 +1,10 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import Sharing from "discourse/lib/sharing";
module("Unit | Utility | sharing", function (hooks) {
setupTest(hooks);
hooks.beforeEach(function () {
Sharing._reset();
});

View File

@ -1,7 +1,10 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import SuffixTrie from "discourse-common/lib/suffix-trie";
module("Unit | SuffixTrie", function () {
module("Unit | SuffixTrie", function (hooks) {
setupTest(hooks);
test("SuffixTrie", function (assert) {
const t = new SuffixTrie("/");
t.add("a/b/c/d");

View File

@ -1,5 +1,6 @@
import { isLTR, isRTL, setTextDirections } from "discourse/lib/text-direction";
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import { isLTR, isRTL, setTextDirections } from "discourse/lib/text-direction";
function quoteHtml() {
return `
@ -30,7 +31,9 @@ function assertDirection(assert, elem, expected, message) {
assert.strictEqual(elem.getAttribute("dir"), expected, message);
}
module("Unit | Utility | text-direction", function () {
module("Unit | Utility | text-direction", function (hooks) {
setupTest(hooks);
test("isRTL", function (assert) {
// Hebrew
assert.strictEqual(isRTL("זה מבחן"), true);

View File

@ -1,4 +1,5 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import {
cookAsync,
excerpt,
@ -6,7 +7,9 @@ import {
parseMentions,
} from "discourse/lib/text";
module("Unit | Utility | text", function () {
module("Unit | Utility | text", function (hooks) {
setupTest(hooks);
test("parseAsync", async function (assert) {
await parseAsync("**test**").then((tokens) => {
assert.strictEqual(
@ -41,7 +44,9 @@ module("Unit | Utility | text", function () {
});
});
module("Unit | Utility | text | parseMentions", function () {
module("Unit | Utility | text | parseMentions", function (hooks) {
setupTest(hooks);
test("parses mentions from markdown", async function (assert) {
const markdown = "Hey @user1, @user2, @group1, @group2, @here, @all";
const mentions = await parseMentions(markdown);

View File

@ -1,10 +1,13 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import toMarkdown, {
addBlockDecorateCallback,
addTagDecorateCallback,
} from "discourse/lib/to-markdown";
module("Unit | Utility | to-markdown", function () {
module("Unit | Utility | to-markdown", function (hooks) {
setupTest(hooks);
test("converts styles between normal words", function (assert) {
const html = `Line with <s>styles</s> <b><i>between</i></b> words.`;
const markdown = `Line with ~~styles~~ ***between*** words.`;

View File

@ -1,9 +1,10 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import {
lookupCachedUploadUrl,
resetCache,
resolveAllShortUrls,
} from "pretty-text/upload-short-url";
import { module, test } from "qunit";
import pretender, { response } from "discourse/tests/helpers/create-pretender";
import { ajax } from "discourse/lib/ajax";
import { fixture } from "discourse/tests/helpers/qunit-helpers";
@ -85,6 +86,8 @@ function stubUrls(imageSrcs, attachmentSrcs, otherMediaSrcs) {
}
module("Unit | Utility | pretty-text/upload-short-url", function (hooks) {
setupTest(hooks);
hooks.afterEach(function () {
resetCache();
});

View File

@ -1,5 +1,6 @@
import UppyChecksum from "discourse/lib/uppy-checksum-plugin";
import { module, skip, test } from "qunit";
import { setupTest } from "ember-qunit";
import UppyChecksum from "discourse/lib/uppy-checksum-plugin";
import { createFile } from "discourse/tests/helpers/qunit-helpers";
import sinon from "sinon";
@ -48,7 +49,9 @@ class FakeUppy {
let withCrypto = window.crypto.subtle ? test : skip;
module("Unit | Utility | UppyChecksum Plugin", function () {
module("Unit | Utility | UppyChecksum Plugin", function (hooks) {
setupTest(hooks);
test("sets the options passed in", function (assert) {
const capabilities = {};
const fakeUppy = new FakeUppy();

View File

@ -1,5 +1,6 @@
import UppyMediaOptimization from "discourse/lib/uppy-media-optimization-plugin";
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import UppyMediaOptimization from "discourse/lib/uppy-media-optimization-plugin";
class FakeUppy {
constructor() {
@ -34,7 +35,9 @@ class FakeUppy {
}
}
module("Unit | Utility | UppyMediaOptimization Plugin", function () {
module("Unit | Utility | UppyMediaOptimization Plugin", function (hooks) {
setupTest(hooks);
test("sets the options passed in", function (assert) {
const fakeUppy = new FakeUppy();
const plugin = new UppyMediaOptimization(fakeUppy, {

View File

@ -1,15 +1,18 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import DiscourseURL, {
getCategoryAndTagUrl,
prefixProtocol,
userPath,
} from "discourse/lib/url";
import { module, test } from "qunit";
import User from "discourse/models/user";
import { logIn } from "discourse/tests/helpers/qunit-helpers";
import { setPrefix } from "discourse-common/lib/get-url";
import sinon from "sinon";
module("Unit | Utility | url", function () {
module("Unit | Utility | url", function (hooks) {
setupTest(hooks);
test("isInternal with a HTTP url", function (assert) {
sinon.stub(DiscourseURL, "origin").returns("http://eviltrout.com");

View File

@ -1,9 +1,12 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import pretender, { response } from "discourse/tests/helpers/create-pretender";
import { CANCELLED_STATUS } from "discourse/lib/autocomplete";
import userSearch from "discourse/lib/user-search";
module("Unit | Utility | user-search", function (hooks) {
setupTest(hooks);
hooks.beforeEach(function () {
pretender.get("/u/search/users", (request) => {
// special responder for per category search

View File

@ -1,8 +1,11 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import EmberObject from "@ember/object";
import Setting from "admin/mixins/setting-object";
module("Unit | Mixin | setting-object", function () {
module("Unit | Mixin | setting-object", function (hooks) {
setupTest(hooks);
test("flat array", function (assert) {
const FooSetting = EmberObject.extend(Setting);

View File

@ -1,8 +1,11 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import EmberObject from "@ember/object";
import Singleton from "discourse/mixins/singleton";
module("Unit | Mixin | singleton", function () {
module("Unit | Mixin | singleton", function (hooks) {
setupTest(hooks);
test("current", function (assert) {
let DummyModel = EmberObject.extend({});
DummyModel.reopenClass(Singleton);

View File

@ -1,8 +1,11 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import { applyLocalDates } from "../initializers/discourse-local-dates";
import { freezeTime } from "./local-date-builder-test";
module("Unit | Discourse Local Dates | discourse-local-dates", function () {
module("Unit | discourse-local-dates", function (hooks) {
setupTest(hooks);
function createElementFromHTML(htmlString) {
const div = document.createElement("div");
div.innerHTML = htmlString.trim();
@ -41,7 +44,9 @@ module("Unit | Discourse Local Dates | discourse-local-dates", function () {
freezeTime(
{ date: "2022-10-07T10:10:10", timezone: "Asia/Singapore" },
() => {
applyLocalDates(dateElements, { discourse_local_dates_enabled: true });
applyLocalDates(dateElements, {
discourse_local_dates_enabled: true,
});
assert.equal(
from.querySelector(".relative-time").textContent,
@ -64,7 +69,9 @@ module("Unit | Discourse Local Dates | discourse-local-dates", function () {
freezeTime(
{ date: "2022-10-07T10:10:10", timezone: "Asia/Singapore" },
() => {
applyLocalDates(dateElements, { discourse_local_dates_enabled: true });
applyLocalDates(dateElements, {
discourse_local_dates_enabled: true,
});
assert.equal(
from.querySelector(".relative-time").textContent,