| 
									
										
										
										
											2016-08-03 15:00:07 -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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-27 17:12:25 -07:00
										 |  |  | import {AsyncTestCompleter, describe, expect, inject, it} from '@angular/core/testing/testing_internal'; | 
					
						
							| 
									
										
										
										
											2016-08-26 16:34:08 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | import {Injector, Metric, Options, ReflectiveInjector, Runner, SampleDescription, SampleState, Sampler, Validator, WebDriverAdapter} from '../index'; | 
					
						
							| 
									
										
										
										
											2015-05-27 14:57:54 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							|  |  |  |   describe('runner', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     let injector: ReflectiveInjector; | 
					
						
							|  |  |  |     let runner: Runner; | 
					
						
							| 
									
										
										
										
											2015-05-27 14:57:54 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-26 16:34:08 -07:00
										 |  |  |     function createRunner(defaultProviders: any[] = null): Runner { | 
					
						
							| 
									
										
										
										
											2016-09-30 09:26:53 -07:00
										 |  |  |       if (!defaultProviders) { | 
					
						
							| 
									
										
										
										
											2016-08-26 16:34:08 -07:00
										 |  |  |         defaultProviders = []; | 
					
						
							| 
									
										
										
										
											2015-05-27 14:57:54 -07:00
										 |  |  |       } | 
					
						
							|  |  |  |       runner = new Runner([ | 
					
						
							| 
									
										
										
										
											2016-08-26 16:34:08 -07:00
										 |  |  |         defaultProviders, { | 
					
						
							| 
									
										
										
										
											2016-06-02 17:30:40 -07:00
										 |  |  |           provide: Sampler, | 
					
						
							| 
									
										
										
										
											2016-08-26 16:34:08 -07:00
										 |  |  |           useFactory: (_injector: ReflectiveInjector) => { | 
					
						
							| 
									
										
										
										
											2016-06-02 17:30:40 -07:00
										 |  |  |             injector = _injector; | 
					
						
							|  |  |  |             return new MockSampler(); | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           deps: [Injector] | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2016-08-03 15:00:07 -07:00
										 |  |  |         {provide: Metric, useFactory: () => new MockMetric(), deps: []}, | 
					
						
							|  |  |  |         {provide: Validator, useFactory: () => new MockValidator(), deps: []}, | 
					
						
							|  |  |  |         {provide: WebDriverAdapter, useFactory: () => new MockWebDriverAdapter(), deps: []} | 
					
						
							| 
									
										
										
										
											2015-05-27 14:57:54 -07:00
										 |  |  |       ]); | 
					
						
							|  |  |  |       return runner; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-26 16:34:08 -07:00
										 |  |  |     it('should set SampleDescription.id', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-05-27 14:57:54 -07:00
										 |  |  |          createRunner() | 
					
						
							|  |  |  |              .sample({id: 'someId'}) | 
					
						
							| 
									
										
										
										
											2015-06-26 15:59:18 -07:00
										 |  |  |              .then((_) => injector.get(SampleDescription)) | 
					
						
							| 
									
										
										
										
											2015-05-27 14:57:54 -07:00
										 |  |  |              .then((desc) => { | 
					
						
							|  |  |  |                expect(desc.id).toBe('someId'); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-26 16:34:08 -07:00
										 |  |  |     it('should merge SampleDescription.description', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-06-02 17:30:40 -07:00
										 |  |  |          createRunner([{provide: Options.DEFAULT_DESCRIPTION, useValue: {'a': 1}}]) | 
					
						
							| 
									
										
										
										
											2016-08-03 15:00:07 -07:00
										 |  |  |              .sample({ | 
					
						
							|  |  |  |                id: 'someId', | 
					
						
							|  |  |  |                providers: [{provide: Options.SAMPLE_DESCRIPTION, useValue: {'b': 2}}] | 
					
						
							|  |  |  |              }) | 
					
						
							| 
									
										
										
										
											2015-06-26 15:59:18 -07:00
										 |  |  |              .then((_) => injector.get(SampleDescription)) | 
					
						
							| 
									
										
										
										
											2015-05-27 14:57:54 -07:00
										 |  |  |              .then((desc) => { | 
					
						
							|  |  |  |                expect(desc.description) | 
					
						
							|  |  |  |                    .toEqual( | 
					
						
							|  |  |  |                        {'forceGc': false, 'userAgent': 'someUserAgent', 'a': 1, 'b': 2, 'v': 11}); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should fill SampleDescription.metrics from the Metric', | 
					
						
							| 
									
										
										
										
											2016-08-26 16:34:08 -07:00
										 |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-05-27 14:57:54 -07:00
										 |  |  |          createRunner() | 
					
						
							|  |  |  |              .sample({id: 'someId'}) | 
					
						
							| 
									
										
										
										
											2015-06-26 15:59:18 -07:00
										 |  |  |              .then((_) => injector.get(SampleDescription)) | 
					
						
							| 
									
										
										
										
											2015-05-27 14:57:54 -07:00
										 |  |  |              .then((desc) => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                expect(desc.metrics).toEqual({'m1': 'some metric'}); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-26 16:34:08 -07:00
										 |  |  |     it('should provide Options.EXECUTE', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |          const execute = () => {}; | 
					
						
							| 
									
										
										
										
											2016-08-03 15:00:07 -07:00
										 |  |  |          createRunner().sample({id: 'someId', execute: execute}).then((_) => { | 
					
						
							|  |  |  |            expect(injector.get(Options.EXECUTE)).toEqual(execute); | 
					
						
							|  |  |  |            async.done(); | 
					
						
							|  |  |  |          }); | 
					
						
							| 
									
										
										
										
											2015-05-27 14:57:54 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-26 16:34:08 -07:00
										 |  |  |     it('should provide Options.PREPARE', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |          const prepare = () => {}; | 
					
						
							| 
									
										
										
										
											2016-08-03 15:00:07 -07:00
										 |  |  |          createRunner().sample({id: 'someId', prepare: prepare}).then((_) => { | 
					
						
							|  |  |  |            expect(injector.get(Options.PREPARE)).toEqual(prepare); | 
					
						
							|  |  |  |            async.done(); | 
					
						
							|  |  |  |          }); | 
					
						
							| 
									
										
										
										
											2015-05-27 14:57:54 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-26 16:34:08 -07:00
										 |  |  |     it('should provide Options.MICRO_METRICS', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-08-03 15:00:07 -07:00
										 |  |  |          createRunner().sample({id: 'someId', microMetrics: {'a': 'b'}}).then((_) => { | 
					
						
							|  |  |  |            expect(injector.get(Options.MICRO_METRICS)).toEqual({'a': 'b'}); | 
					
						
							|  |  |  |            async.done(); | 
					
						
							|  |  |  |          }); | 
					
						
							| 
									
										
										
										
											2015-05-27 14:57:54 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-26 16:34:08 -07:00
										 |  |  |     it('should overwrite providers per sample call', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-06-02 17:30:40 -07:00
										 |  |  |          createRunner([{provide: Options.DEFAULT_DESCRIPTION, useValue: {'a': 1}}]) | 
					
						
							| 
									
										
										
										
											2015-05-27 14:57:54 -07:00
										 |  |  |              .sample({ | 
					
						
							|  |  |  |                id: 'someId', | 
					
						
							| 
									
										
										
										
											2016-06-02 17:30:40 -07:00
										 |  |  |                providers: [{provide: Options.DEFAULT_DESCRIPTION, useValue: {'a': 2}}] | 
					
						
							| 
									
										
										
										
											2015-05-27 14:57:54 -07:00
										 |  |  |              }) | 
					
						
							| 
									
										
										
										
											2015-06-26 15:59:18 -07:00
										 |  |  |              .then((_) => injector.get(SampleDescription)) | 
					
						
							| 
									
										
										
										
											2015-05-27 14:57:54 -07:00
										 |  |  |              .then((desc) => { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-26 15:59:18 -07:00
										 |  |  |                expect(desc.description['a']).toBe(2); | 
					
						
							| 
									
										
										
										
											2015-05-27 14:57:54 -07:00
										 |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MockWebDriverAdapter extends WebDriverAdapter { | 
					
						
							| 
									
										
										
										
											2016-08-26 16:34:08 -07:00
										 |  |  |   executeScript(script: string): Promise<string> { return Promise.resolve('someUserAgent'); } | 
					
						
							|  |  |  |   capabilities(): Promise<Map<string, any>> { return null; } | 
					
						
							| 
									
										
										
										
											2015-05-27 14:57:54 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MockValidator extends Validator { | 
					
						
							|  |  |  |   constructor() { super(); } | 
					
						
							|  |  |  |   describe() { return {'v': 11}; } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MockMetric extends Metric { | 
					
						
							|  |  |  |   constructor() { super(); } | 
					
						
							|  |  |  |   describe() { return {'m1': 'some metric'}; } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MockSampler extends Sampler { | 
					
						
							| 
									
										
										
										
											2016-08-26 16:34:08 -07:00
										 |  |  |   constructor() { super(null, null, null, null, null, null, null); } | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |   sample(): Promise<SampleState> { return Promise.resolve(new SampleState([], [])); } | 
					
						
							| 
									
										
										
										
											2015-05-27 14:57:54 -07:00
										 |  |  | } |