REFACTOR: Remove Discourse constant from get-owner

This commit is contained in:
Robin Ward 2020-08-19 15:41:11 -04:00
parent 185ed80702
commit 347a4981a0
7 changed files with 68 additions and 38 deletions

View File

@ -1,14 +1,20 @@
import deprecated from "discourse-common/lib/deprecated"; 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) { export function getOwner(obj) {
if (emberGetOwner) { if (emberGetOwner) {
return emberGetOwner(obj) || Discourse.__container__; return emberGetOwner(obj) || emberGetOwner(_default);
} }
return obj.container; return obj.container;
} }
export function setDefaultOwner(container) {
setOwner(_default, container);
}
// `this.container` is deprecated, but we can still build a container-like // `this.container` is deprecated, but we can still build a container-like
// object for components to use // object for components to use
export function getRegister(obj) { export function getRegister(obj) {

View File

@ -82,6 +82,7 @@ const Singleton = Mixin.create({
resetCurrent(val) { resetCurrent(val) {
this._current = val; this._current = val;
return val;
} }
}); });

View File

@ -14,6 +14,7 @@ import { setIconList } from "discourse-common/lib/icon-library";
import { setPluginContainer } from "discourse/lib/plugin-api"; import { setPluginContainer } from "discourse/lib/plugin-api";
import { setURLContainer } from "discourse/lib/url"; import { setURLContainer } from "discourse/lib/url";
import { setModalContainer } from "discourse/lib/show-modal"; import { setModalContainer } from "discourse/lib/show-modal";
import { setDefaultOwner } from "discourse-common/lib/get-owner";
export default { export default {
name: "discourse-bootstrap", name: "discourse-bootstrap",
@ -23,6 +24,7 @@ export default {
setPluginContainer(container); setPluginContainer(container);
setURLContainer(container); setURLContainer(container);
setModalContainer(container); setModalContainer(container);
setDefaultOwner(container);
// Our test environment has its own bootstrap code // Our test environment has its own bootstrap code
if (isTesting()) { if (isTesting()) {

View File

@ -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", { componentTest("with children", {
template: "{{themes-list-item theme=theme}}", template: "{{themes-list-item theme=theme}}",
beforeEach() { beforeEach() {
this.childrenList = [1, 2, 3, 4, 5].map(num =>
Theme.create({ name: `Child ${num}`, component: true })
);
this.set( this.set(
"theme", "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(",") .split(",")
.map(n => n.trim()) .map(n => n.trim())
.join(","), .join(","),
childrenList this.childrenList
.splice(0, 4) .splice(0, 4)
.map(theme => theme.get("name")) .map(theme => theme.get("name"))
.join(","), .join(","),

View File

@ -4,25 +4,24 @@ import Theme, { THEMES, COMPONENTS } from "admin/models/theme";
moduleForComponent("themes-list", { integration: true }); 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", { componentTest("current tab is themes", {
template: template:
"{{themes-list themes=themes components=components currentTab=currentTab}}", "{{themes-list themes=themes components=components currentTab=currentTab}}",
beforeEach() { 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({ this.setProperties({
themes, themes: this.themes,
components, components: this.components,
currentTab: THEMES currentTab: THEMES
}); });
}, },
@ -46,10 +45,10 @@ componentTest("current tab is themes", {
); );
assert.equal(find(".themes-list-item").length, 5, "displays all themes"); assert.equal(find(".themes-list-item").length, 5, "displays all themes");
[2, 3].forEach(num => themes[num].set("user_selectable", true)); [2, 3].forEach(num => this.themes[num].set("user_selectable", true));
themes[4].set("default", true); this.themes[4].set("default", true);
this.set("themes", themes); this.set("themes", this.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 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( assert.deepEqual(
Array.from(find(".themes-list-item").find(".name")).map(node => Array.from(find(".themes-list-item").find(".name")).map(node =>
node.innerText.trim() node.innerText.trim()
@ -63,8 +62,8 @@ componentTest("current tab is themes", {
"the separator is in the right location" "the separator is in the right location"
); );
themes.forEach(theme => theme.set("user_selectable", true)); this.themes.forEach(theme => theme.set("user_selectable", true));
this.set("themes", themes); this.set("themes", this.themes);
assert.equal( assert.equal(
find(".inactive-indicator").index(), find(".inactive-indicator").index(),
-1, -1,
@ -91,9 +90,20 @@ componentTest("current tab is components", {
template: template:
"{{themes-list themes=themes components=components currentTab=currentTab}}", "{{themes-list themes=themes components=components currentTab=currentTab}}",
beforeEach() { 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({ this.setProperties({
themes, themes: this.themes,
components, components: this.components,
currentTab: COMPONENTS currentTab: COMPONENTS
}); });
}, },

View File

@ -30,6 +30,7 @@ import { getOwner } from "discourse-common/lib/get-owner";
import { setTopicList } from "discourse/lib/topic-list-tracker"; import { setTopicList } from "discourse/lib/topic-list-tracker";
import { setURLContainer } from "discourse/lib/url"; import { setURLContainer } from "discourse/lib/url";
import { setModalContainer } from "discourse/lib/show-modal"; import { setModalContainer } from "discourse/lib/show-modal";
import { setDefaultOwner } 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);
@ -166,18 +167,21 @@ export function acceptance(name, options) {
} }
this.siteSettings = currentSettings(); this.siteSettings = currentSettings();
if (options.site) {
resetSite(currentSettings(), options.site);
}
clearOutletCache(); clearOutletCache();
clearHTMLCache(); clearHTMLCache();
resetPluginApi(); resetPluginApi();
Discourse.reset(); Discourse.reset();
this.container = getOwner(this); this.container = getOwner(this);
setPluginContainer(this.container); setPluginContainer(this.container);
setURLContainer(this.container); setURLContainer(this.container);
setModalContainer(this.container); setModalContainer(this.container);
setDefaultOwner(this.container);
if (options.site) {
resetSite(currentSettings(), options.site);
}
if (options.beforeEach) { if (options.beforeEach) {
options.beforeEach.call(this); options.beforeEach.call(this);
} }
@ -208,6 +212,7 @@ export function acceptance(name, options) {
_clearSnapshots(); _clearSnapshots();
setURLContainer(null); setURLContainer(null);
setModalContainer(null); setModalContainer(null);
setDefaultOwner(null);
Discourse._runInitializer( Discourse._runInitializer(
"instanceInitializers", "instanceInitializers",
(initName, initializer) => { (initName, initializer) => {

View File

@ -93,6 +93,7 @@ var createPretender = require("helpers/create-pretender", null, null, false),
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, getOwner = require("discourse-common/lib/get-owner").getOwner,
setDefaultOwner = require("discourse-common/lib/get-owner").setDefaultOwner,
server, server,
acceptanceModulePrefix = "Acceptance: "; acceptanceModulePrefix = "Acceptance: ";
@ -106,7 +107,7 @@ function resetSite(siteSettings, extras) {
let Site = require("discourse/models/site").default; let Site = require("discourse/models/site").default;
siteAttrs.store = createStore(); siteAttrs.store = createStore();
siteAttrs.siteSettings = siteSettings; siteAttrs.siteSettings = siteSettings;
Site.resetCurrent(Site.create(siteAttrs)); return Site.resetCurrent(Site.create(siteAttrs));
} }
QUnit.testStart(function(ctx) { QUnit.testStart(function(ctx) {
@ -164,10 +165,11 @@ QUnit.testStart(function(ctx) {
let Session = require("discourse/models/session").default; let Session = require("discourse/models/session").default;
Session.resetCurrent(); Session.resetCurrent();
User.resetCurrent(); User.resetCurrent();
resetSite(settings); let site = resetSite(settings);
createHelperContext({ createHelperContext({
siteSettings: settings, siteSettings: settings,
capabilities: {} capabilities: {},
site
}); });
_DiscourseURL.redirectedTo = null; _DiscourseURL.redirectedTo = null;
@ -233,5 +235,5 @@ Object.keys(requirejs.entries).forEach(function(entry) {
// forces 0 as duration for all jquery animations // forces 0 as duration for all jquery animations
jQuery.fx.off = true; jQuery.fx.off = true;
setDefaultOwner(App.__container__);
resetSite(); resetSite();