| 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
 | describe('$location', function() {
  var scope, $location, $browser;
  beforeEach(function(){
    scope = angular.scope();
    $location = scope.$service('$location');
    $browser = scope.$service('$browser');
  });
  afterEach(function(){
    dealoc(scope);
  });
  it("should update location object immediately when update is called", function() {
    var href = 'http://host:123/p/a/t/h.html?query=value#path?key=value&flag&key2=';
    $location.update(href);
    expect($location.href).toEqual(href);
    expect($location.protocol).toEqual("http");
    expect($location.host).toEqual("host");
    expect($location.port).toEqual("123");
    expect($location.path).toEqual("/p/a/t/h.html");
    expect($location.search).toEqual({query:'value'});
    expect($location.hash).toEqual('path?key=value&flag&key2=');
    expect($location.hashPath).toEqual('path');
    expect($location.hashSearch).toEqual({key: 'value', flag: true, key2: ''});
  });
  it('should update location when browser url changed', function() {
    var origUrl = $location.href;
    expect(origUrl).toEqual($browser.getUrl());
    var newUrl = 'http://somenew/url#foo';
    $browser.setUrl(newUrl);
    $browser.poll();
    expect($location.href).toEqual(newUrl);
  });
  it('should update browser at the end of $eval', function() {
    var origBrowserUrl = $browser.getUrl();
    $location.update('http://www.angularjs.org/');
    $location.update({path: '/a/b'});
    expect($location.href).toEqual('http://www.angularjs.org/a/b');
    expect($browser.getUrl()).toEqual(origBrowserUrl);
    scope.$eval();
    expect($browser.getUrl()).toEqual('http://www.angularjs.org/a/b');
  });
  it('should update hashPath and hashSearch on hash update', function(){
    $location.update('http://server/#path?a=b');
    expect($location.hashPath).toEqual('path');
    expect($location.hashSearch).toEqual({a:'b'});
    $location.update({hash: ''});
    expect($location.hashPath).toEqual('');
    expect($location.hashSearch).toEqual({});
  });
  it('should update hash on hashPath or hashSearch update', function() {
    $location.update('http://server/#path?a=b');
    scope.$eval();
    $location.update({hashPath: '', hashSearch: {}});
    expect($location.hash).toEqual('');
  });
  it('should update hashPath and hashSearch on $location.hash change upon eval', function(){
    $location.update('http://server/#path?a=b');
    scope.$eval();
    $location.hash = '';
    scope.$eval();
    expect($location.href).toEqual('http://server/');
    expect($location.hashPath).toEqual('');
    expect($location.hashSearch).toEqual({});
  });
  it('should update hash on $location.hashPath or $location.hashSearch change upon eval',
      function() {
    $location.update('http://server/#path?a=b');
    scope.$eval();
    $location.hashPath = '';
    $location.hashSearch = {};
    scope.$eval();
    expect($location.href).toEqual('http://server/');
    expect($location.hash).toEqual('');
  });
  it('should sync $location upon eval before watches are fired', function(){
    scope.$location = scope.$service('$location'); //publish to the scope for $watch
    var log = '';
    scope.$watch('$location.hash', function(){
      log += this.$location.hashPath + ';';
    });
    expect(log).toEqual(';');
    log = '';
    scope.$location.hash = '/abc';
    scope.$eval();
    expect(scope.$location.hash).toEqual('/abc');
    expect(log).toEqual('/abc;');
  });
  describe('sync', function() {
    it('should update hash with escaped hashPath', function() {
      $location.hashPath = 'foo=bar';
      scope.$eval();
      expect($location.hash).toBe('foo%3Dbar');
    });
    it('should give $location.href the highest precedence', function() {
      $location.hashPath = 'hashPath';
      $location.hashSearch = {hash:'search'};
      $location.hash = 'hash';
      $location.port = '333';
      $location.host = 'host';
      $location.href = 'https://hrefhost:23/hrefpath';
      scope.$eval();
      expect($location).toEqualData({href: 'https://hrefhost:23/hrefpath',
                                     protocol: 'https',
                                     host: 'hrefhost',
                                     port: '23',
                                     path: '/hrefpath',
                                     search: {},
                                     hash: '',
                                     hashPath: '',
                                     hashSearch: {}
                                    });
    });
    it('should give $location.hash second highest precedence', function() {
      $location.hashPath = 'hashPath';
      $location.hashSearch = {hash:'search'};
      $location.hash = 'hash';
      $location.port = '333';
      $location.host = 'host';
      $location.path = '/path';
      scope.$eval();
      expect($location).toEqualData({href: 'http://host:333/path#hash',
                                     protocol: 'http',
                                     host: 'host',
                                     port: '333',
                                     path: '/path',
                                     search: {},
                                     hash: 'hash',
                                     hashPath: 'hash',
                                     hashSearch: {}
                                    });
    });
  });
  describe('update()', function() {
    it('should accept hash object and update only given properties', function() {
      $location.update("http://host:123/p/a/t/h.html?query=value#path?key=value&flag&key2=");
      $location.update({host: 'new', port: 24});
      expect($location.host).toEqual('new');
      expect($location.port).toEqual(24);
      expect($location.protocol).toEqual('http');
      expect($location.href).toEqual("http://new:24/p/a/t/h.html?query=value#path?key=value&flag&key2=");
    });
    it('should remove # if hash is empty', function() {
      $location.update('http://www.angularjs.org/index.php#');
      expect($location.href).toEqual('http://www.angularjs.org/index.php');
    });
    it('should clear hash when updating to hash-less URL', function() {
      $location.update('http://server');
      expect($location.href).toBe('http://server');
      expect($location.hash).toBe('');
    });
  });
  describe('updateHash()', function() {
    it('should accept single string argument to update path', function() {
      $location.updateHash('path');
      expect($location.hash).toEqual('path');
      expect($location.hashPath).toEqual('path');
    });
    it('should reset hashSearch when updating with a single string', function() {
      $location.updateHash({foo:'bar'}); //set some initial state for hashSearch
      $location.updateHash('path');
      expect($location.hashPath).toEqual('path');
      expect($location.hashSearch).toEqual({});
    });
    it('should accept single object argument to update search', function() {
      $location.updateHash({a: 'b'});
      expect($location.hash).toEqual('?a=b');
      expect($location.hashSearch).toEqual({a: 'b'});
    });
    it('should accept path string and search object arguments to update both', function() {
      $location.updateHash('path', {a: 'b'});
      expect($location.hash).toEqual('path?a=b');
      expect($location.hashSearch).toEqual({a: 'b'});
      expect($location.hashPath).toEqual('path');
    });
    it('should update href and hash when updating to empty string', function() {
      $location.updateHash('');
      expect($location.href).toBe('http://server');
      expect($location.hash).toBe('');
      scope.$eval();
      expect($location.href).toBe('http://server');
      expect($location.hash).toBe('');
    });
  });
  describe('URL_MATCH', function() {
    it('should parse basic url', function() {
      var match = URL_MATCH.exec('http://www.angularjs.org/path?search#hash?x=x');
      expect(match[1]).toEqual('http');
      expect(match[3]).toEqual('www.angularjs.org');
      expect(match[6]).toEqual('/path');
      expect(match[8]).toEqual('search');
      expect(match[10]).toEqual('hash?x=x');
    });
    it('should parse file://', function(){
      var match = URL_MATCH.exec('file:///Users/Shared/misko/work/angular.js/scenario/widgets.html');
      expect(match[1]).toEqual('file');
      expect(match[3]).toEqual('');
      expect(match[5]).toBeFalsy();
      expect(match[6]).toEqual('/Users/Shared/misko/work/angular.js/scenario/widgets.html');
      expect(match[8]).toBeFalsy();
    });
    it('should parse url with "-" in host', function(){
      var match = URL_MATCH.exec('http://a-b1.c-d.09/path');
      expect(match[1]).toEqual('http');
      expect(match[3]).toEqual('a-b1.c-d.09');
      expect(match[5]).toBeFalsy();
      expect(match[6]).toEqual('/path');
      expect(match[8]).toBeFalsy();
    });
    it('should parse host without "/" at the end', function() {
      var match = URL_MATCH.exec('http://host.org');
      expect(match[3]).toEqual('host.org');
      match = URL_MATCH.exec('http://host.org#');
      expect(match[3]).toEqual('host.org');
      match = URL_MATCH.exec('http://host.org?');
      expect(match[3]).toEqual('host.org');
    });
    it('should match with just "/" path', function() {
      var match = URL_MATCH.exec('http://server/#?book=moby');
      expect(match[10]).toEqual('?book=moby');
    });
  });
});
 |