Sunday, January 15, 2012

add form fields to a form within the jasmine-jquery framework

Here is how one might put form fields into a form if they are not already there within a jasmine-jquery framework widget:

ensureInputsExist: function () {

   var self = this;

   

   var sortByInput = self.options.form.find('input[name=sortBy]');

   var sortOrderInput = self.options.form.find('input[name=sortOrder]');

   

   if (sortByInput.length == 0) {

      sortByInput = $('');

      self.options.form.append(sortByInput);

   }

   

   if (sortOrderInput.length == 0) {

      sortOrderInput = $('');

      self.options.form.append(sortOrderInput);

   }

   

   sortByInput.val(self.options.sortBy);

   sortOrderInput.val(self.options.sortOrder);

}

 
 

If the above is kept in whatever.js then the following might be tests in whatever.spec.js:

it('should ensure that the hidden inputs are in the form', function () {

   var sortByInput = filterForm.find('input[name=sortBy]');

   var sortOrderInput = filterForm.find('input[name=sortOrder]');

   expect(sortByInput.length).toEqual(1);

   expect(sortOrderInput.length).toEqual(1);

});

   

it('should ensure that the hidden inputs are in the form', function () {

   var sortByInput = filterForm.find('input[name=sortBy]');

   var sortOrderInput = filterForm.find('input[name=sortOrder]');

   expect(sortByInput).toHaveValue('Id');

   expect(sortOrderInput).toHaveValue('ASC');

});

No comments:

Post a Comment