testing.js 11 KB

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