UNPKG

angular-unit-test-helper

Version:

Helper functions to help write unit tests in Angular using mocks and spies

33 lines 1.65 kB
import { BehaviorSubject, Observable } from 'rxjs'; import { addProperty } from './addProperty'; import { getAllFunctions, getAllProperties } from './getAll'; export var ObservablePropertyStrategy; (function (ObservablePropertyStrategy) { ObservablePropertyStrategy[ObservablePropertyStrategy["Object"] = 0] = "Object"; ObservablePropertyStrategy[ObservablePropertyStrategy["Observable"] = 1] = "Observable"; ObservablePropertyStrategy[ObservablePropertyStrategy["BehaviorSubject"] = 2] = "BehaviorSubject"; })(ObservablePropertyStrategy || (ObservablePropertyStrategy = {})); export function autoSpyObj(classUnderTest, spyProperties = [], observableStrategy = ObservablePropertyStrategy.Observable) { const props = Reflect.ownKeys(classUnderTest.prototype); const spyObj = jasmine.createSpyObj(classUnderTest.name, getAllFunctions(classUnderTest.prototype, props)); const properties = getAllProperties(classUnderTest.prototype, props).concat(spyProperties); properties.map((name) => { let defaultValue = {}; if (typeof name === 'string' && name.endsWith('$')) { defaultValue = getDefaultObservableValue(observableStrategy); } addProperty(spyObj, name, defaultValue); }); return spyObj; } function getDefaultObservableValue(strategy) { switch (strategy) { case ObservablePropertyStrategy.Object: return {}; case ObservablePropertyStrategy.Observable: return new Observable(); case ObservablePropertyStrategy.BehaviorSubject: return new BehaviorSubject(null); } } //# sourceMappingURL=autoSpyObj.js.map