import ReplyToComment from "./reply-to-comment"; const commentHtml = `
This is a comment.
Reply
`; const formHtml = `

Leave a Reply

Your email address will not be published.

`; let comments; let form; let links; let inlineReply; beforeEach(() => { document.body.innerHTML = `${commentHtml}${formHtml}`; form = document.querySelector(".comments__foot"); links = document.querySelectorAll(".comment__reply"); inlineReply = document.querySelector(".comment__inline-reply"); comments = new ReplyToComment({ form, links }); }); describe("ReplyToComment", () => { it("Should initialise", () => { expect(comments.form).toBeInstanceOf(HTMLDivElement); expect(comments.links).toBeInstanceOf(Array); }); it("Should fail to initialise if form is missing", () => { expect(new ReplyToComment()).toEqual({}); }); it("Should fail to initialise if there are no links", () => { expect( new ReplyToComment({ form: document.querySelector("#respond"), }), ).toEqual({}); expect( new ReplyToComment({ form: document.querySelector("#respond"), links: document.querySelectorAll(".no-links-here"), }), ).toEqual({}); }); it("Should remove the form from the DOM when a reply link is clicked", () => { const event = new Event("click"); comments.handleLinkClick(event, links[0]); expect(form.innerHTML).toEqual(""); }); it("Should hide the link when it is clicked", () => { const event = new Event("click"); comments.handleLinkClick(event, links[0]); expect(links[0].classList.contains("is-hidden")).toBe(true); }); it("Should insert the form into the inline reply container", () => { comments.insertForm(inlineReply); const inlineForm = inlineReply.querySelectorAll("form"); expect(inlineForm.length).toEqual(1); }); it("Should update the comment_parent hidden element", () => { comments.insertForm(inlineReply, "123", "Mr Smith"); const { value } = inlineReply.querySelector("#comment_parent"); expect(value).toEqual("123"); }); it("Should add a cancel link to the inline form", () => { comments.insertForm(inlineReply); const el = inlineReply.querySelector("#cancel-comment-reply-link"); expect(el.style.display).toEqual("inline-block"); }); it("Should restore the reply link when the cancel link is clicked", () => { const event = new Event("click"); comments.insertForm(inlineReply); comments.handleCancelClick(event, inlineReply); expect(links[0].classList.contains("is-hidden")).toBe(false); }); it("Should remove the inline form when the cancel link is clicked", () => { const event = new Event("click"); comments.insertForm(inlineReply); comments.handleCancelClick(event, inlineReply); expect(inlineReply.innerHTML).toEqual(""); }); });