2023-10-11 02:38:59 +08:00
|
|
|
import EmberObject from "@ember/object";
|
|
|
|
import { click, render } from "@ember/test-helpers";
|
|
|
|
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";
|
2022-10-20 01:10:06 +08:00
|
|
|
import {
|
|
|
|
count,
|
|
|
|
exists,
|
|
|
|
query,
|
|
|
|
queryAll,
|
|
|
|
} from "discourse/tests/helpers/qunit-helpers";
|
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({
|
|
|
|
attributes: EmberObject.create({
|
|
|
|
post: EmberObject.create({
|
|
|
|
id: 42,
|
|
|
|
topic: {
|
|
|
|
archived: false,
|
|
|
|
},
|
|
|
|
user_id: 29,
|
|
|
|
}),
|
|
|
|
poll: EmberObject.create({
|
|
|
|
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",
|
|
|
|
}),
|
|
|
|
vote: [],
|
|
|
|
groupableUserFields: [],
|
|
|
|
}),
|
|
|
|
preloadedVoters: [],
|
|
|
|
});
|
|
|
|
|
|
|
|
await render(
|
|
|
|
hbs`<Poll @attrs={{this.attributes}} @preloadedVoters={{this.preloadedVoters}} />`
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
Array.from(queryAll(".results li .option p")).map(
|
|
|
|
(span) => span.innerText
|
|
|
|
),
|
|
|
|
["100% yes", "0% no"]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-07-11 18:29:44 +08:00
|
|
|
test("can vote", async function (assert) {
|
2024-07-04 19:34:48 +08:00
|
|
|
this.setProperties({
|
|
|
|
attributes: EmberObject.create({
|
2023-09-11 22:15:44 +08:00
|
|
|
post: EmberObject.create({
|
|
|
|
id: 42,
|
|
|
|
topic: {
|
|
|
|
archived: false,
|
|
|
|
},
|
2024-07-04 19:34:48 +08:00
|
|
|
user_id: 29,
|
2023-09-11 22:15:44 +08:00
|
|
|
}),
|
|
|
|
poll: EmberObject.create({
|
|
|
|
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",
|
|
|
|
}),
|
|
|
|
vote: [],
|
|
|
|
groupableUserFields: [],
|
2024-07-04 19:34:48 +08:00
|
|
|
}),
|
|
|
|
preloadedVoters: [],
|
|
|
|
});
|
2020-10-08 05:08:19 +08:00
|
|
|
|
2023-09-11 22:15:44 +08:00
|
|
|
await render(
|
2024-08-02 14:50:33 +08:00
|
|
|
hbs`<Poll @attrs={{this.attributes}} @preloadedVoters={{this.preloadedVoters}} />`
|
2023-09-11 22:15:44 +08:00
|
|
|
);
|
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);
|
|
|
|
assert.strictEqual(count(".chosen"), 1);
|
2020-02-18 18:13:19 +08:00
|
|
|
|
2022-07-11 18:29:44 +08:00
|
|
|
await click(".toggle-results");
|
|
|
|
assert.strictEqual(
|
|
|
|
count("li[data-poll-option-id='1f972d1df351de3ce35a787c89faad29']"),
|
|
|
|
1
|
|
|
|
);
|
|
|
|
});
|
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({
|
|
|
|
attributes: EmberObject.create({
|
2023-09-11 22:15:44 +08:00
|
|
|
post: EmberObject.create({
|
|
|
|
id: 42,
|
|
|
|
topic: {
|
|
|
|
archived: false,
|
|
|
|
},
|
2024-07-04 19:34:48 +08:00
|
|
|
user_id: 29,
|
2023-09-11 22:15:44 +08:00
|
|
|
}),
|
|
|
|
poll: EmberObject.create({
|
|
|
|
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",
|
|
|
|
}),
|
|
|
|
vote: [],
|
|
|
|
groupableUserFields: [],
|
2024-07-04 19:34:48 +08:00
|
|
|
}),
|
|
|
|
preloadedVoters: [],
|
|
|
|
});
|
2022-01-07 11:05:52 +08:00
|
|
|
|
2023-09-11 22:15:44 +08:00
|
|
|
await render(
|
2024-08-02 14:50:33 +08:00
|
|
|
hbs`<Poll @attrs={{this.attributes}} @preloadedVoters={{this.preloadedVoters}} />`
|
2023-09-11 22:15:44 +08:00
|
|
|
);
|
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"
|
|
|
|
);
|
2022-07-11 18:29:44 +08:00
|
|
|
assert.strictEqual(
|
|
|
|
query(".poll-container .alert").innerText,
|
|
|
|
I18n.t("poll.results.groups.title", { groups: "foo" })
|
|
|
|
);
|
|
|
|
assert.strictEqual(requests, 0);
|
|
|
|
assert.ok(!exists(".chosen"));
|
|
|
|
});
|
|
|
|
|
|
|
|
test("voting on a multiple poll with no min attribute", async function (assert) {
|
2024-07-04 19:34:48 +08:00
|
|
|
this.setProperties({
|
|
|
|
attributes: EmberObject.create({
|
2023-09-11 22:15:44 +08:00
|
|
|
post: EmberObject.create({
|
|
|
|
id: 42,
|
|
|
|
topic: {
|
|
|
|
archived: false,
|
|
|
|
},
|
2024-07-04 19:34:48 +08:00
|
|
|
user_id: 29,
|
2023-09-11 22:15:44 +08:00
|
|
|
}),
|
|
|
|
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",
|
|
|
|
}),
|
|
|
|
vote: [],
|
|
|
|
groupableUserFields: [],
|
2024-07-04 19:34:48 +08:00
|
|
|
}),
|
|
|
|
preloadedVoters: [],
|
|
|
|
});
|
|
|
|
await render(
|
2024-08-02 14:50:33 +08:00
|
|
|
hbs`<Poll @attrs={{this.attributes}} @preloadedVoters={{this.preloadedVoters}} />`
|
2023-09-11 22:15:44 +08:00
|
|
|
);
|
2022-01-07 11:05:52 +08:00
|
|
|
|
2024-07-04 19:34:48 +08:00
|
|
|
assert.ok(exists(".poll-buttons .cast-votes:disabled"));
|
|
|
|
|
|
|
|
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");
|
|
|
|
assert.ok(exists(".chosen"));
|
|
|
|
});
|
|
|
|
});
|