REFACTOR: Move test setup to a module

This is long overdue. We had a lot of (not linted) code to initialize
our test suite as part of the Ruby `test_helper.js` bundle.

This refactor moves that out to a `setup-tests` module, which imports
all the modules properly, rather than using `require`.

It also removes the global `server` variable which some tests were using
for pretender. Those tests are fixed, and in the case of widget tests,
support for a `pretend()` was added, which mimics our acceptance tests.

One problematic test was removed, which overwrites `/posts` - this could
break tons of other tests depending on order.
This commit is contained in:
Robin Ward
2020-10-07 17:08:19 -04:00
parent 5130b4d674
commit ef7d99b0a8
12 changed files with 563 additions and 566 deletions

View File

@@ -1,6 +1,6 @@
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
acceptance("Rendering polls with pie charts - desktop", {
acceptance("Rendering polls with pie charts", {
loggedIn: true,
settings: { poll_enabled: true, poll_groupable_user_fields: "something" },
});

View File

@@ -7,6 +7,37 @@ acceptance("Rendering polls with bar charts - desktop", {
beforeEach() {
clearPopupMenuOptionsCallback();
},
pretend(server) {
server.get("/polls/voters.json", (request) => {
let body = {};
if (
request.queryParams.option_id === "68b434ff88aeae7054e42cd05a4d9056"
) {
body = {
voters: {
"68b434ff88aeae7054e42cd05a4d9056": [
{
id: 777,
username: "bruce777",
avatar_template: "/images/avatar.png",
name: "Bruce Wayne",
},
],
},
};
} else {
body = {
voters: Array.from(new Array(5), (_, i) => ({
id: 600 + i,
username: `bruce${600 + i}`,
avatar_template: "/images/avatar.png",
name: "Bruce Wayne",
})),
};
}
return [200, { "Content-Type": "application/json" }, body];
});
},
});
test("Polls", async (assert) => {
@@ -43,24 +74,6 @@ test("Public poll", async (assert) => {
"it should display the right number of voters"
);
// eslint-disable-next-line
server.get("/polls/voters.json", () => {
const body = {
voters: {
"68b434ff88aeae7054e42cd05a4d9056": [
{
id: 777,
username: "bruce777",
avatar_template: "/images/avatar.png",
name: "Bruce Wayne",
},
],
},
};
return [200, { "Content-Type": "application/json" }, body];
});
await click(".poll-voters-toggle-expand:first a");
assert.equal(
@@ -89,20 +102,6 @@ test("Public number poll", async (assert) => {
"user URL does not exist"
);
// eslint-disable-next-line
server.get("/polls/voters.json", () => {
const body = {
voters: Array.from(new Array(5), (_, i) => ({
id: 600 + i,
username: `bruce${600 + i}`,
avatar_template: "/images/avatar.png",
name: "Bruce Wayne",
})),
};
return [200, { "Content-Type": "application/json" }, body];
});
await click(".poll-voters-toggle-expand:first a");
assert.equal(

View File

@@ -8,6 +8,21 @@ acceptance("Rendering polls with bar charts - mobile", {
beforeEach() {
clearPopupMenuOptionsCallback();
},
pretend(server) {
// eslint-disable-next-line
server.get("/polls/voters.json", () => {
const body = {
voters: Array.from(new Array(10), (_, i) => ({
id: 500 + i,
username: `bruce${500 + i}`,
avatar_template: "/images/avatar.png",
name: "Bruce Wayne",
})),
};
return [200, { "Content-Type": "application/json" }, body];
});
},
});
test("Public number poll", async (assert) => {
@@ -29,20 +44,6 @@ test("Public number poll", async (assert) => {
"user URL does not exist"
);
// eslint-disable-next-line
server.get("/polls/voters.json", () => {
const body = {
voters: Array.from(new Array(10), (_, i) => ({
id: 500 + i,
username: `bruce${500 + i}`,
avatar_template: "/images/avatar.png",
name: "Bruce Wayne",
})),
};
return [200, { "Content-Type": "application/json" }, body];
});
await click(".poll-voters-toggle-expand:first a");
assert.equal(

View File

@@ -5,7 +5,58 @@ import {
widgetTest,
} from "discourse/tests/helpers/widget-test";
moduleForWidget("discourse-poll");
let requests = 0;
moduleForWidget("discourse-poll", {
pretend(server) {
server.put("/polls/vote", () => {
++requests;
return [
200,
{ "Content-Type": "application/json" },
{
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",
},
vote: ["1f972d1df351de3ce35a787c89faad29"],
},
];
});
server.put("/polls/vote", () => {
++requests;
return [
200,
{ "Content-Type": "application/json" },
{
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",
groups: "foo",
},
vote: ["1f972d1df351de3ce35a787c89faad29"],
},
];
});
},
});
const template = `{{mount-widget
widget="discourse-poll"
@@ -44,31 +95,7 @@ widgetTest("can vote", {
},
async test(assert) {
let requests = 0;
/* global server */
server.put("/polls/vote", () => {
++requests;
return [
200,
{ "Content-Type": "application/json" },
{
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",
},
vote: ["1f972d1df351de3ce35a787c89faad29"],
},
];
});
requests = 0;
await click("li[data-poll-option-id='1f972d1df351de3ce35a787c89faad29']");
assert.equal(requests, 1);
@@ -115,32 +142,7 @@ widgetTest("cannot vote if not member of the right group", {
},
async test(assert) {
let requests = 0;
/* global server */
server.put("/polls/vote", () => {
++requests;
return [
200,
{ "Content-Type": "application/json" },
{
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",
groups: "foo",
},
vote: ["1f972d1df351de3ce35a787c89faad29"],
},
];
});
requests = 0;
await click("li[data-poll-option-id='1f972d1df351de3ce35a787c89faad29']");
assert.equal(