mirror of
https://github.com/discourse/discourse.git
synced 2025-05-26 06:01:43 +08:00
DEV: Remove widget wrapper from poll plugin (#28666)
- Uses `helper.renderGlimmer` with GJS to render the `<Poll` component without any widgets - Moves some logic into component, so that only `@post`, `@poll` and `@titleHTML` need to be passed into the component (no more 'attrs') - Updates `modifyClass` calls to modern syntax - Replaces observer in `Post` model with a native setter & tracked property - Replaced Poll EmberObject instances with TrackedObject - Updated component tests with new arguments - Updated some tests to qunit-dom - Fixed up core `repliesBelow` and `repliesAbove` logic to create post models properly. Previously it was passing 'transformed' versions of posts into the model, which doesn't make sense.
This commit is contained in:
@ -1,15 +1,10 @@
|
||||
import EmberObject from "@ember/object";
|
||||
import { click, render } from "@ember/test-helpers";
|
||||
import { TrackedObject } from "@ember-compat/tracked-built-ins";
|
||||
import hbs from "htmlbars-inline-precompile";
|
||||
import { module, test } from "qunit";
|
||||
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
|
||||
import pretender, { response } from "discourse/tests/helpers/create-pretender";
|
||||
import {
|
||||
count,
|
||||
exists,
|
||||
query,
|
||||
queryAll,
|
||||
} from "discourse/tests/helpers/qunit-helpers";
|
||||
import I18n from "discourse-i18n";
|
||||
|
||||
let requests = 0;
|
||||
@ -48,112 +43,86 @@ module("Poll | Component | poll", function (hooks) {
|
||||
|
||||
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: [],
|
||||
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",
|
||||
}),
|
||||
preloadedVoters: [],
|
||||
});
|
||||
|
||||
await render(
|
||||
hbs`<Poll @attrs={{this.attributes}} @preloadedVoters={{this.preloadedVoters}} />`
|
||||
);
|
||||
await render(hbs`<Poll @post={{this.post}} @poll={{this.poll}} />`);
|
||||
|
||||
assert.deepEqual(
|
||||
Array.from(queryAll(".results li .option p")).map(
|
||||
(span) => span.innerText
|
||||
),
|
||||
["100% yes", "0% no"]
|
||||
);
|
||||
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");
|
||||
});
|
||||
|
||||
test("does not show results after voting when results are to be shown only on closed", async function (assert) {
|
||||
this.setProperties({
|
||||
attributes: EmberObject.create({
|
||||
post: EmberObject.create({
|
||||
id: 42,
|
||||
topic: {
|
||||
archived: false,
|
||||
},
|
||||
user_id: 29,
|
||||
}),
|
||||
hasSavedVote: true,
|
||||
poll: EmberObject.create({
|
||||
name: "poll",
|
||||
type: "regular",
|
||||
status: "open",
|
||||
results: "on_close",
|
||||
options: [
|
||||
{ id: "1f972d1df351de3ce35a787c89faad29", html: "yes" },
|
||||
{ id: "d7ebc3a9beea2e680815a1e4f57d6db6", html: "no" },
|
||||
],
|
||||
voters: 1,
|
||||
chart_type: "bar",
|
||||
}),
|
||||
vote: ["1f972d1df351de3ce35a787c89faad29"],
|
||||
groupableUserFields: [],
|
||||
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",
|
||||
}),
|
||||
preloadedVoters: [],
|
||||
});
|
||||
|
||||
await render(
|
||||
hbs`<Poll @attrs={{this.attributes}} @preloadedVoters={{this.preloadedVoters}} />`
|
||||
);
|
||||
await render(hbs`<Poll @post={{this.post}} @poll={{this.poll}} />`);
|
||||
|
||||
assert.ok(exists("ul.options"), "options are shown");
|
||||
assert.ok(!exists("ul.results"), "results are not shown");
|
||||
assert.dom("ul.options").exists("options are shown");
|
||||
assert.dom("ul.results").doesNotExist("results are not shown");
|
||||
});
|
||||
|
||||
test("can 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: "open",
|
||||
results: "always",
|
||||
options: [
|
||||
{ id: "1f972d1df351de3ce35a787c89faad29", html: "yes", votes: 0 },
|
||||
{ id: "d7ebc3a9beea2e680815a1e4f57d6db6", html: "no", votes: 0 },
|
||||
],
|
||||
voters: 0,
|
||||
chart_type: "bar",
|
||||
}),
|
||||
vote: [],
|
||||
groupableUserFields: [],
|
||||
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",
|
||||
}),
|
||||
preloadedVoters: [],
|
||||
});
|
||||
|
||||
await render(
|
||||
hbs`<Poll @attrs={{this.attributes}} @preloadedVoters={{this.preloadedVoters}} />`
|
||||
);
|
||||
await render(hbs`<Poll @post={{this.post}} @poll={{this.poll}} />`);
|
||||
|
||||
requests = 0;
|
||||
|
||||
@ -161,100 +130,84 @@ module("Poll | Component | poll", function (hooks) {
|
||||
"li[data-poll-option-id='1f972d1df351de3ce35a787c89faad29'] button"
|
||||
);
|
||||
assert.strictEqual(requests, 1);
|
||||
assert.strictEqual(count(".chosen"), 1);
|
||||
assert.dom(".chosen").exists({ count: 1 });
|
||||
|
||||
await click(".toggle-results");
|
||||
assert.strictEqual(
|
||||
count("li[data-poll-option-id='1f972d1df351de3ce35a787c89faad29']"),
|
||||
1
|
||||
);
|
||||
assert
|
||||
.dom("li[data-poll-option-id='1f972d1df351de3ce35a787c89faad29']")
|
||||
.exists({ count: 1 });
|
||||
});
|
||||
|
||||
test("cannot vote if not member of the right group", 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: "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: [],
|
||||
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",
|
||||
}),
|
||||
preloadedVoters: [],
|
||||
});
|
||||
|
||||
await render(
|
||||
hbs`<Poll @attrs={{this.attributes}} @preloadedVoters={{this.preloadedVoters}} />`
|
||||
);
|
||||
await render(hbs`<Poll @post={{this.post}} @poll={{this.poll}} />`);
|
||||
|
||||
requests = 0;
|
||||
|
||||
await click(
|
||||
"li[data-poll-option-id='1f972d1df351de3ce35a787c89faad29'] button"
|
||||
);
|
||||
assert.strictEqual(
|
||||
query(".poll-container .alert").innerText,
|
||||
I18n.t("poll.results.groups.title", { groups: "foo" })
|
||||
);
|
||||
assert
|
||||
.dom(".poll-container .alert")
|
||||
.hasText(I18n.t("poll.results.groups.title", { groups: "foo" }));
|
||||
assert.strictEqual(requests, 0);
|
||||
assert.ok(!exists(".chosen"));
|
||||
assert.dom(".chosen").doesNotExist();
|
||||
});
|
||||
|
||||
test("voting on a multiple poll with no min attribute", 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: "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: [],
|
||||
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",
|
||||
}),
|
||||
preloadedVoters: [],
|
||||
});
|
||||
await render(
|
||||
hbs`<Poll @attrs={{this.attributes}} @preloadedVoters={{this.preloadedVoters}} />`
|
||||
);
|
||||
await render(hbs`<Poll @post={{this.post}} @poll={{this.poll}} />`);
|
||||
|
||||
assert.ok(exists(".poll-buttons .cast-votes:disabled"));
|
||||
assert.dom(".poll-buttons .cast-votes:disabled").exists();
|
||||
|
||||
await click(
|
||||
"li[data-poll-option-id='1f972d1df351de3ce35a787c89faad29'] button"
|
||||
);
|
||||
|
||||
await click(".poll-buttons .cast-votes");
|
||||
assert.ok(exists(".chosen"));
|
||||
assert.dom(".chosen").exists();
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user