common-testing.umd.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /**
  2. * @license Angular v8.1.0
  3. * (c) 2010-2019 Google LLC. https://angular.io/
  4. * License: MIT
  5. */
  6. (function (global, factory) {
  7. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common'), require('rxjs')) :
  8. typeof define === 'function' && define.amd ? define('@angular/common/testing', ['exports', '@angular/core', '@angular/common', 'rxjs'], factory) :
  9. (global = global || self, factory((global.ng = global.ng || {}, global.ng.common = global.ng.common || {}, global.ng.common.testing = {}), global.ng.core, global.ng.common, global.rxjs));
  10. }(this, function (exports, core, common, rxjs) { 'use strict';
  11. /*! *****************************************************************************
  12. Copyright (c) Microsoft Corporation. All rights reserved.
  13. Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  14. this file except in compliance with the License. You may obtain a copy of the
  15. License at http://www.apache.org/licenses/LICENSE-2.0
  16. THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  18. WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  19. MERCHANTABLITY OR NON-INFRINGEMENT.
  20. See the Apache Version 2.0 License for specific language governing permissions
  21. and limitations under the License.
  22. ***************************************************************************** */
  23. /* global Reflect, Promise */
  24. var extendStatics = function(d, b) {
  25. extendStatics = Object.setPrototypeOf ||
  26. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  27. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  28. return extendStatics(d, b);
  29. };
  30. function __extends(d, b) {
  31. extendStatics(d, b);
  32. function __() { this.constructor = d; }
  33. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  34. }
  35. var __assign = function() {
  36. __assign = Object.assign || function __assign(t) {
  37. for (var s, i = 1, n = arguments.length; i < n; i++) {
  38. s = arguments[i];
  39. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
  40. }
  41. return t;
  42. };
  43. return __assign.apply(this, arguments);
  44. };
  45. function __decorate(decorators, target, key, desc) {
  46. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  47. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  48. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  49. return c > 3 && r && Object.defineProperty(target, key, r), r;
  50. }
  51. function __param(paramIndex, decorator) {
  52. return function (target, key) { decorator(target, key, paramIndex); }
  53. }
  54. function __metadata(metadataKey, metadataValue) {
  55. if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
  56. }
  57. /**
  58. * @license
  59. * Copyright Google Inc. All Rights Reserved.
  60. *
  61. * Use of this source code is governed by an MIT-style license that can be
  62. * found in the LICENSE file at https://angular.io/license
  63. */
  64. /**
  65. * A spy for {@link Location} that allows tests to fire simulated location events.
  66. *
  67. * @publicApi
  68. */
  69. var SpyLocation = /** @class */ (function () {
  70. function SpyLocation() {
  71. this.urlChanges = [];
  72. this._history = [new LocationState('', '', null)];
  73. this._historyIndex = 0;
  74. /** @internal */
  75. this._subject = new core.EventEmitter();
  76. /** @internal */
  77. this._baseHref = '';
  78. /** @internal */
  79. this._platformStrategy = null;
  80. /** @internal */
  81. this._platformLocation = null;
  82. /** @internal */
  83. this._urlChangeListeners = [];
  84. }
  85. SpyLocation.prototype.setInitialPath = function (url) { this._history[this._historyIndex].path = url; };
  86. SpyLocation.prototype.setBaseHref = function (url) { this._baseHref = url; };
  87. SpyLocation.prototype.path = function () { return this._history[this._historyIndex].path; };
  88. SpyLocation.prototype.getState = function () { return this._history[this._historyIndex].state; };
  89. SpyLocation.prototype.isCurrentPathEqualTo = function (path, query) {
  90. if (query === void 0) { query = ''; }
  91. var givenPath = path.endsWith('/') ? path.substring(0, path.length - 1) : path;
  92. var currPath = this.path().endsWith('/') ? this.path().substring(0, this.path().length - 1) : this.path();
  93. return currPath == givenPath + (query.length > 0 ? ('?' + query) : '');
  94. };
  95. SpyLocation.prototype.simulateUrlPop = function (pathname) {
  96. this._subject.emit({ 'url': pathname, 'pop': true, 'type': 'popstate' });
  97. };
  98. SpyLocation.prototype.simulateHashChange = function (pathname) {
  99. // Because we don't prevent the native event, the browser will independently update the path
  100. this.setInitialPath(pathname);
  101. this.urlChanges.push('hash: ' + pathname);
  102. this._subject.emit({ 'url': pathname, 'pop': true, 'type': 'hashchange' });
  103. };
  104. SpyLocation.prototype.prepareExternalUrl = function (url) {
  105. if (url.length > 0 && !url.startsWith('/')) {
  106. url = '/' + url;
  107. }
  108. return this._baseHref + url;
  109. };
  110. SpyLocation.prototype.go = function (path, query, state) {
  111. if (query === void 0) { query = ''; }
  112. if (state === void 0) { state = null; }
  113. path = this.prepareExternalUrl(path);
  114. if (this._historyIndex > 0) {
  115. this._history.splice(this._historyIndex + 1);
  116. }
  117. this._history.push(new LocationState(path, query, state));
  118. this._historyIndex = this._history.length - 1;
  119. var locationState = this._history[this._historyIndex - 1];
  120. if (locationState.path == path && locationState.query == query) {
  121. return;
  122. }
  123. var url = path + (query.length > 0 ? ('?' + query) : '');
  124. this.urlChanges.push(url);
  125. this._subject.emit({ 'url': url, 'pop': false });
  126. };
  127. SpyLocation.prototype.replaceState = function (path, query, state) {
  128. if (query === void 0) { query = ''; }
  129. if (state === void 0) { state = null; }
  130. path = this.prepareExternalUrl(path);
  131. var history = this._history[this._historyIndex];
  132. if (history.path == path && history.query == query) {
  133. return;
  134. }
  135. history.path = path;
  136. history.query = query;
  137. history.state = state;
  138. var url = path + (query.length > 0 ? ('?' + query) : '');
  139. this.urlChanges.push('replace: ' + url);
  140. };
  141. SpyLocation.prototype.forward = function () {
  142. if (this._historyIndex < (this._history.length - 1)) {
  143. this._historyIndex++;
  144. this._subject.emit({ 'url': this.path(), 'state': this.getState(), 'pop': true });
  145. }
  146. };
  147. SpyLocation.prototype.back = function () {
  148. if (this._historyIndex > 0) {
  149. this._historyIndex--;
  150. this._subject.emit({ 'url': this.path(), 'state': this.getState(), 'pop': true });
  151. }
  152. };
  153. SpyLocation.prototype.onUrlChange = function (fn) {
  154. var _this = this;
  155. this._urlChangeListeners.push(fn);
  156. this.subscribe(function (v) { _this._notifyUrlChangeListeners(v.url, v.state); });
  157. };
  158. /** @internal */
  159. SpyLocation.prototype._notifyUrlChangeListeners = function (url, state) {
  160. if (url === void 0) { url = ''; }
  161. this._urlChangeListeners.forEach(function (fn) { return fn(url, state); });
  162. };
  163. SpyLocation.prototype.subscribe = function (onNext, onThrow, onReturn) {
  164. return this._subject.subscribe({ next: onNext, error: onThrow, complete: onReturn });
  165. };
  166. SpyLocation.prototype.normalize = function (url) { return null; };
  167. SpyLocation = __decorate([
  168. core.Injectable()
  169. ], SpyLocation);
  170. return SpyLocation;
  171. }());
  172. var LocationState = /** @class */ (function () {
  173. function LocationState(path, query, state) {
  174. this.path = path;
  175. this.query = query;
  176. this.state = state;
  177. }
  178. return LocationState;
  179. }());
  180. /**
  181. * @license
  182. * Copyright Google Inc. All Rights Reserved.
  183. *
  184. * Use of this source code is governed by an MIT-style license that can be
  185. * found in the LICENSE file at https://angular.io/license
  186. */
  187. /**
  188. * A mock implementation of {@link LocationStrategy} that allows tests to fire simulated
  189. * location events.
  190. *
  191. * @publicApi
  192. */
  193. var MockLocationStrategy = /** @class */ (function (_super) {
  194. __extends(MockLocationStrategy, _super);
  195. function MockLocationStrategy() {
  196. var _this = _super.call(this) || this;
  197. _this.internalBaseHref = '/';
  198. _this.internalPath = '/';
  199. _this.internalTitle = '';
  200. _this.urlChanges = [];
  201. /** @internal */
  202. _this._subject = new core.EventEmitter();
  203. _this.stateChanges = [];
  204. return _this;
  205. }
  206. MockLocationStrategy.prototype.simulatePopState = function (url) {
  207. this.internalPath = url;
  208. this._subject.emit(new _MockPopStateEvent(this.path()));
  209. };
  210. MockLocationStrategy.prototype.path = function (includeHash) {
  211. if (includeHash === void 0) { includeHash = false; }
  212. return this.internalPath;
  213. };
  214. MockLocationStrategy.prototype.prepareExternalUrl = function (internal) {
  215. if (internal.startsWith('/') && this.internalBaseHref.endsWith('/')) {
  216. return this.internalBaseHref + internal.substring(1);
  217. }
  218. return this.internalBaseHref + internal;
  219. };
  220. MockLocationStrategy.prototype.pushState = function (ctx, title, path, query) {
  221. // Add state change to changes array
  222. this.stateChanges.push(ctx);
  223. this.internalTitle = title;
  224. var url = path + (query.length > 0 ? ('?' + query) : '');
  225. this.internalPath = url;
  226. var externalUrl = this.prepareExternalUrl(url);
  227. this.urlChanges.push(externalUrl);
  228. };
  229. MockLocationStrategy.prototype.replaceState = function (ctx, title, path, query) {
  230. // Reset the last index of stateChanges to the ctx (state) object
  231. this.stateChanges[(this.stateChanges.length || 1) - 1] = ctx;
  232. this.internalTitle = title;
  233. var url = path + (query.length > 0 ? ('?' + query) : '');
  234. this.internalPath = url;
  235. var externalUrl = this.prepareExternalUrl(url);
  236. this.urlChanges.push('replace: ' + externalUrl);
  237. };
  238. MockLocationStrategy.prototype.onPopState = function (fn) { this._subject.subscribe({ next: fn }); };
  239. MockLocationStrategy.prototype.getBaseHref = function () { return this.internalBaseHref; };
  240. MockLocationStrategy.prototype.back = function () {
  241. if (this.urlChanges.length > 0) {
  242. this.urlChanges.pop();
  243. this.stateChanges.pop();
  244. var nextUrl = this.urlChanges.length > 0 ? this.urlChanges[this.urlChanges.length - 1] : '';
  245. this.simulatePopState(nextUrl);
  246. }
  247. };
  248. MockLocationStrategy.prototype.forward = function () { throw 'not implemented'; };
  249. MockLocationStrategy.prototype.getState = function () { return this.stateChanges[(this.stateChanges.length || 1) - 1]; };
  250. MockLocationStrategy = __decorate([
  251. core.Injectable(),
  252. __metadata("design:paramtypes", [])
  253. ], MockLocationStrategy);
  254. return MockLocationStrategy;
  255. }(common.LocationStrategy));
  256. var _MockPopStateEvent = /** @class */ (function () {
  257. function _MockPopStateEvent(newUrl) {
  258. this.newUrl = newUrl;
  259. this.pop = true;
  260. this.type = 'popstate';
  261. }
  262. return _MockPopStateEvent;
  263. }());
  264. /**
  265. * @license
  266. * Copyright Google Inc. All Rights Reserved.
  267. *
  268. * Use of this source code is governed by an MIT-style license that can be
  269. * found in the LICENSE file at https://angular.io/license
  270. */
  271. /**
  272. * Parser from https://tools.ietf.org/html/rfc3986#appendix-B
  273. * ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
  274. * 12 3 4 5 6 7 8 9
  275. *
  276. * Example: http://www.ics.uci.edu/pub/ietf/uri/#Related
  277. *
  278. * Results in:
  279. *
  280. * $1 = http:
  281. * $2 = http
  282. * $3 = //www.ics.uci.edu
  283. * $4 = www.ics.uci.edu
  284. * $5 = /pub/ietf/uri/
  285. * $6 = <undefined>
  286. * $7 = <undefined>
  287. * $8 = #Related
  288. * $9 = Related
  289. */
  290. var urlParse = /^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/;
  291. function parseUrl(urlStr, baseHref) {
  292. var verifyProtocol = /^((http[s]?|ftp):\/\/)/;
  293. var serverBase;
  294. // URL class requires full URL. If the URL string doesn't start with protocol, we need to add
  295. // an arbitrary base URL which can be removed afterward.
  296. if (!verifyProtocol.test(urlStr)) {
  297. serverBase = 'http://empty.com/';
  298. }
  299. var parsedUrl;
  300. try {
  301. parsedUrl = new URL(urlStr, serverBase);
  302. }
  303. catch (e) {
  304. var result = urlParse.exec(serverBase || '' + urlStr);
  305. if (!result) {
  306. throw new Error("Invalid URL: " + urlStr + " with base: " + baseHref);
  307. }
  308. var hostSplit = result[4].split(':');
  309. parsedUrl = {
  310. protocol: result[1],
  311. hostname: hostSplit[0],
  312. port: hostSplit[1] || '',
  313. pathname: result[5],
  314. search: result[6],
  315. hash: result[8],
  316. };
  317. }
  318. if (parsedUrl.pathname && parsedUrl.pathname.indexOf(baseHref) === 0) {
  319. parsedUrl.pathname = parsedUrl.pathname.substring(baseHref.length);
  320. }
  321. return {
  322. hostname: !serverBase && parsedUrl.hostname || '',
  323. protocol: !serverBase && parsedUrl.protocol || '',
  324. port: !serverBase && parsedUrl.port || '',
  325. pathname: parsedUrl.pathname || '/',
  326. search: parsedUrl.search || '',
  327. hash: parsedUrl.hash || '',
  328. };
  329. }
  330. /**
  331. * Provider for mock platform location config
  332. *
  333. * @publicApi
  334. */
  335. var MOCK_PLATFORM_LOCATION_CONFIG = new core.InjectionToken('MOCK_PLATFORM_LOCATION_CONFIG');
  336. /**
  337. * Mock implementation of URL state.
  338. *
  339. * @publicApi
  340. */
  341. var MockPlatformLocation = /** @class */ (function () {
  342. function MockPlatformLocation(config) {
  343. this.baseHref = '';
  344. this.hashUpdate = new rxjs.Subject();
  345. this.urlChanges = [{ hostname: '', protocol: '', port: '', pathname: '/', search: '', hash: '', state: null }];
  346. if (config) {
  347. this.baseHref = config.appBaseHref || '';
  348. var parsedChanges = this.parseChanges(null, config.startUrl || 'http://<empty>/', this.baseHref);
  349. this.urlChanges[0] = __assign({}, parsedChanges);
  350. }
  351. }
  352. Object.defineProperty(MockPlatformLocation.prototype, "hostname", {
  353. get: function () { return this.urlChanges[0].hostname; },
  354. enumerable: true,
  355. configurable: true
  356. });
  357. Object.defineProperty(MockPlatformLocation.prototype, "protocol", {
  358. get: function () { return this.urlChanges[0].protocol; },
  359. enumerable: true,
  360. configurable: true
  361. });
  362. Object.defineProperty(MockPlatformLocation.prototype, "port", {
  363. get: function () { return this.urlChanges[0].port; },
  364. enumerable: true,
  365. configurable: true
  366. });
  367. Object.defineProperty(MockPlatformLocation.prototype, "pathname", {
  368. get: function () { return this.urlChanges[0].pathname; },
  369. enumerable: true,
  370. configurable: true
  371. });
  372. Object.defineProperty(MockPlatformLocation.prototype, "search", {
  373. get: function () { return this.urlChanges[0].search; },
  374. enumerable: true,
  375. configurable: true
  376. });
  377. Object.defineProperty(MockPlatformLocation.prototype, "hash", {
  378. get: function () { return this.urlChanges[0].hash; },
  379. enumerable: true,
  380. configurable: true
  381. });
  382. Object.defineProperty(MockPlatformLocation.prototype, "state", {
  383. get: function () { return this.urlChanges[0].state; },
  384. enumerable: true,
  385. configurable: true
  386. });
  387. MockPlatformLocation.prototype.getBaseHrefFromDOM = function () { return this.baseHref; };
  388. MockPlatformLocation.prototype.onPopState = function (fn) {
  389. // No-op: a state stack is not implemented, so
  390. // no events will ever come.
  391. };
  392. MockPlatformLocation.prototype.onHashChange = function (fn) { this.hashUpdate.subscribe(fn); };
  393. Object.defineProperty(MockPlatformLocation.prototype, "href", {
  394. get: function () {
  395. var url = this.protocol + "//" + this.hostname + (this.port ? ':' + this.port : '');
  396. url += "" + (this.pathname === '/' ? '' : this.pathname) + this.search + this.hash;
  397. return url;
  398. },
  399. enumerable: true,
  400. configurable: true
  401. });
  402. Object.defineProperty(MockPlatformLocation.prototype, "url", {
  403. get: function () { return "" + this.pathname + this.search + this.hash; },
  404. enumerable: true,
  405. configurable: true
  406. });
  407. MockPlatformLocation.prototype.parseChanges = function (state, url, baseHref) {
  408. if (baseHref === void 0) { baseHref = ''; }
  409. // When the `history.state` value is stored, it is always copied.
  410. state = JSON.parse(JSON.stringify(state));
  411. return __assign({}, parseUrl(url, baseHref), { state: state });
  412. };
  413. MockPlatformLocation.prototype.replaceState = function (state, title, newUrl) {
  414. var _a = this.parseChanges(state, newUrl), pathname = _a.pathname, search = _a.search, parsedState = _a.state, hash = _a.hash;
  415. this.urlChanges[0] = __assign({}, this.urlChanges[0], { pathname: pathname, search: search, hash: hash, state: parsedState });
  416. };
  417. MockPlatformLocation.prototype.pushState = function (state, title, newUrl) {
  418. var _a = this.parseChanges(state, newUrl), pathname = _a.pathname, search = _a.search, parsedState = _a.state, hash = _a.hash;
  419. this.urlChanges.unshift(__assign({}, this.urlChanges[0], { pathname: pathname, search: search, hash: hash, state: parsedState }));
  420. };
  421. MockPlatformLocation.prototype.forward = function () { throw new Error('Not implemented'); };
  422. MockPlatformLocation.prototype.back = function () {
  423. var _this = this;
  424. var oldUrl = this.url;
  425. var oldHash = this.hash;
  426. this.urlChanges.shift();
  427. var newHash = this.hash;
  428. if (oldHash !== newHash) {
  429. scheduleMicroTask(function () { return _this.hashUpdate.next({
  430. type: 'hashchange', state: null, oldUrl: oldUrl, newUrl: _this.url
  431. }); });
  432. }
  433. };
  434. MockPlatformLocation.prototype.getState = function () { return this.state; };
  435. MockPlatformLocation = __decorate([
  436. core.Injectable(),
  437. __param(0, core.Inject(MOCK_PLATFORM_LOCATION_CONFIG)), __param(0, core.Optional()),
  438. __metadata("design:paramtypes", [Object])
  439. ], MockPlatformLocation);
  440. return MockPlatformLocation;
  441. }());
  442. function scheduleMicroTask(cb) {
  443. Promise.resolve(null).then(cb);
  444. }
  445. /**
  446. * @license
  447. * Copyright Google Inc. All Rights Reserved.
  448. *
  449. * Use of this source code is governed by an MIT-style license that can be
  450. * found in the LICENSE file at https://angular.io/license
  451. */
  452. /**
  453. * @license
  454. * Copyright Google Inc. All Rights Reserved.
  455. *
  456. * Use of this source code is governed by an MIT-style license that can be
  457. * found in the LICENSE file at https://angular.io/license
  458. */
  459. // This file only reexports content of the `src` folder. Keep it that way.
  460. /**
  461. * @license
  462. * Copyright Google Inc. All Rights Reserved.
  463. *
  464. * Use of this source code is governed by an MIT-style license that can be
  465. * found in the LICENSE file at https://angular.io/license
  466. */
  467. /**
  468. * Generated bundle index. Do not edit.
  469. */
  470. exports.SpyLocation = SpyLocation;
  471. exports.MockLocationStrategy = MockLocationStrategy;
  472. exports.MOCK_PLATFORM_LOCATION_CONFIG = MOCK_PLATFORM_LOCATION_CONFIG;
  473. exports.MockPlatformLocation = MockPlatformLocation;
  474. Object.defineProperty(exports, '__esModule', { value: true });
  475. }));
  476. //# sourceMappingURL=common-testing.umd.js.map