Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion modules/angular2/src/testing/test_injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export class TestInjector {
}

execute(fn: FunctionWithParamTokens): any {
var additionalProviders = fn.additionalProviders();
if (additionalProviders.length > 0) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opinion nit: If we're worried about perf here, I think this check fits better in addProviders, but up to you whether to update.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think perf should be an issue here, it's certainly not a hot code path.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, I'd vote removing the check altogether and collapsing these 4 lines into 1.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we do need the check here - we don't want to call addProviders at all if there's no new ones, to avoid throwing an error if we've already instantiated the injector.

this.addProviders(additionalProviders);
}
if (!this._instantiated) {
this.createInjector();
}
Expand Down Expand Up @@ -119,6 +123,22 @@ export function inject(tokens: any[], fn: Function): FunctionWithParamTokens {
return new FunctionWithParamTokens(tokens, fn, false);
}

export class InjectSetupWrapper {
constructor(private _providers: () => any) {}

inject(tokens: any[], fn: Function): FunctionWithParamTokens {
return new FunctionWithParamTokens(tokens, fn, false, this._providers);
}

injectAsync(tokens: any[], fn: Function): FunctionWithParamTokens {
return new FunctionWithParamTokens(tokens, fn, true, this._providers);
}
}

export function withProviders(providers: () => any) {
return new InjectSetupWrapper(providers);
}

/**
* Allows injecting dependencies in `beforeEach()` and `it()`. The test must return
* a promise which will resolve when all asynchronous activity is complete.
Expand All @@ -141,8 +161,13 @@ export function injectAsync(tokens: any[], fn: Function): FunctionWithParamToken
return new FunctionWithParamTokens(tokens, fn, true);
}

function emptyArray(): Array<any> {
return [];
}

export class FunctionWithParamTokens {
constructor(private _tokens: any[], private _fn: Function, public isAsync: boolean) {}
constructor(private _tokens: any[], private _fn: Function, public isAsync: boolean,
public additionalProviders: () => any = emptyArray) {}

/**
* Returns the value of the executed function.
Expand Down
8 changes: 8 additions & 0 deletions modules/angular2/test/testing/testing_public_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
beforeEach,
inject,
injectAsync,
withProviders,
beforeEachProviders,
TestComponentBuilder
} from 'angular2/testing';
Expand Down Expand Up @@ -186,6 +187,13 @@ export function main() {
inject([FancyService], (service) => { expect(service.value).toEqual('async value'); }));
});
});

describe('per test providers', () => {
it('should allow per test providers',
withProviders(() => [bind(FancyService).toValue(new FancyService())])
.inject([FancyService],
(service) => { expect(service.value).toEqual('real value'); }));
});
});

describe('errors', () => {
Expand Down