angular-unit-test-helper
Advanced tools
Comparing version 9.1.2 to 9.1.3
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./addProperty"), exports); | ||
tslib_1.__exportStar(require("./createComponentMock"), exports); | ||
tslib_1.__exportStar(require("./getAll"), exports); | ||
tslib_1.__exportStar(require("./autoSpyObj"), exports); | ||
tslib_1.__exportStar(require("./injectHelper"), exports); | ||
__export(require("./addProperty")); | ||
__export(require("./createComponentMock")); | ||
__export(require("./getAll")); | ||
__export(require("./autoSpyObj")); | ||
__export(require("./injectHelper")); | ||
//# sourceMappingURL=index.js.map |
/// <reference types="jasmine" /> | ||
import { Type } from '@angular/core'; | ||
export declare function injectOne<TDependency>(dependency: Type<TDependency>, _spyObject: jasmine.SpyObj<TDependency>): void; | ||
export declare function injectMany<TDependency>(mockedDependencies: [Type<TDependency>, jasmine.SpyObj<TDependency>][]): void; | ||
export declare function injectOne<TDependency, TReturn>(dependency: Type<TDependency>, testBed?: import("@angular/core/testing").TestBedStatic): TReturn; | ||
export declare function injectSpy<TDependency>(dependency: Type<TDependency>, testBed?: import("@angular/core/testing").TestBedStatic): jasmine.SpyObj<TDependency>; |
{ | ||
"name": "angular-unit-test-helper", | ||
"version": "9.1.2", | ||
"version": "9.1.3", | ||
"description": "Helper functions to help write unit tests in Angular using mocks and spies", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -43,3 +43,3 @@ # Angular Unit Test Helper | ||
```ts | ||
const weatherServiceSpy = autoSpyObj(WeatherService) | ||
const weatherServiceSpy = autoSpyObj(WeatherService) | ||
``` | ||
@@ -50,7 +50,7 @@ | ||
```ts | ||
const weatherServiceSpy = autoSpyObj( | ||
WeatherService, | ||
['currentWeather$'], | ||
ObservablePropertyStrategy.BehaviorSubject | ||
) | ||
const weatherServiceSpy = autoSpyObj( | ||
WeatherService, | ||
['currentWeather$'], | ||
ObservablePropertyStrategy.BehaviorSubject | ||
) | ||
``` | ||
@@ -61,6 +61,8 @@ | ||
```ts | ||
jasmine.createSpyObj(WeatherService.name, [ | ||
'getCurrentWeather', 'getCurrentWeatherByCoords', 'updateCurrentWeather' | ||
]) | ||
addPropertyAsBehaviorSubject(weatherServiceSpy, 'currentWeather$') | ||
jasmine.createSpyObj(WeatherService.name, [ | ||
'getCurrentWeather', | ||
'getCurrentWeatherByCoords', | ||
'updateCurrentWeather', | ||
]) | ||
addPropertyAsBehaviorSubject(weatherServiceSpy, 'currentWeather$') | ||
``` | ||
@@ -119,5 +121,5 @@ | ||
### `injectOne<TDependency>(dependency: Type<TDependency>, _spyObject: jasmine.SpyObj<TDependency>)` | ||
### `injectOne<TDependency, TReturn>(dependency: Type<TDependency>): TReturn` | ||
Helper function to inject a dependency, like a service, into the TestBed and assign it to the mocked SpyObj. | ||
Helper function to inject a dependency, like a service, into the TestBed with a typed return variable. | ||
@@ -128,3 +130,3 @@ Usage | ||
beforeEach(() => { | ||
injectOne(WeatherService, weatherServiceMock) | ||
weatherServiceMock = injectOne(WeatherService) | ||
}) | ||
@@ -141,5 +143,5 @@ ``` | ||
### `injectMany<TDependency>(mockedDependencies: [Type<TDependency>, jasmine.SpyObj<TDependency>][])` | ||
### `injectSpy<TDependency>(dependency: Type<TDependency>): jasmine.SpyObj<TDependency>` | ||
Helper function to inject multiple dependencies, like services, into the TestBed and assign it to the mocked SpyObj. | ||
Helper function to inject a dependency, like services, into the TestBed and assign it to the mocked SpyObj. | ||
@@ -150,6 +152,3 @@ Usage | ||
beforeEach(() => { | ||
injectMany([ | ||
[WeatherService, weatherServiceMock], | ||
[PostalCodeService, postalCodeServiceMock] | ||
]) | ||
weatherServiceMock = injectSpy(WeatherService) | ||
}) | ||
@@ -159,6 +158,6 @@ ``` | ||
Replaces | ||
```ts | ||
beforeEach(() => { | ||
weatherServiceMock = TestBed.inject(WeatherService) as any | ||
postalCodeServiceMock = TestBed.inject(PostalCodeService) as any | ||
}) | ||
@@ -165,0 +164,0 @@ ``` |
import { Type } from '@angular/core' | ||
import { TestBed } from '@angular/core/testing' | ||
export function injectOne<TDependency>( | ||
export function injectOne<TDependency, TReturn>( | ||
dependency: Type<TDependency>, | ||
_spyObject: jasmine.SpyObj<TDependency> | ||
) { | ||
_spyObject = TestBed.inject(dependency) as jasmine.SpyObj<TDependency> | ||
testBed = TestBed | ||
): TReturn { | ||
return testBed.inject(dependency) as any | ||
} | ||
export function injectMany<TDependency>( | ||
mockedDependencies: [Type<TDependency>, jasmine.SpyObj<TDependency>][] | ||
) { | ||
mockedDependencies.forEach(([dependency, mockedDependency]) => { | ||
injectOne(dependency, mockedDependency) | ||
}) | ||
export function injectSpy<TDependency>( | ||
dependency: Type<TDependency>, | ||
testBed = TestBed | ||
): jasmine.SpyObj<TDependency> { | ||
return injectOne<TDependency, jasmine.SpyObj<TDependency>>(dependency, testBed) | ||
} |
@@ -16,8 +16,2 @@ import { ObservablePropertyStrategy, autoSpyObj } from '../src/index' | ||
// TestBed.configureTestingModule({ | ||
// providers: [{ provide: WeatherService, useValue: weatherServiceSpy }], | ||
// }).compileComponents() | ||
// injectOne(WeatherService, weatherServiceMock) | ||
weatherServiceMock.currentWeather$.next(fakeWeather) | ||
@@ -24,0 +18,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { createComponentMock } from '../src' | ||
import { createComponentMock } from '../src/index' | ||
@@ -3,0 +3,0 @@ const window = {} as any |
@@ -8,7 +8,4 @@ { | ||
"noImplicitAny": false, | ||
"types": [ | ||
"node", | ||
"jasmine" | ||
] | ||
"types": ["node", "jasmine"] | ||
} | ||
} | ||
} |
@@ -0,27 +1,40 @@ | ||
// { | ||
// "compilerOptions": { | ||
// "moduleResolution": "node", | ||
// "emitDecoratorMetadata": true, | ||
// "importHelpers": true, | ||
// "typeRoots": ["node_modules/@types"], | ||
// "target": "es2015", | ||
// "module": "commonjs", | ||
// "strict": true, | ||
// "esModuleInterop": true, | ||
// "lib": ["dom", "es2018", "es2015", "es2016", "es2017", "es2019"], | ||
// "types": ["jasmine", "node"], | ||
// "declaration": true, | ||
// "sourceMap": true, | ||
// "noImplicitAny": true, | ||
// "strictNullChecks": true, | ||
// "noImplicitThis": true, | ||
// "alwaysStrict": true, | ||
// "noUnusedLocals": true, | ||
// "noUnusedParameters": true, | ||
// "noImplicitReturns": true, | ||
// "noFallthroughCasesInSwitch": true, | ||
// "experimentalDecorators": true, | ||
// "allowSyntheticDefaultImports": true, | ||
// "resolveJsonModule": true | ||
// } | ||
// } | ||
{ | ||
"compilerOptions": { | ||
"moduleResolution": "node", | ||
"emitDecoratorMetadata": true, | ||
"importHelpers": true, | ||
"typeRoots": ["node_modules/@types"], | ||
"target": "es2015", | ||
"module": "commonjs", | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"lib": ["dom", "es2018", "es2015", "es2016", "es2017", "es2019"], | ||
"types": ["jasmine", "node"], | ||
"moduleResolution": "node", | ||
"lib": ["dom", "es2015", "es2016", "es2017", "es2018", "es2019"], | ||
"declaration": true, | ||
"sourceMap": true, | ||
"noImplicitAny": true, | ||
"strictNullChecks": true, | ||
"noImplicitThis": true, | ||
"alwaysStrict": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"noImplicitReturns": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"experimentalDecorators": true, | ||
"allowSyntheticDefaultImports": true, | ||
"resolveJsonModule": true | ||
"typeRoots": ["node_modules/@types"], | ||
"resolveJsonModule": true, | ||
"esModuleInterop": true | ||
} | ||
} |
@@ -18,5 +18,3 @@ { | ||
}, | ||
"include": [ | ||
"./src" | ||
] | ||
} | ||
"include": ["./src"] | ||
} |
27568
36
462
173