/* eslint-disable id-length, max-lines */ /* global jest */ const $ = require("jquery"); // Ability to spy on the jQuery methods inside the component in order to test // the sub-elements correctly. Needs to be defined before the modules are loaded // in order for them to define the $ variable correctly. window.$ = jest.fn().mockImplementation((...args) => $(...args)); window.$.ajax = jest.fn().mockImplementation((...args) => $.ajax(...args)); window.$.extend = jest.fn().mockImplementation((...args) => $.extend(...args)); // Rails.ajax is used by the fetching/polling of the comments import Rails from "@rails/ujs"; jest.mock("@rails/ujs"); window.Rails = Rails; // Fake timers for testing polling jest.useFakeTimers(); import { createCharacterCounter } from "src/decidim/input_character_counter"; import Configuration from "src/decidim/configuration"; // Component is loaded with require because using import loads it before $ has been mocked // so tests are not able to check the spied behaviours const CommentsComponent = require("./comments.component_for_testing.js"); // Create a dummy foundation jQuery method for the comments component to call $.fn.foundation = () => {}; // Create the configuration object to make the configurations available for the tests window.Decidim = {} window.Decidim.config = new Configuration() describe("CommentsComponent", () => { const selector = "#comments-for-Dummy-123"; let subject = null; let $doc = null; let $container = null; let addComment = null; let orderLinks = null; let allToggles = null; let allTextareas = null; let allForms = null; const spyOnAddComment = (methodToSpy) => { addComment.each((i) => { addComment[i].$ = $(addComment[i]); addComment[i].opinionToggles = $(".opinion-toggle button", addComment[i].$); addComment[i].commentForm = $("form", addComment[i].$); addComment[i].commentTextarea = $("textarea", addComment[i].commentForm); if (methodToSpy) { jest.spyOn(addComment[i].opinionToggles, methodToSpy); jest.spyOn(addComment[i].commentForm, methodToSpy); jest.spyOn(addComment[i].commentTextarea, methodToSpy); } }); jest.spyOn(window, "$").mockImplementation((...args) => { const jqSelector = args[0]; const parent = args[1]; if (jqSelector === document) { return $doc; } else if (jqSelector === ".comment-order-by a.comment-order-by__item" && parent.is(subject.$element)) { return orderLinks; } else if (jqSelector === ".add-comment" && parent.is(subject.$element)) { return addComment; } else if (jqSelector === ".add-comment .opinion-toggle button" && parent.is(subject.$element)) { return allToggles; } else if (jqSelector === ".add-comment textarea" && parent.is(subject.$element)) { return allTextareas; } else if (jqSelector === ".add-comment form" && parent.is(subject.$element)) { return allForms; } const addCommentsArray = addComment.toArray(); for (let i = 0; i < addCommentsArray.length; i += 1) { if (jqSelector === ".opinion-toggle button" && parent.is(addCommentsArray[i].$)) { return addCommentsArray[i].opinionToggles; } else if (jqSelector === "form" && parent.is(addCommentsArray[i].$)) { return addCommentsArray[i].commentForm; } else if (jqSelector === "textarea" && parent.is(addCommentsArray[i].commentForm)) { return addCommentsArray[i].commentTextarea; } } return $(...args); }); } const generateCommentForm = (modelName, modelId) => { return `
There is an error in this field. 1000 characters left
`; }; const generateFlagModalForm = (commentId) => { return `