REFACTOR: Remove `Discourse.__container__` from tests

This commit is contained in:
Robin Ward 2020-07-28 13:07:33 -04:00
parent 81ab62c72a
commit 01a3fa1ca8
6 changed files with 51 additions and 56 deletions

View File

@ -4,12 +4,11 @@ import { IMAGE_VERSION as v } from "pretty-text/emoji/version";
acceptance("EmojiPicker", {
loggedIn: true,
beforeEach() {
const store = Discourse.__container__.lookup("service:emoji-store");
store.reset();
this.emojiStore = this.container.lookup("service:emoji-store");
this.emojiStore.reset();
},
afterEach() {
const store = Discourse.__container__.lookup("service:emoji-store");
store.reset();
this.emojiStore.reset();
}
});

View File

@ -3,19 +3,14 @@ import { withPluginApi } from "discourse/lib/plugin-api";
import KeyboardShortcuts from "discourse/lib/keyboard-shortcuts";
import KeyboardShortcutInitializer from "discourse/initializers/keyboard-shortcuts";
function initKeyboardShortcuts() {
// this is here because initializers/keyboard-shortcuts is not
// firing for this acceptance test, and it needs to be fired before
// more keyboard shortcuts can be added
KeyboardShortcutInitializer.initialize(Discourse.__container__);
}
acceptance("Plugin Keyboard Shortcuts - Logged In", {
loggedIn: true
loggedIn: true,
beforeEach() {
KeyboardShortcutInitializer.initialize(this.container);
}
});
test("a plugin can add a keyboard shortcut", async assert => {
initKeyboardShortcuts();
withPluginApi("0.8.38", api => {
api.addKeyboardShortcut("]", () => {
$("#qunit-fixture").html(
@ -34,12 +29,14 @@ test("a plugin can add a keyboard shortcut", async assert => {
});
acceptance("Plugin Keyboard Shortcuts - Anonymous", {
loggedIn: false
loggedIn: false,
beforeEach() {
KeyboardShortcutInitializer.initialize(this.container);
}
});
test("a plugin can add a keyboard shortcut with an option", async assert => {
var spy = sandbox.spy(KeyboardShortcuts, "_bindToPath");
initKeyboardShortcuts();
let spy = sandbox.spy(KeyboardShortcuts, "_bindToPath");
withPluginApi("0.8.38", api => {
api.addKeyboardShortcut("]", () => {}, {
anonymous: true,

View File

@ -3,12 +3,13 @@ import User from "discourse/models/user";
import KeyboardShortcutInitializer from "discourse/initializers/keyboard-shortcuts";
import { REMINDER_TYPES } from "discourse/lib/bookmark";
import { fakeTime } from "helpers/qunit-helpers";
let BookmarkController;
moduleFor("controller:bookmark", {
beforeEach() {
logIn();
KeyboardShortcutInitializer.initialize(Discourse.__container__);
KeyboardShortcutInitializer.initialize(this.container);
BookmarkController = this.subject({
currentUser: User.current(),
site: { isMobileDevice: false }

View File

@ -26,6 +26,7 @@ import { _clearSnapshots } from "select-kit/components/composer-actions";
import User from "discourse/models/user";
import { mapRoutes } from "discourse/mapping-router";
import { currentSettings, mergeSettings } from "helpers/site-settings";
import { getOwner } from "discourse-common/lib/get-owner";
export function currentUser() {
return User.create(sessionFixtures["/session/current.json"].current_user);
@ -120,6 +121,7 @@ export function controllerModule(name, args = {}) {
export function discourseModule(name, hooks) {
QUnit.module(name, {
beforeEach() {
this.container = getOwner(this);
this.siteSettings = currentSettings();
if (hooks && hooks.beforeEach) {
hooks.beforeEach.call(this);
@ -148,10 +150,6 @@ export function acceptance(name, options) {
HeaderComponent.reopen({ examineDockHeader: function() {} });
resetExtraClasses();
if (options.beforeEach) {
options.beforeEach.call(this);
}
if (options.mobileView) {
forceMobile();
}
@ -173,6 +171,10 @@ export function acceptance(name, options) {
clearHTMLCache();
resetPluginApi();
Discourse.reset();
this.container = getOwner(this);
if (options.beforeEach) {
options.beforeEach.call(this);
}
},
afterEach() {
@ -197,14 +199,14 @@ export function acceptance(name, options) {
resetOneboxCache();
resetCustomPostMessageCallbacks();
_clearSnapshots();
Discourse._runInitializer("instanceInitializers", function(
initName,
initializer
) {
if (initializer && initializer.teardown) {
initializer.teardown(Discourse.__container__);
Discourse._runInitializer(
"instanceInitializers",
(initName, initializer) => {
if (initializer && initializer.teardown) {
initializer.teardown(this.container);
}
}
});
);
Discourse.reset();
// We do this after reset so that the willClearRender will have already fired
@ -214,7 +216,7 @@ export function acceptance(name, options) {
}
export function controllerFor(controller, model) {
controller = Discourse.__container__.lookup("controller:" + controller);
controller = getOwner(this).lookup("controller:" + controller);
if (model) {
controller.set("model", model);
}

View File

@ -1,37 +1,34 @@
QUnit.module("lib:emoji-store", {
import { discourseModule } from "helpers/qunit-helpers";
discourseModule("lib:emoji-emojiStore", {
beforeEach() {
const store = Discourse.__container__.lookup("service:emoji-store");
store.reset();
this.emojiStore = this.container.lookup("service:emoji-store");
this.emojiStore.reset();
},
afterEach() {
const store = Discourse.__container__.lookup("service:emoji-store");
store.reset();
this.emojiStore.reset();
}
});
QUnit.test("defaults", assert => {
const store = Discourse.__container__.lookup("service:emoji-store");
assert.deepEqual(store.favorites, []);
assert.equal(store.diversity, 1);
QUnit.test("defaults", function(assert) {
assert.deepEqual(this.emojiStore.favorites, []);
assert.equal(this.emojiStore.diversity, 1);
});
QUnit.test("diversity", assert => {
const store = Discourse.__container__.lookup("service:emoji-store");
store.diversity = 2;
assert.equal(store.diversity, 2);
QUnit.test("diversity", function(assert) {
this.emojiStore.diversity = 2;
assert.equal(this.emojiStore.diversity, 2);
});
QUnit.test("favorites", assert => {
const store = Discourse.__container__.lookup("service:emoji-store");
store.favorites = ["smile"];
assert.deepEqual(store.favorites, ["smile"]);
QUnit.test("favorites", function(assert) {
this.emojiStore.favorites = ["smile"];
assert.deepEqual(this.emojiStore.favorites, ["smile"]);
});
QUnit.test("track", assert => {
const store = Discourse.__container__.lookup("service:emoji-store");
store.track("woman:t4");
assert.deepEqual(store.favorites, ["woman:t4"]);
store.track("otter");
store.track(":otter:");
assert.deepEqual(store.favorites, ["otter", "woman:t4"]);
QUnit.test("track", function(assert) {
this.emojiStore.track("woman:t4");
assert.deepEqual(this.emojiStore.favorites, ["woman:t4"]);
this.emojiStore.track("otter");
this.emojiStore.track(":otter:");
assert.deepEqual(this.emojiStore.favorites, ["otter", "woman:t4"]);
});

View File

@ -91,6 +91,7 @@ var createPretender = require("helpers/create-pretender", null, null, false),
_DiscourseURL = require("discourse/lib/url", null, null, false).default,
applyPretender = require("helpers/qunit-helpers", null, null, false)
.applyPretender,
getOwner = require("discourse-common/lib/get-owner").getOwner,
server,
acceptanceModulePrefix = "Acceptance: ";
@ -192,9 +193,7 @@ QUnit.testDone(function() {
// ensures any event not removed is not leaking between tests
// most likely in intialisers, other places (controller, component...)
// should be fixed in code
require("discourse/services/app-events").clearAppEventsCache(
window.Discourse.__container__
);
require("discourse/services/app-events").clearAppEventsCache(getOwner(this));
MessageBus.unsubscribe("*");
delete window.server;