angular-unit-test-helper
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -6,2 +6,2 @@ export declare enum ObservablePropertyStrategy { | ||
} | ||
export declare function autoSpyObj(classUnderTest: Function, observableStrategy?: ObservablePropertyStrategy): any; | ||
export declare function autoSpyObj(classUnderTest: Function, spyProperties?: string[], observableStrategy?: ObservablePropertyStrategy): any; |
@@ -10,7 +10,8 @@ import { BehaviorSubject, Observable } from 'rxjs'; | ||
})(ObservablePropertyStrategy || (ObservablePropertyStrategy = {})); | ||
export function autoSpyObj(classUnderTest, observableStrategy) { | ||
export function autoSpyObj(classUnderTest, spyProperties, observableStrategy) { | ||
if (spyProperties === void 0) { spyProperties = []; } | ||
if (observableStrategy === void 0) { observableStrategy = ObservablePropertyStrategy.Observable; } | ||
var props = Reflect.ownKeys(classUnderTest.prototype); | ||
var spyObj = jasmine.createSpyObj(classUnderTest.name, getAllFunctions(classUnderTest.prototype, props)); | ||
var properties = getAllProperties(classUnderTest.prototype, props); | ||
var properties = getAllProperties(classUnderTest.prototype, props).concat(spyProperties); | ||
properties.map(function (name) { | ||
@@ -17,0 +18,0 @@ var defaultValue = {}; |
@@ -14,2 +14,3 @@ import { BehaviorSubject, Observable } from 'rxjs' | ||
classUnderTest: Function, | ||
spyProperties: string[] = [], | ||
observableStrategy = ObservablePropertyStrategy.Observable | ||
@@ -23,3 +24,5 @@ ) { | ||
const properties = getAllProperties(classUnderTest.prototype, props) | ||
const properties = getAllProperties(classUnderTest.prototype, props).concat( | ||
spyProperties | ||
) | ||
@@ -26,0 +29,0 @@ properties.map(name => { |
{ | ||
"name": "angular-unit-test-helper", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Helper functions to help write unit tests in Angular using mocks and spies", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -26,7 +26,8 @@ # Angular Unit Test Helper | ||
### autoSpyObj(classUnderTest: Function, observableStrategy = ObservablePropertyStrategy.Observable | ||
) | ||
### function autoSpyObj(classUnderTest: Function, spyProperties: string[] = [], observableStrategy = ObservablePropertyStrategy.Observable) | ||
An extension of `jasmine.createSpyObj` with automatic discovery of functions and property getters given a Class, without requiring an instance of an object. | ||
If you'd want to spy on a property without a getter, then you can simply pass in the property name like `autoSpyObj(WeatherService, ['currentWeather$'])`. | ||
Return value of `autoSpyObj` will be a true mock of the Class with spy-able methods and properties, making it easy to control and modify the return values of external dependencies during testing. | ||
@@ -41,2 +42,11 @@ | ||
Alternate Usage | ||
``` | ||
const weatherServiceSpy = autoSpyObj( | ||
WeatherService, | ||
['currentWeather$'], | ||
ObservablePropertyStrategy.BehaviorSubject | ||
) | ||
``` | ||
`autoSpyObj` replaces the more verbose and difficult to maintain code, shown below: | ||
@@ -43,0 +53,0 @@ ``` |
Sorry, the diff of this file is not supported yet
22907
307
112