From c1a0af514f25f7b332677988603eb413b93033db Mon Sep 17 00:00:00 2001 From: Julie Ralph Date: Thu, 10 Dec 2015 12:00:48 -0800 Subject: [PATCH] feat(test): add withProviders for per test providers Closes #5128 --- modules/angular2/src/testing/test_injector.ts | 27 ++++++++++++++++++- .../test/testing/testing_public_spec.ts | 8 ++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/modules/angular2/src/testing/test_injector.ts b/modules/angular2/src/testing/test_injector.ts index c2d82651c1..0863a8b752 100644 --- a/modules/angular2/src/testing/test_injector.ts +++ b/modules/angular2/src/testing/test_injector.ts @@ -36,6 +36,10 @@ export class TestInjector { } execute(fn: FunctionWithParamTokens): any { + var additionalProviders = fn.additionalProviders(); + if (additionalProviders.length > 0) { + this.addProviders(additionalProviders); + } if (!this._instantiated) { this.createInjector(); } @@ -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. @@ -141,8 +161,13 @@ export function injectAsync(tokens: any[], fn: Function): FunctionWithParamToken return new FunctionWithParamTokens(tokens, fn, true); } +function emptyArray(): Array { + 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. diff --git a/modules/angular2/test/testing/testing_public_spec.ts b/modules/angular2/test/testing/testing_public_spec.ts index 83eca100ad..4f1f65cf19 100644 --- a/modules/angular2/test/testing/testing_public_spec.ts +++ b/modules/angular2/test/testing/testing_public_spec.ts @@ -10,6 +10,7 @@ import { beforeEach, inject, injectAsync, + withProviders, beforeEachProviders, TestComponentBuilder } from 'angular2/testing'; @@ -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', () => {