reenabled more tests

This commit is contained in:
Misko Hevery 2010-03-29 21:36:34 -07:00
parent e55c97deba
commit cc6def854f
6 changed files with 50 additions and 31 deletions

View file

@ -228,6 +228,16 @@ function escapeHtml(html) {
replace(/>/g, '>'); replace(/>/g, '>');
} }
function elementDecorateError(element, error) {
if (error) {
element.addClass(NG_VALIDATION_ERROR);
element.attr(NG_ERROR, error);
} else {
element.removeClass(NG_VALIDATION_ERROR);
element.removeAttr(NG_ERROR);
}
}
function escapeAttr(html) { function escapeAttr(html) {
if (!html || !html.replace) if (!html || !html.replace)
return html; return html;

View file

@ -93,6 +93,7 @@ Compiler.prototype = {
rawElement = jqLite(rawElement); rawElement = jqLite(rawElement);
var template = this.templatize(rawElement) || new Template(); var template = this.templatize(rawElement) || new Template();
return function(element, parentScope){ return function(element, parentScope){
element = jqLite(element);
parentScope = parentScope || {}; parentScope = parentScope || {};
var scope = createScope(parentScope); var scope = createScope(parentScope);
parentScope.$root = parentScope.$root || scope; parentScope.$root = parentScope.$root || scope;

View file

@ -82,29 +82,30 @@ foreach({
}, },
'asynchronous': function(text, asynchronousFn) { 'asynchronous': function(text, asynchronousFn) {
var stateKey = '$validateState'; var element = this['$element'];
var lastKey = '$lastKey'; var cache = element.data('$validateState');
var obj = this['$element']; if (!cache) {
var stateCache = obj[stateKey] = obj[stateKey] || {}; cache = { state: {}};
var state = stateCache[text]; element.data('$validateState', cache);
var updateView = this['$updateView']; }
obj[lastKey] = text; var state = cache.state[text];
cache.lastKey = text;
if (state === undefined) { if (state === undefined) {
// we have never seen this before, Request it // we have never seen this before, Request it
jqLite(obj).addClass('ng-input-indicator-wait'); element.addClass('ng-input-indicator-wait');
state = stateCache[text] = null; state = cache.state[text] = null;
asynchronousFn(text, function(error){ (asynchronousFn || noop)(text, function(error){
state = stateCache[text] = error ? error : false; state = cache.state[text] = error ? error : false;
if (stateCache[obj[lastKey]] !== null) { if (cache.state[cache.lastKey] !== null) {
jqLite(obj).removeClass('ng-input-indicator-wait'); element.removeClass('ng-input-indicator-wait');
} }
updateView(); elementDecorateError(element, error);
}); });
} }
if (state === null){ if (state === null){
// request in flight, mark widget invalid, but don't show it to user // request in flight, mark widget invalid, but don't show it to user
this['$invalidWidgets'].push(this.$element); (this['$invalidWidgets']||[]).push(this.$element);
} }
return state; return state;
} }

View file

@ -28,13 +28,7 @@ function valueAccessor(scope, element) {
function validate(value) { function validate(value) {
var error = required && !trim(value) ? "Required" : validator({self:scope, scope:{get:scope.$get, set:scope.$set}}, value); var error = required && !trim(value) ? "Required" : validator({self:scope, scope:{get:scope.$get, set:scope.$set}}, value);
if (error !== lastError) { if (error !== lastError) {
if (error) { elementDecorateError(element, error);
element.addClass(NG_VALIDATION_ERROR);
element.attr(NG_ERROR, error);
} else {
element.removeClass(NG_VALIDATION_ERROR);
element.removeAttr(NG_ERROR);
}
lastError = error; lastError = error;
} }
return value; return value;

View file

@ -91,24 +91,35 @@ describe('Validator:asynchronous', function(){
value = null; value = null;
fn = null; fn = null;
self = { self = {
$element:jqLite('<input />')[0], $element:jqLite('<input />'),
$invalidWidgets:[], $invalidWidgets:[],
$updateView: noop $updateView: noop
}; };
}); });
xit('should make a request and show spinner', function(){ afterEach(function(){
var x = compile('<input name="name" ng-validate="asynchronous:asyncFn"/>'); if (self.$element) self.$element.remove();
var asyncFn = function(v,f){value=v; fn=f;}; var oldCache = jqCache;
var input = x.node.find(":input"); jqCache = {};
x.scope.$set("asyncFn", asyncFn); expect(size(oldCache)).toEqual(0);
x.scope.$set("name", "misko"); });
x.scope.$eval();
it('should make a request and show spinner', function(){
var value, fn;
var scope = angular.compile('<input type="text" name="name" ng-validate="asynchronous:asyncFn"/>');
scope.$init();
var input = scope.$element;
scope.asyncFn = function(v,f){
value=v; fn=f;
};
scope.name = "misko";
scope.$eval();
expect(value).toEqual('misko'); expect(value).toEqual('misko');
expect(input.hasClass('ng-input-indicator-wait')).toBeTruthy(); expect(input.hasClass('ng-input-indicator-wait')).toBeTruthy();
fn("myError"); fn("myError");
expect(input.hasClass('ng-input-indicator-wait')).toBeFalsy(); expect(input.hasClass('ng-input-indicator-wait')).toBeFalsy();
expect(input.attr('ng-error')).toEqual("myError"); expect(input.attr('ng-error')).toEqual("myError");
scope.$element.remove();
}); });
it("should not make second request to same value", function(){ it("should not make second request to same value", function(){

View file

@ -15,7 +15,9 @@ describe("input widget", function(){
afterEach(function(){ afterEach(function(){
if (element) element.remove(); if (element) element.remove();
expect(size(jqCache)).toEqual(0); var oldCache = jqCache;
jqCache = {};
expect(size(oldCache)).toEqual(0);
}); });
it('should input-text auto init and handle keyup/change events', function(){ it('should input-text auto init and handle keyup/change events', function(){