1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
describe("services", function(){
var scope;
beforeEach(function(){
scope = createScope(null, angularService, {});
});
it("should inject $window", function(){
expect(scope.$window).toEqual(window);
});
xit('should add stylesheets', function(){
scope.$document = {
getElementsByTagName: function(name){
expect(name).toEqual('LINK');
return [];
}
};
scope.$document.addStyleSheet('css/angular.css');
});
describe("$location", function(){
it("should inject $location", function(){
scope.$location.parse('http://host:123/p/a/t/h.html?query=value#path?key=value');
expect(scope.$location.href).toEqual("http://host:123/p/a/t/h.html?query=value#path?key=value");
expect(scope.$location.protocol).toEqual("http");
expect(scope.$location.host).toEqual("host");
expect(scope.$location.port).toEqual("123");
expect(scope.$location.path).toEqual("/p/a/t/h.html");
expect(scope.$location.search).toEqual({query:'value'});
expect(scope.$location.hash).toEqual('path?key=value');
expect(scope.$location.hashPath).toEqual('path');
expect(scope.$location.hashSearch).toEqual({key:'value'});
scope.$location.hashPath = 'page=http://path';
scope.$location.hashSearch = {k:'a=b'};
expect(scope.$location.toString()).toEqual('http://host:123/p/a/t/h.html?query=value#page=http://path?k=a%3Db');
});
it('should parse file://', function(){
scope.$location.parse('file:///Users/Shared/misko/work/angular.js/scenario/widgets.html');
expect(scope.$location.href).toEqual("file:///Users/Shared/misko/work/angular.js/scenario/widgets.html");
expect(scope.$location.protocol).toEqual("file");
expect(scope.$location.host).toEqual("");
expect(scope.$location.port).toEqual(null);
expect(scope.$location.path).toEqual("/Users/Shared/misko/work/angular.js/scenario/widgets.html");
expect(scope.$location.search).toEqual({});
expect(scope.$location.hash).toEqual('');
expect(scope.$location.hashPath).toEqual('');
expect(scope.$location.hashSearch).toEqual({});
expect(scope.$location.toString()).toEqual('file:///Users/Shared/misko/work/angular.js/scenario/widgets.html#');
});
it('should update url on hash change', function(){
scope.$location.parse('http://server/#path?a=b');
scope.$location.hash = '';
expect(scope.$location.toString()).toEqual('http://server/#');
expect(scope.$location.hashPath).toEqual('');
});
it('should update url on hashPath change', function(){
scope.$location.parse('http://server/#path?a=b');
scope.$location.hashPath = '';
expect(scope.$location.toString()).toEqual('http://server/#?a=b');
expect(scope.$location.hash).toEqual('?a=b');
});
it('should update hash before any processing', function(){
var scope = compile('<div>');
var log = '';
scope.$watch('$location.hash', function(){
log += this.$location.hashPath + ';';
});
expect(log).toEqual(';');
log = '';
scope.$location.hash = '/abc';
scope.$eval();
expect(log).toEqual('/abc;');
});
});
});
describe("service $invalidWidgets", function(){
var scope;
beforeEach(function(){
scope = null;
});
afterEach(function(){
if (scope && scope.$element)
scope.$element.remove();
});
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).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');
});
});
|