From e6e007e2f1dd1d002843b3712456c447cad09d09 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Tue, 11 Oct 2016 15:44:48 -0700 Subject: [PATCH] refactor(core): cleanup SpyObject (#12221) --- .../test/directive_normalizer_spec.ts | 2 +- .../compiler/test/runtime_compiler_spec.ts | 8 +-- .../core/test/application_ref_spec.ts | 2 +- .../differs/iterable_differs_spec.ts | 10 +-- .../core/test/testing_internal_spec.ts | 10 +-- .../@angular/core/testing/testing_internal.ts | 67 +++++-------------- .../shared/web_worker_test_util.ts | 2 +- .../worker/platform_location_spec.ts | 2 +- 8 files changed, 30 insertions(+), 73 deletions(-) diff --git a/modules/@angular/compiler/test/directive_normalizer_spec.ts b/modules/@angular/compiler/test/directive_normalizer_spec.ts index 6e5ca95474..74c58465d0 100644 --- a/modules/@angular/compiler/test/directive_normalizer_spec.ts +++ b/modules/@angular/compiler/test/directive_normalizer_spec.ts @@ -435,7 +435,7 @@ export function main() { } function programResourceLoaderSpy(spy: SpyResourceLoader, results: {[key: string]: string}) { - spy.spy('get').andCallFake((url: string): Promise => { + spy.spy('get').and.callFake((url: string): Promise => { var result = results[url]; if (result) { return Promise.resolve(result); diff --git a/modules/@angular/compiler/test/runtime_compiler_spec.ts b/modules/@angular/compiler/test/runtime_compiler_spec.ts index ca161ee928..4c8d4f623d 100644 --- a/modules/@angular/compiler/test/runtime_compiler_spec.ts +++ b/modules/@angular/compiler/test/runtime_compiler_spec.ts @@ -117,7 +117,7 @@ export function main() { class SomeModule { } - resourceLoader.spy('get').andCallFake(() => Promise.resolve('hello')); + resourceLoader.spy('get').and.callFake(() => Promise.resolve('hello')); let ngModuleFactory: NgModuleFactory; compiler.compileModuleAsync(SomeModule).then((f) => ngModuleFactory = f); tick(); @@ -132,7 +132,7 @@ export function main() { class SomeModule { } - resourceLoader.spy('get').andCallFake(() => Promise.resolve('')); + resourceLoader.spy('get').and.callFake(() => Promise.resolve('')); expect(() => compiler.compileModuleSync(SomeModule)) .toThrowError( `Can't compile synchronously as ${stringify(SomeCompWithUrlTemplate)} is still being loaded!`); @@ -144,7 +144,7 @@ export function main() { class SomeModule { } - resourceLoader.spy('get').andCallFake(() => Promise.resolve('')); + resourceLoader.spy('get').and.callFake(() => Promise.resolve('')); dirResolver.setView(SomeComp, new ViewMetadata({template: ''})); dirResolver.setView(ChildComp, new ViewMetadata({templateUrl: '/someTpl.html'})); expect(() => compiler.compileModuleSync(SomeModule)) @@ -161,7 +161,7 @@ export function main() { class SomeModule { } - resourceLoader.spy('get').andCallFake(() => Promise.resolve('hello')); + resourceLoader.spy('get').and.callFake(() => Promise.resolve('hello')); compiler.compileModuleAsync(SomeModule); tick(); diff --git a/modules/@angular/core/test/application_ref_spec.ts b/modules/@angular/core/test/application_ref_spec.ts index d81744e9b8..be4911ee16 100644 --- a/modules/@angular/core/test/application_ref_spec.ts +++ b/modules/@angular/core/test/application_ref_spec.ts @@ -74,7 +74,7 @@ export function main() { var cdRef = new SpyChangeDetectorRef(); try { ref.registerChangeDetector(cdRef); - cdRef.spy('detectChanges').andCallFake(() => ref.tick()); + cdRef.spy('detectChanges').and.callFake(() => ref.tick()); expect(() => ref.tick()).toThrowError('ApplicationRef.tick is called recursively'); } finally { ref.unregisterChangeDetector(cdRef); diff --git a/modules/@angular/core/test/change_detection/differs/iterable_differs_spec.ts b/modules/@angular/core/test/change_detection/differs/iterable_differs_spec.ts index 3730397781..9c2d79eec3 100644 --- a/modules/@angular/core/test/change_detection/differs/iterable_differs_spec.ts +++ b/modules/@angular/core/test/change_detection/differs/iterable_differs_spec.ts @@ -31,17 +31,17 @@ export function main() { }); it('should return the first suitable implementation', () => { - factory1.spy('supports').andReturn(false); - factory2.spy('supports').andReturn(true); - factory3.spy('supports').andReturn(true); + factory1.spy('supports').and.returnValue(false); + factory2.spy('supports').and.returnValue(true); + factory3.spy('supports').and.returnValue(true); var differs = IterableDiffers.create([factory1, factory2, factory3]); expect(differs.find('some object')).toBe(factory2); }); it('should copy over differs from the parent repo', () => { - factory1.spy('supports').andReturn(true); - factory2.spy('supports').andReturn(false); + factory1.spy('supports').and.returnValue(true); + factory2.spy('supports').and.returnValue(false); var parent = IterableDiffers.create([factory1]); var child = IterableDiffers.create([factory2], parent); diff --git a/modules/@angular/core/test/testing_internal_spec.ts b/modules/@angular/core/test/testing_internal_spec.ts index 70ccbe86ca..d4d7f8ea47 100644 --- a/modules/@angular/core/test/testing_internal_spec.ts +++ b/modules/@angular/core/test/testing_internal_spec.ts @@ -66,7 +66,7 @@ export function main() { }); describe('spy objects', () => { - var spyObj: any /** TODO #9100 */; + let spyObj: any; beforeEach(() => { spyObj = new SpyTestObj(); }); @@ -74,7 +74,7 @@ export function main() { () => { expect(spyObj.spy('someFunc')).not.toHaveBeenCalled(); }); it('should record function calls', () => { - spyObj.spy('someFunc').andCallFake((a: any, b: any) => a + b); + spyObj.spy('someFunc').and.callFake((a: any, b: any) => a + b); expect(spyObj.someFunc(1, 2)).toEqual(3); expect(spyObj.spy('someFunc')).toHaveBeenCalledWith(1, 2); @@ -106,12 +106,6 @@ export function main() { it('should create spys for all methods', () => { expect(() => spyObj.someFunc()).not.toThrow(); }); - - it('should create a default spy that does not fail for numbers', () => { - // Previously needed for rtts_assert. Revisit this behavior. - expect(spyObj.someFunc()).toBe(null); - }); }); - }); } diff --git a/modules/@angular/core/testing/testing_internal.ts b/modules/@angular/core/testing/testing_internal.ts index 49a6476f87..71b01e858e 100644 --- a/modules/@angular/core/testing/testing_internal.ts +++ b/modules/@angular/core/testing/testing_internal.ts @@ -19,7 +19,7 @@ export {inject} from './test_bed'; export * from './logger'; export * from './ng_zone_mock'; -export var proxy: ClassDecorator = (t: any /** TODO #9100 */) => t; +export var proxy: ClassDecorator = (t: any) => t; var _global = (typeof window === 'undefined' ? global : window); @@ -35,7 +35,6 @@ var jsmIIt = _global.fit; var jsmXIt = _global.xit; var runnerStack: BeforeEachRunner[] = []; -var inIt = false; jasmine.DEFAULT_TIMEOUT_INTERVAL = 3000; var globalTimeOut = jasmine.DEFAULT_TIMEOUT_INTERVAL; @@ -123,7 +122,7 @@ function _it(jsmFn: Function, name: string, testFn: Function, testTimeOut: numbe var runner = runnerStack[runnerStack.length - 1]; var timeOut = Math.max(globalTimeOut, testTimeOut); - jsmFn(name, (done: any /** TODO #9100 */) => { + jsmFn(name, (done: any) => { var completerProvider = { provide: AsyncTestCompleter, useFactory: () => { @@ -134,7 +133,6 @@ function _it(jsmFn: Function, name: string, testFn: Function, testTimeOut: numbe testBed.configureTestingModule({providers: [completerProvider]}); runner.run(); - inIt = true; if (testFn.length == 0) { let retVal = testFn(); if (isPromise(retVal)) { @@ -148,44 +146,26 @@ function _it(jsmFn: Function, name: string, testFn: Function, testTimeOut: numbe // Asynchronous test function that takes in 'done' parameter. testFn(done); } - inIt = false; }, timeOut); } -export function it( - name: any /** TODO #9100 */, fn: any /** TODO #9100 */, - timeOut: any /** TODO #9100 */ = null): void { +export function it(name: any, fn: any, timeOut: any = null): void { return _it(jsmIt, name, fn, timeOut); } -export function xit( - name: any /** TODO #9100 */, fn: any /** TODO #9100 */, - timeOut: any /** TODO #9100 */ = null): void { +export function xit(name: any, fn: any, timeOut: any = null): void { return _it(jsmXIt, name, fn, timeOut); } -export function iit( - name: any /** TODO #9100 */, fn: any /** TODO #9100 */, - timeOut: any /** TODO #9100 */ = null): void { +export function iit(name: any, fn: any, timeOut: any = null): void { return _it(jsmIIt, name, fn, timeOut); } -export interface GuinessCompatibleSpy extends jasmine.Spy { - /** By chaining the spy with and.returnValue, all calls to the function will return a specific - * value. */ - andReturn(val: any): void; - /** By chaining the spy with and.callFake, all calls to the spy will delegate to the supplied - * function. */ - andCallFake(fn: Function): GuinessCompatibleSpy; - /** removes all recorded calls */ - reset(): any /** TODO #9100 */; -} - export class SpyObject { - constructor(type: any /** TODO #9100 */ = null) { + constructor(type?: any) { if (type) { - for (var prop in type.prototype) { - var m: any /** TODO #9100 */ = null; + for (let prop in type.prototype) { + let m: any = null; try { m = type.prototype[prop]; } catch (e) { @@ -200,23 +180,17 @@ export class SpyObject { } } } - // Noop so that SpyObject has the same interface as in Dart - noSuchMethod(args: any /** TODO #9100 */) {} - spy(name: any /** TODO #9100 */) { - if (!(this as any /** TODO #9100 */)[name]) { - (this as any /** TODO #9100 */)[name] = this._createGuinnessCompatibleSpy(name); + spy(name: string) { + if (!(this as any)[name]) { + (this as any)[name] = jasmine.createSpy(name); } - return (this as any /** TODO #9100 */)[name]; + return (this as any)[name]; } - prop(name: any /** TODO #9100 */, value: any /** TODO #9100 */) { - (this as any /** TODO #9100 */)[name] = value; - } + prop(name: string, value: any) { (this as any)[name] = value; } - static stub( - object: any /** TODO #9100 */ = null, config: any /** TODO #9100 */ = null, - overrides: any /** TODO #9100 */ = null) { + static stub(object: any = null, config: any = null, overrides: any = null) { if (!(object instanceof SpyObject)) { overrides = config; config = object; @@ -224,18 +198,7 @@ export class SpyObject { } var m = StringMapWrapper.merge(config, overrides); - Object.keys(m).forEach(key => { object.spy(key).andReturn(m[key]); }); + Object.keys(m).forEach(key => { object.spy(key).and.returnValue(m[key]); }); return object; } - - /** @internal */ - _createGuinnessCompatibleSpy(name: any /** TODO #9100 */): GuinessCompatibleSpy { - var newSpy: GuinessCompatibleSpy = jasmine.createSpy(name); - newSpy.andCallFake = newSpy.and.callFake; - newSpy.andReturn = newSpy.and.returnValue; - newSpy.reset = newSpy.calls.reset; - // revisit return null here (previously needed for rtts_assert). - newSpy.and.returnValue(null); - return newSpy; - } } diff --git a/modules/@angular/platform-webworker/test/web_workers/shared/web_worker_test_util.ts b/modules/@angular/platform-webworker/test/web_workers/shared/web_worker_test_util.ts index 2379c42ee5..2a1b488033 100644 --- a/modules/@angular/platform-webworker/test/web_workers/shared/web_worker_test_util.ts +++ b/modules/@angular/platform-webworker/test/web_workers/shared/web_worker_test_util.ts @@ -42,7 +42,7 @@ export function createPairedMessageBuses(): PairedMessageBuses { export function expectBrokerCall( broker: SpyMessageBroker, methodName: string, vals?: Array, handler?: (..._: any[]) => Promise| void): void { - broker.spy('runOnService').andCallFake((args: UiArguments, returnType: Type) => { + broker.spy('runOnService').and.callFake((args: UiArguments, returnType: Type) => { expect(args.method).toEqual(methodName); if (isPresent(vals)) { expect(args.args.length).toEqual(vals.length); diff --git a/modules/@angular/platform-webworker/test/web_workers/worker/platform_location_spec.ts b/modules/@angular/platform-webworker/test/web_workers/worker/platform_location_spec.ts index 4b06564895..a657fbe41d 100644 --- a/modules/@angular/platform-webworker/test/web_workers/worker/platform_location_spec.ts +++ b/modules/@angular/platform-webworker/test/web_workers/worker/platform_location_spec.ts @@ -28,7 +28,7 @@ export function main() { function createWebWorkerPlatformLocation(loc: LocationType): WebWorkerPlatformLocation { - broker.spy('runOnService').andCallFake((args: UiArguments, returnType: Type) => { + broker.spy('runOnService').and.callFake((args: UiArguments, returnType: Type) => { if (args.method === 'getLocation') { return Promise.resolve(loc); }