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

This commit is contained in:
Jarek Radosz 2022-07-07 09:57:38 +02:00 committed by GitHub
parent 95c257e2ea
commit 39c28cec87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 55 deletions

View File

@ -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);

View File

@ -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),

View File

@ -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,

View File

@ -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"),