diff --git a/app/assets/javascripts/discourse-common/addon/lib/get-owner.js b/app/assets/javascripts/discourse-common/addon/lib/get-owner.js index 0fd653bb9be..23a7218fa60 100644 --- a/app/assets/javascripts/discourse-common/addon/lib/get-owner.js +++ b/app/assets/javascripts/discourse-common/addon/lib/get-owner.js @@ -1,14 +1,20 @@ import deprecated from "discourse-common/lib/deprecated"; -import { getOwner as emberGetOwner } from "@ember/application"; +import { getOwner as emberGetOwner, setOwner } from "@ember/application"; + +let _default = {}; export function getOwner(obj) { if (emberGetOwner) { - return emberGetOwner(obj) || Discourse.__container__; + return emberGetOwner(obj) || emberGetOwner(_default); } return obj.container; } +export function setDefaultOwner(container) { + setOwner(_default, container); +} + // `this.container` is deprecated, but we can still build a container-like // object for components to use export function getRegister(obj) { diff --git a/app/assets/javascripts/discourse/app/mixins/singleton.js b/app/assets/javascripts/discourse/app/mixins/singleton.js index 5c4ac8238df..20b95f8f57a 100644 --- a/app/assets/javascripts/discourse/app/mixins/singleton.js +++ b/app/assets/javascripts/discourse/app/mixins/singleton.js @@ -82,6 +82,7 @@ const Singleton = Mixin.create({ resetCurrent(val) { this._current = val; + return val; } }); diff --git a/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js b/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js index 3cfabb5a201..d8772f3bd17 100644 --- a/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js +++ b/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js @@ -14,6 +14,7 @@ import { setIconList } from "discourse-common/lib/icon-library"; import { setPluginContainer } from "discourse/lib/plugin-api"; import { setURLContainer } from "discourse/lib/url"; import { setModalContainer } from "discourse/lib/show-modal"; +import { setDefaultOwner } from "discourse-common/lib/get-owner"; export default { name: "discourse-bootstrap", @@ -23,6 +24,7 @@ export default { setPluginContainer(container); setURLContainer(container); setModalContainer(container); + setDefaultOwner(container); // Our test environment has its own bootstrap code if (isTesting()) { diff --git a/test/javascripts/admin/components/themes-list-item-test.js b/test/javascripts/admin/components/themes-list-item-test.js index 134cf6cded8..5235d8f1e5d 100644 --- a/test/javascripts/admin/components/themes-list-item-test.js +++ b/test/javascripts/admin/components/themes-list-item-test.js @@ -53,17 +53,21 @@ componentTest("broken theme", { } }); -const childrenList = [1, 2, 3, 4, 5].map(num => - Theme.create({ name: `Child ${num}`, component: true }) -); - componentTest("with children", { template: "{{themes-list-item theme=theme}}", beforeEach() { + this.childrenList = [1, 2, 3, 4, 5].map(num => + Theme.create({ name: `Child ${num}`, component: true }) + ); + this.set( "theme", - Theme.create({ name: "Test", childThemes: childrenList, default: true }) + Theme.create({ + name: "Test", + childThemes: this.childrenList, + default: true + }) ); }, @@ -76,7 +80,7 @@ componentTest("with children", { .split(",") .map(n => n.trim()) .join(","), - childrenList + this.childrenList .splice(0, 4) .map(theme => theme.get("name")) .join(","), diff --git a/test/javascripts/admin/components/themes-list-test.js b/test/javascripts/admin/components/themes-list-test.js index 8a2d595f1e9..2fc68569c6f 100644 --- a/test/javascripts/admin/components/themes-list-test.js +++ b/test/javascripts/admin/components/themes-list-test.js @@ -4,25 +4,24 @@ import Theme, { THEMES, COMPONENTS } from "admin/models/theme"; moduleForComponent("themes-list", { integration: true }); -const themes = [1, 2, 3, 4, 5].map(num => - Theme.create({ name: `Theme ${num}` }) -); -const components = [1, 2, 3, 4, 5].map(num => - Theme.create({ - name: `Child ${num}`, - component: true, - parentThemes: [themes[num - 1]], - parent_themes: [1, 2, 3, 4, 5] - }) -); - componentTest("current tab is themes", { template: "{{themes-list themes=themes components=components currentTab=currentTab}}", beforeEach() { + this.themes = [1, 2, 3, 4, 5].map(num => + Theme.create({ name: `Theme ${num}` }) + ); + this.components = [1, 2, 3, 4, 5].map(num => + Theme.create({ + name: `Child ${num}`, + component: true, + parentThemes: [this.themes[num - 1]], + parent_themes: [1, 2, 3, 4, 5] + }) + ); this.setProperties({ - themes, - components, + themes: this.themes, + components: this.components, currentTab: THEMES }); }, @@ -46,10 +45,10 @@ componentTest("current tab is themes", { ); assert.equal(find(".themes-list-item").length, 5, "displays all themes"); - [2, 3].forEach(num => themes[num].set("user_selectable", true)); - themes[4].set("default", true); - this.set("themes", themes); - const names = [4, 2, 3, 0, 1].map(num => themes[num].get("name")); // default theme always on top, followed by user-selectable ones and then the rest + [2, 3].forEach(num => this.themes[num].set("user_selectable", true)); + this.themes[4].set("default", true); + this.set("themes", this.themes); + const names = [4, 2, 3, 0, 1].map(num => this.themes[num].get("name")); // default theme always on top, followed by user-selectable ones and then the rest assert.deepEqual( Array.from(find(".themes-list-item").find(".name")).map(node => node.innerText.trim() @@ -63,8 +62,8 @@ componentTest("current tab is themes", { "the separator is in the right location" ); - themes.forEach(theme => theme.set("user_selectable", true)); - this.set("themes", themes); + this.themes.forEach(theme => theme.set("user_selectable", true)); + this.set("themes", this.themes); assert.equal( find(".inactive-indicator").index(), -1, @@ -91,9 +90,20 @@ componentTest("current tab is components", { template: "{{themes-list themes=themes components=components currentTab=currentTab}}", beforeEach() { + this.themes = [1, 2, 3, 4, 5].map(num => + Theme.create({ name: `Theme ${num}` }) + ); + this.components = [1, 2, 3, 4, 5].map(num => + Theme.create({ + name: `Child ${num}`, + component: true, + parentThemes: [this.themes[num - 1]], + parent_themes: [1, 2, 3, 4, 5] + }) + ); this.setProperties({ - themes, - components, + themes: this.themes, + components: this.components, currentTab: COMPONENTS }); }, diff --git a/test/javascripts/helpers/qunit-helpers.js b/test/javascripts/helpers/qunit-helpers.js index 70f8a27d0f3..931d09de1f3 100644 --- a/test/javascripts/helpers/qunit-helpers.js +++ b/test/javascripts/helpers/qunit-helpers.js @@ -30,6 +30,7 @@ import { getOwner } from "discourse-common/lib/get-owner"; import { setTopicList } from "discourse/lib/topic-list-tracker"; import { setURLContainer } from "discourse/lib/url"; import { setModalContainer } from "discourse/lib/show-modal"; +import { setDefaultOwner } from "discourse-common/lib/get-owner"; export function currentUser() { return User.create(sessionFixtures["/session/current.json"].current_user); @@ -166,18 +167,21 @@ export function acceptance(name, options) { } this.siteSettings = currentSettings(); - if (options.site) { - resetSite(currentSettings(), options.site); - } - clearOutletCache(); clearHTMLCache(); resetPluginApi(); + Discourse.reset(); this.container = getOwner(this); setPluginContainer(this.container); setURLContainer(this.container); setModalContainer(this.container); + setDefaultOwner(this.container); + + if (options.site) { + resetSite(currentSettings(), options.site); + } + if (options.beforeEach) { options.beforeEach.call(this); } @@ -208,6 +212,7 @@ export function acceptance(name, options) { _clearSnapshots(); setURLContainer(null); setModalContainer(null); + setDefaultOwner(null); Discourse._runInitializer( "instanceInitializers", (initName, initializer) => { diff --git a/test/javascripts/test_helper.js b/test/javascripts/test_helper.js index 63478540d2f..bf50d531535 100644 --- a/test/javascripts/test_helper.js +++ b/test/javascripts/test_helper.js @@ -93,6 +93,7 @@ var createPretender = require("helpers/create-pretender", null, null, false), applyPretender = require("helpers/qunit-helpers", null, null, false) .applyPretender, getOwner = require("discourse-common/lib/get-owner").getOwner, + setDefaultOwner = require("discourse-common/lib/get-owner").setDefaultOwner, server, acceptanceModulePrefix = "Acceptance: "; @@ -106,7 +107,7 @@ function resetSite(siteSettings, extras) { let Site = require("discourse/models/site").default; siteAttrs.store = createStore(); siteAttrs.siteSettings = siteSettings; - Site.resetCurrent(Site.create(siteAttrs)); + return Site.resetCurrent(Site.create(siteAttrs)); } QUnit.testStart(function(ctx) { @@ -164,10 +165,11 @@ QUnit.testStart(function(ctx) { let Session = require("discourse/models/session").default; Session.resetCurrent(); User.resetCurrent(); - resetSite(settings); + let site = resetSite(settings); createHelperContext({ siteSettings: settings, - capabilities: {} + capabilities: {}, + site }); _DiscourseURL.redirectedTo = null; @@ -233,5 +235,5 @@ Object.keys(requirejs.entries).forEach(function(entry) { // forces 0 as duration for all jquery animations jQuery.fx.off = true; - +setDefaultOwner(App.__container__); resetSite();