Skip to content

Commit

Permalink
allow js addComment and addTest to use URL and add test case for both
Browse files Browse the repository at this point in the history
  • Loading branch information
nstjean committed Nov 27, 2019
1 parent 2d3c642 commit 08962f6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
6 changes: 3 additions & 3 deletions app/assets/javascripts/comment.js
Expand Up @@ -53,8 +53,8 @@ function insertTitleSuggestionTemplate() {
}

// 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
function addComment(comment, submitTo) {
submitTo = submitTo || '.comment-form'; // class instead of id because of the bound function on line 3
let data = { body: comment };
sendFormSubmissionAjax(data, selector);
sendFormSubmissionAjax(data, submitTo);
}
2 changes: 1 addition & 1 deletion app/assets/javascripts/submit_form_ajax.js
Expand Up @@ -3,7 +3,7 @@
// Allows data to be submitted from anywhere on the page using Javascript without using the form itself
function sendFormSubmissionAjax(dataObj, submitTo) {
let url = '';
if(submitTo.slice(0,5) === "http:") {
if(submitTo.slice(0,1) === "/") {
url = submitTo;
} else {
url = $(submitTo).attr('action');
Expand Down
6 changes: 3 additions & 3 deletions app/assets/javascripts/tagging.js
@@ -1,16 +1,16 @@

// Taking the prompt+value retrieved in promptTag() or the links in the drop-down menu and populating the form field before submitting it
// Instead we want to take the tag value and directly submit it with AJAX
function addTag(tagname, selector) {
selector = selector || '#tagform';
function addTag(tagname, submitTo) {
submitTo = submitTo || '#tagform';
if (tagname.slice(0,5).toLowerCase() === "place") {
place = tagname.split(":")[1];
place.replace("-", " ");
geo = geocodeStringAndPan(place);
}
else {
let data = { name: tagname };
sendFormSubmissionAjax(data, selector);
sendFormSubmissionAjax(data, submitTo);
}
}

Expand Down
20 changes: 19 additions & 1 deletion test/system/comment_test.rb
Expand Up @@ -5,7 +5,7 @@
class CommentTest < ApplicationSystemTestCase
Capybara.default_max_wait_time = 60

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

click_on 'Login'
Expand All @@ -23,4 +23,22 @@ class CommentTest < ApplicationSystemTestCase
assert_selector('#comments-list .comment-body p', text: 'fantastic four')
end

test 'adding a comment via javascript with url only' 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('superhero', '/comment/create/11')")

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

end
19 changes: 19 additions & 0 deletions test/system/tag_test.rb
Expand Up @@ -24,4 +24,23 @@ class TagTest < ApplicationSystemTestCase

end

test 'adding a tag via javascript with url only' do
visit '/'

click_on 'Login'

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

# run the javascript function
page.evaluate_script("addTag('roses', '/tag/create/11')")

visit "/wiki/wiki-page-path"

# check that the tag showed up on the page
assert_selector('.tags-list .card-body h5', text: 'roses')

end

end

0 comments on commit 08962f6

Please sign in to comment.