testing.js 9.4 KB

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