Skip to content

Commit

Permalink
add js function AddComment and system test for function
Browse files Browse the repository at this point in the history
  • Loading branch information
nstjean committed Nov 2, 2019
1 parent 12a9137 commit 8ad2ca3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
12 changes: 12 additions & 0 deletions app/assets/javascripts/comment.js
Expand Up @@ -51,3 +51,15 @@ function insertTitleSuggestionTemplate() {
var newText = currentText+template;
element.val(newText);
}

// JS API for submitting comments
function addComment(comment, selector) {
selector = selector || '.comment-form'; // class instead of id because of the bound function on line 3
$.ajax({
url: $(selector).attr('action'), // grab the URL from the form itself
data: { body: comment },
success: (event, success) => {
$(selector).trigger('ajax:success', event);
}
});
}
4 changes: 3 additions & 1 deletion app/views/notes/_comment.html.erb
Expand Up @@ -127,7 +127,9 @@
<div style="border: 1px solid #e7e7e7;padding: 18px;" id="c<%= comment.cid %>show">

<% comment_body = comment.render_body %>
<p class="comment-body" id="comment-body-<%= comment.cid %>"><%= raw insert_extras(filtered_comment_body(comment_body)) %></p>
<div class="comment-body" id="comment-body-<%= comment.cid %>">
<%= raw insert_extras(filtered_comment_body(comment_body)) %>
</div>

<% if contain_trimmed_body?(comment_body) %>
<span style="background-color: #DCDCDC; border-radius: 7px; cursor: pointer;" ><a class="email-toggle" data-toggle="collapse" data-target="#comment-<%= comment.cid %>-extra-content" style='text-decoration: none;'>&nbsp;&ensp;&#x2022&#x2022&#x2022&nbsp;&ensp;</a></span>
Expand Down
26 changes: 26 additions & 0 deletions test/system/comment_test.rb
@@ -0,0 +1,26 @@
require 'test_helper'
require "application_system_test_case"
# https://guides.rubyonrails.org/testing.html#implementing-a-system-test

class CommentTest < ApplicationSystemTestCase
Capybara.default_max_wait_time = 60

test 'adding a tag via javascript' do
visit '/'

click_on 'Login'

fill_in("username-login", with: "jeff")
fill_in("password-signup", with: "secretive")
click_on "Log in"

visit "/wiki/wiki-page-path/comments"

# run the javascript function
page.evaluate_script("addComment('fantastic four')")

# check that the tag showed up on the page
assert_selector('#comments-list .comment-body p', text: 'fantastic four')
end

end

0 comments on commit 8ad2ca3

Please sign in to comment.