Skip to content

Commit

Permalink
Issue 5 Added spies and made code improvements (joomla#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruchiranga authored and yvesh committed Jul 30, 2016
1 parent ea7e52d commit 4bd2191
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 124 deletions.
4 changes: 2 additions & 2 deletions tests/javascript/validate/spec-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* @version 1.0.0
*/

define(['jquery', 'text!testsRoot/validate/fixtures/fixture.html', 'libs/validate', 'libs/core', 'jasmineJquery'], function ($, fixture) {
define(['jquery', 'text!testsRoot/validate/fixtures/fixture.html', 'libs/validate', 'libs/core'], function ($, fixture) {
$('body').append(fixture);

document.formvalidator = new JFormValidator();
});
278 changes: 156 additions & 122 deletions tests/javascript/validate/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,162 +8,196 @@
*/

define(['jquery', 'testsRoot/validate/spec-setup', 'jasmineJquery'], function ($) {
var element = $('#validatejs');
var $element = $('#validatejs');

describe('The input fields in the form', function () {
it('with class \'required\' should have attributes aria-required = true', function () {
expect(element.find('#attach-to-form input')).toHaveAttr('aria-required', 'true');
});
it('with class \'required\' should have attributes required = required', function () {
expect(element.find('#attach-to-form input')).toHaveAttr('required', 'required');
});
});
describe('Validate', function () {
beforeAll(function () {
renderFn = Joomla.renderMessages;
jtxtFn = Joomla.JText._;

describe('The textarea fields in the form', function () {
it('with class \'required\' should have attributes aria-required = true', function () {
expect(element.find('#attach-to-form textarea')).toHaveAttr('aria-required', 'true');
Joomla.renderMessages = jasmine.createSpy('renderMessages');
Joomla.JText._ = jasmine.createSpy('JText._');
});
it('with class \'required\' should have attributes required = required', function () {
expect(element.find('#attach-to-form textarea')).toHaveAttr('required', 'required');
});
});

describe('The select fields in the form', function () {
it('with class \'required\' should have attributes aria-required = true', function () {
expect(element.find('#attach-to-form select')).toHaveAttr('aria-required', 'true');
});
it('with class \'required\' should have attributes required = required', function () {
expect(element.find('#attach-to-form select')).toHaveAttr('required', 'required');
afterAll(function () {
Joomla.renderMessages = renderFn;
Joomla.JText._ = jtxtFn;
});
});

describe('The fieldset fields in the form', function () {
it('with class \'required\' should have attributes aria-required = true', function () {
expect(element.find('#attach-to-form fieldset')).toHaveAttr('aria-required', 'true');
});
it('with class \'required\' should have attributes required = required', function () {
expect(element.find('#attach-to-form fieldset')).toHaveAttr('required', 'required');
describe('The input fields in the form', function () {
it('with class \'required\' should have attributes aria-required = true', function () {
expect($element.find('#attach-to-form input')).toHaveAttr('aria-required', 'true');
});

it('with class \'required\' should have attributes required = required', function () {
expect($element.find('#attach-to-form input')).toHaveAttr('required', 'required');
});
});
});

describe('validate method on #validate-disabled', function () {
var res = document.formvalidator.validate(element.find('#validate-disabled'))
describe('The textarea fields in the form', function () {
it('with class \'required\' should have attributes aria-required = true', function () {
expect($element.find('#attach-to-form textarea')).toHaveAttr('aria-required', 'true');
});

it('should return true', function () {
expect(res).toEqual(true);
});
it('should remove class invalid from element', function () {
expect(element.find('#validate-disabled')).not.toHaveClass('invalid');
});
it('should have aria-invalid = false in element', function () {
expect(element.find('#validate-disabled')).toHaveAttr('aria-invalid', 'false');
});
it('should remove class invalid from the label for element', function () {
expect(element.find('#validate-test label[for=validate-disabled]')).not.toHaveClass('invalid');
it('with class \'required\' should have attributes required = required', function () {
expect($element.find('#attach-to-form textarea')).toHaveAttr('required', 'required');
});
});
});

describe('validate method on #validate-required-unchecked', function () {
var res = document.formvalidator.validate(element.find('#validate-required-unchecked'));
describe('The select fields in the form', function () {
it('with class \'required\' should have attributes aria-required = true', function () {
expect($element.find('#attach-to-form select')).toHaveAttr('aria-required', 'true');
});

it('should return false', function () {
expect(res).toEqual(false);
});
it('should add class invalid to element', function () {
expect(element.find('#validate-required-unchecked')).toHaveClass('invalid');
it('with class \'required\' should have attributes required = required', function () {
expect($element.find('#attach-to-form select')).toHaveAttr('required', 'required');
});
});
it('should have aria-invalid = true in element', function () {
expect(element.find('#validate-required-unchecked')).toHaveAttr('aria-invalid', 'true');
});
});

describe('validate method on #validate-required-checked', function () {
var res = document.formvalidator.validate(element.find('#validate-required-checked'));
describe('The fieldset fields in the form', function () {
it('with class \'required\' should have attributes aria-required = true', function () {
expect($element.find('#attach-to-form fieldset')).toHaveAttr('aria-required', 'true');
});

it('should return true', function () {
expect(res).toEqual(true);
it('with class \'required\' should have attributes required = required', function () {
expect($element.find('#attach-to-form fieldset')).toHaveAttr('required', 'required');
});
});
});

describe('validate method on #validate-numeric-number', function () {
var res = document.formvalidator.validate(element.find('#validate-numeric-number'));
describe('validate method on #validate-disabled', function () {
var res = document.formvalidator.validate($element.find('#validate-disabled'));

it('should return true', function () {
expect(res).toEqual(true);
});
it('should remove class invalid from element', function () {
expect(element.find('#validate-numeric-number')).not.toHaveClass('invalid');
});
it('should have aria-invalid = false in element', function () {
expect(element.find('#validate-numeric-number')).toHaveAttr('aria-invalid', 'false');
});
it('should remove class invalid from the label for element', function () {
expect(element.find('#validate-numeric-number-lbl')).not.toHaveClass('invalid');
});
});
it('should return true', function () {
expect(res).toEqual(true);
});

describe('validate method on #validate-numeric-nan', function () {
var $label = element.find('#validate-numeric-nan-label');
element.find('#validate-numeric-nan').data('label', $label);
it('should remove class invalid from element', function () {
expect($element.find('#validate-disabled')).not.toHaveClass('invalid');
});

var res = document.formvalidator.validate(element.find('#validate-numeric-nan'));
it('should have aria-invalid = false in element', function () {
expect($element.find('#validate-disabled')).toHaveAttr('aria-invalid', 'false');
});

it('should return false', function () {
expect(res).toEqual(false);
});
it('should add class invalid to element', function () {
expect(element.find('#validate-numeric-nan')).toHaveClass('invalid');
it('should remove class invalid from the label for element', function () {
expect($element.find('#validate-test label[for=validate-disabled]')).not.toHaveClass('invalid');
});
});
it('should have aria-invalid = true in element', function () {
expect(element.find('#validate-numeric-nan')).toHaveAttr('aria-invalid', 'true');
});
it('should add class invalid to the label for element', function () {
expect(element.find('#validate-numeric-nan-label')).toHaveClass('invalid');
});
});

describe('validate method on #validate-no-options', function () {
var res = document.formvalidator.validate(element.find('#validate-no-options'));
describe('validate method on #validate-required-unchecked', function () {
var res = document.formvalidator.validate($element.find('#validate-required-unchecked'));

it('should return true', function () {
expect(res).toEqual(true);
});
it('should remove class invalid from element', function () {
expect(element.find('#validate-no-options')).not.toHaveClass('invalid');
});
it('should have aria-invalid = false in element', function () {
expect(element.find('#validate-no-options')).toHaveAttr('aria-invalid', 'false');
});
});
it('should return false', function () {
expect(res).toEqual(false);
});

describe('isValid method on button click', function () {
beforeAll(function () {
fn = Joomla.renderMessages;
it('should add class invalid to element', function () {
expect($element.find('#validate-required-unchecked')).toHaveClass('invalid');
});

Joomla.renderMessages = function() {
return true;
};
$('#button').trigger( "click" );
it('should have aria-invalid = true in element', function () {
expect($element.find('#validate-required-unchecked')).toHaveAttr('aria-invalid', 'true');
});
});

afterAll(function () {
Joomla.renderMessages = fn;
describe('validate method on #validate-required-checked', function () {
var res = document.formvalidator.validate($element.find('#validate-required-checked'));

it('should return true', function () {
expect(res).toEqual(true);
});
});

it('should add class invalid to element #isvalid-numeric-nan', function () {
expect(element.find('#isvalid-numeric-nan')).toHaveClass('invalid');
describe('validate method on #validate-numeric-number', function () {
var res = document.formvalidator.validate($element.find('#validate-numeric-number'));

it('should return true', function () {
expect(res).toEqual(true);
});

it('should remove class invalid from element', function () {
expect($element.find('#validate-numeric-number')).not.toHaveClass('invalid');
});

it('should have aria-invalid = false in element', function () {
expect($element.find('#validate-numeric-number')).toHaveAttr('aria-invalid', 'false');
});

it('should remove class invalid from the label for element', function () {
expect($element.find('#validate-numeric-number-lbl')).not.toHaveClass('invalid');
});
});
it('should have aria-invalid = true in element #isvalid-numeric-nan', function () {
expect(element.find('#isvalid-numeric-nan')).toHaveAttr('aria-invalid', 'true');

describe('validate method on #validate-numeric-nan', function () {
var $label = $element.find('#validate-numeric-nan-label');
$element.find('#validate-numeric-nan').data('label', $label);

var res = document.formvalidator.validate($element.find('#validate-numeric-nan'));

it('should return false', function () {
expect(res).toEqual(false);
});

it('should add class invalid to element', function () {
expect($element.find('#validate-numeric-nan')).toHaveClass('invalid');
});

it('should have aria-invalid = true in element', function () {
expect($element.find('#validate-numeric-nan')).toHaveAttr('aria-invalid', 'true');
});

it('should add class invalid to the label for element', function () {
expect($element.find('#validate-numeric-nan-label')).toHaveClass('invalid');
});
});
it('should not add class invalid to element #isvalid-novalidate', function () {
expect(element.find('#isvalid-novalidate')).not.toHaveClass('invalid');

describe('validate method on #validate-no-options', function () {
var res = document.formvalidator.validate($element.find('#validate-no-options'));

it('should return true', function () {
expect(res).toEqual(true);
});

it('should remove class invalid from element', function () {
expect($element.find('#validate-no-options')).not.toHaveClass('invalid');
});

it('should have aria-invalid = false in element', function () {
expect($element.find('#validate-no-options')).toHaveAttr('aria-invalid', 'false');
});
});
it('should remove class invalid from element #isvalid-numeric-nan after correcting value', function () {
$('#isvalid-numeric-nan').val('12345');
$('#button').trigger( "click" );

expect(element.find('#isvalid-numeric-nan')).not.toHaveClass('invalid');
describe('isValid method on button click', function () {
beforeAll(function () {
$('#button').trigger( "click" );
});

it('should call Joomla.JText._(\'JLIB_FORM_FIELD_INVALID\')', function () {
expect(Joomla.JText._).toHaveBeenCalledWith('JLIB_FORM_FIELD_INVALID');
});

it('should call Joomla.renderMessages({error: ["undefined"]})', function () {
expect(Joomla.renderMessages).toHaveBeenCalledWith({error: ["undefined"]});
});

it('should add class invalid to element #isvalid-numeric-nan', function () {
expect($element.find('#isvalid-numeric-nan')).toHaveClass('invalid');
});

it('should have aria-invalid = true in element #isvalid-numeric-nan', function () {
expect($element.find('#isvalid-numeric-nan')).toHaveAttr('aria-invalid', 'true');
});

it('should not add class invalid to element #isvalid-novalidate', function () {
expect($element.find('#isvalid-novalidate')).not.toHaveClass('invalid');
});

it('should remove class invalid from element #isvalid-numeric-nan after correcting value', function () {
$('#isvalid-numeric-nan').val('12345');
$('#button').trigger( "click" );

expect($element.find('#isvalid-numeric-nan')).not.toHaveClass('invalid');
});
});
});
});

0 comments on commit 4bd2191

Please sign in to comment.