mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-23 21:35:47 +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;
|
return $route;
|
||||||
}, {inject: ['$location']});
|
}, {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() {
|
describe("resource", function() {
|
||||||
var xhr, resource, CreditCard, callback;
|
var xhr, resource, CreditCard, callback;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,17 @@
|
||||||
describe("services", function(){
|
describe("service", function(){
|
||||||
var scope;
|
var scope;
|
||||||
|
|
||||||
beforeEach(function(){
|
beforeEach(function(){
|
||||||
scope = createScope(null, angularService, {});
|
scope = createScope(null, angularService, {});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
afterEach(function(){
|
||||||
|
if (scope && scope.$element)
|
||||||
|
scope.$element.remove();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
it("should inject $window", function(){
|
it("should inject $window", function(){
|
||||||
expect(scope.$window).toEqual(window);
|
expect(scope.$window).toEqual(window);
|
||||||
});
|
});
|
||||||
|
|
@ -82,75 +89,76 @@ describe("services", function(){
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
describe("service $invalidWidgets", function(){
|
describe("$invalidWidgets", function(){
|
||||||
var scope;
|
it("should count number of invalid widgets", function(){
|
||||||
beforeEach(function(){
|
var scope = compile('<input name="price" ng-required ng-validate="number"></input>').$init();
|
||||||
scope = null;
|
expect(scope.$invalidWidgets.length).toEqual(1);
|
||||||
});
|
scope.price = 123;
|
||||||
afterEach(function(){
|
scope.$eval();
|
||||||
if (scope && scope.$element)
|
expect(scope.$invalidWidgets.length).toEqual(0);
|
||||||
scope.$element.remove();
|
scope.$element.remove();
|
||||||
});
|
scope.price = 'abc';
|
||||||
|
scope.$eval();
|
||||||
|
expect(scope.$invalidWidgets.length).toEqual(1);
|
||||||
|
|
||||||
it("should count number of invalid widgets", function(){
|
jqLite(document.body).append(scope.$element);
|
||||||
var scope = compile('<input name="price" ng-required ng-validate="number"></input>').$init();
|
scope.$invalidWidgets.clearOrphans();
|
||||||
expect(scope.$invalidWidgets.length).toEqual(1);
|
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);
|
jqLite(document.body).html('');
|
||||||
scope.$invalidWidgets.clearOrphans();
|
scope.$invalidWidgets.clearOrphans();
|
||||||
expect(scope.$invalidWidgets.length).toEqual(1);
|
expect(scope.$invalidWidgets.length).toEqual(0);
|
||||||
|
|
||||||
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();';
|
|
||||||
});
|
});
|
||||||
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