Commit 17f35612 by Jonathan Piacenti

Test and Javascript refactoring.

parent 30017854
{{ js_template|safe }}
<div class="survey-block">
<div class="poll-block">
{# If no form is present, the Javascript will load the results instead. #}
{% if not choices %}
<form>
......
/* Javascript for PollBlock. */
function PollUtil (runtime, element) {
function PollUtil (runtime, element, pollType) {
var self = this;
this.init = function(runtime, element) {
// Initialization function used for both Poll Types
this.voteUrl = runtime.handlerUrl(element, 'vote');
this.tallyURL = runtime.handlerUrl(element, 'get_results');
this.element = element;
......@@ -10,45 +11,70 @@ function PollUtil (runtime, element) {
this.submit = $('input[type=button]', element);
this.answers = $('input[type=radio]', element);
this.resultsTemplate = Handlebars.compile($("#poll-results-template", element).html());
var getResults = this.getResults();
// If the submit button doesn't exist, the user has already
// selected a choice. Render results instead of initializing machinery.
if (! self.submit.length) {
getResults({'success': true});
return false;
}
return true;
};
this.pollInit = function(){
// If the submit button doesn't exist, the user has already
// selected a choice.
var self = this;
// Initialization function for PollBlocks.
var enableSubmit = self.enableSubmit();
var getResults = self.getResults();
if (self.submit.length) {
var radio = $('input[name=choice]:checked', self.element);
self.submit.click(function () {
// Refresh.
radio = $(radio.selector, self.element);
var choice = radio.val();
$.ajax({
type: "POST",
url: self.voteUrl,
data: JSON.stringify({"choice": choice}),
success: getResults
});
var radio = $('input[name=choice]:checked', self.element);
self.submit.click(function () {
// Refresh.
radio = $(radio.selector, self.element);
var choice = radio.val();
$.ajax({
type: "POST",
url: self.voteUrl,
data: JSON.stringify({"choice": choice}),
success: self.getResults()
});
// If the user has refreshed the page, they may still have an answer
// selected and the submit button should be enabled.
var answers = $('input[type=radio]', self.element);
if (! radio.val()) {
answers.bind("change.EnableSubmit", enableSubmit);
} else {
enableSubmit();
}
});
// If the user has refreshed the page, they may still have an answer
// selected and the submit button should be enabled.
var answers = $('input[type=radio]', self.element);
if (! radio.val()) {
answers.bind("change.enableSubmit", enableSubmit);
} else {
getResults({'success': true});
enableSubmit();
}
};
this.surveyInit = function () {
var self = this;
// Initialization function for Survey Blocks
var verifyAll = self.verifyAll();
self.answers.bind("change.enableSubmit", verifyAll)
};
this.verifyAll = function () {
// Generates a function that will verify all questions have an answer selected.
var self = this;
var enableSubmit = self.enableSubmit();
return function () {
var doEnable = true;
self.answers.each(function (index, el) {
if (! $("input[name='" + $(el).prop('name') + "']:checked", self.element).length) {
doEnable = false;
return false
}
});
if (doEnable){
enableSubmit();
}
}
};
this.getResults = function () {
// Generates a function that will grab and display results.
var self = this;
return function(data) {
if (!data['success']) {
......@@ -69,21 +95,24 @@ function PollUtil (runtime, element) {
};
this.enableSubmit = function () {
// Generates a function which will enable the submit button.
var self = this;
return function () {
self.submit.removeAttr("disabled");
self.answers.unbind("change.EnableSubmit");
self.answers.unbind("change.enableSubmit");
}
};
this.init(runtime, element);
var run_init = this.init(runtime, element);
if (run_init) {
var init_map = {'poll': self.pollInit, 'survey': self.surveyInit};
init_map[pollType].call(self)
}
}
function PollBlock(runtime, element) {
var util = new PollUtil(runtime, element);
util.pollInit();
new PollUtil(runtime, element, 'poll');
}
function SurveyBlock(runtime, element) {
var util = new PollUtil(runtime, element);
util.surveyInit();
new PollUtil(runtime, element, 'survey');
}
......@@ -7,9 +7,9 @@ from selenium.common.exceptions import NoSuchElementException
from .base_test import PollBaseTest
class TestDefaults(PollBaseTest):
class TestDefault(PollBaseTest):
"""
Tests to run against the default poll.
Tests to run against the default XBlock configurations.
"""
def test_default_poll(self):
"""
......@@ -17,7 +17,7 @@ class TestDefaults(PollBaseTest):
the tally displays afterward. Verifies that the feedback section does
not load since it is not enabled by default.
"""
self.go_to_page('Defaults')
self.go_to_page('Poll Defaults')
button = self.browser.find_element_by_css_selector('input[type=radio]')
button.click()
submit = self.get_submit()
......@@ -29,4 +29,12 @@ class TestDefaults(PollBaseTest):
self.assertEqual(self.browser.find_element_by_css_selector('.poll-percent-display').text, '100%')
# No feedback section.
self.assertRaises(NoSuchElementException, self.browser.find_element_by_css_selector, '.poll-feedback')
\ No newline at end of file
self.assertRaises(NoSuchElementException, self.browser.find_element_by_css_selector, '.poll-feedback')
def test_default_survey(self):
"""
Verifies that a default survey loads, that it can be voted on, and
that the tally displays afterward. Verifies that the feedback section
does not load since it is not enabled by default.
"""
self.go_to_page('Survey Defaults')
......@@ -16,7 +16,7 @@ class TestLayout(PollBaseTest):
"""
Verify img tags are created for answers when they're all set.
"""
self.go_to_page('All Pictures')
self.go_to_page('Poll All Pictures')
pics = self.browser.find_elements_by_css_selector('.poll-image')
self.assertEqual(len(pics), 4)
......@@ -32,7 +32,7 @@ class TestLayout(PollBaseTest):
"""
Verify layout is sane when only one answer has an image.
"""
self.go_to_page('One Picture')
self.go_to_page('Poll One Picture')
pics = self.browser.find_elements_by_css_selector('.poll-image')
# On the polling page, there should only be one pics div.
self.assertEqual(len(pics), 1)
......@@ -43,4 +43,4 @@ class TestLayout(PollBaseTest):
self.wait_until_exists('.poll-image.result-image')
# ...But on the results page, we need four, for table layout.
self.assertEqual(len(self.browser.find_elements_by_css_selector('.poll-image')), 4)
\ No newline at end of file
self.assertEqual(len(self.browser.find_elements_by_css_selector('.poll-image')), 4)
......@@ -12,7 +12,7 @@ class MarkdownTestCase(PollBaseTest):
"""
Ensure Markdown is parsed for questions.
"""
self.go_to_page("Markdown")
self.go_to_page("Poll Markdown")
self.assertEqual(
self.browser.find_element_by_css_selector('.poll-question-container').text,
"""This is a test
......@@ -30,7 +30,7 @@ We shall find out if markdown is respected.
"""
Ensure Markdown is parsed for feedback.
"""
self.go_to_page("Markdown")
self.go_to_page("Poll Markdown")
self.browser.find_element_by_css_selector('input[type=radio]').click()
self.get_submit().click()
......@@ -40,4 +40,4 @@ We shall find out if markdown is respected.
"""This is some feedback
This is a link
This is also a link.
This is a paragraph with emphasized and bold text, and both.""")
\ No newline at end of file
This is a paragraph with emphasized and bold text, and both.""")
<vertical_demo>
<survey url_name="defaults"/>
</vertical_demo>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment