Commit 60a9e998 by stv

Fix JSInput tests

Select DOM elements with wildcard syntax

- DOM lookups were being done with non-existent literal selectors, so it
  was returning empty lists. As assertions were to be made while
  iterating over the list of elements, nothing was actually being
  verified.

- Common code has been centralized in the setup function. By declaring
  CSS selectors once, we minimize the odds of inadvertently using the
  wrong selector, as happened here.

- Had these tests actually been iterating over a non-empty list, this
  would have thrown undefined exceptions. jQuery.each calls its handler
  with an index and an item/object as the arguments. However, the object
  is a DOM-object, not a jQuery-object.  These tests break, as they had
  assumed the latter.
parent e2cab190
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 data-processed attribute to true on subsequent load', function() {
var section1 = $(document).find('section[id="inputtype_1"]'), var section1 = $(this.sections[0]);
section2 = $(document).find('section[id="inputtype_2"]'); var section2 = $(this.sections[1]);
section1.attr('data-processed', false); section1.attr('data-processed', false);
JSInput.walkDOM(); JSInput.walkDOM();
expect(section1.attr('data-processed')).toEqual('true'); expect(section1.attr('data-processed')).toEqual('true');
expect(section2.attr('data-processed')).toEqual('true'); expect(section2.attr('data-processed')).toEqual('true');
}); });
it('sets the waitfor attribute to its update function', function() { it('sets the waitfor attribute to its update function', function () {
var inputFields = $(document).find('input[id="input_"]'); inputFields.each(function (index, item) {
JSInput.walkDOM(); expect(item).toHaveAttr('waitfor');
inputFields.each(function(index, inputField) {
expect(inputField.data('waitfor')).toBeDefined();
}); });
}); });
it('tests the correct number of sections', function () { it('tests the correct number of sections', function () {
var sections = $(document).find('section[id="inputtype_"]');
var inputFields = $(document).find('input[id="input_"]');
expect(sections.length).toEqual(2); expect(sections.length).toEqual(2);
expect(sections.length).toEqual(inputFields.length); expect(sections.length).toEqual(inputFields.length);
}); });
......
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