More backwards compatible changes to sync with ember-cli-test (#11718)

* More backwards compatible changes to sync with ember-cli-test

* Update app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js

Co-authored-by: Jarek Radosz <jradosz@gmail.com>

* Various router fixes

Co-authored-by: Jarek Radosz <jradosz@gmail.com>
This commit is contained in:
Robin Ward 2021-01-15 12:16:05 -05:00 committed by GitHub
parent 9057379aac
commit f772a0beac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 129 additions and 77 deletions

View File

@ -1,11 +1,11 @@
import Theme from "admin/models/theme"; import Theme from "admin/models/theme";
import { mapRoutes } from "discourse/mapping-router";
import { moduleFor } from "ember-qunit"; import { moduleFor } from "ember-qunit";
import { registerRouter } from "discourse/mapping-router";
import { test } from "qunit"; import { test } from "qunit";
moduleFor("controller:admin-customize-themes-show", { moduleFor("controller:admin-customize-themes-show", {
beforeEach() { beforeEach() {
this.registry.register("router:main", mapRoutes()); registerRouter(this.registry);
}, },
needs: ["controller:adminUser"], needs: ["controller:adminUser"],
}); });

View File

@ -1,11 +1,11 @@
import Theme from "admin/models/theme"; import Theme from "admin/models/theme";
import { mapRoutes } from "discourse/mapping-router";
import { moduleFor } from "ember-qunit"; import { moduleFor } from "ember-qunit";
import { registerRouter } from "discourse/mapping-router";
import { test } from "qunit"; import { test } from "qunit";
moduleFor("controller:admin-customize-themes", { moduleFor("controller:admin-customize-themes", {
beforeEach() { beforeEach() {
this.registry.register("router:main", mapRoutes()); registerRouter(this.registry);
}, },
needs: ["controller:adminUser"], needs: ["controller:adminUser"],
}); });

View File

@ -1,11 +1,11 @@
import Badge from "discourse/models/badge"; import Badge from "discourse/models/badge";
import { mapRoutes } from "discourse/mapping-router";
import { moduleFor } from "ember-qunit"; import { moduleFor } from "ember-qunit";
import { registerRouter } from "discourse/mapping-router";
import { test } from "qunit"; import { test } from "qunit";
moduleFor("controller:admin-user-badges", { moduleFor("controller:admin-user-badges", {
beforeEach() { beforeEach() {
this.registry.register("router:main", mapRoutes()); registerRouter(this.registry);
}, },
needs: ["controller:adminUser"], needs: ["controller:adminUser"],
}); });

View File

@ -105,8 +105,9 @@ export function buildResolver(baseName) {
dashed = dasherize(suffix), dashed = dasherize(suffix),
moduleName = Object.keys(requirejs.entries).find(function (e) { moduleName = Object.keys(requirejs.entries).find(function (e) {
return ( return (
e.indexOf(suffix, e.length - suffix.length) !== -1 || e.indexOf("/templates/") === -1 &&
e.indexOf(dashed, e.length - dashed.length) !== -1 (e.indexOf(suffix, e.length - suffix.length) !== -1 ||
e.indexOf(dashed, e.length - dashed.length) !== -1)
); );
}); });

View File

@ -39,7 +39,6 @@ import { findRawTemplate } from "discourse-common/lib/raw-templates";
import getURL from "discourse-common/lib/get-url"; import getURL from "discourse-common/lib/get-url";
import { iconHTML } from "discourse-common/lib/icon-library"; import { iconHTML } from "discourse-common/lib/icon-library";
import { isTesting } from "discourse-common/config/environment"; import { isTesting } from "discourse-common/config/environment";
import { loadOneboxes } from "discourse/lib/load-oneboxes"; import { loadOneboxes } from "discourse/lib/load-oneboxes";
import putCursorAtEnd from "discourse/lib/put-cursor-at-end"; import putCursorAtEnd from "discourse/lib/put-cursor-at-end";
import userSearch from "discourse/lib/user-search"; import userSearch from "discourse/lib/user-search";

View File

@ -141,3 +141,11 @@ export function mapRoutes() {
this.route("unknown", { path: "*path" }); this.route("unknown", { path: "*path" });
}); });
} }
export function registerRouter(registry) {
registry.unregister("router:main");
let router = mapRoutes();
registry.register("router:main", router);
return router;
}

View File

@ -9,6 +9,7 @@ import I18n from "I18n";
import PreloadStore from "discourse/lib/preload-store"; import PreloadStore from "discourse/lib/preload-store";
import RSVP from "rsvp"; import RSVP from "rsvp";
import Session from "discourse/models/session"; import Session from "discourse/models/session";
import { camelize } from "@ember/string";
import deprecated from "discourse-common/lib/deprecated"; import deprecated from "discourse-common/lib/deprecated";
import { setDefaultOwner } from "discourse-common/lib/get-owner"; import { setDefaultOwner } from "discourse-common/lib/get-owner";
import { setIconList } from "discourse-common/lib/icon-library"; import { setIconList } from "discourse-common/lib/icon-library";
@ -26,11 +27,27 @@ export default {
if (isTesting()) { if (isTesting()) {
return; return;
} }
const preloadedDataElement = document.getElementById("data-preloaded");
const setupData = document.getElementById("data-discourse-setup").dataset;
let setupData;
let preloaded;
if (app.bootstrap) {
// This is annoying but our old way of using `data-*` attributes used camelCase by default
setupData = {};
Object.keys(app.bootstrap.setup_data).forEach((k) => {
setupData[camelize(k)] = app.bootstrap.setup_data[k];
});
preloaded = app.bootstrap.preloaded;
}
const setupDataElement = document.getElementById("data-discourse-setup");
if (setupDataElement) {
setupData = setupDataElement.dataset;
}
const preloadedDataElement = document.getElementById("data-preloaded");
if (preloadedDataElement) { if (preloadedDataElement) {
const preloaded = JSON.parse(preloadedDataElement.dataset.preloaded); preloaded = JSON.parse(preloadedDataElement.dataset.preloaded);
}
Object.keys(preloaded).forEach(function (key) { Object.keys(preloaded).forEach(function (key) {
PreloadStore.store(key, JSON.parse(preloaded[key])); PreloadStore.store(key, JSON.parse(preloaded[key]));
@ -41,7 +58,6 @@ export default {
/* eslint-enable no-console */ /* eslint-enable no-console */
} }
}); });
}
let baseUrl = setupData.baseUrl; let baseUrl = setupData.baseUrl;
Object.defineProperty(app, "BaseUrl", { Object.defineProperty(app, "BaseUrl", {
@ -94,8 +110,11 @@ export default {
parseInt(setupData.userColorSchemeId, 10) || null; parseInt(setupData.userColorSchemeId, 10) || null;
session.userDarkSchemeId = parseInt(setupData.userDarkSchemeId, 10) || -1; session.userDarkSchemeId = parseInt(setupData.userDarkSchemeId, 10) || -1;
if (isDevelopment()) { let iconList = setupData.svgIconList;
setIconList(JSON.parse(setupData.svgIconList)); if (isDevelopment() && iconList) {
setIconList(
typeof iconList === "string" ? JSON.parse(iconList) : iconList
);
} }
if (setupData.s3BaseUrl) { if (setupData.s3BaseUrl) {

View File

@ -13,65 +13,53 @@ import User from "discourse/models/user";
const ALL_TARGETS = ["controller", "component", "route", "model", "adapter"]; const ALL_TARGETS = ["controller", "component", "route", "model", "adapter"];
export function registerObjects(container, app) {
if (app.__registeredObjects__) {
// don't run registrations twice.
return;
}
app.__registeredObjects__ = true;
app.register("store:main", Store);
app.register("service:store", Store);
// backwards compatibility: remove when plugins have updated
app.appEvents = container.lookup("service:app-events");
// TODO: This should be included properly
app.register("message-bus:main", MessageBus, { instantiate: false });
const siteSettings = app.SiteSettings;
app.register("site-settings:main", siteSettings, { instantiate: false });
}
export default { export default {
name: "inject-discourse-objects", name: "inject-discourse-objects",
after: "discourse-bootstrap", after: "discourse-bootstrap",
initialize(container, app) { initialize(container, app) {
ALL_TARGETS.forEach((t) => registerObjects(container, app);
app.inject(t, "appEvents", "service:app-events")
);
// backwards compatibility: remove when plugins have updated let siteSettings = container.lookup("site-settings:main");
app.register("store:main", Store);
app.appEvents = container.lookup("service:app-events");
if (!app.hasRegistration("service:store")) {
app.register("service:store", Store);
ALL_TARGETS.forEach((t) => app.inject(t, "store", "service:store"));
}
app.register("message-bus:main", MessageBus, { instantiate: false });
ALL_TARGETS.concat("service").forEach((t) =>
app.inject(t, "messageBus", "message-bus:main")
);
const siteSettings = app.SiteSettings;
app.register("site-settings:main", siteSettings, { instantiate: false });
ALL_TARGETS.concat("service").forEach((t) =>
app.inject(t, "siteSettings", "site-settings:main")
);
const currentUser = User.current(); const currentUser = User.current();
app.register("current-user:main", currentUser, { instantiate: false }); app.register("current-user:main", currentUser, { instantiate: false });
app.currentUser = currentUser; app.currentUser = currentUser;
const topicTrackingState = TopicTrackingState.create({ const topicTrackingState = TopicTrackingState.create({
messageBus: MessageBus, messageBus: MessageBus,
siteSettings, siteSettings,
currentUser, currentUser,
}); });
app.register("topic-tracking-state:main", topicTrackingState, {
instantiate: false,
});
ALL_TARGETS.forEach((t) =>
app.inject(t, "topicTrackingState", "topic-tracking-state:main")
);
const site = Site.current(); const site = Site.current();
app.register("site:main", site, { instantiate: false }); app.register("site:main", site, { instantiate: false });
ALL_TARGETS.forEach((t) => app.inject(t, "site", "site:main"));
app.register("search-service:main", SearchService); app.register("topic-tracking-state:main", topicTrackingState, {
ALL_TARGETS.forEach((t) => instantiate: false,
app.inject(t, "searchService", "search-service:main") });
);
const session = Session.current(); const session = Session.current();
app.register("session:main", session, { instantiate: false }); app.register("session:main", session, { instantiate: false });
ALL_TARGETS.forEach((t) => app.inject(t, "session", "session:main"));
app.inject("service", "session", "session:main");
// TODO: Automatically register this service // TODO: Automatically register this service
const screenTrack = new ScreenTrack( const screenTrack = new ScreenTrack(
@ -82,16 +70,45 @@ export default {
); );
app.register("service:screen-track", screenTrack, { instantiate: false }); app.register("service:screen-track", screenTrack, { instantiate: false });
app.register("location:discourse-location", DiscourseLocation);
const keyValueStore = new KeyValueStore("discourse_");
app.register("key-value-store:main", keyValueStore, { instantiate: false });
app.register("search-service:main", SearchService);
ALL_TARGETS.forEach((t) => app.inject(t, "store", "service:store"));
ALL_TARGETS.forEach((t) =>
app.inject(t, "appEvents", "service:app-events")
);
ALL_TARGETS.concat("service").forEach((t) =>
app.inject(t, "messageBus", "message-bus:main")
);
ALL_TARGETS.concat("service").forEach((t) =>
app.inject(t, "siteSettings", "site-settings:main")
);
ALL_TARGETS.forEach((t) =>
app.inject(t, "topicTrackingState", "topic-tracking-state:main")
);
ALL_TARGETS.forEach((t) => app.inject(t, "site", "site:main"));
ALL_TARGETS.forEach((t) =>
app.inject(t, "searchService", "search-service:main")
);
ALL_TARGETS.forEach((t) => app.inject(t, "session", "session:main"));
app.inject("service", "session", "session:main");
if (currentUser) { if (currentUser) {
["component", "route", "controller", "service"].forEach((t) => { ["component", "route", "controller", "service"].forEach((t) => {
app.inject(t, "currentUser", "current-user:main"); app.inject(t, "currentUser", "current-user:main");
}); });
} }
app.register("location:discourse-location", DiscourseLocation);
const keyValueStore = new KeyValueStore("discourse_");
app.register("key-value-store:main", keyValueStore, { instantiate: false });
ALL_TARGETS.forEach((t) => ALL_TARGETS.forEach((t) =>
app.inject(t, "keyValueStore", "key-value-store:main") app.inject(t, "keyValueStore", "key-value-store:main")
); );

View File

@ -1,21 +1,27 @@
import Application from "@ember/application"; import Application from "@ember/application";
import { mapRoutes } from "discourse/mapping-router"; import Ember from "ember";
import { registerRouter } from "discourse/mapping-router";
let originalBuildInstance;
export default { export default {
name: "map-routes", name: "map-routes",
after: "inject-discourse-objects", after: "inject-discourse-objects",
initialize(container, app) { initialize(container, app) {
app.unregister("router:main"); let router = registerRouter(app);
app.register("router:main", mapRoutes()); container.registry.register("router:main", router);
// TODO: Remove this once we've upgraded Ember everywhere
if (Ember.VERSION.startsWith("3.12")) {
// HACK to fix: https://github.com/emberjs/ember.js/issues/10310 // HACK to fix: https://github.com/emberjs/ember.js/issues/10310
const originalBuildInstance = originalBuildInstance =
originalBuildInstance || Application.prototype.buildInstance; originalBuildInstance || Application.prototype.buildInstance;
Application.prototype.buildInstance = function () { Application.prototype.buildInstance = function () {
this.buildRegistry(); this.buildRegistry();
return originalBuildInstance.apply(this); return originalBuildInstance.apply(this);
}; };
}
}, },
}; };

View File

@ -203,6 +203,7 @@ export function acceptance(name, optionsOrCallback) {
resetSite(currentSettings(), siteChanges); resetSite(currentSettings(), siteChanges);
} }
getApplication().__registeredObjects__ = false;
getApplication().reset(); getApplication().reset();
this.container = getOwner(this); this.container = getOwner(this);
if (loggedIn) { if (loggedIn) {
@ -249,6 +250,7 @@ export function acceptance(name, optionsOrCallback) {
initializer.teardown(this.container); initializer.teardown(this.container);
} }
}); });
app.__registeredObjects__ = false;
app.reset(); app.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

View File

@ -1,11 +1,11 @@
import EmberObject from "@ember/object"; import EmberObject from "@ember/object";
import { discourseModule } from "discourse/tests/helpers/qunit-helpers"; import { discourseModule } from "discourse/tests/helpers/qunit-helpers";
import { mapRoutes } from "discourse/mapping-router"; import { registerRouter } from "discourse/mapping-router";
import { test } from "qunit"; import { test } from "qunit";
discourseModule("Unit | Controller | avatar-selector", function (hooks) { discourseModule("Unit | Controller | avatar-selector", function (hooks) {
hooks.beforeEach(function () { hooks.beforeEach(function () {
this.registry.register("router:main", mapRoutes()); registerRouter(this.registry);
}); });
test("avatarTemplate", function (assert) { test("avatarTemplate", function (assert) {