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 { 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) {

View File

@ -82,6 +82,7 @@ const Singleton = Mixin.create({
resetCurrent(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 { 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()) {

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", {
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(","),

View File

@ -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
});
},

View File

@ -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) => {

View File

@ -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();