style(tests): clean up testing_public_spec (#11452)

This commit is contained in:
Joao Dias 2016-10-30 20:39:53 +01:00 committed by vsavkin
parent 3045d02b9a
commit e0ad413a8e
1 changed files with 78 additions and 91 deletions

View File

@ -60,8 +60,7 @@ class FancyService {
value: string = 'real value'; value: string = 'real value';
getAsyncValue() { return Promise.resolve('async value'); } getAsyncValue() { return Promise.resolve('async value'); }
getTimeoutValue() { getTimeoutValue() {
return new Promise( return new Promise<string>((resolve, reject) => setTimeout(() => resolve('timeout value'), 10));
(resolve, reject) => { setTimeout(() => { resolve('timeout value'); }, 10); });
} }
} }
@ -95,7 +94,7 @@ class SomeDirective {
@Pipe({name: 'somePipe'}) @Pipe({name: 'somePipe'})
class SomePipe { class SomePipe {
transform(value: string): any { return `transformed ${value}`; } transform(value: string) { return `transformed ${value}`; }
} }
@Component({selector: 'comp', template: `<div [someDir]="'someValue' | somePipe"></div>`}) @Component({selector: 'comp', template: `<div [someDir]="'someValue' | somePipe"></div>`})
@ -116,15 +115,15 @@ class CompWithUrlTemplate {
export function main() { export function main() {
describe('public testing API', () => { describe('public testing API', () => {
describe('using the async helper', () => { describe('using the async helper', () => {
var actuallyDone: boolean; let actuallyDone: boolean;
beforeEach(() => { actuallyDone = false; }); beforeEach(() => actuallyDone = false);
afterEach(() => { expect(actuallyDone).toEqual(true); }); afterEach(() => expect(actuallyDone).toEqual(true));
it('should run normal tests', () => { actuallyDone = true; }); it('should run normal tests', () => actuallyDone = true);
it('should run normal async tests', (done: any /** TODO #9100 */) => { it('should run normal async tests', (done) => {
setTimeout(() => { setTimeout(() => {
actuallyDone = true; actuallyDone = true;
done(); done();
@ -132,11 +131,11 @@ export function main() {
}); });
it('should run async tests with tasks', it('should run async tests with tasks',
async(() => { setTimeout(() => { actuallyDone = true; }, 0); })); async(() => setTimeout(() => actuallyDone = true, 0)));
it('should run async tests with promises', async(() => { it('should run async tests with promises', async(() => {
var p = new Promise((resolve, reject) => { setTimeout(resolve, 10); }); const p = new Promise((resolve, reject) => setTimeout(resolve, 10));
p.then(() => { actuallyDone = true; }); p.then(() => actuallyDone = true);
})); }));
}); });
@ -146,29 +145,26 @@ export function main() {
TestBed.configureTestingModule( TestBed.configureTestingModule(
{providers: [{provide: FancyService, useValue: new FancyService()}]}); {providers: [{provide: FancyService, useValue: new FancyService()}]});
it('should use set up providers', it('should use set up providers', inject([FancyService], (service: FancyService) => {
inject([FancyService], (service: any /** TODO #9100 */) => {
expect(service.value).toEqual('real value'); expect(service.value).toEqual('real value');
})); }));
it('should wait until returned promises', it('should wait until returned promises',
async(inject([FancyService], (service: any /** TODO #9100 */) => { async(inject([FancyService], (service: FancyService) => {
service.getAsyncValue().then( service.getAsyncValue().then((value) => expect(value).toEqual('async value'));
(value: any /** TODO #9100 */) => { expect(value).toEqual('async value'); }); service.getTimeoutValue().then((value) => expect(value).toEqual('timeout value'));
service.getTimeoutValue().then(
(value: any /** TODO #9100 */) => { expect(value).toEqual('timeout value'); });
}))); })));
it('should allow the use of fakeAsync', it('should allow the use of fakeAsync',
fakeAsync(inject([FancyService], (service: any /** TODO #9100 */) => { fakeAsync(inject([FancyService], (service: FancyService) => {
var value: any /** TODO #9100 */; let value: string;
service.getAsyncValue().then(function(val: any /** TODO #9100 */) { value = val; }); service.getAsyncValue().then((val) => value = val);
tick(); tick();
expect(value).toEqual('async value'); expect(value).toEqual('async value');
}))); })));
it('should allow use of "done"', (done: any /** TODO #9100 */) => { it('should allow use of "done"', (done) => {
inject([FancyService], (service: any /** TODO #9100 */) => { inject([FancyService], (service: FancyService) => {
let count = 0; let count = 0;
let id = setInterval(() => { let id = setInterval(() => {
count++; count++;
@ -181,24 +177,22 @@ export function main() {
}); });
describe('using beforeEach', () => { describe('using beforeEach', () => {
beforeEach(inject([FancyService], (service: any /** TODO #9100 */) => { beforeEach(inject([FancyService], (service: FancyService) => {
service.value = 'value modified in beforeEach'; service.value = 'value modified in beforeEach';
})); }));
it('should use modified providers', it('should use modified providers', inject([FancyService], (service: FancyService) => {
inject([FancyService], (service: any /** TODO #9100 */) => {
expect(service.value).toEqual('value modified in beforeEach'); expect(service.value).toEqual('value modified in beforeEach');
})); }));
}); });
describe('using async beforeEach', () => { describe('using async beforeEach', () => {
beforeEach(async(inject([FancyService], (service: any /** TODO #9100 */) => { beforeEach(async(inject([FancyService], (service: FancyService) => {
service.getAsyncValue().then( service.getAsyncValue().then((value) => service.value = value);
(value: any /** TODO #9100 */) => { service.value = value; });
}))); })));
it('should use asynchronously modified value', it('should use asynchronously modified value',
inject([FancyService], (service: any /** TODO #9100 */) => { inject([FancyService], (service: FancyService) => {
expect(service.value).toEqual('async value'); expect(service.value).toEqual('async value');
})); }));
}); });
@ -312,7 +306,7 @@ export function main() {
class SomeModule { class SomeModule {
} }
beforeEach(() => { TestBed.configureTestingModule({imports: [SomeModule]}); }); beforeEach(() => TestBed.configureTestingModule({imports: [SomeModule]}));
describe('module', () => { describe('module', () => {
beforeEach(() => { beforeEach(() => {
@ -384,14 +378,14 @@ export function main() {
}); });
describe('useJit true', () => { describe('useJit true', () => {
beforeEach(() => { TestBed.configureCompiler({useJit: true}); }); beforeEach(() => TestBed.configureCompiler({useJit: true}));
it('should set the value into CompilerConfig', it('should set the value into CompilerConfig',
inject([CompilerConfig], (config: CompilerConfig) => { inject([CompilerConfig], (config: CompilerConfig) => {
expect(config.useJit).toBe(true); expect(config.useJit).toBe(true);
})); }));
}); });
describe('useJit false', () => { describe('useJit false', () => {
beforeEach(() => { TestBed.configureCompiler({useJit: false}); }); beforeEach(() => TestBed.configureCompiler({useJit: false}));
it('should set the value into CompilerConfig', it('should set the value into CompilerConfig',
inject([CompilerConfig], (config: CompilerConfig) => { inject([CompilerConfig], (config: CompilerConfig) => {
expect(config.useJit).toBe(false); expect(config.useJit).toBe(false);
@ -401,82 +395,77 @@ export function main() {
}); });
describe('errors', () => { describe('errors', () => {
var originalJasmineIt: any; let originalJasmineIt: (description: string, func: () => void) => jasmine.Spec;
var originalJasmineBeforeEach: any; let originalJasmineBeforeEach: (beforeEachFunction: () => void) => void;
var patchJasmineIt = () => { const patchJasmineIt = () => {
var resolve: (result: any) => void; let resolve: (result: any) => void;
var reject: (error: any) => void; let reject: (error: any) => void;
var promise = new Promise((res, rej) => { const promise = new Promise((res, rej) => {
resolve = res; resolve = res;
reject = rej; reject = rej;
}); });
originalJasmineIt = jasmine.getEnv().it; originalJasmineIt = jasmine.getEnv().it;
jasmine.getEnv().it = (description: string, fn: any /** TODO #9100 */) => { jasmine.getEnv().it = (description: string, fn: (done: DoneFn) => void) => {
var done = () => { resolve(null); }; const done = <DoneFn>(() => resolve(null));
(<any>done).fail = (err: any /** TODO #9100 */) => { reject(err); }; done.fail = (err) => reject(err);
fn(done); fn(done);
return null; return null;
}; };
return promise; return promise;
}; };
var restoreJasmineIt = () => { jasmine.getEnv().it = originalJasmineIt; }; const restoreJasmineIt = () => jasmine.getEnv().it = originalJasmineIt;
var patchJasmineBeforeEach = () => { const patchJasmineBeforeEach = () => {
var resolve: (result: any) => void; let resolve: (result: any) => void;
var reject: (error: any) => void; let reject: (error: any) => void;
var promise = new Promise((res, rej) => { const promise = new Promise((res, rej) => {
resolve = res; resolve = res;
reject = rej; reject = rej;
}); });
originalJasmineBeforeEach = jasmine.getEnv().beforeEach; originalJasmineBeforeEach = jasmine.getEnv().beforeEach;
jasmine.getEnv().beforeEach = (fn: any) => { jasmine.getEnv().beforeEach = (fn: (done: DoneFn) => void) => {
var done = () => { resolve(null); }; const done = <DoneFn>(() => resolve(null));
(<any>done).fail = (err: any /** TODO #9100 */) => { reject(err); }; done.fail = (err) => reject(err);
fn(done); fn(done);
return null;
}; };
return promise; return promise;
}; };
var restoreJasmineBeforeEach = const restoreJasmineBeforeEach = () => jasmine.getEnv().beforeEach =
() => { jasmine.getEnv().beforeEach = originalJasmineBeforeEach; }; originalJasmineBeforeEach;
it('should fail when an asynchronous error is thrown', (done: any /** TODO #9100 */) => { it('should fail when an asynchronous error is thrown', (done) => {
var itPromise = patchJasmineIt(); const itPromise = patchJasmineIt();
var barError = new Error('bar'); const barError = new Error('bar');
it('throws an async error', it('throws an async error',
async(inject([], () => { setTimeout(() => { throw barError; }, 0); }))); async(inject([], () => setTimeout(() => { throw barError; }, 0))));
itPromise.then( itPromise.then(() => done.fail('Expected test to fail, but it did not'), (err) => {
() => { done.fail('Expected test to fail, but it did not'); }, expect(err).toEqual(barError);
(err) => { done();
expect(err).toEqual(barError); });
done();
});
restoreJasmineIt(); restoreJasmineIt();
}); });
it('should fail when a returned promise is rejected', (done: any /** TODO #9100 */) => { it('should fail when a returned promise is rejected', (done) => {
var itPromise = patchJasmineIt(); const itPromise = patchJasmineIt();
it('should fail with an error from a promise', async(inject([], () => { it('should fail with an error from a promise', async(inject([], () => {
var reject: (error: any) => void; let reject: (error: any) => void;
var promise = new Promise((_, rej) => { reject = rej; }); const promise = new Promise((_, rej) => reject = rej);
var p = promise.then(() => { expect(1).toEqual(2); }); const p = promise.then(() => expect(1).toEqual(2));
reject('baz'); reject('baz');
return p; return p;
}))); })));
itPromise.then( itPromise.then(() => done.fail('Expected test to fail, but it did not'), (err) => {
() => { done.fail('Expected test to fail, but it did not'); }, expect(err.message).toEqual('Uncaught (in promise): baz');
(err) => { done();
expect(err.message).toEqual('Uncaught (in promise): baz'); });
done();
});
restoreJasmineIt(); restoreJasmineIt();
}); });
@ -491,13 +480,13 @@ export function main() {
it('should report an error for declared components with templateUrl which never call TestBed.compileComponents', it('should report an error for declared components with templateUrl which never call TestBed.compileComponents',
() => { () => {
var itPromise = patchJasmineIt(); const itPromise = patchJasmineIt();
expect( expect(
() => it( () =>
'should fail', withModule( it('should fail', withModule(
{declarations: [CompWithUrlTemplate]}, {declarations: [CompWithUrlTemplate]},
() => { TestBed.createComponent(CompWithUrlTemplate); }))) () => TestBed.createComponent(CompWithUrlTemplate))))
.toThrowError( .toThrowError(
`This test module uses the component ${stringify(CompWithUrlTemplate)} which is using a "templateUrl", but they were never compiled. ` + `This test module uses the component ${stringify(CompWithUrlTemplate)} which is using a "templateUrl", but they were never compiled. ` +
`Please call "TestBed.compileComponents" before your test.`); `Please call "TestBed.compileComponents" before your test.`);
@ -512,21 +501,20 @@ export function main() {
class ComponentUsingInvalidProperty { class ComponentUsingInvalidProperty {
} }
var itPromise = patchJasmineIt(); const itPromise = patchJasmineIt();
expect( expect(
() => () => it(
it('should fail', 'should fail', withModule(
withModule( {declarations: [ComponentUsingInvalidProperty]},
{declarations: [ComponentUsingInvalidProperty]}, () => TestBed.createComponent(ComponentUsingInvalidProperty))))
() => { TestBed.createComponent(ComponentUsingInvalidProperty); })))
.toThrowError(/Can't bind to 'someUnknownProp'/); .toThrowError(/Can't bind to 'someUnknownProp'/);
restoreJasmineIt(); restoreJasmineIt();
}); });
}); });
describe('creating components', function() { describe('creating components', () => {
beforeEach(() => { beforeEach(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
@ -542,15 +530,14 @@ export function main() {
}); });
it('should instantiate a component with valid DOM', async(() => { it('should instantiate a component with valid DOM', async(() => {
var fixture = TestBed.createComponent(ChildComp); const fixture = TestBed.createComponent(ChildComp);
fixture.detectChanges(); fixture.detectChanges();
expect(fixture.nativeElement).toHaveText('Original Child'); expect(fixture.nativeElement).toHaveText('Original Child');
})); }));
it('should allow changing members of the component', async(() => { it('should allow changing members of the component', async(() => {
const componentFixture = TestBed.createComponent(MyIfComp);
var componentFixture = TestBed.createComponent(MyIfComp);
componentFixture.detectChanges(); componentFixture.detectChanges();
expect(componentFixture.nativeElement).toHaveText('MyIf()'); expect(componentFixture.nativeElement).toHaveText('MyIf()');
@ -571,7 +558,7 @@ export function main() {
TestBed.overrideComponent( TestBed.overrideComponent(
TestProvidersComp, TestProvidersComp,
{set: {providers: [{provide: FancyService, useClass: MockFancyService}]}}); {set: {providers: [{provide: FancyService, useClass: MockFancyService}]}});
var componentFixture = TestBed.createComponent(TestProvidersComp); const componentFixture = TestBed.createComponent(TestProvidersComp);
componentFixture.detectChanges(); componentFixture.detectChanges();
expect(componentFixture.nativeElement).toHaveText('injected value: mocked out value'); expect(componentFixture.nativeElement).toHaveText('injected value: mocked out value');
})); }));
@ -582,7 +569,7 @@ export function main() {
TestViewProvidersComp, TestViewProvidersComp,
{set: {viewProviders: [{provide: FancyService, useClass: MockFancyService}]}}); {set: {viewProviders: [{provide: FancyService, useClass: MockFancyService}]}});
var componentFixture = TestBed.createComponent(TestViewProvidersComp); const componentFixture = TestBed.createComponent(TestViewProvidersComp);
componentFixture.detectChanges(); componentFixture.detectChanges();
expect(componentFixture.nativeElement).toHaveText('injected value: mocked out value'); expect(componentFixture.nativeElement).toHaveText('injected value: mocked out value');
})); }));