DEV: A bunch of tests (like controller) weren't cleaning up

This creates a helper function with all the cleanup tasks we need to do
after tests, then makes sure to call it after tests that previously
weren't.

This fixes a lot of flakey tests.
This commit is contained in:
Robin Ward 2022-01-19 15:26:08 -05:00
parent 23aceedbd5
commit 2dc0f36e07
1 changed files with 43 additions and 49 deletions

View File

@ -13,7 +13,7 @@ import { forceMobile, resetMobile } from "discourse/lib/mobile";
import { getApplication, getContext, settled } from "@ember/test-helpers";
import { getOwner } from "discourse-common/lib/get-owner";
import { later, run } from "@ember/runloop";
import { moduleFor, setupApplicationTest } from "ember-qunit";
import { setupApplicationTest } from "ember-qunit";
import { Promise } from "rsvp";
import Site from "discourse/models/site";
import User from "discourse/models/user";
@ -25,7 +25,6 @@ import { flushMap } from "discourse/services/store";
import { initSearchData } from "discourse/widgets/search-menu";
import { resetPostMenuExtraButtons } from "discourse/widgets/post-menu";
import { isEmpty } from "@ember/utils";
import { mapRoutes } from "discourse/mapping-router";
import { resetCustomPostMessageCallbacks } from "discourse/controllers/topic";
import { resetDecorators } from "discourse/widgets/widget";
import { resetCache as resetOneboxCache } from "pretty-text/oneboxer";
@ -118,18 +117,43 @@ export function applyPretender(name, server, helper) {
}
}
export function controllerModule(name, args = {}) {
moduleFor(name, name, {
setup() {
this.registry.register("router:main", mapRoutes());
let controller = this.subject();
controller.siteSettings = currentSettings();
if (args.setupController) {
args.setupController(controller);
}
},
needs: args.needs,
});
// Add clean up code here to run after every test
function testCleanup() {
flushMap();
localStorage.clear();
User.resetCurrent();
resetExtraClasses();
clearOutletCache();
clearHTMLCache();
clearRewrites();
initSearchData();
resetDecorators();
resetPostCookedDecorators();
resetPluginOutletDecorators();
resetTopicTitleDecorators();
resetUsernameDecorators();
resetOneboxCache();
resetCustomPostMessageCallbacks();
resetUserSearchCache();
resetCardClickListenerSelector();
resetComposerCustomizations();
resetQuickSearchRandomTips();
resetPostMenuExtraButtons();
clearNavItems();
setTopicList(null);
_clearSnapshots();
cleanUpComposerUploadHandler();
cleanUpComposerUploadMarkdownResolver();
cleanUpComposerUploadPreProcessor();
clearTopicFooterDropdowns();
clearTopicFooterButtons();
clearDesktopNotificationHandlers();
resetLastEditNotificationClick();
clearAuthMethods();
setTestPresence(true);
if (!LEGACY_ENV) {
clearPresenceCallbacks();
}
}
export function discourseModule(name, options) {
@ -147,6 +171,9 @@ export function discourseModule(name, options) {
this.siteSettings = currentSettings();
clearResolverOptions();
});
hooks.afterEach(function () {
testCleanup();
});
this.getController = function (controllerName, properties) {
let controller = this.container.lookup(`controller:${controllerName}`);
@ -180,6 +207,7 @@ export function discourseModule(name, options) {
if (options && options.afterEach) {
options.afterEach.call(this);
}
testCleanup();
},
});
}
@ -270,41 +298,7 @@ export function acceptance(name, optionsOrCallback) {
if (options && options.afterEach) {
options.afterEach.call(this);
}
flushMap();
localStorage.clear();
User.resetCurrent();
resetExtraClasses();
clearOutletCache();
clearHTMLCache();
clearRewrites();
initSearchData();
resetDecorators();
resetPostCookedDecorators();
resetPluginOutletDecorators();
resetTopicTitleDecorators();
resetUsernameDecorators();
resetOneboxCache();
resetCustomPostMessageCallbacks();
resetUserSearchCache();
resetCardClickListenerSelector();
resetComposerCustomizations();
resetQuickSearchRandomTips();
resetPostMenuExtraButtons();
clearNavItems();
setTopicList(null);
_clearSnapshots();
cleanUpComposerUploadHandler();
cleanUpComposerUploadMarkdownResolver();
cleanUpComposerUploadPreProcessor();
clearTopicFooterDropdowns();
clearTopicFooterButtons();
clearDesktopNotificationHandlers();
resetLastEditNotificationClick();
clearAuthMethods();
setTestPresence(true);
if (!LEGACY_ENV) {
clearPresenceCallbacks();
}
testCleanup();
app._runInitializer("instanceInitializers", (_, initializer) => {
initializer.teardown?.();