From 871650053c80404ba9b439cf793fc4edae062102 Mon Sep 17 00:00:00 2001 From: Nick Taylor Date: Tue, 30 Mar 2021 07:02:54 -0400 Subject: [PATCH] [15 minute fix] Fixed bug with templates not being inserted for comments. (#13149) * Fixed bug with templates not being inserted for comments. * Removed some trailing white space from a heading. * Added custom Cypress command to create an article. * Added E2E tests for creating comments on an article. * Fixed adding a comment issue caused by mention auto-complete. * Fixed a selector for the E2E tests. * Removed some unnecessary white space and also making code climate happy. * Updated E2E test. * Added a bit more to the E2E tests. * Now validation for comment doesn't need to loop through textareas. * Merge remote-tracking branch 'origin/master' into nickytonline/fix-comment-template-insertion-bug * Removed a similar spec file and added tests to the other file. * wip * Fixed bug where a response template could not be submitted. * Added/updated E2E tests. * Reverted a small change to condition for showing the combobox popover. * Undoing some white space changes as not related to PR. * Trigger Build * Put back article seed data as it's used in tests outside of comment tests. --- .../initializeCommentsPage.js.erb | 26 +- .../responseTemplates/responseTemplates.js | 10 +- .../articles/_full_comment_area.html.erb | 2 +- .../articleFlows/commentOnArticle.spec.js | 565 ++++++++++-------- spec/support/seeds/seeds_e2e.rb | 1 - 5 files changed, 344 insertions(+), 260 deletions(-) diff --git a/app/assets/javascripts/initializers/initializeCommentsPage.js.erb b/app/assets/javascripts/initializers/initializeCommentsPage.js.erb index a09cbb2a0..960c10cbb 100644 --- a/app/assets/javascripts/initializers/initializeCommentsPage.js.erb +++ b/app/assets/javascripts/initializers/initializeCommentsPage.js.erb @@ -256,9 +256,17 @@ function handleCommentSubmit(event) { var mainCommentsForm = document.getElementById("new_comment"); mainCommentsForm.classList.remove("submitting"); mainCommentsForm.classList.remove('preview-open'); - const textArea = form.getElementsByClassName("comment-textarea")[0]; - textArea.closest('.comment-form').classList.remove('comment-form--initiated'); - textArea.value = newComment.comment_template || ""; + + const commentInputs = [...form.getElementsByClassName("comment-textarea")] + commentInputs[0].closest('.comment-form').classList.remove('comment-form--initiated'); + + // Clearing out all comment textboxes because + // there is an additional one generated by the comment + // mention auto-complete component + commentInputs.forEach(input => { + input.value = newComment.comment_template || ""; + }); + var preview = document.getElementById("preview-div"); preview.classList.add("preview-toggle"); preview.innerHTML = ""; @@ -447,12 +455,12 @@ function handleButtonsActivation(event) { } function validateField(event) { - var textarea = event.target.closest('.comment-form').getElementsByClassName('comment-textarea')[0]; - if (textarea) { - var commentField = textarea.value; - if (commentField == '') { - event.preventDefault(); - } + // We only need to validate the textarea that is not the comment mention auto-complete component + const textArea = event.target.form.querySelector('.comment-textarea:not([role=combobox])'); + + if (textArea.value === '') { + event.preventDefault(); + return; } } diff --git a/app/javascript/responseTemplates/responseTemplates.js b/app/javascript/responseTemplates/responseTemplates.js index 8cb58aaf4..cc9bdcba3 100644 --- a/app/javascript/responseTemplates/responseTemplates.js +++ b/app/javascript/responseTemplates/responseTemplates.js @@ -124,9 +124,12 @@ function addClickListeners(form) { ); insertButtons.forEach((button) => { - button.addEventListener('click', (e) => { - const { content } = e.target.dataset; - const textArea = form.getElementsByTagName('textarea')[0]; + button.addEventListener('click', (event) => { + const { content } = event.target.dataset; + // We need to grab the textarea that is not the comment mention auto-complete component + const textArea = event.target.form.querySelector( + '.comment-textarea:not([role=combobox])', + ); const textAreaReplaceable = textArea.value === null || textArea.value === '' || @@ -134,6 +137,7 @@ function addClickListeners(form) { if (textAreaReplaceable) { textArea.value = content; + textArea.dispatchEvent(new Event('input', { target: textArea })); textArea.focus(); responsesContainer.classList.toggle('hidden'); } diff --git a/app/views/articles/_full_comment_area.html.erb b/app/views/articles/_full_comment_area.html.erb index 696d9ba36..40ad1e3e2 100644 --- a/app/views/articles/_full_comment_area.html.erb +++ b/app/views/articles/_full_comment_area.html.erb @@ -2,7 +2,7 @@
<% if @article.show_comments %>
-

Discussion (<%= @article.comments_count %>)

+

Discussion (<%= @article.comments_count %>)