REFACTOR: Remove `Discourse.__container__` from tests

REFACTOR: Remove Discourse.__container__ from tests

diff --git a/test/javascripts/acceptance/emoji-picker-test.js b/test/javascripts/acceptance/emoji-picker-test.js
index a3d8472..31af756 100644
--- a/test/javascripts/acceptance/emoji-picker-test.js
+++ b/test/javascripts/acceptance/emoji-picker-test.js
@@ -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();
   }
 });
 
diff --git a/test/javascripts/acceptance/plugin-keyboard-shortcut-test.js b/test/javascripts/acceptance/plugin-keyboard-shortcut-test.js
index 3541bb2..a2fd1c0 100644
--- a/test/javascripts/acceptance/plugin-keyboard-shortcut-test.js
+++ b/test/javascripts/acceptance/plugin-keyboard-shortcut-test.js
@@ -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,
diff --git a/test/javascripts/controllers/bookmark-test.js b/test/javascripts/controllers/bookmark-test.js
index 1e7c784..9919897 100644
--- a/test/javascripts/controllers/bookmark-test.js
+++ b/test/javascripts/controllers/bookmark-test.js
@@ -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 }
diff --git a/test/javascripts/helpers/qunit-helpers.js b/test/javascripts/helpers/qunit-helpers.js
index aed99d6..2572068 100644
--- a/test/javascripts/helpers/qunit-helpers.js
+++ b/test/javascripts/helpers/qunit-helpers.js
@@ -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);
   }
diff --git a/test/javascripts/lib/emoji-store-test.js b/test/javascripts/lib/emoji-store-test.js
index 3a137ed..14d1048 100644
--- a/test/javascripts/lib/emoji-store-test.js
+++ b/test/javascripts/lib/emoji-store-test.js
@@ -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"]);

[... diff too long, it was truncated ...]

GitHub sha: 01a3fa1c

1 Like

This commit appears in #10329 which was merged by eviltrout.