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.

332 lines
8.3 KiB
Plaintext
Raw Normal View History

2018-06-15 12:42:20 -04:00
import { mapRoutes } from "discourse/mapping-router";
2016-11-08 14:29:50 -05:00
2016-06-13 06:21:14 -04:00
moduleFor("controller:poll-ui-builder", "controller:poll-ui-builder", {
2016-11-08 14:29:50 -05:00
setup() {
2018-06-15 12:42:20 -04:00
this.registry.register("router:main", mapRoutes());
this.subject().set("toolbarEvent", {
2016-11-21 14:49:00 -05:00
getText: () => ""
});
2016-11-08 14:29:50 -05:00
},
2018-06-15 12:42:20 -04:00
needs: ["controller:modal"]
2016-06-13 06:21:14 -04:00
});
2017-06-14 13:57:58 -04:00
test("isMultiple", function(assert) {
2016-06-13 06:21:14 -04:00
const controller = this.subject();
controller.setProperties({
pollType: controller.multiplePollType,
2016-06-13 06:21:14 -04:00
pollOptionsCount: 1
});
assert.equal(controller.isMultiple, true, "it should be true");
2016-06-13 06:21:14 -04:00
controller.set("pollOptionsCount", 0);
assert.equal(controller.isMultiple, false, "it should be false");
2016-06-13 06:21:14 -04:00
controller.setProperties({ pollType: "random", pollOptionsCount: 1 });
assert.equal(controller.isMultiple, false, "it should be false");
2016-06-13 06:21:14 -04:00
});
2017-06-14 13:57:58 -04:00
test("isNumber", function(assert) {
2016-06-13 06:21:14 -04:00
const controller = this.subject();
controller.siteSettings = Discourse.SiteSettings;
controller.set("pollType", controller.regularPollType);
2016-06-13 06:21:14 -04:00
assert.equal(controller.isNumber, false, "it should be false");
2016-06-13 06:21:14 -04:00
controller.set("pollType", controller.numberPollType);
2016-06-13 06:21:14 -04:00
assert.equal(controller.isNumber, true, "it should be true");
2016-06-13 06:21:14 -04:00
});
2017-06-14 13:57:58 -04:00
test("showMinMax", function(assert) {
2016-06-13 06:21:14 -04:00
const controller = this.subject();
controller.siteSettings = Discourse.SiteSettings;
controller.set("pollType", controller.numberPollType);
assert.equal(controller.showMinMax, true, "it should be true");
2016-06-13 06:21:14 -04:00
controller.set("pollType", controller.multiplePollType);
assert.equal(controller.showMinMax, true, "it should be true");
2017-04-05 17:07:50 -04:00
controller.set("pollType", controller.regularPollType);
assert.equal(controller.showMinMax, false, "it should be false");
2016-06-13 06:21:14 -04:00
});
2017-06-14 13:57:58 -04:00
test("pollOptionsCount", function(assert) {
2016-06-13 06:21:14 -04:00
const controller = this.subject();
controller.siteSettings = Discourse.SiteSettings;
controller.set("pollOptions", "1\n2\n");
2016-06-13 06:21:14 -04:00
assert.equal(controller.pollOptionsCount, 2, "it should equal 2");
2016-06-13 06:21:14 -04:00
controller.set("pollOptions", "");
2016-06-13 06:21:14 -04:00
assert.equal(controller.pollOptionsCount, 0, "it should equal 0");
2016-06-13 06:21:14 -04:00
});
2017-06-14 13:57:58 -04:00
test("pollMinOptions", function(assert) {
2016-06-13 06:21:14 -04:00
const controller = this.subject();
controller.siteSettings = Discourse.SiteSettings;
controller.setProperties({
pollType: controller.multiplePollType,
2016-06-13 06:21:14 -04:00
pollOptionsCount: 1
});
2018-06-15 12:42:20 -04:00
assert.deepEqual(
controller.pollMinOptions,
2018-06-15 12:42:20 -04:00
[{ name: 1, value: 1 }],
"it should return the right options"
);
2016-06-13 06:21:14 -04:00
controller.set("pollOptionsCount", 2);
2018-06-15 12:42:20 -04:00
assert.deepEqual(
controller.pollMinOptions,
2019-11-25 13:12:44 -05:00
[
{ name: 1, value: 1 },
{ name: 2, value: 2 }
],
2018-06-15 12:42:20 -04:00
"it should return the right options"
);
2016-06-13 06:21:14 -04:00
controller.set("pollType", controller.numberPollType);
2016-06-13 06:21:14 -04:00
controller.siteSettings.poll_maximum_options = 2;
2018-06-15 12:42:20 -04:00
assert.deepEqual(
controller.pollMinOptions,
2019-11-25 13:12:44 -05:00
[
{ name: 1, value: 1 },
{ name: 2, value: 2 }
],
2018-06-15 12:42:20 -04:00
"it should return the right options"
);
2016-06-13 06:21:14 -04:00
});
2017-06-14 13:57:58 -04:00
test("pollMaxOptions", function(assert) {
2016-06-13 06:21:14 -04:00
const controller = this.subject();
controller.siteSettings = Discourse.SiteSettings;
2018-06-15 12:42:20 -04:00
controller.setProperties({
pollType: controller.multiplePollType,
2018-06-15 12:42:20 -04:00
pollOptionsCount: 1,
pollMin: 1
});
2016-06-13 06:21:14 -04:00
2018-06-15 12:42:20 -04:00
assert.deepEqual(
controller.pollMaxOptions,
2018-06-15 12:42:20 -04:00
[],
"it should return the right options"
);
2016-06-13 06:21:14 -04:00
controller.set("pollOptionsCount", 2);
2018-06-15 12:42:20 -04:00
assert.deepEqual(
controller.pollMaxOptions,
2018-06-15 12:42:20 -04:00
[{ name: 2, value: 2 }],
"it should return the right options"
);
2016-06-13 06:21:14 -04:00
controller.siteSettings.poll_maximum_options = 3;
2018-06-15 12:42:20 -04:00
controller.setProperties({
pollType: controller.get("numberPollType"),
2018-06-15 12:42:20 -04:00
pollStep: 2,
pollMin: 1
});
assert.deepEqual(
controller.pollMaxOptions,
2018-06-15 12:42:20 -04:00
[
{ name: 2, value: 2 },
{ name: 3, value: 3 },
{ name: 4, value: 4 },
{ name: 5, value: 5 },
{ name: 6, value: 6 }
],
"it should return the right options"
);
2016-06-13 06:21:14 -04:00
});
2017-06-14 13:57:58 -04:00
test("pollStepOptions", function(assert) {
2016-06-13 06:21:14 -04:00
const controller = this.subject();
controller.siteSettings = Discourse.SiteSettings;
controller.siteSettings.poll_maximum_options = 3;
assert.equal(controller.pollStepOptions, null, "is should return null");
2016-06-13 06:21:14 -04:00
controller.set("pollType", controller.numberPollType);
2016-06-13 06:21:14 -04:00
2018-06-15 12:42:20 -04:00
assert.deepEqual(
controller.pollStepOptions,
2019-11-25 13:12:44 -05:00
[
{ name: 1, value: 1 },
{ name: 2, value: 2 },
{ name: 3, value: 3 }
],
2018-06-15 12:42:20 -04:00
"it should return the right options"
);
2016-06-13 06:21:14 -04:00
});
2017-06-14 13:57:58 -04:00
test("disableInsert", function(assert) {
2016-06-13 06:21:14 -04:00
const controller = this.subject();
controller.siteSettings = Discourse.SiteSettings;
assert.equal(controller.disableInsert, true, "it should be true");
2017-04-05 17:07:50 -04:00
controller.set("pollOptionsCount", 2);
2017-04-05 17:07:50 -04:00
assert.equal(controller.disableInsert, false, "it should be false");
2017-04-05 17:07:50 -04:00
controller.set("pollType", controller.numberPollType);
2016-06-13 06:21:14 -04:00
assert.equal(controller.disableInsert, false, "it should be false");
2016-06-13 06:21:14 -04:00
controller.setProperties({
pollType: controller.regularPollType,
pollOptionsCount: 3
});
2016-06-13 06:21:14 -04:00
assert.equal(controller.disableInsert, false, "it should be false");
2016-06-13 06:21:14 -04:00
controller.setProperties({
pollType: controller.regularPollType,
pollOptionsCount: 0
});
2016-06-13 06:21:14 -04:00
assert.equal(controller.disableInsert, true, "it should be true");
controller.setProperties({
pollType: controller.regularPollType,
pollOptionsCount: 1
});
assert.equal(controller.disableInsert, false, "it should be false");
2016-06-13 06:21:14 -04:00
});
2017-06-14 13:57:58 -04:00
test("number pollOutput", function(assert) {
2016-06-13 06:21:14 -04:00
const controller = this.subject();
controller.siteSettings = Discourse.SiteSettings;
controller.siteSettings.poll_maximum_options = 20;
controller.setProperties({
pollType: controller.numberPollType,
2016-06-13 06:21:14 -04:00
pollMin: 1
});
2018-06-15 12:42:20 -04:00
assert.equal(
controller.pollOutput,
2019-10-16 21:53:37 -04:00
"[poll type=number min=1 max=20 step=1]\n[/poll]\n",
2018-06-15 12:42:20 -04:00
"it should return the right output"
);
2016-06-13 06:21:14 -04:00
controller.set("pollStep", 2);
2018-06-15 12:42:20 -04:00
assert.equal(
controller.pollOutput,
2019-10-16 21:53:37 -04:00
"[poll type=number min=1 max=20 step=2]\n[/poll]\n",
2018-06-15 12:42:20 -04:00
"it should return the right output"
);
2016-06-13 06:21:14 -04:00
controller.set("publicPoll", true);
2018-06-15 12:42:20 -04:00
assert.equal(
controller.pollOutput,
2019-10-16 21:53:37 -04:00
"[poll type=number min=1 max=20 step=2 public=true]\n[/poll]\n",
2018-06-15 12:42:20 -04:00
"it should return the right output"
);
controller.set("pollStep", 0);
2018-06-15 12:42:20 -04:00
assert.equal(
controller.pollOutput,
2019-10-16 21:53:37 -04:00
"[poll type=number min=1 max=20 step=1 public=true]\n[/poll]\n",
2018-06-15 12:42:20 -04:00
"it should return the right output"
);
2016-06-13 06:21:14 -04:00
});
2017-06-14 13:57:58 -04:00
test("regular pollOutput", function(assert) {
2016-06-13 06:21:14 -04:00
const controller = this.subject();
controller.siteSettings = Discourse.SiteSettings;
controller.siteSettings.poll_maximum_options = 20;
controller.set("pollOptions", "1\n2");
2017-04-05 17:07:50 -04:00
controller.setProperties({
pollOptions: "1\n2",
pollType: controller.regularPollType
2017-04-05 17:07:50 -04:00
});
2016-06-13 06:21:14 -04:00
2018-06-15 12:42:20 -04:00
assert.equal(
controller.pollOutput,
"[poll type=regular chartType=bar]\n* 1\n* 2\n[/poll]\n",
2018-06-15 12:42:20 -04:00
"it should return the right output"
);
2016-06-13 06:21:14 -04:00
controller.set("publicPoll", "true");
2018-06-15 12:42:20 -04:00
assert.equal(
controller.pollOutput,
"[poll type=regular public=true chartType=bar]\n* 1\n* 2\n[/poll]\n",
2018-06-15 12:42:20 -04:00
"it should return the right output"
);
controller.set("pollGroups", "test");
assert.equal(
controller.get("pollOutput"),
"[poll type=regular public=true chartType=bar groups=test]\n* 1\n* 2\n[/poll]\n",
"it should return the right output"
);
2016-06-13 06:21:14 -04:00
});
2017-06-14 13:57:58 -04:00
test("multiple pollOutput", function(assert) {
2016-06-13 06:21:14 -04:00
const controller = this.subject();
controller.siteSettings = Discourse.SiteSettings;
controller.siteSettings.poll_maximum_options = 20;
controller.setProperties({
isMultiple: true,
pollType: controller.multiplePollType,
2016-06-13 06:21:14 -04:00
pollMin: 1,
pollOptions: "\n\n1\n\n2"
2016-06-13 06:21:14 -04:00
});
2018-06-15 12:42:20 -04:00
assert.equal(
controller.pollOutput,
"[poll type=multiple min=1 max=2 chartType=bar]\n* 1\n* 2\n[/poll]\n",
2018-06-15 12:42:20 -04:00
"it should return the right output"
);
2016-06-13 06:21:14 -04:00
controller.set("publicPoll", "true");
2018-06-15 12:42:20 -04:00
assert.equal(
controller.pollOutput,
"[poll type=multiple min=1 max=2 public=true chartType=bar]\n* 1\n* 2\n[/poll]\n",
2018-06-15 12:42:20 -04:00
"it should return the right output"
);
2016-06-13 06:21:14 -04: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("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"
);
});