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 { autoLoadModules } from "discourse/initializers/auto-load-modules";
import QUnit, { test } from "qunit"; 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 || {}; opts = opts || {};
if (opts.skip) { if (opts.skip) {
@ -25,48 +67,15 @@ export default function (name, opts) {
} }
test(name, async function (assert) { test(name, async function (assert) {
this.site = Site.current(); if (opts.anonymous) {
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;
this.owner.unregister("current-user:main"); 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) { if (opts.beforeEach) {
const store = this.owner.lookup("service:store");
await opts.beforeEach.call(this, store); await opts.beforeEach.call(this, store);
} }
$.fn.autocomplete = function () {};
try { try {
await render(opts.template); await render(opts.template);
await opts.test.call(this, assert); await opts.test.call(this, assert);

View File

@ -12,14 +12,13 @@ import {
query, query,
} from "discourse/tests/helpers/qunit-helpers"; } from "discourse/tests/helpers/qunit-helpers";
discourseModule("Component | bookmark-icon", function (hooks) { discourseModule("Integration | Component | bookmark-icon", function (hooks) {
setupRenderingTest(hooks); setupRenderingTest(hooks);
componentTest("with reminder", { componentTest("with reminder", {
template: hbs`{{bookmark-icon bookmark=bookmark}}`, template: hbs`{{bookmark-icon bookmark=bookmark}}`,
beforeEach() { beforeEach() {
this.currentUser.set("timezone", "Australia/Brisbane");
this.setProperties({ this.setProperties({
bookmark: Bookmark.create({ bookmark: Bookmark.create({
reminder_at: tomorrow(this.currentUser.timezone), 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) { discourseModule("Integration | Component | d-navigation", function (hooks) {
setupRenderingTest(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", { componentTest("filters indirectly muted categories", {
template: hbs` template: hbs`
{{d-navigation {{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) { async test(assert) {
await click(".category-drop .select-kit-header-wrapper"); await click(".category-drop .select-kit-header-wrapper");
assert.strictEqual( assert.strictEqual(
document.querySelectorAll(".category-row").length, document.querySelectorAll(".category-row").length,
1, 1,

View File

@ -13,14 +13,14 @@ import { click } from "@ember/test-helpers";
discourseModule("Integration | Component | site-header", function (hooks) { discourseModule("Integration | Component | site-header", function (hooks) {
setupRenderingTest(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", { componentTest("first notification mask", {
template: hbs`{{site-header}}`, template: hbs`{{site-header}}`,
beforeEach() {
this.set("currentUser.unread_high_priority_notifications", 1);
this.set("currentUser.read_first_notification", false);
},
async test(assert) { async test(assert) {
assert.strictEqual( assert.strictEqual(
count(".ring-backdrop"), count(".ring-backdrop"),