Skip to content

Commit

Permalink
Cleaning up global namespace in the testing suite
Browse files Browse the repository at this point in the history
  • Loading branch information
LearningStation committed Aug 6, 2011
1 parent 4ddab6a commit 670d566
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 95 deletions.
17 changes: 9 additions & 8 deletions test/form.js
@@ -1,9 +1,10 @@
module("Form behaviours");

test("Ask on Leave", function() {
var prompted = false;

$form = jQuery('#qunit-form');
var prompted = false,
$form = jQuery('#qunit-form'),
$name = $('#name');

$form.uniform({
ask_on_leave : true,
on_leave_callback : function() {
Expand All @@ -14,7 +15,6 @@ test("Ask on Leave", function() {
// The autofocus on the #name is causing a change on load
// We want this test to pass, and don't really care to test
// the combination of autofocus and the ask_on_leave
$name = $('#name')
$name.val($name.attr('data-default-value'));

jQuery(window).trigger('beforeunload')
Expand All @@ -36,7 +36,8 @@ test("Ask on Leave", function() {
});

test("Prevent submit", function() {
$form = jQuery('#qunit-form');
var $form = jQuery('#qunit-form');

$form.uniform({prevent_submit : true});

jQuery('#email', $form)
Expand All @@ -53,9 +54,9 @@ test("Prevent submit", function() {
});

test("Submit callback is executed", function() {
var was_called = false;

$form = jQuery('#qunit-form');
var was_called = false,
$form = jQuery('#qunit-form');
$form.uniform({
submit_callback : function() {
was_called = true;
Expand Down
83 changes: 45 additions & 38 deletions test/issues.js
Expand Up @@ -8,9 +8,10 @@ module("Github cases");
* @link https://github.com/LearningStation/uni-form/issues/issue/1
*/
test("Case 1 : Prevent submit fails for existing data", function() {

$form = jQuery('#qunit-form');
var $form = jQuery('#qunit-form');

jQuery('#email', $form).val('invalid@example');

$form.uniform({
prevent_submit : true
});
Expand All @@ -34,21 +35,23 @@ test("Case 1 : Prevent submit fails for existing data", function() {
* @link https://github.com/LearningStation/uni-form/issues/issue/2
*/
test("Case 2 : Required validation for radio button", function() {
var hasError = false;
$form = jQuery('#qunit-form');
$form.uniform({
invalid_class : 'invalid',
error_class : 'error',
prevent_submit : true,
prevent_submit_callback : function($submit_form) {
hasError =
$('input[name="color"]:radio:first', $submit_form)
.parents('div.ctrlHolder')
.hasClass('error');
return false;
}
});
$form.trigger('submit');
var hasError = false,
$form = jQuery('#qunit-form');

$form
.uniform({
invalid_class : 'invalid',
error_class : 'error',
prevent_submit : true,
prevent_submit_callback : function($submit_form) {
hasError =
$('input[name="color"]:radio:first', $submit_form)
.parents('div.ctrlHolder')
.hasClass('error');
return false;
}
})
.trigger('submit');

equals(
hasError,
Expand Down Expand Up @@ -76,7 +79,8 @@ test("Case 2 : Required validation for radio button", function() {
*/
test("Case 3 : data-default-value should not be submitted", function() {

$form = jQuery('#qunit-form');
var $form = jQuery('#qunit-form');

$form.uniform();

equals(
Expand All @@ -103,21 +107,23 @@ test("Case 3 : data-default-value should not be submitted", function() {
* @link https://github.com/LearningStation/uni-form/issues/issue/4
*/
test("Case 4 : Required validation for checkbox", function() {
var hasError = false;
$form = jQuery('#qunit-form');
$form.uniform({
invalid_class : 'invalid',
error_class : 'error',
prevent_submit : true,
prevent_submit_callback : function($submit_form) {
hasError =
$('input[name="agreement"]:checkbox', $submit_form)
.parents('div.ctrlHolder')
.hasClass('error');
return false;
}
});
$form.trigger('submit');
var hasError = false,
$form = jQuery('#qunit-form');

$form
.uniform({
invalid_class : 'invalid',
error_class : 'error',
prevent_submit : true,
prevent_submit_callback : function($submit_form) {
hasError =
$('input[name="agreement"]:checkbox', $submit_form)
.parents('div.ctrlHolder')
.hasClass('error');
return false;
}
})
.trigger('submit');

equals(
hasError,
Expand Down Expand Up @@ -145,10 +151,11 @@ test("Case 4 : Required validation for checkbox", function() {
*/
test("Case 9 : Autofocus field works with highlight and default data", function() {

$input = $('#name');
var $input = $('#name'),
$form = jQuery('#qunit-form');

$input.attr('data-default-value', 'Sample data in case there is none');

$form = jQuery('#qunit-form');
$form.uniform({
focused_class : 'focused'
});
Expand Down Expand Up @@ -176,11 +183,11 @@ test("Case 9 : Autofocus field works with highlight and default data", function(
* @link https://github.com/LearningStation/uni-form/issues/issue/15
*/
test("Case 15 : Default value with a period should not be rounded", function() {

$input = $('#issue_15_a');
var $input = $('#issue_15_a'),
$form = jQuery('#qunit-form');

$input.attr('data-default-value', '100.000');

$form = jQuery('#qunit-form');
$form.uniform();

equals(
Expand Down
103 changes: 54 additions & 49 deletions test/validation.js
Expand Up @@ -196,20 +196,21 @@ test("SameAs test", function() {
});

test("Email address test", function() {
var $input = getInput('text','');

var addresses = {
'spam@example.com' : true,
'spam@example.co.uk' : true,
'spam.spam@example.co.uk' : true,
'user+filter@gmail.com' : true,
'spam@.com' : false,
'spam@com' : false,
'spam.com' : false
};

var $input = getInput('text',''),
address,
addresses = {
'spam@example.com' : true,
'spam@example.co.uk' : true,
'spam.spam@example.co.uk' : true,
'user+filter@gmail.com' : true,
'spam@.com' : false,
'spam@com' : false,
'spam.com' : false
},
explanation;

for (address in addresses) {
var explanation = (addresses[address]) ? ' passes' : ' fails'
explanation = (addresses[address]) ? ' passes' : ' fails'
validationTest(
validators.validateEmail($input.val(address)),
addresses[address],
Expand All @@ -220,18 +221,19 @@ test("Email address test", function() {
});

test("Url test", function() {
var $input = getInput('text','');

var addresses = {
'http://www.example.com/test/url' : true,
'http://www.example.com/test.html' : true,
'ftp://www.example.com' : true,
'htp://www.example.com' : false,
'http://www example.com' : false
};
var $input = getInput('text',''),
explanation,
address,
addresses = {
'http://www.example.com/test/url' : true,
'http://www.example.com/test.html' : true,
'ftp://www.example.com' : true,
'htp://www.example.com' : false,
'http://www example.com' : false
};

for (address in addresses) {
var explanation = (addresses[address]) ? ' passes' : ' fails'
explanation = (addresses[address]) ? ' passes' : ' fails'
validationTest(
validators.validateUrl($input.val(address)),
addresses[address],
Expand Down Expand Up @@ -363,19 +365,20 @@ test("Phrase test", function() {
});

test("Phone test", function() {
var $input = getInput('text','');

var numbers = {
'(308)-135-7895' : true,
'(123) 456-7890' : true,
'123-345-6789' : true,
'123 456-7890' : true,
'456-7890' : false,
'23456789' : false
};
var $input = getInput('text',''),
numbers = {
'(308)-135-7895' : true,
'(123) 456-7890' : true,
'123-345-6789' : true,
'123 456-7890' : true,
'456-7890' : false,
'23456789' : false
},
number,
explanation;

for (number in numbers) {
var explanation = (numbers[number]) ? ' passes' : ' fails'
explanation = (numbers[number]) ? ' passes' : ' fails'
validationTest(
validators.validatePhone($input.val(number)),
numbers[number],
Expand All @@ -386,19 +389,20 @@ test("Phone test", function() {
});

test("Date test", function() {
var $input = getInput('text','');

var dates = {
'1/1/11' : true,
'1/1/2011' : true,
'1/1/2011' : true,
'11/1/2011' : true,
'01/01/2001' : true, // Case 6
'16/40/2011' : false
};
var $input = getInput('text',''),
explanation,
date,
dates = {
'1/1/11' : true,
'1/1/2011' : true,
'1/1/2011' : true,
'11/1/2011' : true,
'01/01/2001' : true, // Case 6
'16/40/2011' : false
};

for (date in dates) {
var explanation = (dates[date]) ? ' passes' : ' fails'
explanation = (dates[date]) ? ' passes' : ' fails'
validationTest(
validators.validateDate($input.val(date)),
dates[date],
Expand All @@ -410,12 +414,13 @@ test("Date test", function() {

test("Default data hides correctly", function() {

default_text = 'This is a sample';

$input = $('#issue_15_a');
var default_text = 'This is a sample',
$input = $('#issue_15_a'),
$form = jQuery('#qunit-form');

$input.attr('data-default-value', default_text);

$form = jQuery('#qunit-form');

$form.uniform();

// should be showing the default
Expand Down

0 comments on commit 670d566

Please sign in to comment.