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", { acceptance("EmojiPicker", {
loggedIn: true, loggedIn: true,
beforeEach() { beforeEach() {
const store = Discourse.__container__.lookup("service:emoji-store"); this.emojiStore = this.container.lookup("service:emoji-store");
store.reset(); this.emojiStore.reset();
}, },
afterEach() { afterEach() {
const store = Discourse.__container__.lookup("service:emoji-store"); this.emojiStore.reset();
store.reset();
} }
}); });

View File

@ -3,19 +3,14 @@ import { withPluginApi } from "discourse/lib/plugin-api";
import KeyboardShortcuts from "discourse/lib/keyboard-shortcuts"; import KeyboardShortcuts from "discourse/lib/keyboard-shortcuts";
import KeyboardShortcutInitializer from "discourse/initializers/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", { 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 => { test("a plugin can add a keyboard shortcut", async assert => {
initKeyboardShortcuts();
withPluginApi("0.8.38", api => { withPluginApi("0.8.38", api => {
api.addKeyboardShortcut("]", () => { api.addKeyboardShortcut("]", () => {
$("#qunit-fixture").html( $("#qunit-fixture").html(
@ -34,12 +29,14 @@ test("a plugin can add a keyboard shortcut", async assert => {
}); });
acceptance("Plugin Keyboard Shortcuts - Anonymous", { 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 => { test("a plugin can add a keyboard shortcut with an option", async assert => {
var spy = sandbox.spy(KeyboardShortcuts, "_bindToPath"); let spy = sandbox.spy(KeyboardShortcuts, "_bindToPath");
initKeyboardShortcuts();
withPluginApi("0.8.38", api => { withPluginApi("0.8.38", api => {
api.addKeyboardShortcut("]", () => {}, { api.addKeyboardShortcut("]", () => {}, {
anonymous: true, anonymous: true,

View File

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

View File

@ -26,6 +26,7 @@ import { _clearSnapshots } from "select-kit/components/composer-actions";
import User from "discourse/models/user"; import User from "discourse/models/user";
import { mapRoutes } from "discourse/mapping-router"; import { mapRoutes } from "discourse/mapping-router";
import { currentSettings, mergeSettings } from "helpers/site-settings"; import { currentSettings, mergeSettings } from "helpers/site-settings";
import { getOwner } from "discourse-common/lib/get-owner";
export function currentUser() { export function currentUser() {
return User.create(sessionFixtures["/session/current.json"].current_user); return User.create(sessionFixtures["/session/current.json"].current_user);
@ -120,6 +121,7 @@ export function controllerModule(name, args = {}) {
export function discourseModule(name, hooks) { export function discourseModule(name, hooks) {
QUnit.module(name, { QUnit.module(name, {
beforeEach() { beforeEach() {
this.container = getOwner(this);
this.siteSettings = currentSettings(); this.siteSettings = currentSettings();
if (hooks && hooks.beforeEach) { if (hooks && hooks.beforeEach) {
hooks.beforeEach.call(this); hooks.beforeEach.call(this);
@ -148,10 +150,6 @@ export function acceptance(name, options) {
HeaderComponent.reopen({ examineDockHeader: function() {} }); HeaderComponent.reopen({ examineDockHeader: function() {} });
resetExtraClasses(); resetExtraClasses();
if (options.beforeEach) {
options.beforeEach.call(this);
}
if (options.mobileView) { if (options.mobileView) {
forceMobile(); forceMobile();
} }
@ -173,6 +171,10 @@ export function acceptance(name, options) {
clearHTMLCache(); clearHTMLCache();
resetPluginApi(); resetPluginApi();
Discourse.reset(); Discourse.reset();
this.container = getOwner(this);
if (options.beforeEach) {
options.beforeEach.call(this);
}
}, },
afterEach() { afterEach() {
@ -197,14 +199,14 @@ export function acceptance(name, options) {
resetOneboxCache(); resetOneboxCache();
resetCustomPostMessageCallbacks(); resetCustomPostMessageCallbacks();
_clearSnapshots(); _clearSnapshots();
Discourse._runInitializer("instanceInitializers", function( Discourse._runInitializer(
initName, "instanceInitializers",
initializer (initName, initializer) => {
) { if (initializer && initializer.teardown) {
if (initializer && initializer.teardown) { initializer.teardown(this.container);
initializer.teardown(Discourse.__container__); }
} }
}); );
Discourse.reset(); Discourse.reset();
// We do this after reset so that the willClearRender will have already fired // 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) { export function controllerFor(controller, model) {
controller = Discourse.__container__.lookup("controller:" + controller); controller = getOwner(this).lookup("controller:" + controller);
if (model) { if (model) {
controller.set("model", 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() { beforeEach() {
const store = Discourse.__container__.lookup("service:emoji-store"); this.emojiStore = this.container.lookup("service:emoji-store");
store.reset(); this.emojiStore.reset();
}, },
afterEach() { afterEach() {
const store = Discourse.__container__.lookup("service:emoji-store"); this.emojiStore.reset();
store.reset();
} }
}); });
QUnit.test("defaults", assert => { QUnit.test("defaults", function(assert) {
const store = Discourse.__container__.lookup("service:emoji-store"); assert.deepEqual(this.emojiStore.favorites, []);
assert.deepEqual(store.favorites, []); assert.equal(this.emojiStore.diversity, 1);
assert.equal(store.diversity, 1);
}); });
QUnit.test("diversity", assert => { QUnit.test("diversity", function(assert) {
const store = Discourse.__container__.lookup("service:emoji-store"); this.emojiStore.diversity = 2;
store.diversity = 2; assert.equal(this.emojiStore.diversity, 2);
assert.equal(store.diversity, 2);
}); });
QUnit.test("favorites", assert => { QUnit.test("favorites", function(assert) {
const store = Discourse.__container__.lookup("service:emoji-store"); this.emojiStore.favorites = ["smile"];
store.favorites = ["smile"]; assert.deepEqual(this.emojiStore.favorites, ["smile"]);
assert.deepEqual(store.favorites, ["smile"]);
}); });
QUnit.test("track", assert => { QUnit.test("track", function(assert) {
const store = Discourse.__container__.lookup("service:emoji-store"); this.emojiStore.track("woman:t4");
store.track("woman:t4"); assert.deepEqual(this.emojiStore.favorites, ["woman:t4"]);
assert.deepEqual(store.favorites, ["woman:t4"]); this.emojiStore.track("otter");
store.track("otter"); this.emojiStore.track(":otter:");
store.track(":otter:"); assert.deepEqual(this.emojiStore.favorites, ["otter", "woman:t4"]);
assert.deepEqual(store.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, _DiscourseURL = require("discourse/lib/url", null, null, false).default,
applyPretender = require("helpers/qunit-helpers", null, null, false) applyPretender = require("helpers/qunit-helpers", null, null, false)
.applyPretender, .applyPretender,
getOwner = require("discourse-common/lib/get-owner").getOwner,
server, server,
acceptanceModulePrefix = "Acceptance: "; acceptanceModulePrefix = "Acceptance: ";
@ -192,9 +193,7 @@ QUnit.testDone(function() {
// ensures any event not removed is not leaking between tests // ensures any event not removed is not leaking between tests
// most likely in intialisers, other places (controller, component...) // most likely in intialisers, other places (controller, component...)
// should be fixed in code // should be fixed in code
require("discourse/services/app-events").clearAppEventsCache( require("discourse/services/app-events").clearAppEventsCache(getOwner(this));
window.Discourse.__container__
);
MessageBus.unsubscribe("*"); MessageBus.unsubscribe("*");
delete window.server; delete window.server;