testing.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /**
  2. * @license Angular v8.1.0
  3. * (c) 2010-2019 Google LLC. https://angular.io/
  4. * License: MIT
  5. */
  6. import { __extends } from 'tslib';
  7. import { ResourceLoader, core, DirectiveResolver, NgModuleResolver, PipeResolver } from '@angular/compiler';
  8. /**
  9. * @license
  10. * Copyright Google Inc. All Rights Reserved.
  11. *
  12. * Use of this source code is governed by an MIT-style license that can be
  13. * found in the LICENSE file at https://angular.io/license
  14. */
  15. /**
  16. * A mock implementation of {@link ResourceLoader} that allows outgoing requests to be mocked
  17. * and responded to within a single test, without going to the network.
  18. */
  19. var MockResourceLoader = /** @class */ (function (_super) {
  20. __extends(MockResourceLoader, _super);
  21. function MockResourceLoader() {
  22. var _this = _super !== null && _super.apply(this, arguments) || this;
  23. _this._expectations = [];
  24. _this._definitions = new Map();
  25. _this._requests = [];
  26. return _this;
  27. }
  28. MockResourceLoader.prototype.get = function (url) {
  29. var request = new _PendingRequest(url);
  30. this._requests.push(request);
  31. return request.getPromise();
  32. };
  33. MockResourceLoader.prototype.hasPendingRequests = function () { return !!this._requests.length; };
  34. /**
  35. * Add an expectation for the given URL. Incoming requests will be checked against
  36. * the next expectation (in FIFO order). The `verifyNoOutstandingExpectations` method
  37. * can be used to check if any expectations have not yet been met.
  38. *
  39. * The response given will be returned if the expectation matches.
  40. */
  41. MockResourceLoader.prototype.expect = function (url, response) {
  42. var expectation = new _Expectation(url, response);
  43. this._expectations.push(expectation);
  44. };
  45. /**
  46. * Add a definition for the given URL to return the given response. Unlike expectations,
  47. * definitions have no order and will satisfy any matching request at any time. Also
  48. * unlike expectations, unused definitions do not cause `verifyNoOutstandingExpectations`
  49. * to return an error.
  50. */
  51. MockResourceLoader.prototype.when = function (url, response) { this._definitions.set(url, response); };
  52. /**
  53. * Process pending requests and verify there are no outstanding expectations. Also fails
  54. * if no requests are pending.
  55. */
  56. MockResourceLoader.prototype.flush = function () {
  57. if (this._requests.length === 0) {
  58. throw new Error('No pending requests to flush');
  59. }
  60. do {
  61. this._processRequest(this._requests.shift());
  62. } while (this._requests.length > 0);
  63. this.verifyNoOutstandingExpectations();
  64. };
  65. /**
  66. * Throw an exception if any expectations have not been satisfied.
  67. */
  68. MockResourceLoader.prototype.verifyNoOutstandingExpectations = function () {
  69. if (this._expectations.length === 0)
  70. return;
  71. var urls = [];
  72. for (var i = 0; i < this._expectations.length; i++) {
  73. var expectation = this._expectations[i];
  74. urls.push(expectation.url);
  75. }
  76. throw new Error("Unsatisfied requests: " + urls.join(', '));
  77. };
  78. MockResourceLoader.prototype._processRequest = function (request) {
  79. var url = request.url;
  80. if (this._expectations.length > 0) {
  81. var expectation = this._expectations[0];
  82. if (expectation.url == url) {
  83. remove(this._expectations, expectation);
  84. request.complete(expectation.response);
  85. return;
  86. }
  87. }
  88. if (this._definitions.has(url)) {
  89. var response = this._definitions.get(url);
  90. request.complete(response == null ? null : response);
  91. return;
  92. }
  93. throw new Error("Unexpected request " + url);
  94. };
  95. return MockResourceLoader;
  96. }(ResourceLoader));
  97. var _PendingRequest = /** @class */ (function () {
  98. function _PendingRequest(url) {
  99. var _this = this;
  100. this.url = url;
  101. this.promise = new Promise(function (res, rej) {
  102. _this.resolve = res;
  103. _this.reject = rej;
  104. });
  105. }
  106. _PendingRequest.prototype.complete = function (response) {
  107. if (response == null) {
  108. this.reject("Failed to load " + this.url);
  109. }
  110. else {
  111. this.resolve(response);
  112. }
  113. };
  114. _PendingRequest.prototype.getPromise = function () { return this.promise; };
  115. return _PendingRequest;
  116. }());
  117. var _Expectation = /** @class */ (function () {
  118. function _Expectation(url, response) {
  119. this.url = url;
  120. this.response = response;
  121. }
  122. return _Expectation;
  123. }());
  124. function remove(list, el) {
  125. var index = list.indexOf(el);
  126. if (index > -1) {
  127. list.splice(index, 1);
  128. }
  129. }
  130. /**
  131. * @license
  132. * Copyright Google Inc. All Rights Reserved.
  133. *
  134. * Use of this source code is governed by an MIT-style license that can be
  135. * found in the LICENSE file at https://angular.io/license
  136. */
  137. var MockSchemaRegistry = /** @class */ (function () {
  138. function MockSchemaRegistry(existingProperties, attrPropMapping, existingElements, invalidProperties, invalidAttributes) {
  139. this.existingProperties = existingProperties;
  140. this.attrPropMapping = attrPropMapping;
  141. this.existingElements = existingElements;
  142. this.invalidProperties = invalidProperties;
  143. this.invalidAttributes = invalidAttributes;
  144. }
  145. MockSchemaRegistry.prototype.hasProperty = function (tagName, property, schemas) {
  146. var value = this.existingProperties[property];
  147. return value === void 0 ? true : value;
  148. };
  149. MockSchemaRegistry.prototype.hasElement = function (tagName, schemaMetas) {
  150. var value = this.existingElements[tagName.toLowerCase()];
  151. return value === void 0 ? true : value;
  152. };
  153. MockSchemaRegistry.prototype.allKnownElementNames = function () { return Object.keys(this.existingElements); };
  154. MockSchemaRegistry.prototype.securityContext = function (selector, property, isAttribute) {
  155. return core.SecurityContext.NONE;
  156. };
  157. MockSchemaRegistry.prototype.getMappedPropName = function (attrName) { return this.attrPropMapping[attrName] || attrName; };
  158. MockSchemaRegistry.prototype.getDefaultComponentElementName = function () { return 'ng-component'; };
  159. MockSchemaRegistry.prototype.validateProperty = function (name) {
  160. if (this.invalidProperties.indexOf(name) > -1) {
  161. return { error: true, msg: "Binding to property '" + name + "' is disallowed for security reasons" };
  162. }
  163. else {
  164. return { error: false };
  165. }
  166. };
  167. MockSchemaRegistry.prototype.validateAttribute = function (name) {
  168. if (this.invalidAttributes.indexOf(name) > -1) {
  169. return {
  170. error: true,
  171. msg: "Binding to attribute '" + name + "' is disallowed for security reasons"
  172. };
  173. }
  174. else {
  175. return { error: false };
  176. }
  177. };
  178. MockSchemaRegistry.prototype.normalizeAnimationStyleProperty = function (propName) { return propName; };
  179. MockSchemaRegistry.prototype.normalizeAnimationStyleValue = function (camelCaseProp, userProvidedProp, val) {
  180. return { error: null, value: val.toString() };
  181. };
  182. return MockSchemaRegistry;
  183. }());
  184. /**
  185. * An implementation of {@link DirectiveResolver} that allows overriding
  186. * various properties of directives.
  187. */
  188. var MockDirectiveResolver = /** @class */ (function (_super) {
  189. __extends(MockDirectiveResolver, _super);
  190. function MockDirectiveResolver(reflector) {
  191. var _this = _super.call(this, reflector) || this;
  192. _this._directives = new Map();
  193. return _this;
  194. }
  195. MockDirectiveResolver.prototype.resolve = function (type, throwIfNotFound) {
  196. if (throwIfNotFound === void 0) { throwIfNotFound = true; }
  197. return this._directives.get(type) || _super.prototype.resolve.call(this, type, throwIfNotFound);
  198. };
  199. /**
  200. * Overrides the {@link core.Directive} for a directive.
  201. */
  202. MockDirectiveResolver.prototype.setDirective = function (type, metadata) {
  203. this._directives.set(type, metadata);
  204. };
  205. return MockDirectiveResolver;
  206. }(DirectiveResolver));
  207. /**
  208. * @license
  209. * Copyright Google Inc. All Rights Reserved.
  210. *
  211. * Use of this source code is governed by an MIT-style license that can be
  212. * found in the LICENSE file at https://angular.io/license
  213. */
  214. var MockNgModuleResolver = /** @class */ (function (_super) {
  215. __extends(MockNgModuleResolver, _super);
  216. function MockNgModuleResolver(reflector) {
  217. var _this = _super.call(this, reflector) || this;
  218. _this._ngModules = new Map();
  219. return _this;
  220. }
  221. /**
  222. * Overrides the {@link NgModule} for a module.
  223. */
  224. MockNgModuleResolver.prototype.setNgModule = function (type, metadata) {
  225. this._ngModules.set(type, metadata);
  226. };
  227. /**
  228. * Returns the {@link NgModule} for a module:
  229. * - Set the {@link NgModule} to the overridden view when it exists or fallback to the
  230. * default
  231. * `NgModuleResolver`, see `setNgModule`.
  232. */
  233. MockNgModuleResolver.prototype.resolve = function (type, throwIfNotFound) {
  234. if (throwIfNotFound === void 0) { throwIfNotFound = true; }
  235. return this._ngModules.get(type) || _super.prototype.resolve.call(this, type, throwIfNotFound);
  236. };
  237. return MockNgModuleResolver;
  238. }(NgModuleResolver));
  239. /**
  240. * @license
  241. * Copyright Google Inc. All Rights Reserved.
  242. *
  243. * Use of this source code is governed by an MIT-style license that can be
  244. * found in the LICENSE file at https://angular.io/license
  245. */
  246. var MockPipeResolver = /** @class */ (function (_super) {
  247. __extends(MockPipeResolver, _super);
  248. function MockPipeResolver(refector) {
  249. var _this = _super.call(this, refector) || this;
  250. _this._pipes = new Map();
  251. return _this;
  252. }
  253. /**
  254. * Overrides the {@link Pipe} for a pipe.
  255. */
  256. MockPipeResolver.prototype.setPipe = function (type, metadata) { this._pipes.set(type, metadata); };
  257. /**
  258. * Returns the {@link Pipe} for a pipe:
  259. * - Set the {@link Pipe} to the overridden view when it exists or fallback to the
  260. * default
  261. * `PipeResolver`, see `setPipe`.
  262. */
  263. MockPipeResolver.prototype.resolve = function (type, throwIfNotFound) {
  264. if (throwIfNotFound === void 0) { throwIfNotFound = true; }
  265. var metadata = this._pipes.get(type);
  266. if (!metadata) {
  267. metadata = _super.prototype.resolve.call(this, type, throwIfNotFound);
  268. }
  269. return metadata;
  270. };
  271. return MockPipeResolver;
  272. }(PipeResolver));
  273. /**
  274. * @license
  275. * Copyright Google Inc. All Rights Reserved.
  276. *
  277. * Use of this source code is governed by an MIT-style license that can be
  278. * found in the LICENSE file at https://angular.io/license
  279. */
  280. /**
  281. * @license
  282. * Copyright Google Inc. All Rights Reserved.
  283. *
  284. * Use of this source code is governed by an MIT-style license that can be
  285. * found in the LICENSE file at https://angular.io/license
  286. */
  287. // This file only reexports content of the `src` folder. Keep it that way.
  288. /**
  289. * @license
  290. * Copyright Google Inc. All Rights Reserved.
  291. *
  292. * Use of this source code is governed by an MIT-style license that can be
  293. * found in the LICENSE file at https://angular.io/license
  294. */
  295. /**
  296. * Generated bundle index. Do not edit.
  297. */
  298. export { MockResourceLoader, MockSchemaRegistry, MockDirectiveResolver, MockNgModuleResolver, MockPipeResolver };
  299. //# sourceMappingURL=testing.js.map