Commit 7c71725f by Steven Burch

Merge pull request #4008 from edx/stv/jsinput

Fix Jasmine tests for JSInput
parents 230aa5e9 fd10fbf7
<div>
<section <section
id="inputtype_1" id="inputtype_1"
data="getGrade" data="getGrade"
data-stored="{&quot;answer&quot;:&quot;{\&quot;cylinder\&quot;:true,\&quot;cube\&quot;:true}&quot;,&quot;state&quot;:&quot;{\&quot;selectedObjects\&quot;:{\&quot;cylinder\&quot;:true,\&quot;cube\&quot;:true}}&quot;}" data-stored="{&quot;answer&quot;:&quot;{\&quot;cylinder\&quot;:true,\&quot;cube\&quot;:true}&quot;,&quot;state&quot;:&quot;{\&quot;selectedObjects\&quot;:{\&quot;cylinder\&quot;:true,\&quot;cube\&quot;:true}}&quot;}"
data-getstate="getState" data-getstate="getState"
...@@ -10,11 +11,11 @@ ...@@ -10,11 +11,11 @@
<div class="script_placeholder"/> <div class="script_placeholder"/>
<iframe <iframe
name="iframe_1" name="iframe_1"
sandbox="allow-scripts sandbox="allow-scripts
allow-popups allow-popups
allow-same-origin allow-same-origin
allow-forms allow-forms
allow-pointer-lock" allow-pointer-lock"
height="500" height="500"
width="500" width="500"
...@@ -24,7 +25,7 @@ ...@@ -24,7 +25,7 @@
</div> </div>
</section> </section>
<section <section
id="inputtype_2" id="inputtype_2"
data="getGrade" data="getGrade"
data-stored="{&quot;answer&quot;:&quot;{\&quot;cylinder\&quot;:true,\&quot;cube\&quot;:true}&quot;,&quot;state&quot;:&quot;{\&quot;selectedObjects\&quot;:{\&quot;cylinder\&quot;:true,\&quot;cube\&quot;:true}}&quot;}" data-stored="{&quot;answer&quot;:&quot;{\&quot;cylinder\&quot;:true,\&quot;cube\&quot;:true}&quot;,&quot;state&quot;:&quot;{\&quot;selectedObjects\&quot;:{\&quot;cylinder\&quot;:true,\&quot;cube\&quot;:true}}&quot;}"
data-getstate="getState" data-getstate="getState"
...@@ -35,16 +36,18 @@ ...@@ -35,16 +36,18 @@
<div class="script_placeholder"/> <div class="script_placeholder"/>
<iframe <iframe
name="iframe_2" name="iframe_2"
sandbox="allow-scripts sandbox="allow-scripts
allow-popups allow-popups
allow-same-origin allow-same-origin
allow-forms allow-forms
allow-pointer-lock" allow-pointer-lock"
height="500" height="500"
width="500" width="500"
src="https://studio.edx.org/c4x/edX/DemoX/asset/webGLDemo.html"> src="https://studio.edx.org/c4x/edX/DemoX/asset/webGLDemo.html">
</iframe> </iframe>
<input type="hidden" name="input_2" id="input_1" waitfor value="{&quot;answer&quot;:&quot;{\&quot;cylinder\&quot;:true,\&quot;cube\&quot;:true}&quot;,&quot;state&quot;:&quot;{\&quot;selectedObjects\&quot;:{\&quot;cylinder\&quot;:true,\&quot;cube\&quot;:true}}&quot;}"> <input type="hidden" name="input_2" id="input_2" waitfor value="{&quot;answer&quot;:&quot;{\&quot;cylinder\&quot;:true,\&quot;cube\&quot;:true}&quot;,&quot;state&quot;:&quot;{\&quot;selectedObjects\&quot;:{\&quot;cylinder\&quot;:true,\&quot;cube\&quot;:true}}&quot;}">
</div> </div>
</section> </section>
\ No newline at end of file </div>
describe("JSInput", function() { describe("JSInput", function () {
var sections;
var inputFields;
beforeEach(function () { beforeEach(function () {
loadFixtures('js/capa/fixtures/jsinput.html'); 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() { it('sets all data-processed attributes to true on first load', function () {
var sections = $(document).find('section[id="inputtype_"]'); sections.each(function (index, item) {
JSInput.walkDOM(); expect(item).toHaveData('processed', true);
sections.each(function(index, section) {
expect(section.attr('data-processed')).toEqual('true');
}); });
}); });
it('sets the data-processed attribute to true on subsequent load', function() { it('sets the waitfor attribute to its update function', function () {
var section1 = $(document).find('section[id="inputtype_1"]'), inputFields.each(function (index, item) {
section2 = $(document).find('section[id="inputtype_2"]'); expect(item).toHaveAttr('waitfor');
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() { it('tests the correct number of sections', function () {
var inputFields = $(document).find('input[id="input_"]'); expect(sections.length).toEqual(2);
JSInput.walkDOM(); expect(sections.length).toEqual(inputFields.length);
inputFields.each(function(index, inputField) {
expect(inputField.data('waitfor')).toBeDefined();
});
}); });
}); });
...@@ -181,19 +181,15 @@ var JSInput = (function ($, undefined) { ...@@ -181,19 +181,15 @@ var JSInput = (function ($, undefined) {
} }
function walkDOM() { function walkDOM() {
var dataProcessed, all; var allSections = $('section.jsinput');
// Find all jsinput elements
allSections = $('section.jsinput');
// When a JSInput problem loads, its data-processed attribute is false, // When a JSInput problem loads, its data-processed attribute is false,
// so the jsconstructor will be called for it. // so the jsconstructor will be called for it.
// The constructor will not be called again on subsequent reruns of // The constructor will not be called again on subsequent reruns of
// this file by other JSInput. Only if it is reloaded, either with the // 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 // rest of the page or when it is submitted, will this constructor be
// called again. // called again.
allSections.each(function(index, value) { allSections.each(function(index, value) {
dataProcessed = ($(value).attr("data-processed") === "true"); var dataProcessed = ($(value).attr("data-processed") === "true");
if (!dataProcessed) { if (!dataProcessed) {
jsinputConstructor(value); jsinputConstructor(value);
$(value).attr("data-processed", 'true'); $(value).attr("data-processed", 'true');
......
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