0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-11 02:59:07 +08:00
discourse/plugins/discourse-reactions/test/javascripts/acceptance/discourse-reactions-rollback-test.js
small-lovely-cat 48e7df1b35
FIX: discourse-reactions rollback not working (#36989)
The discourse-reactions plugin has a rollback function, when the
`toggle.json` request fails, the page should rollback the reactions to
the original state.

However, the function is broken, and the original `_rollbackState()`
function never works as the state itself changes when `toggleReaction`
is called. And the reaction state never goes back without
refreshing/reloading the window, leading to the heart/reactions leaving
on the page even when the request failed.

This commit fixs this by adding a new function `_captureState()`,
keeping the state in a const value, and revert it to the page data when
necessary.

Also, a spec for this is added to prevent regression.
2026-01-08 14:23:59 +10:00

45 lines
1.4 KiB
JavaScript
Vendored

import { click, visit } from "@ember/test-helpers";
import { test } from "qunit";
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
import ReactionsTopics from "../fixtures/reactions-topic-fixtures";
acceptance("Discourse Reactions - API Error Rollback", function (needs) {
needs.user();
needs.settings({
discourse_reactions_enabled: true,
discourse_reactions_enabled_reactions: "otter|open_mouth",
discourse_reactions_reaction_for_like: "heart",
discourse_reactions_like_icon: "heart",
});
needs.pretender((server, helper) => {
const topicPath = "/t/374.json";
server.get(topicPath, () => helper.response(ReactionsTopics[topicPath]));
server.put(
"/discourse-reactions/posts/:post_id/custom-reactions/:reaction/toggle.json",
() => helper.response(403, { errors: ["forbidden"] })
);
});
test("reverts reaction state when API call fails", async function (assert) {
await visit("/t/topic_with_reactions_and_likes/374");
const initialCount = document
.querySelector("#post_2 .reactions-counter")
?.textContent?.trim();
await click("#post_2 .btn-toggle-reaction-like");
const finalCount = document
.querySelector("#post_2 .reactions-counter")
?.textContent?.trim();
assert.strictEqual(
finalCount,
initialCount,
"reaction count reverts after API failure"
);
});
});