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