mirror of
https://github.com/discourse/discourse.git
synced 2025-06-02 04:08:41 +08:00
DEV: Modernize controller unit tests (#17412)
Aligns controller tests with the Ember standard, by using `module` and `setupTest` instead of our custom `discourseModule`)
This commit is contained in:
@ -1,20 +1,22 @@
|
||||
import { discourseModule } from "discourse/tests/helpers/qunit-helpers";
|
||||
import { module, test } from "qunit";
|
||||
import { setupTest } from "ember-qunit";
|
||||
import {
|
||||
MULTIPLE_POLL_TYPE,
|
||||
NUMBER_POLL_TYPE,
|
||||
REGULAR_POLL_TYPE,
|
||||
} from "discourse/plugins/poll/controllers/poll-ui-builder";
|
||||
import { test } from "qunit";
|
||||
import { settled } from "@ember/test-helpers";
|
||||
|
||||
function setupController(ctx) {
|
||||
let controller = ctx.getController("poll-ui-builder");
|
||||
const controller = ctx.owner.lookup("controller:poll-ui-builder");
|
||||
controller.set("toolbarEvent", { getText: () => "" });
|
||||
controller.onShow();
|
||||
return controller;
|
||||
}
|
||||
|
||||
discourseModule("Unit | Controller | poll-ui-builder", function () {
|
||||
module("Unit | Controller | poll-ui-builder", function (hooks) {
|
||||
setupTest(hooks);
|
||||
|
||||
test("isMultiple", function (assert) {
|
||||
const controller = setupController(this);
|
||||
|
||||
@ -22,14 +24,12 @@ discourseModule("Unit | Controller | poll-ui-builder", function () {
|
||||
pollType: MULTIPLE_POLL_TYPE,
|
||||
pollOptions: [{ value: "a" }],
|
||||
});
|
||||
|
||||
assert.strictEqual(controller.isMultiple, true, "it should be true");
|
||||
|
||||
controller.setProperties({
|
||||
pollType: "random",
|
||||
pollOptions: [{ value: "b" }],
|
||||
});
|
||||
|
||||
assert.strictEqual(controller.isMultiple, false, "it should be false");
|
||||
});
|
||||
|
||||
@ -37,11 +37,9 @@ discourseModule("Unit | Controller | poll-ui-builder", function () {
|
||||
const controller = setupController(this);
|
||||
|
||||
controller.set("pollType", REGULAR_POLL_TYPE);
|
||||
|
||||
assert.strictEqual(controller.isNumber, false, "it should be false");
|
||||
|
||||
controller.set("pollType", NUMBER_POLL_TYPE);
|
||||
|
||||
assert.strictEqual(controller.isNumber, true, "it should be true");
|
||||
});
|
||||
|
||||
@ -49,68 +47,60 @@ discourseModule("Unit | Controller | poll-ui-builder", function () {
|
||||
const controller = setupController(this);
|
||||
|
||||
controller.set("pollOptions", [{ value: "1" }, { value: "2" }]);
|
||||
|
||||
assert.strictEqual(controller.pollOptionsCount, 2, "it should equal 2");
|
||||
|
||||
controller.set("pollOptions", []);
|
||||
|
||||
assert.strictEqual(controller.pollOptionsCount, 0, "it should equal 0");
|
||||
});
|
||||
|
||||
test("disableInsert", function (assert) {
|
||||
const controller = setupController(this);
|
||||
controller.siteSettings.poll_maximum_options = 20;
|
||||
|
||||
controller.siteSettings.poll_maximum_options = 20;
|
||||
assert.strictEqual(controller.disableInsert, true, "it should be true");
|
||||
|
||||
controller.set("pollOptions", [{ value: "a" }, { value: "b" }]);
|
||||
|
||||
assert.strictEqual(controller.disableInsert, false, "it should be false");
|
||||
|
||||
controller.set("pollType", NUMBER_POLL_TYPE);
|
||||
|
||||
assert.strictEqual(controller.disableInsert, false, "it should be false");
|
||||
|
||||
controller.setProperties({
|
||||
pollType: REGULAR_POLL_TYPE,
|
||||
pollOptions: [{ value: "a" }, { value: "b" }, { value: "c" }],
|
||||
});
|
||||
|
||||
assert.strictEqual(controller.disableInsert, false, "it should be false");
|
||||
|
||||
controller.setProperties({
|
||||
pollType: REGULAR_POLL_TYPE,
|
||||
pollOptions: [],
|
||||
});
|
||||
|
||||
assert.strictEqual(controller.disableInsert, true, "it should be true");
|
||||
|
||||
controller.setProperties({
|
||||
pollType: REGULAR_POLL_TYPE,
|
||||
pollOptions: [{ value: "w" }],
|
||||
});
|
||||
|
||||
assert.strictEqual(controller.disableInsert, false, "it should be false");
|
||||
});
|
||||
|
||||
test("number pollOutput", async function (assert) {
|
||||
this.siteSettings.poll_maximum_options = 20;
|
||||
const controller = setupController(this);
|
||||
controller.siteSettings.poll_maximum_options = 20;
|
||||
|
||||
controller.setProperties({
|
||||
pollType: NUMBER_POLL_TYPE,
|
||||
pollMin: 1,
|
||||
});
|
||||
await settled();
|
||||
|
||||
assert.strictEqual(
|
||||
controller.pollOutput,
|
||||
"[poll type=number results=always min=1 max=20 step=1]\n[/poll]\n",
|
||||
"it should return the right output"
|
||||
);
|
||||
|
||||
controller.set("pollStep", 2);
|
||||
await settled();
|
||||
|
||||
assert.strictEqual(
|
||||
controller.pollOutput,
|
||||
"[poll type=number results=always min=1 max=20 step=2]\n[/poll]\n",
|
||||
@ -118,7 +108,6 @@ discourseModule("Unit | Controller | poll-ui-builder", function () {
|
||||
);
|
||||
|
||||
controller.set("publicPoll", true);
|
||||
|
||||
assert.strictEqual(
|
||||
controller.pollOutput,
|
||||
"[poll type=number results=always min=1 max=20 step=2 public=true]\n[/poll]\n",
|
||||
@ -126,7 +115,6 @@ discourseModule("Unit | Controller | poll-ui-builder", function () {
|
||||
);
|
||||
|
||||
controller.set("pollStep", 0);
|
||||
|
||||
assert.strictEqual(
|
||||
controller.pollOutput,
|
||||
"[poll type=number results=always min=1 max=20 step=1 public=true]\n[/poll]\n",
|
||||
@ -142,7 +130,6 @@ discourseModule("Unit | Controller | poll-ui-builder", function () {
|
||||
pollOptions: [{ value: "1" }, { value: "2" }],
|
||||
pollType: REGULAR_POLL_TYPE,
|
||||
});
|
||||
|
||||
assert.strictEqual(
|
||||
controller.pollOutput,
|
||||
"[poll type=regular results=always chartType=bar]\n* 1\n* 2\n[/poll]\n",
|
||||
@ -150,7 +137,6 @@ discourseModule("Unit | Controller | poll-ui-builder", function () {
|
||||
);
|
||||
|
||||
controller.set("publicPoll", "true");
|
||||
|
||||
assert.strictEqual(
|
||||
controller.pollOutput,
|
||||
"[poll type=regular results=always public=true chartType=bar]\n* 1\n* 2\n[/poll]\n",
|
||||
@ -158,7 +144,6 @@ discourseModule("Unit | Controller | poll-ui-builder", function () {
|
||||
);
|
||||
|
||||
controller.set("pollGroups", "test");
|
||||
|
||||
assert.strictEqual(
|
||||
controller.get("pollOutput"),
|
||||
"[poll type=regular results=always public=true chartType=bar groups=test]\n* 1\n* 2\n[/poll]\n",
|
||||
@ -175,7 +160,6 @@ discourseModule("Unit | Controller | poll-ui-builder", function () {
|
||||
pollMin: 1,
|
||||
pollOptions: [{ value: "1" }, { value: "2" }],
|
||||
});
|
||||
|
||||
assert.strictEqual(
|
||||
controller.pollOutput,
|
||||
"[poll type=multiple results=always min=1 max=2 chartType=bar]\n* 1\n* 2\n[/poll]\n",
|
||||
@ -183,7 +167,6 @@ discourseModule("Unit | Controller | poll-ui-builder", function () {
|
||||
);
|
||||
|
||||
controller.set("publicPoll", "true");
|
||||
|
||||
assert.strictEqual(
|
||||
controller.pollOutput,
|
||||
"[poll type=multiple results=always min=1 max=2 public=true chartType=bar]\n* 1\n* 2\n[/poll]\n",
|
Reference in New Issue
Block a user