| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-02 12:12:46 -08:00
										 |  |  | import {MockResourceLoader} from '@angular/compiler/testing/src/resource_loader_mock'; | 
					
						
							|  |  |  | import {AsyncTestCompleter, beforeEach, describe, expect, inject, it} from '@angular/core/testing/src/testing_internal'; | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 14:42:55 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  |   describe('MockResourceLoader', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     let resourceLoader: MockResourceLoader; | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  |     beforeEach(() => { resourceLoader = new MockResourceLoader(); }); | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     function expectResponse( | 
					
						
							| 
									
										
										
										
											2017-03-24 09:59:58 -07:00
										 |  |  |         request: Promise<string>, url: string, response: string, done: () => void = null !) { | 
					
						
							| 
									
										
										
										
											2015-06-02 17:41:44 -07:00
										 |  |  |       function onResponse(text: string): string { | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  |         if (response === null) { | 
					
						
							|  |  |  |           throw `Unexpected response ${url} -> ${text}`; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           expect(text).toEqual(response); | 
					
						
							| 
									
										
										
										
											2017-03-02 09:37:01 -08:00
										 |  |  |           if (done != null) done(); | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-06-02 17:41:44 -07:00
										 |  |  |         return text; | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 17:41:44 -07:00
										 |  |  |       function onError(error: string): string { | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  |         if (response !== null) { | 
					
						
							|  |  |  |           throw `Unexpected error ${url}`; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           expect(error).toEqual(`Failed to load ${url}`); | 
					
						
							| 
									
										
										
										
											2017-03-02 09:37:01 -08:00
										 |  |  |           if (done != null) done(); | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-06-02 17:41:44 -07:00
										 |  |  |         return error; | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |       request.then(onResponse, onError); | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should return a response from the definitions', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |          const url = '/foo'; | 
					
						
							|  |  |  |          const response = 'bar'; | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  |          resourceLoader.when(url, response); | 
					
						
							|  |  |  |          expectResponse(resourceLoader.get(url), url, response, () => async.done()); | 
					
						
							|  |  |  |          resourceLoader.flush(); | 
					
						
							| 
									
										
										
										
											2015-06-02 17:41:44 -07:00
										 |  |  |        })); | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should return an error from the definitions', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |          const url = '/foo'; | 
					
						
							| 
									
										
										
										
											2017-03-24 09:59:58 -07:00
										 |  |  |          const response: string = null !; | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  |          resourceLoader.when(url, response); | 
					
						
							|  |  |  |          expectResponse(resourceLoader.get(url), url, response, () => async.done()); | 
					
						
							|  |  |  |          resourceLoader.flush(); | 
					
						
							| 
									
										
										
										
											2015-06-02 17:41:44 -07:00
										 |  |  |        })); | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should return a response from the expectations', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |          const url = '/foo'; | 
					
						
							|  |  |  |          const response = 'bar'; | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  |          resourceLoader.expect(url, response); | 
					
						
							|  |  |  |          expectResponse(resourceLoader.get(url), url, response, () => async.done()); | 
					
						
							|  |  |  |          resourceLoader.flush(); | 
					
						
							| 
									
										
										
										
											2015-06-02 17:41:44 -07:00
										 |  |  |        })); | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should return an error from the expectations', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |          const url = '/foo'; | 
					
						
							| 
									
										
										
										
											2017-03-24 09:59:58 -07:00
										 |  |  |          const response: string = null !; | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  |          resourceLoader.expect(url, response); | 
					
						
							|  |  |  |          expectResponse(resourceLoader.get(url), url, response, () => async.done()); | 
					
						
							|  |  |  |          resourceLoader.flush(); | 
					
						
							| 
									
										
										
										
											2015-06-02 17:41:44 -07:00
										 |  |  |        })); | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should not reuse expectations', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const url = '/foo'; | 
					
						
							|  |  |  |       const response = 'bar'; | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  |       resourceLoader.expect(url, response); | 
					
						
							|  |  |  |       resourceLoader.get(url); | 
					
						
							|  |  |  |       resourceLoader.get(url); | 
					
						
							|  |  |  |       expect(() => { resourceLoader.flush(); }).toThrowError('Unexpected request /foo'); | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should return expectations before definitions', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |          const url = '/foo'; | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  |          resourceLoader.when(url, 'when'); | 
					
						
							|  |  |  |          resourceLoader.expect(url, 'expect'); | 
					
						
							|  |  |  |          expectResponse(resourceLoader.get(url), url, 'expect'); | 
					
						
							|  |  |  |          expectResponse(resourceLoader.get(url), url, 'when', () => async.done()); | 
					
						
							|  |  |  |          resourceLoader.flush(); | 
					
						
							| 
									
										
										
										
											2015-06-02 17:41:44 -07:00
										 |  |  |        })); | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should throw when there is no definitions or expectations', () => { | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  |       resourceLoader.get('/foo'); | 
					
						
							|  |  |  |       expect(() => { resourceLoader.flush(); }).toThrowError('Unexpected request /foo'); | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  |     it('should throw when flush is called without any pending requests', () => { | 
					
						
							|  |  |  |       expect(() => { resourceLoader.flush(); }).toThrowError('No pending requests to flush'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 15:47:48 +08:00
										 |  |  |     it('should throw on unsatisfied expectations', () => { | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  |       resourceLoader.expect('/foo', 'bar'); | 
					
						
							|  |  |  |       resourceLoader.when('/bar', 'foo'); | 
					
						
							|  |  |  |       resourceLoader.get('/bar'); | 
					
						
							|  |  |  |       expect(() => { resourceLoader.flush(); }).toThrowError('Unsatisfied requests: /foo'); | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } |