refactor(core): cleanup SpyObject (#12221)
This commit is contained in:
parent
91dd138fa5
commit
e6e007e2f1
|
@ -435,7 +435,7 @@ export function main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function programResourceLoaderSpy(spy: SpyResourceLoader, results: {[key: string]: string}) {
|
function programResourceLoaderSpy(spy: SpyResourceLoader, results: {[key: string]: string}) {
|
||||||
spy.spy('get').andCallFake((url: string): Promise<any> => {
|
spy.spy('get').and.callFake((url: string): Promise<any> => {
|
||||||
var result = results[url];
|
var result = results[url];
|
||||||
if (result) {
|
if (result) {
|
||||||
return Promise.resolve(result);
|
return Promise.resolve(result);
|
||||||
|
|
|
@ -117,7 +117,7 @@ export function main() {
|
||||||
class SomeModule {
|
class SomeModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
resourceLoader.spy('get').andCallFake(() => Promise.resolve('hello'));
|
resourceLoader.spy('get').and.callFake(() => Promise.resolve('hello'));
|
||||||
let ngModuleFactory: NgModuleFactory<any>;
|
let ngModuleFactory: NgModuleFactory<any>;
|
||||||
compiler.compileModuleAsync(SomeModule).then((f) => ngModuleFactory = f);
|
compiler.compileModuleAsync(SomeModule).then((f) => ngModuleFactory = f);
|
||||||
tick();
|
tick();
|
||||||
|
@ -132,7 +132,7 @@ export function main() {
|
||||||
class SomeModule {
|
class SomeModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
resourceLoader.spy('get').andCallFake(() => Promise.resolve(''));
|
resourceLoader.spy('get').and.callFake(() => Promise.resolve(''));
|
||||||
expect(() => compiler.compileModuleSync(SomeModule))
|
expect(() => compiler.compileModuleSync(SomeModule))
|
||||||
.toThrowError(
|
.toThrowError(
|
||||||
`Can't compile synchronously as ${stringify(SomeCompWithUrlTemplate)} is still being loaded!`);
|
`Can't compile synchronously as ${stringify(SomeCompWithUrlTemplate)} is still being loaded!`);
|
||||||
|
@ -144,7 +144,7 @@ export function main() {
|
||||||
class SomeModule {
|
class SomeModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
resourceLoader.spy('get').andCallFake(() => Promise.resolve(''));
|
resourceLoader.spy('get').and.callFake(() => Promise.resolve(''));
|
||||||
dirResolver.setView(SomeComp, new ViewMetadata({template: ''}));
|
dirResolver.setView(SomeComp, new ViewMetadata({template: ''}));
|
||||||
dirResolver.setView(ChildComp, new ViewMetadata({templateUrl: '/someTpl.html'}));
|
dirResolver.setView(ChildComp, new ViewMetadata({templateUrl: '/someTpl.html'}));
|
||||||
expect(() => compiler.compileModuleSync(SomeModule))
|
expect(() => compiler.compileModuleSync(SomeModule))
|
||||||
|
@ -161,7 +161,7 @@ export function main() {
|
||||||
class SomeModule {
|
class SomeModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
resourceLoader.spy('get').andCallFake(() => Promise.resolve('hello'));
|
resourceLoader.spy('get').and.callFake(() => Promise.resolve('hello'));
|
||||||
compiler.compileModuleAsync(SomeModule);
|
compiler.compileModuleAsync(SomeModule);
|
||||||
tick();
|
tick();
|
||||||
|
|
||||||
|
|
|
@ -74,7 +74,7 @@ export function main() {
|
||||||
var cdRef = <any>new SpyChangeDetectorRef();
|
var cdRef = <any>new SpyChangeDetectorRef();
|
||||||
try {
|
try {
|
||||||
ref.registerChangeDetector(cdRef);
|
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');
|
expect(() => ref.tick()).toThrowError('ApplicationRef.tick is called recursively');
|
||||||
} finally {
|
} finally {
|
||||||
ref.unregisterChangeDetector(cdRef);
|
ref.unregisterChangeDetector(cdRef);
|
||||||
|
|
|
@ -31,17 +31,17 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return the first suitable implementation', () => {
|
it('should return the first suitable implementation', () => {
|
||||||
factory1.spy('supports').andReturn(false);
|
factory1.spy('supports').and.returnValue(false);
|
||||||
factory2.spy('supports').andReturn(true);
|
factory2.spy('supports').and.returnValue(true);
|
||||||
factory3.spy('supports').andReturn(true);
|
factory3.spy('supports').and.returnValue(true);
|
||||||
|
|
||||||
var differs = IterableDiffers.create(<any>[factory1, factory2, factory3]);
|
var differs = IterableDiffers.create(<any>[factory1, factory2, factory3]);
|
||||||
expect(differs.find('some object')).toBe(factory2);
|
expect(differs.find('some object')).toBe(factory2);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should copy over differs from the parent repo', () => {
|
it('should copy over differs from the parent repo', () => {
|
||||||
factory1.spy('supports').andReturn(true);
|
factory1.spy('supports').and.returnValue(true);
|
||||||
factory2.spy('supports').andReturn(false);
|
factory2.spy('supports').and.returnValue(false);
|
||||||
|
|
||||||
var parent = IterableDiffers.create(<any>[factory1]);
|
var parent = IterableDiffers.create(<any>[factory1]);
|
||||||
var child = IterableDiffers.create(<any>[factory2], parent);
|
var child = IterableDiffers.create(<any>[factory2], parent);
|
||||||
|
|
|
@ -66,7 +66,7 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('spy objects', () => {
|
describe('spy objects', () => {
|
||||||
var spyObj: any /** TODO #9100 */;
|
let spyObj: any;
|
||||||
|
|
||||||
beforeEach(() => { spyObj = <any>new SpyTestObj(); });
|
beforeEach(() => { spyObj = <any>new SpyTestObj(); });
|
||||||
|
|
||||||
|
@ -74,7 +74,7 @@ export function main() {
|
||||||
() => { expect(spyObj.spy('someFunc')).not.toHaveBeenCalled(); });
|
() => { expect(spyObj.spy('someFunc')).not.toHaveBeenCalled(); });
|
||||||
|
|
||||||
it('should record function calls', () => {
|
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.someFunc(1, 2)).toEqual(3);
|
||||||
expect(spyObj.spy('someFunc')).toHaveBeenCalledWith(1, 2);
|
expect(spyObj.spy('someFunc')).toHaveBeenCalledWith(1, 2);
|
||||||
|
@ -106,12 +106,6 @@ export function main() {
|
||||||
|
|
||||||
it('should create spys for all methods',
|
it('should create spys for all methods',
|
||||||
() => { expect(() => spyObj.someFunc()).not.toThrow(); });
|
() => { 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);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ export {inject} from './test_bed';
|
||||||
export * from './logger';
|
export * from './logger';
|
||||||
export * from './ng_zone_mock';
|
export * from './ng_zone_mock';
|
||||||
|
|
||||||
export var proxy: ClassDecorator = (t: any /** TODO #9100 */) => t;
|
export var proxy: ClassDecorator = (t: any) => t;
|
||||||
|
|
||||||
var _global = <any>(typeof window === 'undefined' ? global : window);
|
var _global = <any>(typeof window === 'undefined' ? global : window);
|
||||||
|
|
||||||
|
@ -35,7 +35,6 @@ var jsmIIt = _global.fit;
|
||||||
var jsmXIt = _global.xit;
|
var jsmXIt = _global.xit;
|
||||||
|
|
||||||
var runnerStack: BeforeEachRunner[] = [];
|
var runnerStack: BeforeEachRunner[] = [];
|
||||||
var inIt = false;
|
|
||||||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 3000;
|
jasmine.DEFAULT_TIMEOUT_INTERVAL = 3000;
|
||||||
var globalTimeOut = jasmine.DEFAULT_TIMEOUT_INTERVAL;
|
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 runner = runnerStack[runnerStack.length - 1];
|
||||||
var timeOut = Math.max(globalTimeOut, testTimeOut);
|
var timeOut = Math.max(globalTimeOut, testTimeOut);
|
||||||
|
|
||||||
jsmFn(name, (done: any /** TODO #9100 */) => {
|
jsmFn(name, (done: any) => {
|
||||||
var completerProvider = {
|
var completerProvider = {
|
||||||
provide: AsyncTestCompleter,
|
provide: AsyncTestCompleter,
|
||||||
useFactory: () => {
|
useFactory: () => {
|
||||||
|
@ -134,7 +133,6 @@ function _it(jsmFn: Function, name: string, testFn: Function, testTimeOut: numbe
|
||||||
testBed.configureTestingModule({providers: [completerProvider]});
|
testBed.configureTestingModule({providers: [completerProvider]});
|
||||||
runner.run();
|
runner.run();
|
||||||
|
|
||||||
inIt = true;
|
|
||||||
if (testFn.length == 0) {
|
if (testFn.length == 0) {
|
||||||
let retVal = testFn();
|
let retVal = testFn();
|
||||||
if (isPromise(retVal)) {
|
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.
|
// Asynchronous test function that takes in 'done' parameter.
|
||||||
testFn(done);
|
testFn(done);
|
||||||
}
|
}
|
||||||
inIt = false;
|
|
||||||
}, timeOut);
|
}, timeOut);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function it(
|
export function it(name: any, fn: any, timeOut: any = null): void {
|
||||||
name: any /** TODO #9100 */, fn: any /** TODO #9100 */,
|
|
||||||
timeOut: any /** TODO #9100 */ = null): void {
|
|
||||||
return _it(jsmIt, name, fn, timeOut);
|
return _it(jsmIt, name, fn, timeOut);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function xit(
|
export function xit(name: any, fn: any, timeOut: any = null): void {
|
||||||
name: any /** TODO #9100 */, fn: any /** TODO #9100 */,
|
|
||||||
timeOut: any /** TODO #9100 */ = null): void {
|
|
||||||
return _it(jsmXIt, name, fn, timeOut);
|
return _it(jsmXIt, name, fn, timeOut);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function iit(
|
export function iit(name: any, fn: any, timeOut: any = null): void {
|
||||||
name: any /** TODO #9100 */, fn: any /** TODO #9100 */,
|
|
||||||
timeOut: any /** TODO #9100 */ = null): void {
|
|
||||||
return _it(jsmIIt, name, fn, timeOut);
|
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 {
|
export class SpyObject {
|
||||||
constructor(type: any /** TODO #9100 */ = null) {
|
constructor(type?: any) {
|
||||||
if (type) {
|
if (type) {
|
||||||
for (var prop in type.prototype) {
|
for (let prop in type.prototype) {
|
||||||
var m: any /** TODO #9100 */ = null;
|
let m: any = null;
|
||||||
try {
|
try {
|
||||||
m = type.prototype[prop];
|
m = type.prototype[prop];
|
||||||
} catch (e) {
|
} 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 */) {
|
spy(name: string) {
|
||||||
if (!(this as any /** TODO #9100 */)[name]) {
|
if (!(this as any)[name]) {
|
||||||
(this as any /** TODO #9100 */)[name] = this._createGuinnessCompatibleSpy(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 */) {
|
prop(name: string, value: any) { (this as any)[name] = value; }
|
||||||
(this as any /** TODO #9100 */)[name] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
static stub(
|
static stub(object: any = null, config: any = null, overrides: any = null) {
|
||||||
object: any /** TODO #9100 */ = null, config: any /** TODO #9100 */ = null,
|
|
||||||
overrides: any /** TODO #9100 */ = null) {
|
|
||||||
if (!(object instanceof SpyObject)) {
|
if (!(object instanceof SpyObject)) {
|
||||||
overrides = config;
|
overrides = config;
|
||||||
config = object;
|
config = object;
|
||||||
|
@ -224,18 +198,7 @@ export class SpyObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
var m = StringMapWrapper.merge(config, overrides);
|
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;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @internal */
|
|
||||||
_createGuinnessCompatibleSpy(name: any /** TODO #9100 */): GuinessCompatibleSpy {
|
|
||||||
var newSpy: GuinessCompatibleSpy = <any>jasmine.createSpy(name);
|
|
||||||
newSpy.andCallFake = <any>newSpy.and.callFake;
|
|
||||||
newSpy.andReturn = <any>newSpy.and.returnValue;
|
|
||||||
newSpy.reset = <any>newSpy.calls.reset;
|
|
||||||
// revisit return null here (previously needed for rtts_assert).
|
|
||||||
newSpy.and.returnValue(null);
|
|
||||||
return newSpy;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,7 @@ export function createPairedMessageBuses(): PairedMessageBuses {
|
||||||
export function expectBrokerCall(
|
export function expectBrokerCall(
|
||||||
broker: SpyMessageBroker, methodName: string, vals?: Array<any>,
|
broker: SpyMessageBroker, methodName: string, vals?: Array<any>,
|
||||||
handler?: (..._: any[]) => Promise<any>| void): void {
|
handler?: (..._: any[]) => Promise<any>| void): void {
|
||||||
broker.spy('runOnService').andCallFake((args: UiArguments, returnType: Type<any>) => {
|
broker.spy('runOnService').and.callFake((args: UiArguments, returnType: Type<any>) => {
|
||||||
expect(args.method).toEqual(methodName);
|
expect(args.method).toEqual(methodName);
|
||||||
if (isPresent(vals)) {
|
if (isPresent(vals)) {
|
||||||
expect(args.args.length).toEqual(vals.length);
|
expect(args.args.length).toEqual(vals.length);
|
||||||
|
|
|
@ -28,7 +28,7 @@ export function main() {
|
||||||
|
|
||||||
|
|
||||||
function createWebWorkerPlatformLocation(loc: LocationType): WebWorkerPlatformLocation {
|
function createWebWorkerPlatformLocation(loc: LocationType): WebWorkerPlatformLocation {
|
||||||
broker.spy('runOnService').andCallFake((args: UiArguments, returnType: Type<any>) => {
|
broker.spy('runOnService').and.callFake((args: UiArguments, returnType: Type<any>) => {
|
||||||
if (args.method === 'getLocation') {
|
if (args.method === 'getLocation') {
|
||||||
return Promise.resolve(loc);
|
return Promise.resolve(loc);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue