UNPKG

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