DEV: Refactor Site creation in tests (#15707)

Previously, `resetSite()` would immediately generate a new `Site` instance, and run all the initialization logic within the model. This included initializing Category objects.

This was problematic because `resetSite()` is called before any initializers have been run. That means that any modifications to the Site or Category classes would not have any effect on the already-initialized Site/Category instances.

This commit makes two main changes so so that the test environment is more production-like:

1. Update `resetSite` so that it simply stores the new data in the PreloadStore, and destroys the old Site instance. Initialization of a new site instance happens 'just in time' (normally during the `inject-discourse-objects` initializer)

2. Update the `helperContext` in tests to use getters. This avoids the need to look up `Site.current()` before initializers have run

It also makes a minor adjustment to one test which was relying on a side-effect of the previous behavior.

This should resolve the failing tests for discourse-category-expert under Ember-CLI: https://github.com/discourse/discourse-category-experts/pull/69
This commit is contained in:
David Taylor 2022-02-03 10:02:47 +00:00 committed by GitHub
parent 76022132f7
commit 84c2c2f477
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 6 deletions

View File

@ -72,18 +72,21 @@ acceptance(
id: 1,
name: "test won",
slug: "test-won",
permission: 1,
topic_template: null,
},
{
id: 2,
name: "test too",
slug: "test-too",
permission: 1,
topic_template: "",
},
{
id: 3,
name: "test free",
slug: "test-free",
permission: 1,
topic_template: null,
},
],

View File

@ -5,6 +5,7 @@ import TopicListAdapter from "discourse/adapters/topic-list";
import TopicTrackingState from "discourse/models/topic-tracking-state";
import { buildResolver } from "discourse-common/resolver";
import { currentSettings } from "discourse/tests/helpers/site-settings";
import Site from "discourse/models/site";
const CatAdapter = RestAdapter.extend({
primaryKey: "cat_id",
@ -13,6 +14,10 @@ const CatAdapter = RestAdapter.extend({
export default function (customLookup = () => {}) {
const resolver = buildResolver("discourse").create();
// Normally this would happen in inject-discourse-objects.
// However, `create-store` is used by unit tests which do not init the application.
Site.current();
return Store.create({
register: {
lookup(type) {

View File

@ -19,7 +19,6 @@ import Site from "discourse/models/site";
import User from "discourse/models/user";
import { _clearSnapshots } from "select-kit/components/composer-actions";
import { clearHTMLCache } from "discourse/helpers/custom-html";
import createStore from "discourse/tests/helpers/create-store";
import deprecated from "discourse-common/lib/deprecated";
import { restoreBaseUri } from "discourse-common/lib/get-url";
import { flushMap } from "discourse/services/store";
@ -58,6 +57,7 @@ import {
clearPresenceCallbacks,
setTestPresence,
} from "discourse/lib/user-presence";
import PreloadStore from "discourse/lib/preload-store";
const LEGACY_ENV = !setupApplicationTest;
@ -116,9 +116,9 @@ export function resetSite(siteSettings, extras) {
siteFixtures["site.json"].site,
extras || {}
);
siteAttrs.store = createStore();
siteAttrs.siteSettings = siteSettings;
return Site.resetCurrent(Site.create(siteAttrs));
PreloadStore.store("site", siteAttrs);
Site.resetCurrent();
}
export function applyPretender(name, server, helper) {

View File

@ -25,6 +25,7 @@ import QUnit from "qunit";
import { ScrollingDOMMethods } from "discourse/mixins/scrolling";
import Session from "discourse/models/session";
import User from "discourse/models/user";
import Site from "discourse/models/site";
import bootbox from "bootbox";
import { buildResolver } from "discourse-common/resolver";
import { createHelperContext } from "discourse-common/lib/helpers";
@ -319,15 +320,28 @@ function setupTestsCommon(application, container, config) {
session.highlightJsPath = setupData.highlightJsPath;
}
User.resetCurrent();
let site = resetSite(settings);
createHelperContext({
siteSettings: settings,
get siteSettings() {
if (isLegacyEmber() && container.isDestroyed) {
return settings;
} else {
return container.lookup("site-settings:main");
}
},
capabilities: {},
site,
get site() {
if (isLegacyEmber() && container.isDestroyed) {
return Site.current();
} else {
return container.lookup("site:main") || Site.current();
}
},
registry: app.__registry__,
});
PreloadStore.reset();
resetSite(settings);
sinon.stub(ScrollingDOMMethods, "screenNotFull");
sinon.stub(ScrollingDOMMethods, "bindOnScroll");