-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor JS Function add_tag() and create add_comment() for Javascrip…
…t API (#6422) * change addTag to AJAX * addTag ajax now works * add system test for addTag js function * fix codeclimate error * add js function AddComment and system test for function * add parentheses to logic statement * add JS function sendFormSubmissionAjax to reduce duplicate code * rename ajax.js to submit_form_ajax.js * remove parentheses in Node.find_by_tag_and_author * allow sendFormSubmissionAjax to take in a selector or a url * allow js addComment and addTest to use URL and add test case for both * fix codeclimate error * add to JS addComment ability to reply to parent comment - and add test for it * attempt to fix tag_test travis error
- Loading branch information
Showing
8 changed files
with
156 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Takes in a data object that contains the info to be submitted in the form property: dataString | ||
// and the url to submit to the controller | ||
// 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,1) === "/") { | ||
url = submitTo; | ||
} else { | ||
url = $(submitTo).attr('action'); | ||
} | ||
$.ajax({ | ||
url: url, | ||
data: dataObj, | ||
success: (event, success) => { | ||
if (url !== submitTo) { | ||
$(submitTo).trigger('ajax:success', event); | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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 comment 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 | ||
|
||
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 | ||
|
||
test 'adding a reply 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 | ||
parentid = "#" + page.find('#comments-list').first('.comment')[:id] | ||
parentid_num = parentid.slice(2..-1) | ||
page.evaluate_script("addComment('batman', '/comment/create/11', #{parentid_num})") | ||
|
||
# check that the tag showed up on the page | ||
assert_selector("#{parentid} .comment .comment-body p", text: 'batman') | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require 'test_helper' | ||
require "application_system_test_case" | ||
# https://guides.rubyonrails.org/testing.html#implementing-a-system-test | ||
|
||
class TagTest < 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" | ||
|
||
# run the javascript function | ||
page.evaluate_script("addTag('bluebell')") | ||
|
||
# check that the tag showed up on the page | ||
assert_selector('.tags-list .tag-name', text: 'bluebell') | ||
|
||
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" | ||
|
||
visit "/wiki/wiki-page-path" | ||
|
||
# run the javascript function | ||
page.evaluate_script("addTag('roses', '/tag/create/11')") | ||
|
||
visit "/wiki/wiki-page-path" # refresh page | ||
|
||
# check that the tag showed up on the page | ||
assert_selector('.tags-list .card-body h5', text: 'roses') | ||
|
||
end | ||
|
||
end |