diff --git a/app/assets/javascripts/discourse/tests/helpers/component-test.js b/app/assets/javascripts/discourse/tests/helpers/component-test.js index b3ea05ff343..b3be51d2ecf 100644 --- a/app/assets/javascripts/discourse/tests/helpers/component-test.js +++ b/app/assets/javascripts/discourse/tests/helpers/component-test.js @@ -1,48 +1,12 @@ -/* global andThen */ - -import { TestModuleForComponent, render } from "@ember/test-helpers"; -import MessageBus from "message-bus-client"; -import EmberObject from "@ember/object"; -import { setupRenderingTest as EmberSetupRenderingTest } from "ember-qunit"; +import { render } from "@ember/test-helpers"; import Session from "discourse/models/session"; import Site from "discourse/models/site"; import TopicTrackingState from "discourse/models/topic-tracking-state"; import User from "discourse/models/user"; import { autoLoadModules } from "discourse/initializers/auto-load-modules"; -import createStore from "discourse/tests/helpers/create-store"; -import { currentSettings } from "discourse/tests/helpers/site-settings"; import QUnit, { test } from "qunit"; -import KeyValueStore from "discourse/lib/key-value-store"; -const LEGACY_ENV = !EmberSetupRenderingTest; - -export function setupRenderingTest(hooks) { - if (!LEGACY_ENV) { - return EmberSetupRenderingTest.apply(this, arguments); - } - - let testModule; - - hooks.before(function () { - const name = this.moduleName.split("|").pop(); - testModule = new TestModuleForComponent(name, { - integration: true, - }); - }); - - hooks.beforeEach(function () { - testModule.setContext(this); - return testModule.setup(...arguments); - }); - - hooks.afterEach(function () { - return testModule.teardown(...arguments); - }); - - hooks.after(function () { - testModule = null; - }); -} +export { setupRenderingTest } from "ember-qunit"; export default function (name, opts) { opts = opts || {}; @@ -51,7 +15,7 @@ export default function (name, opts) { return; } - if (typeof opts.template === "string" && !LEGACY_ENV) { + if (typeof opts.template === "string") { let testName = QUnit.config.currentModule.name + " " + name; // eslint-disable-next-line console.warn( @@ -63,61 +27,10 @@ export default function (name, opts) { test(name, async function (assert) { this.site = Site.current(); this.session = Session.current(); + this.container = this.owner; + const store = this.owner.lookup("service:store"); - let owner = LEGACY_ENV ? this.registry : this.owner; - let store; - - if (LEGACY_ENV) { - this.registry.register("site-settings:main", currentSettings(), { - instantiate: false, - }); - this.registry.register("capabilities:main", EmberObject); - this.registry.register("message-bus:main", MessageBus, { - instantiate: false, - }); - this.registry.register("site:main", this.site, { instantiate: false }); - this.registry.register("session:main", this.session, { - instantiate: false, - }); - const keyValueStore = new KeyValueStore("discourse_"); - this.registry.register("key-value-store:main", keyValueStore, { - instantiate: false, - }); - - this.registry.injection( - "component", - "siteSettings", - "site-settings:main" - ); - this.registry.injection("component", "appEvents", "service:app-events"); - this.registry.injection("component", "capabilities", "capabilities:main"); - this.registry.injection("component", "site", "site:main"); - this.registry.injection("component", "session", "session:main"); - this.registry.injection("component", "messageBus", "message-bus:main"); - this.registry.injection( - "component", - "keyValueStore", - "key-value-store:main" - ); - - this.registry.injection("service", "session", "session:main"); - this.registry.injection("service", "messageBus", "message-bus:main"); - this.registry.injection("service", "siteSettings", "site-settings:main"); - this.registry.injection( - "service", - "keyValueStore", - "key-value-store:main" - ); - - this.siteSettings = currentSettings(); - store = createStore(); - this.registry.register("service:store", store, { instantiate: false }); - } else { - this.container = owner; - store = this.container.lookup("service:store"); - } - - autoLoadModules(this.container, this.registry); + autoLoadModules(this.owner, this.registry); if (!opts.anonymous) { const currentUser = User.create({ @@ -126,58 +39,41 @@ export default function (name, opts) { }); this.currentUser = currentUser; - owner.unregister("current-user:main"); - owner.register("current-user:main", currentUser, { + this.owner.unregister("current-user:main"); + this.owner.register("current-user:main", currentUser, { instantiate: false, }); - if (LEGACY_ENV) { - owner.injection("component", "currentUser", "current-user:main"); - owner.injection("service", "currentUser", "current-user:main"); - } else { - owner.inject("component", "currentUser", "current-user:main"); - owner.inject("service", "currentUser", "current-user:main"); - } + this.owner.inject("component", "currentUser", "current-user:main"); + this.owner.inject("service", "currentUser", "current-user:main"); - owner.unregister("topic-tracking-state:main"); - owner.register( + this.owner.unregister("topic-tracking-state:main"); + this.owner.register( "topic-tracking-state:main", TopicTrackingState.create({ currentUser }), { instantiate: false } ); - if (LEGACY_ENV) { - owner.injection( - "service", - "topicTrackingState", - "topic-tracking-state:main" - ); - } else { - owner.inject( - "service", - "topicTrackingState", - "topic-tracking-state:main" - ); - } + this.owner.inject( + "service", + "topicTrackingState", + "topic-tracking-state:main" + ); } if (opts.beforeEach) { - opts.beforeEach.call(this, store); + await opts.beforeEach.call(this, store); } $.fn.autocomplete = function () {}; - andThen(() => { - return LEGACY_ENV ? this.render(opts.template) : render(opts.template); - }); - andThen(() => { - return opts.test.call(this, assert); - }).finally(async () => { + try { + await render(opts.template); + await opts.test.call(this, assert); + } finally { if (opts.afterEach) { - await andThen(() => { - return opts.afterEach.call(opts); - }); + await opts.afterEach.call(opts); } - }); + } }); } diff --git a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js index 908a01511a5..bde6d6577db 100644 --- a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js +++ b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js @@ -70,8 +70,6 @@ import { clearTextDecorateCallbacks, } from "discourse/lib/to-markdown"; -const LEGACY_ENV = !setupApplicationTest; - export function currentUser() { return User.create(sessionFixtures["/session/current.json"].current_user); } @@ -188,9 +186,7 @@ function testCleanup(container, app) { resetLastEditNotificationClick(); clearAuthMethods(); setTestPresence(true); - if (!LEGACY_ENV) { - clearPresenceCallbacks(); - } + clearPresenceCallbacks(); restoreBaseUri(); resetTopicsSectionLinks(); clearTagDecorateCallbacks(); @@ -217,9 +213,7 @@ export function discourseModule(name, options) { this.getController = function (controllerName, properties) { let controller = this.container.lookup(`controller:${controllerName}`); - if (!LEGACY_ENV) { - controller.application = {}; - } + controller.application = {}; controller.siteSettings = this.siteSettings; if (properties) { controller.setProperties(properties); @@ -304,16 +298,7 @@ export function acceptance(name, optionsOrCallback) { resetSite(currentSettings(), siteChanges); - if (LEGACY_ENV) { - getApplication().__registeredObjects__ = false; - getApplication().reset(); - } this.container = getOwner(this); - if (LEGACY_ENV && loggedIn) { - updateCurrentUser({ - appEvents: this.container.lookup("service:app-events"), - }); - } if (!this.owner) { this.owner = this.container; @@ -330,11 +315,6 @@ export function acceptance(name, optionsOrCallback) { options?.afterEach?.call(this); testCleanup(this.container, app); - if (LEGACY_ENV) { - app.__registeredObjects__ = false; - app.reset(); - } - // We do this after reset so that the willClearRender will have already fired resetWidgetCleanCallbacks(); }, @@ -380,18 +360,16 @@ export function acceptance(name, optionsOrCallback) { hooks.afterEach(setup.afterEach); callback(needs); - if (!LEGACY_ENV && getContext) { - setupApplicationTest(hooks); + setupApplicationTest(hooks); - hooks.beforeEach(function () { - // This hack seems necessary to allow `DiscourseURL` to use the testing router - let ctx = getContext(); - this.container.registry.unregister("router:main"); - this.container.registry.register("router:main", ctx.owner.router, { - instantiate: false, - }); + hooks.beforeEach(function () { + // This hack seems necessary to allow `DiscourseURL` to use the testing router + let ctx = getContext(); + this.container.registry.unregister("router:main"); + this.container.registry.register("router:main", ctx.owner.router, { + instantiate: false, }); - } + }); }); } else { // Old way @@ -523,25 +501,7 @@ export async function selectText(selector, endOffset = null) { selection.addRange(range); }; - if (LEGACY_ENV) { - // In the Ember CLI environment, the settled() helper seems to take care of waiting - // for this event to fire. In legacy, we need to do it manually. - let callback; - const selectEventFiredPromise = new Promise((resolve) => { - callback = resolve; - document.addEventListener("selectionchange", callback); - }); - - performSelection(); - - try { - await selectEventFiredPromise; - } finally { - document.removeEventListener("selectionchange", callback); - } - } else { - performSelection(); - } + performSelection(); await settled(); } diff --git a/app/assets/javascripts/discourse/tests/integration/components/d-editor-test.js b/app/assets/javascripts/discourse/tests/integration/components/d-editor-test.js index 02a5af80fcd..0f62df37c39 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/d-editor-test.js +++ b/app/assets/javascripts/discourse/tests/integration/components/d-editor-test.js @@ -98,9 +98,9 @@ discourseModule("Integration | Component | d-editor", function (hooks) { beforeEach() { this.set("value", "hello world."); }, - test(assert) { + async test(assert) { const textarea = jumpEnd(query("textarea.d-editor-input")); - testFunc.call(this, assert, textarea); + await testFunc.call(this, assert, textarea); }, skip: !navigator.userAgent.includes("Chrome"), }); @@ -113,9 +113,9 @@ discourseModule("Integration | Component | d-editor", function (hooks) { this.set("value", "hello world."); }, - test(assert) { + async test(assert) { const textarea = jumpEnd(query("textarea.d-editor-input")); - testFunc.call(this, assert, textarea); + await testFunc.call(this, assert, textarea); }, }); } diff --git a/app/assets/javascripts/discourse/tests/setup-tests.js b/app/assets/javascripts/discourse/tests/setup-tests.js index d441da9f1f6..6d7d0943f41 100644 --- a/app/assets/javascripts/discourse/tests/setup-tests.js +++ b/app/assets/javascripts/discourse/tests/setup-tests.js @@ -10,10 +10,7 @@ import pretender, { pretenderHelpers, resetPretender, } from "discourse/tests/helpers/create-pretender"; -import { - currentSettings, - resetSettings, -} from "discourse/tests/helpers/site-settings"; +import { resetSettings } from "discourse/tests/helpers/site-settings"; import { setDefaultOwner } from "discourse-common/lib/get-owner"; import { setApplication, setResolver } from "@ember/test-helpers"; import { setupS3CDN, setupURL } from "discourse-common/lib/get-url"; @@ -394,20 +391,6 @@ function setupTestsCommon(application, container, config) { resetSite(); } -export function setupTestsLegacy(application) { - app = application; - setResolver(buildResolver("discourse").create({ namespace: app })); - setupTestsCommon(application, app.__container__); - - app.instanceInitializer({ - name: "test-helper", - initialize: testsInitialized, - teardown: testsTornDown, - }); - app.SiteSettings = currentSettings(); - app.start(); -} - export default function setupTests(config) { let settings = resetSettings(); app = createApplication(config, settings); diff --git a/app/assets/javascripts/discourse/tests/test_starter.js b/app/assets/javascripts/discourse/tests/test_starter.js deleted file mode 100644 index 067e890fc16..00000000000 --- a/app/assets/javascripts/discourse/tests/test_starter.js +++ /dev/null @@ -1,115 +0,0 @@ -// discourse-skip-module - -document.body.insertAdjacentHTML( - "afterbegin", - ` -