angular-docs-cn/modules/benchpress/test/web_driver_extension_spec.js
Victor Berchet 33b5ba863e feat(tests): add a test injector
fixes #614

Asynchronous test should inject an AsyncTestCompleter:

Before:

  it("async test", (done) => {
    // ...
    done();
  });

After:

  it("async test", inject([AsyncTestCompleter], (async) => {
    // ...
    async.done();
  }));

Note: inject() is currently a function and the first parameter is the
array of DI tokens to inject as the test function parameters. This
construct is linked to Traceur limitations. The planned syntax is:

  it("async test", @Inject (async: AsyncTestCompleter) => {
    // ...
    async.done();
  });
2015-03-13 18:20:02 +01:00

63 lines
1.5 KiB
JavaScript

import {
afterEach,
AsyncTestCompleter,
beforeEach,
ddescribe,
describe,
expect,
iit,
inject,
it,
xit,
} from 'angular2/test_lib';
import { StringMap, ListWrapper } from 'angular2/src/facade/collection';
import { isPresent, StringWrapper } from 'angular2/src/facade/lang';
import { PromiseWrapper } from 'angular2/src/facade/async';
import { WebDriverExtension, bind, Injector, Options } from 'benchpress/common';
export function main() {
function createExtension(ids, caps) {
return new Injector([
ListWrapper.map(ids, (id) => bind(id).toValue(new MockExtension(id)) ),
bind(Options.CAPABILITIES).toValue(caps),
WebDriverExtension.bindTo(ids)
]).asyncGet(WebDriverExtension);
}
describe('WebDriverExtension.bindTo', () => {
it('should bind the extension that matches the capabilities', inject([AsyncTestCompleter], (async) => {
createExtension(['m1', 'm2', 'm3'], {'browser': 'm2'}).then( (m) => {
expect(m.id).toEqual('m2');
async.done();
});
}));
it('should throw if there is no match', inject([AsyncTestCompleter], (async) => {
PromiseWrapper.catchError(
createExtension(['m1'], {'browser': 'm2'}),
(err) => {
expect(isPresent(err)).toBe(true);
async.done();
}
);
}));
});
}
class MockExtension extends WebDriverExtension {
id:string;
constructor(id) {
super();
this.id = id;
}
supports(capabilities:StringMap):boolean {
return StringWrapper.equals(capabilities['browser'], this.id);
}
}