Skip to content

A platform for community discussion. Free, open, simple.

DEV: Allow hooks.beforeEach usage w/ component tests (#17360)

Jarek Radosz39c28cec8724
diff --git a/app/assets/javascripts/discourse/tests/helpers/component-test.js b/app/assets/javascripts/discourse/tests/helpers/component-test.js
index b3be51d2ecf..99396c2b618 100644
--- a/app/assets/javascripts/discourse/tests/helpers/component-test.js
+++ b/app/assets/javascripts/discourse/tests/helpers/component-test.js
@@ -6,9 +6,51 @@ import User from "discourse/models/user";
import { autoLoadModules } from "discourse/initializers/auto-load-modules";
import QUnit, { test } from "qunit";
-export { setupRenderingTest } from "ember-qunit";
+import { setupRenderingTest as emberSetupRenderingTest } from "ember-qunit";
+
+export function setupRenderingTest(hooks) {
+ emberSetupRenderingTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.site = Site.current();
+ this.session = Session.current();
+ this.container = this.owner;
+
+ const currentUser = User.create({
+ username: "eviltrout",
+ timezone: "Australia/Brisbane",
+ });
+ this.currentUser = currentUser;
+ this.owner.unregister("current-user:main");
+ this.owner.register("current-user:main", currentUser, {
+ instantiate: false,
+ });
+ this.owner.inject("component", "currentUser", "current-user:main");
+ this.owner.inject("service", "currentUser", "current-user:main");
+
+ this.owner.unregister("topic-tracking-state:main");
+ this.owner.register(
+ "topic-tracking-state:main",
+ TopicTrackingState.create({ currentUser }),
+ { instantiate: false }
+ );
+ this.owner.inject(
+ "service",
+ "topicTrackingState",
+ "topic-tracking-state:main"
+ );
+
+ autoLoadModules(this.owner, this.registry);
+
+ $.fn.autocomplete = function () {};
+ });
+}
+
+export default function (name, hooks, opts) {
+ if (opts === undefined) {
+ opts = hooks;
+ }
-export default function (name, opts) {
opts = opts || {};
if (opts.skip) {
@@ -25,48 +67,15 @@ 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");
-
- autoLoadModules(this.owner, this.registry);
-
- if (!opts.anonymous) {
- const currentUser = User.create({
- username: "eviltrout",
- timezone: "Australia/Brisbane",
- });
- this.currentUser = currentUser;
-
+ if (opts.anonymous) {
this.owner.unregister("current-user:main");
- this.owner.register("current-user:main", currentUser, {
- instantiate: false,
- });
-
- this.owner.inject("component", "currentUser", "current-user:main");
- this.owner.inject("service", "currentUser", "current-user:main");
-
- this.owner.unregister("topic-tracking-state:main");
- this.owner.register(
- "topic-tracking-state:main",
- TopicTrackingState.create({ currentUser }),
- { instantiate: false }
- );
-
- this.owner.inject(
- "service",
- "topicTrackingState",
- "topic-tracking-state:main"
- );
}
if (opts.beforeEach) {
+ const store = this.owner.lookup("service:store");
await opts.beforeEach.call(this, store);
}
- $.fn.autocomplete = function () {};
-
try {
await render(opts.template);
await opts.test.call(this, assert);
diff --git a/app/assets/javascripts/discourse/tests/integration/components/bookmark-icon-test.js b/app/assets/javascripts/discourse/tests/integration/components/bookmark-icon-test.js
index cbe228ec034..ffb9832fdac 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/bookmark-icon-test.js
+++ b/app/assets/javascripts/discourse/tests/integration/components/bookmark-icon-test.js
@@ -12,14 +12,13 @@ import {
query,
} from "discourse/tests/helpers/qunit-helpers";
-discourseModule("Component | bookmark-icon", function (hooks) {
+discourseModule("Integration | Component | bookmark-icon", function (hooks) {
setupRenderingTest(hooks);
componentTest("with reminder", {
template: hbs`{{bookmark-icon bookmark=bookmark}}`,
beforeEach() {
- this.currentUser.set("timezone", "Australia/Brisbane");
this.setProperties({
bookmark: Bookmark.create({
reminder_at: tomorrow(this.currentUser.timezone),
diff --git a/app/assets/javascripts/discourse/tests/integration/components/d-navigation-test.js b/app/assets/javascripts/discourse/tests/integration/components/d-navigation-test.js
index 54eedc875bf..50c748c264c 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/d-navigation-test.js
+++ b/app/assets/javascripts/discourse/tests/integration/components/d-navigation-test.js
@@ -8,6 +8,17 @@ import hbs from "htmlbars-inline-precompile";
discourseModule("Integration | Component | d-navigation", function (hooks) {
setupRenderingTest(hooks);
+ hooks.beforeEach(function () {
+ const categories = this.site.categoriesList
+ .filter((category) => !category.parent_category_id)
+ .slice(0, 4);
+ this.site.setProperties({ categories });
+ this.currentUser.set(
+ "indirectly_muted_category_ids",
+ categories.slice(0, 3).map((category) => category.id)
+ );
+ });
+
componentTest("filters indirectly muted categories", {
template: hbs`
{{d-navigation
@@ -15,19 +26,9 @@ discourseModule("Integration | Component | d-navigation", function (hooks) {
}}
`,
- beforeEach() {
- const categories = this.site.categoriesList
- .filter((category) => !category.parent_category_id)
- .slice(0, 4);
- this.site.setProperties({ categories });
- this.currentUser.set(
- "indirectly_muted_category_ids",
- categories.slice(0, 3).map((category) => category.id)
- );
- },
-
async test(assert) {
await click(".category-drop .select-kit-header-wrapper");
+
assert.strictEqual(
document.querySelectorAll(".category-row").length,
1,
diff --git a/app/assets/javascripts/discourse/tests/integration/components/site-header-test.js b/app/assets/javascripts/discourse/tests/integration/components/site-header-test.js
index 8f4ba6d2c9d..6574a061440 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/site-header-test.js
+++ b/app/assets/javascripts/discourse/tests/integration/components/site-header-test.js
@@ -13,14 +13,14 @@ import { click } from "@ember/test-helpers";
discourseModule("Integration | Component | site-header", function (hooks) {
setupRenderingTest(hooks);
+ hooks.beforeEach(function () {
+ this.currentUser.set("unread_high_priority_notifications", 1);
+ this.currentUser.set("read_first_notification", false);
+ });
+
componentTest("first notification mask", {
template: hbs`{{site-header}}`,
- beforeEach() {
- this.set("currentUser.unread_high_priority_notifications", 1);
- this.set("currentUser.read_first_notification", false);
- },
-
async test(assert) {
assert.strictEqual(
count(".ring-backdrop"),