discourse/plugins/poll/test/javascripts/controllers/poll-ui-builder-test.js.es6

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

215 lines
5.6 KiB
Plaintext
Raw Normal View History

import { controllerModule } from "discourse/tests/helpers/qunit-helpers";
import {
MULTIPLE_POLL_TYPE,
NUMBER_POLL_TYPE,
REGULAR_POLL_TYPE,
} from "discourse/plugins/poll/controllers/poll-ui-builder";
2016-11-09 03:29:50 +08:00
controllerModule("controller:poll-ui-builder", {
setupController(controller) {
controller.set("toolbarEvent", { getText: () => "" });
controller.onShow();
2016-11-09 03:29:50 +08:00
},
needs: ["controller:modal"],
2016-06-13 18:21:14 +08:00
});
test("isMultiple", function (assert) {
2016-06-13 18:21:14 +08:00
const controller = this.subject();
controller.setProperties({
pollType: MULTIPLE_POLL_TYPE,
pollOptions: [{ value: "a" }],
2016-06-13 18:21:14 +08:00
});
assert.equal(controller.isMultiple, true, "it should be true");
2016-06-13 18:21:14 +08:00
controller.setProperties({
pollType: "random",
pollOptions: [{ value: "b" }],
});
2016-06-13 18:21:14 +08:00
assert.equal(controller.isMultiple, false, "it should be false");
2016-06-13 18:21:14 +08:00
});
test("isNumber", function (assert) {
2016-06-13 18:21:14 +08:00
const controller = this.subject();
controller.set("pollType", REGULAR_POLL_TYPE);
2016-06-13 18:21:14 +08:00
assert.equal(controller.isNumber, false, "it should be false");
2016-06-13 18:21:14 +08:00
controller.set("pollType", NUMBER_POLL_TYPE);
2016-06-13 18:21:14 +08:00
assert.equal(controller.isNumber, true, "it should be true");
2016-06-13 18:21:14 +08:00
});
test("pollOptionsCount", function (assert) {
2016-06-13 18:21:14 +08:00
const controller = this.subject();
controller.set("pollOptions", [{ value: "1" }, { value: "2" }]);
2016-06-13 18:21:14 +08:00
assert.equal(controller.pollOptionsCount, 2, "it should equal 2");
2016-06-13 18:21:14 +08:00
controller.set("pollOptions", []);
2016-06-13 18:21:14 +08:00
assert.equal(controller.pollOptionsCount, 0, "it should equal 0");
2016-06-13 18:21:14 +08:00
});
test("disableInsert", function (assert) {
2016-06-13 18:21:14 +08:00
const controller = this.subject();
controller.siteSettings.poll_maximum_options = 20;
2016-06-13 18:21:14 +08:00
assert.equal(controller.disableInsert, true, "it should be true");
2017-04-06 05:07:50 +08:00
controller.set("pollOptions", [{ value: "a" }, { value: "b" }]);
2017-04-06 05:07:50 +08:00
assert.equal(controller.disableInsert, false, "it should be false");
2017-04-06 05:07:50 +08:00
controller.set("pollType", NUMBER_POLL_TYPE);
2016-06-13 18:21:14 +08:00
assert.equal(controller.disableInsert, false, "it should be false");
2016-06-13 18:21:14 +08:00
controller.setProperties({
pollType: REGULAR_POLL_TYPE,
pollOptions: [{ value: "a" }, { value: "b" }, { value: "c" }],
});
2016-06-13 18:21:14 +08:00
assert.equal(controller.disableInsert, false, "it should be false");
2016-06-13 18:21:14 +08:00
controller.setProperties({
pollType: REGULAR_POLL_TYPE,
pollOptions: [],
});
2016-06-13 18:21:14 +08:00
assert.equal(controller.disableInsert, true, "it should be true");
controller.setProperties({
pollType: REGULAR_POLL_TYPE,
pollOptions: [{ value: "w" }],
});
assert.equal(controller.disableInsert, false, "it should be false");
2016-06-13 18:21:14 +08:00
});
test("number pollOutput", function (assert) {
2016-06-13 18:21:14 +08:00
const controller = this.subject();
controller.siteSettings.poll_maximum_options = 20;
controller.setProperties({
pollType: NUMBER_POLL_TYPE,
pollMin: 1,
2016-06-13 18:21:14 +08:00
});
2018-06-16 00:42:20 +08:00
assert.equal(
controller.pollOutput,
"[poll type=number results=always min=1 max=20 step=1]\n[/poll]\n",
2018-06-16 00:42:20 +08:00
"it should return the right output"
);
2016-06-13 18:21:14 +08:00
controller.set("pollStep", 2);
2018-06-16 00:42:20 +08:00
assert.equal(
controller.pollOutput,
"[poll type=number results=always min=1 max=20 step=2]\n[/poll]\n",
2018-06-16 00:42:20 +08:00
"it should return the right output"
);
2016-06-13 18:21:14 +08:00
controller.set("publicPoll", true);
2018-06-16 00:42:20 +08:00
assert.equal(
controller.pollOutput,
"[poll type=number results=always min=1 max=20 step=2 public=true]\n[/poll]\n",
2018-06-16 00:42:20 +08:00
"it should return the right output"
);
controller.set("pollStep", 0);
2018-06-16 00:42:20 +08:00
assert.equal(
controller.pollOutput,
"[poll type=number results=always min=1 max=20 step=1 public=true]\n[/poll]\n",
2018-06-16 00:42:20 +08:00
"it should return the right output"
);
2016-06-13 18:21:14 +08:00
});
test("regular pollOutput", function (assert) {
2016-06-13 18:21:14 +08:00
const controller = this.subject();
controller.siteSettings.poll_maximum_options = 20;
2017-04-06 05:07:50 +08:00
controller.setProperties({
pollOptions: [{ value: "1" }, { value: "2" }],
pollType: REGULAR_POLL_TYPE,
2017-04-06 05:07:50 +08:00
});
2016-06-13 18:21:14 +08:00
2018-06-16 00:42:20 +08:00
assert.equal(
controller.pollOutput,
"[poll type=regular results=always chartType=bar]\n* 1\n* 2\n[/poll]\n",
2018-06-16 00:42:20 +08:00
"it should return the right output"
);
2016-06-13 18:21:14 +08:00
controller.set("publicPoll", "true");
2018-06-16 00:42:20 +08:00
assert.equal(
controller.pollOutput,
"[poll type=regular results=always public=true chartType=bar]\n* 1\n* 2\n[/poll]\n",
2018-06-16 00:42:20 +08:00
"it should return the right output"
);
controller.set("pollGroups", "test");
assert.equal(
controller.get("pollOutput"),
"[poll type=regular results=always public=true chartType=bar groups=test]\n* 1\n* 2\n[/poll]\n",
"it should return the right output"
);
2016-06-13 18:21:14 +08:00
});
test("multiple pollOutput", function (assert) {
2016-06-13 18:21:14 +08:00
const controller = this.subject();
controller.siteSettings.poll_maximum_options = 20;
controller.setProperties({
pollType: MULTIPLE_POLL_TYPE,
2016-06-13 18:21:14 +08:00
pollMin: 1,
pollOptions: [{ value: "1" }, { value: "2" }],
2016-06-13 18:21:14 +08:00
});
2018-06-16 00:42:20 +08:00
assert.equal(
controller.pollOutput,
"[poll type=multiple results=always min=1 max=2 chartType=bar]\n* 1\n* 2\n[/poll]\n",
2018-06-16 00:42:20 +08:00
"it should return the right output"
);
2016-06-13 18:21:14 +08:00
controller.set("publicPoll", "true");
2018-06-16 00:42:20 +08:00
assert.equal(
controller.pollOutput,
"[poll type=multiple results=always min=1 max=2 public=true chartType=bar]\n* 1\n* 2\n[/poll]\n",
2018-06-16 00:42:20 +08:00
"it should return the right output"
);
2016-06-13 18:21:14 +08:00
});
test("staff_only option is not present for non-staff", function (assert) {
const controller = this.subject();
controller.currentUser = { staff: false };
assert.ok(
controller.pollResults.filterBy("value", "staff_only").length === 0,
"staff_only is not present"
);
});
test("poll result is always by default", function (assert) {
const controller = this.subject();
assert.equal(controller.pollResult, "always");
});
test("staff_only option is present for staff", function (assert) {
const controller = this.subject();
controller.currentUser = { staff: true };
assert.ok(
controller.pollResults.filterBy("value", "staff_only").length === 1,
"staff_only is present"
);
});