2023-10-11 02:38:59 +08:00
|
|
|
import EmberObject from "@ember/object";
|
|
|
|
import { click, render } from "@ember/test-helpers";
|
2024-09-04 16:38:22 +08:00
|
|
|
import { TrackedObject } from "@ember-compat/tracked-built-ins";
|
2023-10-11 02:38:59 +08:00
|
|
|
import hbs from "htmlbars-inline-precompile";
|
2022-07-11 18:29:44 +08:00
|
|
|
import { module, test } from "qunit";
|
|
|
|
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
|
2023-10-11 02:38:59 +08:00
|
|
|
import pretender, { response } from "discourse/tests/helpers/create-pretender";
|
2023-10-18 18:07:09 +08:00
|
|
|
import I18n from "discourse-i18n";
|
2020-02-18 18:13:19 +08:00
|
|
|
|
2020-10-08 05:08:19 +08:00
|
|
|
let requests = 0;
|
|
|
|
|
2024-07-04 19:34:48 +08:00
|
|
|
module("Poll | Component | poll", function (hooks) {
|
2022-07-11 18:29:44 +08:00
|
|
|
setupRenderingTest(hooks);
|
|
|
|
|
2022-07-21 16:53:21 +08:00
|
|
|
hooks.beforeEach(function () {
|
|
|
|
pretender.put("/polls/vote", () => {
|
|
|
|
++requests;
|
2022-07-25 08:31:52 +08:00
|
|
|
return response({
|
|
|
|
poll: {
|
|
|
|
name: "poll",
|
|
|
|
type: "regular",
|
|
|
|
status: "open",
|
|
|
|
results: "always",
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
id: "1f972d1df351de3ce35a787c89faad29",
|
|
|
|
html: "yes",
|
|
|
|
votes: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "d7ebc3a9beea2e680815a1e4f57d6db6",
|
|
|
|
html: "no",
|
|
|
|
votes: 0,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
voters: 1,
|
|
|
|
chart_type: "bar",
|
2020-10-08 05:08:19 +08:00
|
|
|
},
|
2022-07-25 08:31:52 +08:00
|
|
|
vote: ["1f972d1df351de3ce35a787c89faad29"],
|
|
|
|
});
|
2022-07-21 16:53:21 +08:00
|
|
|
});
|
2022-07-11 18:29:44 +08:00
|
|
|
});
|
|
|
|
|
2024-08-02 14:50:33 +08:00
|
|
|
test("shows vote", async function (assert) {
|
|
|
|
this.setProperties({
|
2024-09-04 16:38:22 +08:00
|
|
|
post: EmberObject.create({
|
|
|
|
id: 42,
|
|
|
|
topic: {
|
|
|
|
archived: false,
|
|
|
|
},
|
|
|
|
user_id: 29,
|
|
|
|
}),
|
|
|
|
poll: new TrackedObject({
|
|
|
|
name: "poll",
|
|
|
|
type: "regular",
|
|
|
|
status: "closed",
|
|
|
|
results: "always",
|
|
|
|
options: [
|
|
|
|
{ id: "1f972d1df351de3ce35a787c89faad29", html: "yes", votes: 1 },
|
|
|
|
{ id: "d7ebc3a9beea2e680815a1e4f57d6db6", html: "no", votes: 0 },
|
|
|
|
],
|
|
|
|
voters: 1,
|
|
|
|
chart_type: "bar",
|
2024-08-02 14:50:33 +08:00
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
2024-09-04 16:38:22 +08:00
|
|
|
await render(hbs`<Poll @post={{this.post}} @poll={{this.poll}} />`);
|
2024-08-02 14:50:33 +08:00
|
|
|
|
2024-09-04 16:38:22 +08:00
|
|
|
assert.dom(".results li:nth-of-type(1) .option p").hasText("100% yes");
|
|
|
|
assert.dom(".results li:nth-of-type(2) .option p").hasText("0% no");
|
2024-08-02 14:50:33 +08:00
|
|
|
});
|
|
|
|
|
2024-08-13 15:29:16 +08:00
|
|
|
test("does not show results after voting when results are to be shown only on closed", async function (assert) {
|
|
|
|
this.setProperties({
|
2024-09-04 16:38:22 +08:00
|
|
|
post: EmberObject.create({
|
|
|
|
id: 42,
|
|
|
|
topic: {
|
|
|
|
archived: false,
|
|
|
|
},
|
|
|
|
user_id: 29,
|
|
|
|
}),
|
|
|
|
poll: new TrackedObject({
|
|
|
|
name: "poll",
|
|
|
|
type: "regular",
|
|
|
|
status: "open",
|
|
|
|
results: "on_close",
|
|
|
|
options: [
|
|
|
|
{ id: "1f972d1df351de3ce35a787c89faad29", html: "yes" },
|
|
|
|
{ id: "d7ebc3a9beea2e680815a1e4f57d6db6", html: "no" },
|
|
|
|
],
|
|
|
|
voters: 1,
|
|
|
|
chart_type: "bar",
|
2024-08-13 15:29:16 +08:00
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
2024-09-04 16:38:22 +08:00
|
|
|
await render(hbs`<Poll @post={{this.post}} @poll={{this.poll}} />`);
|
2024-08-13 15:29:16 +08:00
|
|
|
|
2024-09-04 16:38:22 +08:00
|
|
|
assert.dom("ul.options").exists("options are shown");
|
|
|
|
assert.dom("ul.results").doesNotExist("results are not shown");
|
2024-08-13 15:29:16 +08:00
|
|
|
});
|
|
|
|
|
2022-07-11 18:29:44 +08:00
|
|
|
test("can vote", async function (assert) {
|
2024-07-04 19:34:48 +08:00
|
|
|
this.setProperties({
|
2024-09-04 16:38:22 +08:00
|
|
|
post: EmberObject.create({
|
|
|
|
id: 42,
|
|
|
|
topic: {
|
|
|
|
archived: false,
|
|
|
|
},
|
|
|
|
user_id: 29,
|
|
|
|
}),
|
|
|
|
poll: new TrackedObject({
|
|
|
|
name: "poll",
|
|
|
|
type: "regular",
|
|
|
|
status: "open",
|
|
|
|
results: "always",
|
|
|
|
options: [
|
|
|
|
{ id: "1f972d1df351de3ce35a787c89faad29", html: "yes", votes: 0 },
|
|
|
|
{ id: "d7ebc3a9beea2e680815a1e4f57d6db6", html: "no", votes: 0 },
|
|
|
|
],
|
|
|
|
voters: 0,
|
|
|
|
chart_type: "bar",
|
2024-07-04 19:34:48 +08:00
|
|
|
}),
|
|
|
|
});
|
2020-10-08 05:08:19 +08:00
|
|
|
|
2024-09-04 16:38:22 +08:00
|
|
|
await render(hbs`<Poll @post={{this.post}} @poll={{this.poll}} />`);
|
2020-02-18 18:13:19 +08:00
|
|
|
|
2022-07-11 18:29:44 +08:00
|
|
|
requests = 0;
|
2020-02-18 18:13:19 +08:00
|
|
|
|
2024-07-04 19:34:48 +08:00
|
|
|
await click(
|
|
|
|
"li[data-poll-option-id='1f972d1df351de3ce35a787c89faad29'] button"
|
|
|
|
);
|
2022-07-11 18:29:44 +08:00
|
|
|
assert.strictEqual(requests, 1);
|
2024-09-04 16:38:22 +08:00
|
|
|
assert.dom(".chosen").exists({ count: 1 });
|
2020-02-18 18:13:19 +08:00
|
|
|
|
2022-07-11 18:29:44 +08:00
|
|
|
await click(".toggle-results");
|
2024-09-04 16:38:22 +08:00
|
|
|
assert
|
|
|
|
.dom("li[data-poll-option-id='1f972d1df351de3ce35a787c89faad29']")
|
|
|
|
.exists({ count: 1 });
|
2022-07-11 18:29:44 +08:00
|
|
|
});
|
2020-02-18 18:13:19 +08:00
|
|
|
|
2022-07-11 18:29:44 +08:00
|
|
|
test("cannot vote if not member of the right group", async function (assert) {
|
2024-07-04 19:34:48 +08:00
|
|
|
this.setProperties({
|
2024-09-04 16:38:22 +08:00
|
|
|
post: EmberObject.create({
|
|
|
|
id: 42,
|
|
|
|
topic: {
|
|
|
|
archived: false,
|
|
|
|
},
|
|
|
|
user_id: 29,
|
|
|
|
}),
|
|
|
|
poll: new TrackedObject({
|
|
|
|
name: "poll",
|
|
|
|
type: "regular",
|
|
|
|
status: "open",
|
|
|
|
results: "always",
|
|
|
|
options: [
|
|
|
|
{ id: "1f972d1df351de3ce35a787c89faad29", html: "yes", votes: 0 },
|
|
|
|
{ id: "d7ebc3a9beea2e680815a1e4f57d6db6", html: "no", votes: 0 },
|
|
|
|
],
|
|
|
|
voters: 0,
|
|
|
|
chart_type: "bar",
|
|
|
|
groups: "foo",
|
2024-07-04 19:34:48 +08:00
|
|
|
}),
|
|
|
|
});
|
2022-01-07 11:05:52 +08:00
|
|
|
|
2024-09-04 16:38:22 +08:00
|
|
|
await render(hbs`<Poll @post={{this.post}} @poll={{this.poll}} />`);
|
2022-07-11 18:29:44 +08:00
|
|
|
|
|
|
|
requests = 0;
|
|
|
|
|
2024-07-04 19:34:48 +08:00
|
|
|
await click(
|
|
|
|
"li[data-poll-option-id='1f972d1df351de3ce35a787c89faad29'] button"
|
|
|
|
);
|
2024-09-04 16:38:22 +08:00
|
|
|
assert
|
|
|
|
.dom(".poll-container .alert")
|
|
|
|
.hasText(I18n.t("poll.results.groups.title", { groups: "foo" }));
|
2022-07-11 18:29:44 +08:00
|
|
|
assert.strictEqual(requests, 0);
|
2024-09-04 16:38:22 +08:00
|
|
|
assert.dom(".chosen").doesNotExist();
|
2022-07-11 18:29:44 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test("voting on a multiple poll with no min attribute", async function (assert) {
|
2024-07-04 19:34:48 +08:00
|
|
|
this.setProperties({
|
2024-09-04 16:38:22 +08:00
|
|
|
post: EmberObject.create({
|
|
|
|
id: 42,
|
|
|
|
topic: {
|
|
|
|
archived: false,
|
|
|
|
},
|
|
|
|
user_id: 29,
|
|
|
|
}),
|
|
|
|
poll: EmberObject.create({
|
|
|
|
name: "poll",
|
|
|
|
type: "multiple",
|
|
|
|
status: "open",
|
|
|
|
results: "always",
|
|
|
|
max: 2,
|
|
|
|
options: [
|
|
|
|
{ id: "1f972d1df351de3ce35a787c89faad29", html: "yes", votes: 0 },
|
|
|
|
{ id: "d7ebc3a9beea2e680815a1e4f57d6db6", html: "no", votes: 0 },
|
|
|
|
],
|
|
|
|
voters: 0,
|
|
|
|
chart_type: "bar",
|
2024-07-04 19:34:48 +08:00
|
|
|
}),
|
|
|
|
});
|
2024-09-04 16:38:22 +08:00
|
|
|
await render(hbs`<Poll @post={{this.post}} @poll={{this.poll}} />`);
|
2022-01-07 11:05:52 +08:00
|
|
|
|
2024-09-04 16:38:22 +08:00
|
|
|
assert.dom(".poll-buttons .cast-votes:disabled").exists();
|
2024-07-04 19:34:48 +08:00
|
|
|
|
|
|
|
await click(
|
|
|
|
"li[data-poll-option-id='1f972d1df351de3ce35a787c89faad29'] button"
|
2023-09-11 22:15:44 +08:00
|
|
|
);
|
2022-01-07 11:05:52 +08:00
|
|
|
|
2022-07-11 18:29:44 +08:00
|
|
|
await click(".poll-buttons .cast-votes");
|
2024-09-04 16:38:22 +08:00
|
|
|
assert.dom(".chosen").exists();
|
2022-07-11 18:29:44 +08:00
|
|
|
});
|
|
|
|
});
|