2015-09-04 04:55:55 +08:00
|
|
|
import createStore from "helpers/create-store";
|
2020-07-10 03:54:53 +08:00
|
|
|
import { discourseModule } from "helpers/qunit-helpers";
|
2020-07-18 02:03:59 +08:00
|
|
|
import Site from "discourse/models/site";
|
2015-08-12 00:27:07 +08:00
|
|
|
|
2020-07-10 03:54:53 +08:00
|
|
|
discourseModule("lib:category-link");
|
2015-01-21 00:36:28 +08:00
|
|
|
|
|
|
|
import { categoryBadgeHTML } from "discourse/helpers/category-link";
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("categoryBadge without a category", (assert) => {
|
|
|
|
assert.blank(categoryBadgeHTML(), "it returns no HTML");
|
2015-01-21 00:36:28 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("Regular categoryBadge", (assert) => {
|
2015-09-04 04:55:55 +08:00
|
|
|
const store = createStore();
|
|
|
|
const category = store.createRecord("category", {
|
|
|
|
name: "hello",
|
|
|
|
id: 123,
|
|
|
|
description_text: "cool description",
|
|
|
|
color: "ff0",
|
|
|
|
text_color: "f00",
|
|
|
|
});
|
2019-04-10 17:21:22 +08:00
|
|
|
const tag = $.parseHTML(categoryBadgeHTML(category))[0];
|
2015-01-21 00:36:28 +08:00
|
|
|
|
2019-04-10 17:21:22 +08:00
|
|
|
assert.equal(tag.tagName, "A", "it creates a `a` wrapper tag");
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(
|
2019-04-10 17:21:22 +08:00
|
|
|
tag.className.trim(),
|
2017-06-15 01:57:58 +08:00
|
|
|
"badge-wrapper",
|
|
|
|
"it has the correct class"
|
|
|
|
);
|
2015-01-21 00:36:28 +08:00
|
|
|
|
2015-09-04 04:55:55 +08:00
|
|
|
const label = tag.children[1];
|
2019-04-10 17:21:22 +08:00
|
|
|
assert.equal(label.title, "cool description", "it has the correct title");
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(
|
2019-04-10 17:21:22 +08:00
|
|
|
label.children[0].innerText,
|
2018-01-11 20:43:05 +08:00
|
|
|
"hello",
|
|
|
|
"it has the category name"
|
|
|
|
);
|
2015-01-21 00:36:28 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("undefined color", (assert) => {
|
2015-09-04 04:55:55 +08:00
|
|
|
const store = createStore();
|
|
|
|
const noColor = store.createRecord("category", { name: "hello", id: 123 });
|
2019-04-10 17:21:22 +08:00
|
|
|
const tag = $.parseHTML(categoryBadgeHTML(noColor))[0];
|
2015-01-21 00:36:28 +08:00
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.blank(
|
2019-04-10 17:21:22 +08:00
|
|
|
tag.attributes["style"],
|
2017-06-15 01:57:58 +08:00
|
|
|
"it has no color style because there are no colors"
|
|
|
|
);
|
2015-01-21 00:36:28 +08:00
|
|
|
});
|
|
|
|
|
2020-02-13 03:34:16 +08:00
|
|
|
QUnit.test("topic count", (assert) => {
|
|
|
|
const store = createStore();
|
|
|
|
const category = store.createRecord("category", { name: "hello", id: 123 });
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
categoryBadgeHTML(category).indexOf("topic-count"),
|
|
|
|
-1,
|
|
|
|
"it does not include topic count by default"
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
categoryBadgeHTML(category, { topicCount: 20 }).indexOf("topic-count") > 20,
|
|
|
|
"is included when specified"
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("allowUncategorized", (assert) => {
|
2015-09-04 04:55:55 +08:00
|
|
|
const store = createStore();
|
|
|
|
const uncategorized = store.createRecord("category", {
|
|
|
|
name: "uncategorized",
|
|
|
|
id: 345,
|
|
|
|
});
|
2020-07-18 02:03:59 +08:00
|
|
|
|
2015-01-21 00:36:28 +08:00
|
|
|
sandbox
|
2020-07-18 02:03:59 +08:00
|
|
|
.stub(Site, "currentProp")
|
2015-01-21 00:36:28 +08:00
|
|
|
.withArgs("uncategorized_category_id")
|
|
|
|
.returns(345);
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.blank(
|
|
|
|
categoryBadgeHTML(uncategorized),
|
|
|
|
"it doesn't return HTML for uncategorized by default"
|
|
|
|
);
|
|
|
|
assert.present(
|
|
|
|
categoryBadgeHTML(uncategorized, { allowUncategorized: true }),
|
|
|
|
"it returns HTML"
|
|
|
|
);
|
2017-12-15 12:58:20 +08:00
|
|
|
});
|
2018-01-30 09:42:19 +08:00
|
|
|
|
2020-07-10 03:54:53 +08:00
|
|
|
QUnit.test("category names are wrapped in dir-spans", function (assert) {
|
|
|
|
this.siteSettings.support_mixed_text_direction = true;
|
2018-01-30 09:42:19 +08:00
|
|
|
const store = createStore();
|
|
|
|
const rtlCategory = store.createRecord("category", {
|
|
|
|
name: "תכנות עם Ruby",
|
|
|
|
id: 123,
|
|
|
|
description_text: "cool description",
|
|
|
|
color: "ff0",
|
|
|
|
text_color: "f00",
|
|
|
|
});
|
|
|
|
|
|
|
|
const ltrCategory = store.createRecord("category", {
|
|
|
|
name: "Programming in Ruby",
|
|
|
|
id: 234,
|
|
|
|
});
|
|
|
|
|
2019-04-10 17:21:22 +08:00
|
|
|
let tag = $.parseHTML(categoryBadgeHTML(rtlCategory))[0];
|
2018-01-30 09:42:19 +08:00
|
|
|
let dirSpan = tag.children[1].children[0];
|
2019-04-10 17:21:22 +08:00
|
|
|
assert.equal(dirSpan.dir, "rtl");
|
2018-01-30 09:42:19 +08:00
|
|
|
|
2019-04-10 17:21:22 +08:00
|
|
|
tag = $.parseHTML(categoryBadgeHTML(ltrCategory))[0];
|
2018-01-30 09:42:19 +08:00
|
|
|
dirSpan = tag.children[1].children[0];
|
2019-04-10 17:21:22 +08:00
|
|
|
assert.equal(dirSpan.dir, "ltr");
|
2018-01-30 09:42:19 +08:00
|
|
|
});
|
2020-01-19 19:07:54 +08:00
|
|
|
|
2020-07-10 03:54:53 +08:00
|
|
|
QUnit.test("recursive", function (assert) {
|
2020-01-19 19:07:54 +08:00
|
|
|
const store = createStore();
|
|
|
|
|
|
|
|
const foo = store.createRecord("category", {
|
|
|
|
name: "foo",
|
|
|
|
id: 1,
|
|
|
|
});
|
|
|
|
|
|
|
|
const bar = store.createRecord("category", {
|
|
|
|
name: "bar",
|
|
|
|
id: 2,
|
|
|
|
parent_category_id: foo.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
const baz = store.createRecord("category", {
|
|
|
|
name: "baz",
|
|
|
|
id: 3,
|
|
|
|
parent_category_id: bar.id,
|
|
|
|
});
|
|
|
|
|
2020-07-10 03:54:53 +08:00
|
|
|
this.siteSettings.max_category_nesting = 0;
|
2020-01-19 19:07:54 +08:00
|
|
|
assert.ok(categoryBadgeHTML(baz, { recursive: true }).indexOf("baz") !== -1);
|
|
|
|
assert.ok(categoryBadgeHTML(baz, { recursive: true }).indexOf("bar") === -1);
|
|
|
|
|
2020-07-10 03:54:53 +08:00
|
|
|
this.siteSettings.max_category_nesting = 1;
|
2020-01-19 19:07:54 +08:00
|
|
|
assert.ok(categoryBadgeHTML(baz, { recursive: true }).indexOf("baz") !== -1);
|
|
|
|
assert.ok(categoryBadgeHTML(baz, { recursive: true }).indexOf("bar") === -1);
|
|
|
|
|
2020-07-10 03:54:53 +08:00
|
|
|
this.siteSettings.max_category_nesting = 2;
|
2020-01-19 19:07:54 +08:00
|
|
|
assert.ok(categoryBadgeHTML(baz, { recursive: true }).indexOf("baz") !== -1);
|
|
|
|
assert.ok(categoryBadgeHTML(baz, { recursive: true }).indexOf("bar") !== -1);
|
|
|
|
assert.ok(categoryBadgeHTML(baz, { recursive: true }).indexOf("foo") === -1);
|
|
|
|
|
2020-07-10 03:54:53 +08:00
|
|
|
this.siteSettings.max_category_nesting = 3;
|
2020-01-19 19:07:54 +08:00
|
|
|
assert.ok(categoryBadgeHTML(baz, { recursive: true }).indexOf("baz") !== -1);
|
|
|
|
assert.ok(categoryBadgeHTML(baz, { recursive: true }).indexOf("bar") !== -1);
|
|
|
|
assert.ok(categoryBadgeHTML(baz, { recursive: true }).indexOf("foo") !== -1);
|
|
|
|
});
|