Commit 17f35612 by Jonathan Piacenti

Test and Javascript refactoring.

parent 30017854
{{ js_template|safe }} {{ js_template|safe }}
<div class="survey-block"> <div class="poll-block">
{# If no form is present, the Javascript will load the results instead. #} {# If no form is present, the Javascript will load the results instead. #}
{% if not choices %} {% if not choices %}
<form> <form>
......
/* Javascript for PollBlock. */ /* Javascript for PollBlock. */
function PollUtil (runtime, element) { function PollUtil (runtime, element, pollType) {
var self = this;
this.init = function(runtime, element) { this.init = function(runtime, element) {
// Initialization function used for both Poll Types
this.voteUrl = runtime.handlerUrl(element, 'vote'); this.voteUrl = runtime.handlerUrl(element, 'vote');
this.tallyURL = runtime.handlerUrl(element, 'get_results'); this.tallyURL = runtime.handlerUrl(element, 'get_results');
this.element = element; this.element = element;
...@@ -10,15 +11,21 @@ function PollUtil (runtime, element) { ...@@ -10,15 +11,21 @@ function PollUtil (runtime, element) {
this.submit = $('input[type=button]', element); this.submit = $('input[type=button]', element);
this.answers = $('input[type=radio]', element); this.answers = $('input[type=radio]', element);
this.resultsTemplate = Handlebars.compile($("#poll-results-template", element).html()); 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(){ this.pollInit = function(){
// If the submit button doesn't exist, the user has already
// selected a choice.
var self = this; var self = this;
// Initialization function for PollBlocks.
var enableSubmit = self.enableSubmit(); var enableSubmit = self.enableSubmit();
var getResults = self.getResults();
if (self.submit.length) {
var radio = $('input[name=choice]:checked', self.element); var radio = $('input[name=choice]:checked', self.element);
self.submit.click(function () { self.submit.click(function () {
// Refresh. // Refresh.
...@@ -28,27 +35,46 @@ function PollUtil (runtime, element) { ...@@ -28,27 +35,46 @@ function PollUtil (runtime, element) {
type: "POST", type: "POST",
url: self.voteUrl, url: self.voteUrl,
data: JSON.stringify({"choice": choice}), data: JSON.stringify({"choice": choice}),
success: getResults success: self.getResults()
}); });
}); });
// If the user has refreshed the page, they may still have an answer // If the user has refreshed the page, they may still have an answer
// selected and the submit button should be enabled. // selected and the submit button should be enabled.
var answers = $('input[type=radio]', self.element); var answers = $('input[type=radio]', self.element);
if (! radio.val()) { if (! radio.val()) {
answers.bind("change.EnableSubmit", enableSubmit); answers.bind("change.enableSubmit", enableSubmit);
} else { } else {
enableSubmit(); enableSubmit();
} }
} else {
getResults({'success': true});
}
}; };
this.surveyInit = function () { 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 () { this.getResults = function () {
// Generates a function that will grab and display results.
var self = this; var self = this;
return function(data) { return function(data) {
if (!data['success']) { if (!data['success']) {
...@@ -69,21 +95,24 @@ function PollUtil (runtime, element) { ...@@ -69,21 +95,24 @@ function PollUtil (runtime, element) {
}; };
this.enableSubmit = function () { this.enableSubmit = function () {
// Generates a function which will enable the submit button.
var self = this; var self = this;
return function () { return function () {
self.submit.removeAttr("disabled"); 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) { function PollBlock(runtime, element) {
var util = new PollUtil(runtime, element); new PollUtil(runtime, element, 'poll');
util.pollInit();
} }
function SurveyBlock(runtime, element) { function SurveyBlock(runtime, element) {
var util = new PollUtil(runtime, element); new PollUtil(runtime, element, 'survey');
util.surveyInit();
} }
...@@ -7,9 +7,9 @@ from selenium.common.exceptions import NoSuchElementException ...@@ -7,9 +7,9 @@ from selenium.common.exceptions import NoSuchElementException
from .base_test import PollBaseTest 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): def test_default_poll(self):
""" """
...@@ -17,7 +17,7 @@ class TestDefaults(PollBaseTest): ...@@ -17,7 +17,7 @@ class TestDefaults(PollBaseTest):
the tally displays afterward. Verifies that the feedback section does the tally displays afterward. Verifies that the feedback section does
not load since it is not enabled by default. 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 = self.browser.find_element_by_css_selector('input[type=radio]')
button.click() button.click()
submit = self.get_submit() submit = self.get_submit()
...@@ -30,3 +30,11 @@ class TestDefaults(PollBaseTest): ...@@ -30,3 +30,11 @@ class TestDefaults(PollBaseTest):
# No feedback section. # No feedback section.
self.assertRaises(NoSuchElementException, self.browser.find_element_by_css_selector, '.poll-feedback') 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): ...@@ -16,7 +16,7 @@ class TestLayout(PollBaseTest):
""" """
Verify img tags are created for answers when they're all set. 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') pics = self.browser.find_elements_by_css_selector('.poll-image')
self.assertEqual(len(pics), 4) self.assertEqual(len(pics), 4)
...@@ -32,7 +32,7 @@ class TestLayout(PollBaseTest): ...@@ -32,7 +32,7 @@ class TestLayout(PollBaseTest):
""" """
Verify layout is sane when only one answer has an image. 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') pics = self.browser.find_elements_by_css_selector('.poll-image')
# On the polling page, there should only be one pics div. # On the polling page, there should only be one pics div.
self.assertEqual(len(pics), 1) self.assertEqual(len(pics), 1)
......
...@@ -12,7 +12,7 @@ class MarkdownTestCase(PollBaseTest): ...@@ -12,7 +12,7 @@ class MarkdownTestCase(PollBaseTest):
""" """
Ensure Markdown is parsed for questions. Ensure Markdown is parsed for questions.
""" """
self.go_to_page("Markdown") self.go_to_page("Poll Markdown")
self.assertEqual( self.assertEqual(
self.browser.find_element_by_css_selector('.poll-question-container').text, self.browser.find_element_by_css_selector('.poll-question-container').text,
"""This is a test """This is a test
...@@ -30,7 +30,7 @@ We shall find out if markdown is respected. ...@@ -30,7 +30,7 @@ We shall find out if markdown is respected.
""" """
Ensure Markdown is parsed for feedback. 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.browser.find_element_by_css_selector('input[type=radio]').click()
self.get_submit().click() self.get_submit().click()
......
<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