From b460a6d059b87b133410f2a6e77b1d5c7cc82cb3 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 15 Oct 2020 13:56:19 -0400 Subject: [PATCH] REFACTOR: Continue to converge on what Ember CLI wants us to do * The creation of a testing div is specific to Rails, so that is moved back out of setupTests(); * We've removed the `Discourse` globals from the acceptance helpers in favor of `setApplication`/`getApplication`. * We pass the container to setupTests because there is no `__container__` in later Ember versions. * `App` is now `app` because it's not a constant or class, it's an instance of an application. --- .../discourse/tests/helpers/qunit-helpers.js | 17 +++++++------ .../discourse/tests/setup-tests.js | 24 +++++++------------ .../discourse/tests/test_helper.js | 12 ++++++++-- app/assets/javascripts/test-shims.js | 7 ++++++ 4 files changed, 34 insertions(+), 26 deletions(-) diff --git a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js index f69f7361fed..be98a1faeba 100644 --- a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js +++ b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js @@ -38,6 +38,7 @@ import QUnit, { module } from "qunit"; import siteFixtures from "discourse/tests/fixtures/site-fixtures"; import Site from "discourse/models/site"; import createStore from "discourse/tests/helpers/create-store"; +import { getApplication } from "@ember/test-helpers"; export function currentUser() { return User.create(sessionFixtures["/session/current.json"].current_user); @@ -201,7 +202,7 @@ export function acceptance(name, options) { resetSite(currentSettings(), options.site); } - Discourse.reset(); + getApplication().reset(); this.container = getOwner(this); setURLContainer(this.container); setDefaultOwner(this.container); @@ -212,6 +213,7 @@ export function acceptance(name, options) { }, afterEach() { + let app = getApplication(); if (options && options.afterEach) { options.afterEach.call(this); } @@ -236,15 +238,12 @@ export function acceptance(name, options) { _clearSnapshots(); setURLContainer(null); setDefaultOwner(null); - Discourse._runInitializer( - "instanceInitializers", - (initName, initializer) => { - if (initializer && initializer.teardown) { - initializer.teardown(this.container); - } + app._runInitializer("instanceInitializers", (initName, initializer) => { + if (initializer && initializer.teardown) { + initializer.teardown(this.container); } - ); - Discourse.reset(); + }); + app.reset(); // We do this after reset so that the willClearRender will have already fired resetWidgetCleanCallbacks(); diff --git a/app/assets/javascripts/discourse/tests/setup-tests.js b/app/assets/javascripts/discourse/tests/setup-tests.js index befe6b64500..2e001d12810 100644 --- a/app/assets/javascripts/discourse/tests/setup-tests.js +++ b/app/assets/javascripts/discourse/tests/setup-tests.js @@ -25,10 +25,10 @@ import QUnit from "qunit"; import MessageBus from "message-bus-client"; import deprecated from "discourse-common/lib/deprecated"; import sinon from "sinon"; -import { setResolver } from "@ember/test-helpers"; +import { setApplication, setResolver } from "@ember/test-helpers"; -export default function setupTests(App) { - setResolver(buildResolver("discourse").create({ namespace: App })); +export default function setupTests(app, container) { + setResolver(buildResolver("discourse").create({ namespace: app })); sinon.config = { injectIntoThis: false, @@ -41,17 +41,10 @@ export default function setupTests(App) { // Stop the message bus so we don't get ajax calls MessageBus.stop(); - document.write( - '
' - ); - document.write( - "" - ); - - App.rootElement = "#ember-testing"; - App.setupForTesting(); - App.SiteSettings = currentSettings(); - App.start(); + app.rootElement = "#ember-testing"; + app.setupForTesting(); + app.SiteSettings = currentSettings(); + app.start(); // disable logster error reporting if (window.Logster) { @@ -185,6 +178,7 @@ export default function setupTests(App) { // forces 0 as duration for all jquery animations jQuery.fx.off = true; - setDefaultOwner(App.__container__); + setApplication(app); + setDefaultOwner(container); resetSite(); } diff --git a/app/assets/javascripts/discourse/tests/test_helper.js b/app/assets/javascripts/discourse/tests/test_helper.js index d0e3c629e62..610c363a95e 100644 --- a/app/assets/javascripts/discourse/tests/test_helper.js +++ b/app/assets/javascripts/discourse/tests/test_helper.js @@ -43,6 +43,14 @@ //= require test-shims //= require jquery.magnific-popup.min.js -Discourse.injectTestHelpers(); +document.write( + '
' +); +document.write( + "" +); + +let app = window.Discourse; +app.injectTestHelpers(); let setupTests = require("discourse/tests/setup-tests").default; -setupTests(window.Discourse); +setupTests(app, app.__container__); diff --git a/app/assets/javascripts/test-shims.js b/app/assets/javascripts/test-shims.js index 9e2664bc523..4ae82951e0e 100644 --- a/app/assets/javascripts/test-shims.js +++ b/app/assets/javascripts/test-shims.js @@ -17,9 +17,16 @@ define("ember-qunit", () => { moduleForComponent: window.moduleForComponent, }; }); +let _app; define("@ember/test-helpers", () => { return { setResolver: window.setResolver, + setApplication(app) { + _app = app; + }, + getApplication() { + return _app; + }, visit() { return window.visit(...arguments); },