2016-08-03 18:00:07 -04: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 15:12:46 -05:00
|
|
|
import {AsyncTestCompleter, describe, expect, inject, it} from '@angular/core/testing/src/testing_internal';
|
2016-08-26 19:34:08 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
import {Injector, Metric, Options, Runner, SampleDescription, Sampler, SampleState, Validator, WebDriverAdapter} from '../index';
|
2015-05-27 17:57:54 -04:00
|
|
|
|
2017-12-16 17:42:55 -05:00
|
|
|
{
|
2015-05-27 17:57:54 -04:00
|
|
|
describe('runner', () => {
|
perf: switch angular to use StaticInjector instead of ReflectiveInjector
This change allows ReflectiveInjector to be tree shaken resulting
in not needed Reflect polyfil and smaller bundles.
Code savings for HelloWorld using Closure:
Reflective: bundle.js: 105,864(34,190 gzip)
Static: bundle.js: 154,889(33,555 gzip)
645( 2%)
BREAKING CHANGE:
`platformXXXX()` no longer accepts providers which depend on reflection.
Specifically the method signature when from `Provider[]` to
`StaticProvider[]`.
Example:
Before:
```
[
MyClass,
{provide: ClassA, useClass: SubClassA}
]
```
After:
```
[
{provide: MyClass, deps: [Dep1,...]},
{provide: ClassA, useClass: SubClassA, deps: [Dep1,...]}
]
```
NOTE: This only applies to platform creation and providers for the JIT
compiler. It does not apply to `@Compotent` or `@NgModule` provides
declarations.
Benchpress note: Previously Benchpress also supported reflective
provides, which now require static providers.
DEPRECATION:
- `ReflectiveInjector` is now deprecated as it will be remove. Use
`Injector.create` as a replacement.
closes #18496
2017-08-03 15:33:29 -04:00
|
|
|
let injector: Injector;
|
2016-11-12 08:08:58 -05:00
|
|
|
let runner: Runner;
|
2015-05-27 17:57:54 -04:00
|
|
|
|
2017-03-24 12:56:50 -04:00
|
|
|
function createRunner(defaultProviders?: any[]): Runner {
|
2016-09-30 12:26:53 -04:00
|
|
|
if (!defaultProviders) {
|
2016-08-26 19:34:08 -04:00
|
|
|
defaultProviders = [];
|
2015-05-27 17:57:54 -04:00
|
|
|
}
|
|
|
|
runner = new Runner([
|
2016-08-26 19:34:08 -04:00
|
|
|
defaultProviders, {
|
2016-06-02 20:30:40 -04:00
|
|
|
provide: Sampler,
|
perf: switch angular to use StaticInjector instead of ReflectiveInjector
This change allows ReflectiveInjector to be tree shaken resulting
in not needed Reflect polyfil and smaller bundles.
Code savings for HelloWorld using Closure:
Reflective: bundle.js: 105,864(34,190 gzip)
Static: bundle.js: 154,889(33,555 gzip)
645( 2%)
BREAKING CHANGE:
`platformXXXX()` no longer accepts providers which depend on reflection.
Specifically the method signature when from `Provider[]` to
`StaticProvider[]`.
Example:
Before:
```
[
MyClass,
{provide: ClassA, useClass: SubClassA}
]
```
After:
```
[
{provide: MyClass, deps: [Dep1,...]},
{provide: ClassA, useClass: SubClassA, deps: [Dep1,...]}
]
```
NOTE: This only applies to platform creation and providers for the JIT
compiler. It does not apply to `@Compotent` or `@NgModule` provides
declarations.
Benchpress note: Previously Benchpress also supported reflective
provides, which now require static providers.
DEPRECATION:
- `ReflectiveInjector` is now deprecated as it will be remove. Use
`Injector.create` as a replacement.
closes #18496
2017-08-03 15:33:29 -04:00
|
|
|
useFactory: (_injector: Injector) => {
|
2016-06-02 20:30:40 -04:00
|
|
|
injector = _injector;
|
|
|
|
return new MockSampler();
|
|
|
|
},
|
|
|
|
deps: [Injector]
|
|
|
|
},
|
2016-08-03 18:00:07 -04:00
|
|
|
{provide: Metric, useFactory: () => new MockMetric(), deps: []},
|
|
|
|
{provide: Validator, useFactory: () => new MockValidator(), deps: []},
|
|
|
|
{provide: WebDriverAdapter, useFactory: () => new MockWebDriverAdapter(), deps: []}
|
2015-05-27 17:57:54 -04:00
|
|
|
]);
|
|
|
|
return runner;
|
|
|
|
}
|
|
|
|
|
2016-08-26 19:34:08 -04:00
|
|
|
it('should set SampleDescription.id',
|
|
|
|
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
2015-05-27 17:57:54 -04:00
|
|
|
createRunner()
|
|
|
|
.sample({id: 'someId'})
|
2015-06-26 18:59:18 -04:00
|
|
|
.then((_) => injector.get(SampleDescription))
|
2015-05-27 17:57:54 -04:00
|
|
|
.then((desc) => {
|
|
|
|
expect(desc.id).toBe('someId');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2016-08-26 19:34:08 -04:00
|
|
|
it('should merge SampleDescription.description',
|
|
|
|
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
2016-06-02 20:30:40 -04:00
|
|
|
createRunner([{provide: Options.DEFAULT_DESCRIPTION, useValue: {'a': 1}}])
|
2016-08-03 18:00:07 -04:00
|
|
|
.sample({
|
|
|
|
id: 'someId',
|
|
|
|
providers: [{provide: Options.SAMPLE_DESCRIPTION, useValue: {'b': 2}}]
|
|
|
|
})
|
2015-06-26 18:59:18 -04:00
|
|
|
.then((_) => injector.get(SampleDescription))
|
2015-05-27 17:57:54 -04: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 19:34:08 -04:00
|
|
|
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
2015-05-27 17:57:54 -04:00
|
|
|
createRunner()
|
|
|
|
.sample({id: 'someId'})
|
2015-06-26 18:59:18 -04:00
|
|
|
.then((_) => injector.get(SampleDescription))
|
2015-05-27 17:57:54 -04:00
|
|
|
.then((desc) => {
|
|
|
|
expect(desc.metrics).toEqual({'m1': 'some metric'});
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2016-08-26 19:34:08 -04:00
|
|
|
it('should provide Options.EXECUTE',
|
|
|
|
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const execute = () => {};
|
2016-08-03 18:00:07 -04:00
|
|
|
createRunner().sample({id: 'someId', execute: execute}).then((_) => {
|
|
|
|
expect(injector.get(Options.EXECUTE)).toEqual(execute);
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-27 17:57:54 -04:00
|
|
|
}));
|
|
|
|
|
2016-08-26 19:34:08 -04:00
|
|
|
it('should provide Options.PREPARE',
|
|
|
|
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const prepare = () => {};
|
2016-08-03 18:00:07 -04:00
|
|
|
createRunner().sample({id: 'someId', prepare: prepare}).then((_) => {
|
|
|
|
expect(injector.get(Options.PREPARE)).toEqual(prepare);
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-27 17:57:54 -04:00
|
|
|
}));
|
|
|
|
|
2016-08-26 19:34:08 -04:00
|
|
|
it('should provide Options.MICRO_METRICS',
|
|
|
|
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
2016-08-03 18:00:07 -04:00
|
|
|
createRunner().sample({id: 'someId', microMetrics: {'a': 'b'}}).then((_) => {
|
|
|
|
expect(injector.get(Options.MICRO_METRICS)).toEqual({'a': 'b'});
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-27 17:57:54 -04:00
|
|
|
}));
|
|
|
|
|
2016-08-26 19:34:08 -04:00
|
|
|
it('should overwrite providers per sample call',
|
|
|
|
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
2016-06-02 20:30:40 -04:00
|
|
|
createRunner([{provide: Options.DEFAULT_DESCRIPTION, useValue: {'a': 1}}])
|
2015-05-27 17:57:54 -04:00
|
|
|
.sample({
|
|
|
|
id: 'someId',
|
2016-06-02 20:30:40 -04:00
|
|
|
providers: [{provide: Options.DEFAULT_DESCRIPTION, useValue: {'a': 2}}]
|
2015-05-27 17:57:54 -04:00
|
|
|
})
|
2015-06-26 18:59:18 -04:00
|
|
|
.then((_) => injector.get(SampleDescription))
|
2015-05-27 17:57:54 -04:00
|
|
|
.then((desc) => {
|
2015-06-26 18:59:18 -04:00
|
|
|
expect(desc.description['a']).toBe(2);
|
2015-05-27 17:57:54 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class MockWebDriverAdapter extends WebDriverAdapter {
|
2020-04-13 19:40:21 -04:00
|
|
|
executeScript(script: string): Promise<string> {
|
|
|
|
return Promise.resolve('someUserAgent');
|
|
|
|
}
|
|
|
|
capabilities(): Promise<Map<string, any>> {
|
|
|
|
return null!;
|
|
|
|
}
|
2015-05-27 17:57:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class MockValidator extends Validator {
|
2020-04-13 19:40:21 -04:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
describe() {
|
|
|
|
return {'v': 11};
|
|
|
|
}
|
2015-05-27 17:57:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class MockMetric extends Metric {
|
2020-04-13 19:40:21 -04:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
describe() {
|
|
|
|
return {'m1': 'some metric'};
|
|
|
|
}
|
2015-05-27 17:57:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class MockSampler extends Sampler {
|
2020-04-13 19:40:21 -04:00
|
|
|
constructor() {
|
|
|
|
super(null!, null!, null!, null!, null!, null!, null!);
|
|
|
|
}
|
|
|
|
sample(): Promise<SampleState> {
|
|
|
|
return Promise.resolve(new SampleState([], []));
|
|
|
|
}
|
2015-05-27 17:57:54 -04:00
|
|
|
}
|