mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-16 23:30:23 +00:00
fix isssue where the jasmine currentSpec does not get updated and hence everything runs as last spec context.
This commit is contained in:
parent
fce48eb60a
commit
913729ee01
3 changed files with 78 additions and 124 deletions
|
|
@ -189,3 +189,7 @@ angularService('$route', function(location, params){
|
|||
return $route;
|
||||
}, {inject: ['$location']});
|
||||
|
||||
angularService('$resource', function(browser){
|
||||
var resource = new ResourceFactory(bind(browser, browser.xhr));
|
||||
return bind(resource, resource.route);
|
||||
}, {inject: ['$browser']});
|
||||
|
|
|
|||
|
|
@ -1,61 +1,3 @@
|
|||
function MockXHR(){
|
||||
this.expectations = {
|
||||
'GET': {},
|
||||
'POST': {},
|
||||
'DELETE': {}
|
||||
};
|
||||
this.queue = [];
|
||||
}
|
||||
MockXHR.prototype = {
|
||||
method: function(verb, url, data, callback) {
|
||||
if (verb == 'POST')
|
||||
url += '|' + angular.toJson(data);
|
||||
var response = this.expectations[verb][url];
|
||||
if (!response)
|
||||
throw "No expectation for " + verb + " on '" + url + "'.";
|
||||
this.queue.push(function(){
|
||||
callback(response);
|
||||
});
|
||||
},
|
||||
|
||||
expectGET: function(url) {
|
||||
var self = this;
|
||||
return {
|
||||
respond: function(response){
|
||||
self.expectations.GET[url] = response;
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
expectDELETE: function(url) {
|
||||
var self = this;
|
||||
return {
|
||||
respond: function(response){
|
||||
self.expectations.DELETE[url] = response;
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
expectPOST: function(url) {
|
||||
var self = this;
|
||||
return {
|
||||
data: function(data){
|
||||
return {
|
||||
respond: function(response){
|
||||
self.expectations.POST[url + '|' + angular.toJson(data)] = response;
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
flush: function(){
|
||||
while(this.queue.length) {
|
||||
this.queue.shift()();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
describe("resource", function() {
|
||||
var xhr, resource, CreditCard, callback;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,17 @@
|
|||
describe("services", function(){
|
||||
describe("service", function(){
|
||||
var scope;
|
||||
|
||||
beforeEach(function(){
|
||||
scope = createScope(null, angularService, {});
|
||||
});
|
||||
|
||||
afterEach(function(){
|
||||
if (scope && scope.$element)
|
||||
scope.$element.remove();
|
||||
});
|
||||
|
||||
|
||||
|
||||
it("should inject $window", function(){
|
||||
expect(scope.$window).toEqual(window);
|
||||
});
|
||||
|
|
@ -82,75 +89,76 @@ describe("services", function(){
|
|||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
describe("service $invalidWidgets", function(){
|
||||
var scope;
|
||||
beforeEach(function(){
|
||||
scope = null;
|
||||
});
|
||||
afterEach(function(){
|
||||
if (scope && scope.$element)
|
||||
describe("$invalidWidgets", function(){
|
||||
it("should count number of invalid widgets", function(){
|
||||
var scope = compile('<input name="price" ng-required ng-validate="number"></input>').$init();
|
||||
expect(scope.$invalidWidgets.length).toEqual(1);
|
||||
scope.price = 123;
|
||||
scope.$eval();
|
||||
expect(scope.$invalidWidgets.length).toEqual(0);
|
||||
scope.$element.remove();
|
||||
});
|
||||
scope.price = 'abc';
|
||||
scope.$eval();
|
||||
expect(scope.$invalidWidgets.length).toEqual(1);
|
||||
|
||||
it("should count number of invalid widgets", function(){
|
||||
var scope = compile('<input name="price" ng-required ng-validate="number"></input>').$init();
|
||||
expect(scope.$invalidWidgets.length).toEqual(1);
|
||||
scope.price = 123;
|
||||
scope.$eval();
|
||||
expect(scope.$invalidWidgets.length).toEqual(0);
|
||||
scope.$element.remove();
|
||||
scope.price = 'abc';
|
||||
scope.$eval();
|
||||
expect(scope.$invalidWidgets.length).toEqual(1);
|
||||
jqLite(document.body).append(scope.$element);
|
||||
scope.$invalidWidgets.clearOrphans();
|
||||
expect(scope.$invalidWidgets.length).toEqual(1);
|
||||
|
||||
jqLite(document.body).append(scope.$element);
|
||||
scope.$invalidWidgets.clearOrphans();
|
||||
expect(scope.$invalidWidgets.length).toEqual(1);
|
||||
|
||||
jqLite(document.body).html('');
|
||||
scope.$invalidWidgets.clearOrphans();
|
||||
expect(scope.$invalidWidgets.length).toEqual(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("service $route", function(){
|
||||
it('should route and fire change event', function(){
|
||||
var log = '';
|
||||
function BookChapter() {
|
||||
this.log = '<init>';
|
||||
}
|
||||
BookChapter.prototype.init = function(){
|
||||
log += 'init();';
|
||||
};
|
||||
var scope = compile('<div></div>').$init();
|
||||
scope.$route.when('/Book/:book/Chapter/:chapter', {controller: BookChapter, template:'Chapter.html'});
|
||||
scope.$route.when('/Blank');
|
||||
scope.$route.onChange(function(){
|
||||
log += 'onChange();';
|
||||
jqLite(document.body).html('');
|
||||
scope.$invalidWidgets.clearOrphans();
|
||||
expect(scope.$invalidWidgets.length).toEqual(0);
|
||||
});
|
||||
scope.$location.parse('http://server#/Book/Moby/Chapter/Intro?p=123');
|
||||
scope.$eval();
|
||||
expect(log).toEqual('onChange();init();');
|
||||
expect(scope.$route.current.params).toEqual({book:'Moby', chapter:'Intro', p:'123'});
|
||||
expect(scope.$route.current.scope.log).toEqual('<init>');
|
||||
var lastId = scope.$route.current.scope.$id;
|
||||
|
||||
log = '';
|
||||
scope.$location.parse('http://server#/Blank?ignore');
|
||||
scope.$eval();
|
||||
expect(log).toEqual('onChange();');
|
||||
expect(scope.$route.current.params).toEqual({ignore:true});
|
||||
expect(scope.$route.current.scope.$id).not.toEqual(lastId);
|
||||
|
||||
log = '';
|
||||
scope.$location.parse('http://server#/NONE');
|
||||
scope.$eval();
|
||||
expect(log).toEqual('onChange();');
|
||||
expect(scope.$route.current).toEqual(null);
|
||||
|
||||
scope.$route.when('/NONE', {template:'instant update'});
|
||||
expect(scope.$route.current.template).toEqual('instant update');
|
||||
});
|
||||
|
||||
|
||||
describe("$route", function(){
|
||||
it('should route and fire change event', function(){
|
||||
var log = '';
|
||||
function BookChapter() {
|
||||
this.log = '<init>';
|
||||
}
|
||||
BookChapter.prototype.init = function(){
|
||||
log += 'init();';
|
||||
};
|
||||
var scope = compile('<div></div>').$init();
|
||||
scope.$route.when('/Book/:book/Chapter/:chapter', {controller: BookChapter, template:'Chapter.html'});
|
||||
scope.$route.when('/Blank');
|
||||
scope.$route.onChange(function(){
|
||||
log += 'onChange();';
|
||||
});
|
||||
scope.$location.parse('http://server#/Book/Moby/Chapter/Intro?p=123');
|
||||
scope.$eval();
|
||||
expect(log).toEqual('onChange();init();');
|
||||
expect(scope.$route.current.params).toEqual({book:'Moby', chapter:'Intro', p:'123'});
|
||||
expect(scope.$route.current.scope.log).toEqual('<init>');
|
||||
var lastId = scope.$route.current.scope.$id;
|
||||
|
||||
log = '';
|
||||
scope.$location.parse('http://server#/Blank?ignore');
|
||||
scope.$eval();
|
||||
expect(log).toEqual('onChange();');
|
||||
expect(scope.$route.current.params).toEqual({ignore:true});
|
||||
expect(scope.$route.current.scope.$id).not.toEqual(lastId);
|
||||
|
||||
log = '';
|
||||
scope.$location.parse('http://server#/NONE');
|
||||
scope.$eval();
|
||||
expect(log).toEqual('onChange();');
|
||||
expect(scope.$route.current).toEqual(null);
|
||||
|
||||
scope.$route.when('/NONE', {template:'instant update'});
|
||||
expect(scope.$route.current.template).toEqual('instant update');
|
||||
});
|
||||
});
|
||||
|
||||
describe('$resource', function(){
|
||||
it('should publish to root scope', function(){
|
||||
expect(scope.$resource).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue