Commit 50fd135c by Christina Roberts

Merge pull request #10193 from edx/christina/move-require-test

Clean up of common JS specs using RequireJS.
parents 3e5ad298 b67f07af
...@@ -162,7 +162,8 @@ ...@@ -162,7 +162,8 @@
'common-requirejs/include/common/js/spec/components/paging_header_spec.js', 'common-requirejs/include/common/js/spec/components/paging_header_spec.js',
'common-requirejs/include/common/js/spec/components/paging_footer_spec.js', 'common-requirejs/include/common/js/spec/components/paging_footer_spec.js',
'common-requirejs/include/common/js/spec/components/search_field_spec.js', 'common-requirejs/include/common/js/spec/components/search_field_spec.js',
'common-requirejs/include/common/js/spec/components/view_utils_spec.js' 'common-requirejs/include/common/js/spec/components/view_utils_spec.js',
'common-requirejs/include/common/js/spec/utils/edx.utils.validate_spec.js'
]); ]);
}).call(this, requirejs, define); }).call(this, requirejs, define);
;(function (define) { ;(function (define) {
'use strict'; 'use strict';
define(['jquery', 'js/utils/edx.utils.validate'], define(['jquery', 'common/js/utils/edx.utils.validate'],
function($) { function($, EdxUtilsValidate) {
var fixture = null, describe("EdxUtilsValidate", function() {
field = null, var fixture = null,
result = null, field = null,
MIN_LENGTH = 2, result = null,
MAX_LENGTH = 20, MIN_LENGTH = 2,
VALID_STRING = 'xsy_is_awesome', MAX_LENGTH = 20,
SHORT_STRING = 'x', VALID_STRING = 'xsy_is_awesome',
LONG_STRING = 'xsy_is_way_too_awesome', SHORT_STRING = 'x',
EMAIL_ERROR_FRAGMENT = 'formatted', LONG_STRING = 'xsy_is_way_too_awesome',
MIN_ERROR_FRAGMENT = 'least', EMAIL_ERROR_FRAGMENT = 'formatted',
MAX_ERROR_FRAGMENT = 'up to', MIN_ERROR_FRAGMENT = 'least',
REQUIRED_ERROR_FRAGMENT = 'Please enter your', MAX_ERROR_FRAGMENT = 'up to',
CUSTOM_MESSAGE = 'custom message'; REQUIRED_ERROR_FRAGMENT = 'Please enter your',
CUSTOM_MESSAGE = 'custom message';
var createFixture = function( type, name, required, minlength, maxlength, value ) {
setFixtures('<input id="field" type=' + type + '>'); var createFixture = function( type, name, required, minlength, maxlength, value ) {
setFixtures('<input id="field" type=' + type + '>');
field = $('#field');
field.prop('required', required); field = $('#field');
field.attr({ field.prop('required', required);
name: name, field.attr({
minlength: minlength, name: name,
maxlength: maxlength, minlength: minlength,
value: value maxlength: maxlength,
value: value
});
};
var expectValid = function() {
result = EdxUtilsValidate.validate(field);
expect(result.isValid).toBe(true);
};
var expectInvalid = function( errorFragment ) {
result = EdxUtilsValidate.validate(field);
expect(result.isValid).toBe(false);
expect(result.message).toMatch(errorFragment);
};
it('succeeds if an optional field is left blank', function () {
createFixture('text', 'username', false, MIN_LENGTH, MAX_LENGTH, '');
expectValid();
}); });
};
var expectValid = function() {
result = edx.utils.validate(field);
expect(result.isValid).toBe(true);
};
var expectInvalid = function( errorFragment ) {
result = edx.utils.validate(field);
expect(result.isValid).toBe(false);
expect(result.message).toMatch(errorFragment);
};
it('succeeds if an optional field is left blank', function () {
createFixture('text', 'username', false, MIN_LENGTH, MAX_LENGTH, '');
expectValid();
});
it('succeeds if a required field is provided a valid value', function () { it('succeeds if a required field is provided a valid value', function () {
createFixture('text', 'username', true, MIN_LENGTH, MAX_LENGTH, VALID_STRING); createFixture('text', 'username', true, MIN_LENGTH, MAX_LENGTH, VALID_STRING);
expectValid(); expectValid();
}); });
it('fails if a required field is left blank', function () { it('fails if a required field is left blank', function () {
createFixture('text', 'username', true, MIN_LENGTH, MAX_LENGTH, ''); createFixture('text', 'username', true, MIN_LENGTH, MAX_LENGTH, '');
expectInvalid(REQUIRED_ERROR_FRAGMENT); expectInvalid(REQUIRED_ERROR_FRAGMENT);
}); });
it('fails if a field is provided a value below its minimum character limit', function () { it('fails if a field is provided a value below its minimum character limit', function () {
createFixture('text', 'username', false, MIN_LENGTH, MAX_LENGTH, SHORT_STRING); createFixture('text', 'username', false, MIN_LENGTH, MAX_LENGTH, SHORT_STRING);
// Verify optional field behavior // Verify optional field behavior
expectInvalid(MIN_ERROR_FRAGMENT); expectInvalid(MIN_ERROR_FRAGMENT);
// Verify required field behavior // Verify required field behavior
field.prop('required', true); field.prop('required', true);
expectInvalid(MIN_ERROR_FRAGMENT); expectInvalid(MIN_ERROR_FRAGMENT);
}); });
it('succeeds if a field with no minimum character limit is provided a value below its maximum character limit', function () { it('succeeds if a field with no minimum character limit is provided a value below its maximum character limit', function () {
createFixture('text', 'username', false, null, MAX_LENGTH, SHORT_STRING); createFixture('text', 'username', false, null, MAX_LENGTH, SHORT_STRING);
// Verify optional field behavior // Verify optional field behavior
expectValid(); expectValid();
// Verify required field behavior // Verify required field behavior
field.prop('required', true); field.prop('required', true);
expectValid(); expectValid();
}); });
it('fails if a required field with no minimum character limit is left blank', function () { it('fails if a required field with no minimum character limit is left blank', function () {
createFixture('text', 'username', true, null, MAX_LENGTH, ''); createFixture('text', 'username', true, null, MAX_LENGTH, '');
expectInvalid(REQUIRED_ERROR_FRAGMENT); expectInvalid(REQUIRED_ERROR_FRAGMENT);
}); });
it('fails if a field is provided a value above its maximum character limit', function () { it('fails if a field is provided a value above its maximum character limit', function () {
createFixture('text', 'username', false, MIN_LENGTH, MAX_LENGTH, LONG_STRING); createFixture('text', 'username', false, MIN_LENGTH, MAX_LENGTH, LONG_STRING);
// Verify optional field behavior // Verify optional field behavior
expectInvalid(MAX_ERROR_FRAGMENT); expectInvalid(MAX_ERROR_FRAGMENT);
// Verify required field behavior // Verify required field behavior
field.prop('required', true); field.prop('required', true);
expectInvalid(MAX_ERROR_FRAGMENT); expectInvalid(MAX_ERROR_FRAGMENT);
}); });
it('succeeds if a field with no maximum character limit is provided a value above its minimum character limit', function () { it('succeeds if a field with no maximum character limit is provided a value above its minimum character limit', function () {
createFixture('text', 'username', false, MIN_LENGTH, null, LONG_STRING); createFixture('text', 'username', false, MIN_LENGTH, null, LONG_STRING);
// Verify optional field behavior // Verify optional field behavior
expectValid(); expectValid();
// Verify required field behavior // Verify required field behavior
field.prop('required', true); field.prop('required', true);
expectValid(); expectValid();
}); });
it('succeeds if a field with no character limits is provided a value', function () { it('succeeds if a field with no character limits is provided a value', function () {
createFixture('text', 'username', false, null, null, VALID_STRING); createFixture('text', 'username', false, null, null, VALID_STRING);
// Verify optional field behavior // Verify optional field behavior
expectValid(); expectValid();
// Verify required field behavior // Verify required field behavior
field.prop('required', true); field.prop('required', true);
expectValid(); expectValid();
}); });
it('fails if an email field is provided an invalid address', function () { it('fails if an email field is provided an invalid address', function () {
createFixture('email', 'email', false, MIN_LENGTH, MAX_LENGTH, 'localpart'); createFixture('email', 'email', false, MIN_LENGTH, MAX_LENGTH, 'localpart');
// Verify optional field behavior // Verify optional field behavior
expectInvalid(EMAIL_ERROR_FRAGMENT); expectInvalid(EMAIL_ERROR_FRAGMENT);
// Verify required field behavior // Verify required field behavior
field.prop('required', false); field.prop('required', false);
expectInvalid(EMAIL_ERROR_FRAGMENT); expectInvalid(EMAIL_ERROR_FRAGMENT);
}); });
it('succeeds if an email field is provided a valid address', function () { it('succeeds if an email field is provided a valid address', function () {
createFixture('email', 'email', false, MIN_LENGTH, MAX_LENGTH, 'localpart@label.tld'); createFixture('email', 'email', false, MIN_LENGTH, MAX_LENGTH, 'localpart@label.tld');
// Verify optional field behavior // Verify optional field behavior
expectValid(); expectValid();
// Verify required field behavior // Verify required field behavior
field.prop('required', true); field.prop('required', true);
expectValid(); expectValid();
}); });
it('succeeds if a checkbox is optional, or required and checked, but fails if a required checkbox is unchecked', function () { it('succeeds if a checkbox is optional, or required and checked, but fails if a required checkbox is unchecked', function () {
createFixture('checkbox', 'checkbox', false, null, null, 'value'); createFixture('checkbox', 'checkbox', false, null, null, 'value');
// Optional, unchecked // Optional, unchecked
expectValid(); expectValid();
// Optional, checked // Optional, checked
field.prop('checked', true); field.prop('checked', true);
expectValid(); expectValid();
// Required, checked // Required, checked
field.prop('required', true); field.prop('required', true);
expectValid(); expectValid();
// Required, unchecked // Required, unchecked
field.prop('checked', false); field.prop('checked', false);
expectInvalid(REQUIRED_ERROR_FRAGMENT); expectInvalid(REQUIRED_ERROR_FRAGMENT);
}); });
it('succeeds if a select is optional, or required and default is selected, but fails if a required select has the default option selected', function () { it('succeeds if a select is optional, or required and default is selected, but fails if a required select has the default option selected', function () {
var select = [ var select = [
'<select id="dropdown" name="country">', '<select id="dropdown" name="country">',
'<option value="" data-isdefault="true">Please select a country</option>', '<option value="" data-isdefault="true">Please select a country</option>',
'<option value="BE">Belgium</option>', '<option value="BE">Belgium</option>',
'<option value="DE">Germany</option>', '<option value="DE">Germany</option>',
'</select>' '</select>'
].join(''); ].join('');
setFixtures(select); setFixtures(select);
field = $('#dropdown'); field = $('#dropdown');
// Optional // Optional
expectValid(); expectValid();
// Required, default text selected // Required, default text selected
field.attr('required', true); field.attr('required', true);
expectInvalid(REQUIRED_ERROR_FRAGMENT); expectInvalid(REQUIRED_ERROR_FRAGMENT);
// Required, country selected // Required, country selected
field.val('BE'); field.val('BE');
expectValid(); expectValid();
}); });
it('returns a custom error message if an invalid field has one attached', function () { it('returns a custom error message if an invalid field has one attached', function () {
// Create a blank required field // Create a blank required field
createFixture('text', 'username', true, MIN_LENGTH, MAX_LENGTH, ''); createFixture('text', 'username', true, MIN_LENGTH, MAX_LENGTH, '');
// Attach a custom error message to the field // Attach a custom error message to the field
field.data('errormsg-required', CUSTOM_MESSAGE); field.data('errormsg-required', CUSTOM_MESSAGE);
expectInvalid(CUSTOM_MESSAGE); expectInvalid(CUSTOM_MESSAGE);
});
}); });
}); });
}).call(this, define || RequireJS.define); }).call(this, define || RequireJS.define);
...@@ -48,7 +48,6 @@ src_paths: ...@@ -48,7 +48,6 @@ src_paths:
- js/xblock - js/xblock
- coffee/src - coffee/src
- js/src - js/src
- js/utils
- js/capa/src - js/capa/src
# Paths to spec (test) JavaScript files # Paths to spec (test) JavaScript files
......
...@@ -54,7 +54,6 @@ src_paths: ...@@ -54,7 +54,6 @@ src_paths:
# Paths to spec (test) JavaScript files # Paths to spec (test) JavaScript files
spec_paths: spec_paths:
- common/js/spec - common/js/spec
- js/spec/main_requirejs.js
# Paths to fixture files (optional) # Paths to fixture files (optional)
# The fixture path will be set automatically when using jasmine-jquery. # The fixture path will be set automatically when using jasmine-jquery.
...@@ -70,7 +69,7 @@ fixture_paths: ...@@ -70,7 +69,7 @@ fixture_paths:
requirejs: requirejs:
paths: paths:
main: js/spec/main_requirejs main: common/js/spec/main_requirejs
# Because require.js is responsible for loading all dependencies, we exclude # Because require.js is responsible for loading all dependencies, we exclude
# all files from being included in <script> tags # all files from being included in <script> tags
......
...@@ -89,7 +89,7 @@ ...@@ -89,7 +89,7 @@
'annotator_1.2.9': 'xmodule_js/common_static/js/vendor/edxnotes/annotator-full.min', 'annotator_1.2.9': 'xmodule_js/common_static/js/vendor/edxnotes/annotator-full.min',
// Common edx utils // Common edx utils
'js/utils/edx.utils.validate': 'xmodule_js/common_static/js/utils/edx.utils.validate' 'common/js/utils/edx.utils.validate': 'xmodule_js/common_static/common/js/utils/edx.utils.validate'
}, },
shim: { shim: {
'gettext': { 'gettext': {
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
'jquery', 'jquery',
'underscore', 'underscore',
'backbone', 'backbone',
'js/utils/edx.utils.validate' 'common/js/utils/edx.utils.validate'
], ],
function($, _, Backbone, EdxUtilsValidate) { function($, _, Backbone, EdxUtilsValidate) {
......
...@@ -64,7 +64,7 @@ lib_paths: ...@@ -64,7 +64,7 @@ lib_paths:
- xmodule_js/common_static/js/vendor/date.js - xmodule_js/common_static/js/vendor/date.js
- xmodule_js/common_static/js/vendor/moment.min.js - xmodule_js/common_static/js/vendor/moment.min.js
- xmodule_js/common_static/js/vendor/moment-with-locales.min.js - xmodule_js/common_static/js/vendor/moment-with-locales.min.js
- xmodule_js/common_static/js/utils/edx.utils.validate.js - xmodule_js/common_static/common/js/utils/edx.utils.validate.js
# Paths to source JavaScript files # Paths to source JavaScript files
src_paths: src_paths:
......
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