2021-06-09 10:58:55 -04:00
|
|
|
import { discourseModule } from "discourse/tests/helpers/qunit-helpers";
|
2021-04-12 12:48:01 -04:00
|
|
|
import {
|
|
|
|
MULTIPLE_POLL_TYPE,
|
|
|
|
NUMBER_POLL_TYPE,
|
|
|
|
REGULAR_POLL_TYPE,
|
|
|
|
} from "discourse/plugins/poll/controllers/poll-ui-builder";
|
2021-06-09 10:58:55 -04:00
|
|
|
import { test } from "qunit";
|
|
|
|
import { settled } from "@ember/test-helpers";
|
2016-11-08 14:29:50 -05:00
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
function setupController(ctx) {
|
|
|
|
let controller = ctx.getController("poll-ui-builder");
|
|
|
|
controller.set("toolbarEvent", { getText: () => "" });
|
|
|
|
controller.onShow();
|
|
|
|
return controller;
|
|
|
|
}
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
discourseModule("Unit | Controller | poll-ui-builder", function () {
|
|
|
|
test("isMultiple", function (assert) {
|
|
|
|
const controller = setupController(this);
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
controller.setProperties({
|
|
|
|
pollType: MULTIPLE_POLL_TYPE,
|
|
|
|
pollOptions: [{ value: "a" }],
|
|
|
|
});
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-11-08 04:26:28 -05:00
|
|
|
assert.strictEqual(controller.isMultiple, true, "it should be true");
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
controller.setProperties({
|
|
|
|
pollType: "random",
|
|
|
|
pollOptions: [{ value: "b" }],
|
|
|
|
});
|
|
|
|
|
2021-11-08 04:26:28 -05:00
|
|
|
assert.strictEqual(controller.isMultiple, false, "it should be false");
|
2021-04-12 12:48:01 -04:00
|
|
|
});
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
test("isNumber", function (assert) {
|
|
|
|
const controller = setupController(this);
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
controller.set("pollType", REGULAR_POLL_TYPE);
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-11-08 04:26:28 -05:00
|
|
|
assert.strictEqual(controller.isNumber, false, "it should be false");
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
controller.set("pollType", NUMBER_POLL_TYPE);
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-11-08 04:26:28 -05:00
|
|
|
assert.strictEqual(controller.isNumber, true, "it should be true");
|
2021-06-09 10:58:55 -04:00
|
|
|
});
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
test("pollOptionsCount", function (assert) {
|
|
|
|
const controller = setupController(this);
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
controller.set("pollOptions", [{ value: "1" }, { value: "2" }]);
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-11-08 04:26:28 -05:00
|
|
|
assert.strictEqual(controller.pollOptionsCount, 2, "it should equal 2");
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
controller.set("pollOptions", []);
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-11-08 04:26:28 -05:00
|
|
|
assert.strictEqual(controller.pollOptionsCount, 0, "it should equal 0");
|
2021-06-09 10:58:55 -04:00
|
|
|
});
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
test("disableInsert", function (assert) {
|
|
|
|
const controller = setupController(this);
|
|
|
|
controller.siteSettings.poll_maximum_options = 20;
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-11-08 04:26:28 -05:00
|
|
|
assert.strictEqual(controller.disableInsert, true, "it should be true");
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
controller.set("pollOptions", [{ value: "a" }, { value: "b" }]);
|
2017-04-05 17:07:50 -04:00
|
|
|
|
2021-11-08 04:26:28 -05:00
|
|
|
assert.strictEqual(controller.disableInsert, false, "it should be false");
|
2017-04-05 17:07:50 -04:00
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
controller.set("pollType", NUMBER_POLL_TYPE);
|
2017-04-05 17:07:50 -04:00
|
|
|
|
2021-11-08 04:26:28 -05:00
|
|
|
assert.strictEqual(controller.disableInsert, false, "it should be false");
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
controller.setProperties({
|
|
|
|
pollType: REGULAR_POLL_TYPE,
|
|
|
|
pollOptions: [{ value: "a" }, { value: "b" }, { value: "c" }],
|
|
|
|
});
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-11-08 04:26:28 -05:00
|
|
|
assert.strictEqual(controller.disableInsert, false, "it should be false");
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
controller.setProperties({
|
|
|
|
pollType: REGULAR_POLL_TYPE,
|
|
|
|
pollOptions: [],
|
|
|
|
});
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-11-08 04:26:28 -05:00
|
|
|
assert.strictEqual(controller.disableInsert, true, "it should be true");
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
controller.setProperties({
|
|
|
|
pollType: REGULAR_POLL_TYPE,
|
|
|
|
pollOptions: [{ value: "w" }],
|
|
|
|
});
|
2020-02-05 09:03:27 -05:00
|
|
|
|
2021-11-08 04:26:28 -05:00
|
|
|
assert.strictEqual(controller.disableInsert, false, "it should be false");
|
2020-02-05 09:03:27 -05:00
|
|
|
});
|
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
test("number pollOutput", async function (assert) {
|
|
|
|
this.siteSettings.poll_maximum_options = 20;
|
|
|
|
const controller = setupController(this);
|
|
|
|
|
|
|
|
controller.setProperties({
|
|
|
|
pollType: NUMBER_POLL_TYPE,
|
|
|
|
pollMin: 1,
|
|
|
|
});
|
|
|
|
await settled();
|
|
|
|
|
2021-11-08 04:26:28 -05:00
|
|
|
assert.strictEqual(
|
2021-06-09 10:58:55 -04:00
|
|
|
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();
|
|
|
|
|
2021-11-08 04:26:28 -05:00
|
|
|
assert.strictEqual(
|
2021-06-09 10:58:55 -04:00
|
|
|
controller.pollOutput,
|
|
|
|
"[poll type=number results=always min=1 max=20 step=2]\n[/poll]\n",
|
|
|
|
"it should return the right output"
|
|
|
|
);
|
|
|
|
|
|
|
|
controller.set("publicPoll", true);
|
|
|
|
|
2021-11-08 04:26:28 -05:00
|
|
|
assert.strictEqual(
|
2021-06-09 10:58:55 -04:00
|
|
|
controller.pollOutput,
|
|
|
|
"[poll type=number results=always min=1 max=20 step=2 public=true]\n[/poll]\n",
|
|
|
|
"it should return the right output"
|
|
|
|
);
|
|
|
|
|
|
|
|
controller.set("pollStep", 0);
|
|
|
|
|
2021-11-08 04:26:28 -05:00
|
|
|
assert.strictEqual(
|
2021-06-09 10:58:55 -04:00
|
|
|
controller.pollOutput,
|
|
|
|
"[poll type=number results=always min=1 max=20 step=1 public=true]\n[/poll]\n",
|
|
|
|
"it should return the right output"
|
|
|
|
);
|
2016-06-13 06:21:14 -04:00
|
|
|
});
|
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
test("regular pollOutput", function (assert) {
|
|
|
|
const controller = setupController(this);
|
|
|
|
controller.siteSettings.poll_maximum_options = 20;
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
controller.setProperties({
|
|
|
|
pollOptions: [{ value: "1" }, { value: "2" }],
|
|
|
|
pollType: REGULAR_POLL_TYPE,
|
|
|
|
});
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-11-08 04:26:28 -05:00
|
|
|
assert.strictEqual(
|
2021-06-09 10:58:55 -04:00
|
|
|
controller.pollOutput,
|
|
|
|
"[poll type=regular results=always chartType=bar]\n* 1\n* 2\n[/poll]\n",
|
|
|
|
"it should return the right output"
|
|
|
|
);
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
controller.set("publicPoll", "true");
|
2017-11-30 11:04:41 -05:00
|
|
|
|
2021-11-08 04:26:28 -05:00
|
|
|
assert.strictEqual(
|
2021-06-09 10:58:55 -04:00
|
|
|
controller.pollOutput,
|
|
|
|
"[poll type=regular results=always public=true chartType=bar]\n* 1\n* 2\n[/poll]\n",
|
|
|
|
"it should return the right output"
|
|
|
|
);
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
controller.set("pollGroups", "test");
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-11-08 04:26:28 -05:00
|
|
|
assert.strictEqual(
|
2021-06-09 10:58:55 -04:00
|
|
|
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"
|
|
|
|
);
|
2017-04-05 17:07:50 -04:00
|
|
|
});
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
test("multiple pollOutput", function (assert) {
|
|
|
|
const controller = setupController(this);
|
|
|
|
controller.siteSettings.poll_maximum_options = 20;
|
|
|
|
|
|
|
|
controller.setProperties({
|
|
|
|
pollType: MULTIPLE_POLL_TYPE,
|
|
|
|
pollMin: 1,
|
|
|
|
pollOptions: [{ value: "1" }, { value: "2" }],
|
|
|
|
});
|
|
|
|
|
2021-11-08 04:26:28 -05:00
|
|
|
assert.strictEqual(
|
2021-06-09 10:58:55 -04:00
|
|
|
controller.pollOutput,
|
|
|
|
"[poll type=multiple results=always min=1 max=2 chartType=bar]\n* 1\n* 2\n[/poll]\n",
|
|
|
|
"it should return the right output"
|
|
|
|
);
|
|
|
|
|
|
|
|
controller.set("publicPoll", "true");
|
|
|
|
|
2021-11-08 04:26:28 -05:00
|
|
|
assert.strictEqual(
|
2021-06-09 10:58:55 -04:00
|
|
|
controller.pollOutput,
|
|
|
|
"[poll type=multiple results=always min=1 max=2 public=true chartType=bar]\n* 1\n* 2\n[/poll]\n",
|
|
|
|
"it should return the right output"
|
|
|
|
);
|
2016-06-13 06:21:14 -04:00
|
|
|
});
|
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
test("staff_only option is not present for non-staff", async function (assert) {
|
|
|
|
const controller = setupController(this);
|
|
|
|
controller.currentUser = { staff: false };
|
|
|
|
controller.notifyPropertyChange("pollResults");
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2022-07-13 13:29:19 -04:00
|
|
|
assert.strictEqual(
|
|
|
|
controller.pollResults.filterBy("value", "staff_only").length,
|
|
|
|
0,
|
2021-06-09 10:58:55 -04:00
|
|
|
"staff_only is not present"
|
|
|
|
);
|
|
|
|
});
|
2019-12-17 16:43:15 -05:00
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
test("poll result is always by default", function (assert) {
|
|
|
|
const controller = setupController(this);
|
2021-11-08 04:26:28 -05:00
|
|
|
assert.strictEqual(controller.pollResult, "always");
|
2021-06-09 10:58:55 -04:00
|
|
|
});
|
2020-07-07 10:23:21 -04:00
|
|
|
|
2021-06-09 10:58:55 -04:00
|
|
|
test("staff_only option is present for staff", async function (assert) {
|
|
|
|
const controller = setupController(this);
|
|
|
|
controller.currentUser = { staff: true };
|
|
|
|
controller.notifyPropertyChange("pollResults");
|
2019-12-17 16:43:15 -05:00
|
|
|
|
2022-07-13 13:29:19 -04:00
|
|
|
assert.strictEqual(
|
|
|
|
controller.pollResults.filterBy("value", "staff_only").length,
|
|
|
|
1,
|
2021-06-09 10:58:55 -04:00
|
|
|
"staff_only is present"
|
|
|
|
);
|
|
|
|
});
|
2019-12-17 16:43:15 -05:00
|
|
|
});
|