diff --git a/common/static/js/capa/fixtures/jsinput.html b/common/static/js/capa/fixtures/jsinput.html
index 87feaaaffe..1862d490c3 100644
--- a/common/static/js/capa/fixtures/jsinput.html
+++ b/common/static/js/capa/fixtures/jsinput.html
@@ -1,5 +1,6 @@
+
-
\ No newline at end of file
+
+
+
diff --git a/common/static/js/capa/spec/jsinput_spec.js b/common/static/js/capa/spec/jsinput_spec.js
index b857972fb4..b62097b72c 100644
--- a/common/static/js/capa/spec/jsinput_spec.js
+++ b/common/static/js/capa/spec/jsinput_spec.js
@@ -1,30 +1,29 @@
-describe("JSInput", function() {
+describe("JSInput", function () {
+ var sections;
+ var inputFields;
+
beforeEach(function () {
loadFixtures('js/capa/fixtures/jsinput.html');
+ sections = $('section[id^="inputtype_"]');
+ inputFields = $('input[id^="input_"]');
+ JSInput.walkDOM();
});
- it('sets all data-processed attributes to true on first load', function() {
- var sections = $(document).find('section[id="inputtype_"]');
- JSInput.walkDOM();
- sections.each(function(index, section) {
- expect(section.attr('data-processed')).toEqual('true');
+ it('sets all data-processed attributes to true on first load', function () {
+ sections.each(function (index, item) {
+ expect(item).toHaveData('processed', true);
});
});
- it('sets the data-processed attribute to true on subsequent load', function() {
- var section1 = $(document).find('section[id="inputtype_1"]'),
- section2 = $(document).find('section[id="inputtype_2"]');
- section1.attr('data-processed', false);
- JSInput.walkDOM();
- expect(section1.attr('data-processed')).toEqual('true');
- expect(section2.attr('data-processed')).toEqual('true');
+ it('sets the waitfor attribute to its update function', function () {
+ inputFields.each(function (index, item) {
+ expect(item).toHaveAttr('waitfor');
+ });
});
- it('sets the waitfor attribute to its update function', function() {
- var inputFields = $(document).find('input[id="input_"]');
- JSInput.walkDOM();
- inputFields.each(function(index, inputField) {
- expect(inputField.data('waitfor')).toBeDefined();
- });
+ it('tests the correct number of sections', function () {
+ expect(sections.length).toEqual(2);
+ expect(sections.length).toEqual(inputFields.length);
});
});
+
diff --git a/common/static/js/capa/src/jsinput.js b/common/static/js/capa/src/jsinput.js
index a10f18fa76..a1ad7e8bc7 100644
--- a/common/static/js/capa/src/jsinput.js
+++ b/common/static/js/capa/src/jsinput.js
@@ -181,19 +181,15 @@ var JSInput = (function ($, undefined) {
}
function walkDOM() {
- var dataProcessed, all;
-
- // Find all jsinput elements
- allSections = $('section.jsinput');
-
+ var allSections = $('section.jsinput');
// When a JSInput problem loads, its data-processed attribute is false,
// so the jsconstructor will be called for it.
// The constructor will not be called again on subsequent reruns of
// this file by other JSInput. Only if it is reloaded, either with the
// rest of the page or when it is submitted, will this constructor be
- // called again.
+ // called again.
allSections.each(function(index, value) {
- dataProcessed = ($(value).attr("data-processed") === "true");
+ var dataProcessed = ($(value).attr("data-processed") === "true");
if (!dataProcessed) {
jsinputConstructor(value);
$(value).attr("data-processed", 'true');