angular-unit-test-helper
Advanced tools
| import { ObservablePropertyStrategy, autoSpyObj } from '../src/index' | ||
| import { WeatherService, fakeWeather } from './testObjects' | ||
| describe('autoSpyObj', () => { | ||
| let weatherServiceMock: jasmine.SpyObj<WeatherService> | ||
| it('should create a spy', () => { | ||
| const weatherServiceSpy = autoSpyObj({ | ||
| classUnderTest: WeatherService, | ||
| spyProperties: ['currentWeather$'], | ||
| observableStrategy: ObservablePropertyStrategy.BehaviorSubject, | ||
| }) | ||
| weatherServiceMock = weatherServiceSpy | ||
| // TestBed.configureTestingModule({ | ||
| // providers: [{ provide: WeatherService, useValue: weatherServiceSpy }], | ||
| // }).compileComponents() | ||
| // injectOne(WeatherService, weatherServiceMock) | ||
| weatherServiceMock.currentWeather$.next(fakeWeather) | ||
| weatherServiceMock.currentWeather$.subscribe(current => { | ||
| expect(weatherServiceSpy).toBeDefined() | ||
| expect(weatherServiceMock).toBeDefined() | ||
| expect(current.city).toEqual('Bethesda') | ||
| expect(current.temperature).toEqual(280.32) | ||
| }) | ||
| }) | ||
| }) |
| import { createComponentMock } from '../src' | ||
| const window = {} as any | ||
| describe('createComponentMock', () => { | ||
| it('should create a mocked component', () => { | ||
| window.hello = 'world' | ||
| createComponentMock('CurrentWeatherComponent') | ||
| expect().nothing() | ||
| }) | ||
| }) |
| import { BehaviorSubject } from 'rxjs' | ||
| export interface ICurrentWeather { | ||
| city: string | ||
| country: string | ||
| date: number | ||
| image: string | ||
| temperature: number | ||
| description: string | ||
| } | ||
| export const fakeWeather: ICurrentWeather = { | ||
| city: 'Bethesda', | ||
| country: 'US', | ||
| date: 1485789600, | ||
| image: '', | ||
| temperature: 280.32, | ||
| description: 'light intensity drizzle', | ||
| } | ||
| export const defaultWeather: ICurrentWeather = { | ||
| city: '--', | ||
| country: '--', | ||
| date: Date.now(), | ||
| image: '', | ||
| temperature: 0, | ||
| description: '', | ||
| } | ||
| export class WeatherService { | ||
| readonly currentWeather$ = new BehaviorSubject<ICurrentWeather>(defaultWeather) | ||
| constructor(a: string, b: string) { | ||
| console.log(a + b) | ||
| } | ||
| } |
@@ -6,2 +6,6 @@ export declare enum ObservablePropertyStrategy { | ||
| } | ||
| export declare function autoSpyObj(classUnderTest: () => any, spyProperties?: string[], observableStrategy?: ObservablePropertyStrategy): any; | ||
| export declare function autoSpyObj({ classUnderTest, spyProperties, observableStrategy, }: { | ||
| classUnderTest: NewableFunction; | ||
| spyProperties?: string[]; | ||
| observableStrategy?: ObservablePropertyStrategy; | ||
| }): any; |
+2
-1
| { | ||
| "name": "angular-unit-test-helper", | ||
| "version": "9.1.1", | ||
| "version": "9.1.2", | ||
| "description": "Helper functions to help write unit tests in Angular using mocks and spies", | ||
@@ -50,2 +50,3 @@ "main": "dist/index.js", | ||
| "devDependencies": { | ||
| "@angular/compiler": "^9.0.5", | ||
| "@angular/core": "^9.0.5", | ||
@@ -52,0 +53,0 @@ "@istanbuljs/nyc-config-typescript": "^1.0.1", |
@@ -12,7 +12,11 @@ import { BehaviorSubject, Observable } from 'rxjs' | ||
| export function autoSpyObj( | ||
| classUnderTest: () => any, | ||
| spyProperties: string[] = [], | ||
| observableStrategy = ObservablePropertyStrategy.Observable | ||
| ) { | ||
| export function autoSpyObj({ | ||
| classUnderTest, | ||
| spyProperties = [], | ||
| observableStrategy = ObservablePropertyStrategy.Observable, | ||
| }: { | ||
| classUnderTest: NewableFunction | ||
| spyProperties?: string[] | ||
| observableStrategy?: ObservablePropertyStrategy | ||
| }) { | ||
| const props = Reflect.ownKeys(classUnderTest.prototype) | ||
@@ -19,0 +23,0 @@ const spyObj = jasmine.createSpyObj( |
@@ -20,6 +20,13 @@ import { Component } from '@angular/core' | ||
| const newClass: any = ((window as any)[className] = () => {}) | ||
| const newClass: any = (getWindow()[className] = () => {}) | ||
| return __decorate([Component({ selector: selectorName, template })], newClass) | ||
| } | ||
| function getWindow(): any { | ||
| if (typeof window === 'undefined') { | ||
| return {} | ||
| } | ||
| return window | ||
| } | ||
| function inferSelectorName(className: string) { | ||
@@ -26,0 +33,0 @@ className = className.replace('Component', '') |
+1
-4
| { | ||
| "compilerOptions": { | ||
| "baseUrl": "./", | ||
| "moduleResolution": "node", | ||
@@ -15,3 +14,2 @@ "emitDecoratorMetadata": true, | ||
| "declaration": true, | ||
| "outDir": "./dist", | ||
| "sourceMap": true, | ||
@@ -29,4 +27,3 @@ "noImplicitAny": true, | ||
| "resolveJsonModule": true | ||
| }, | ||
| "include": ["./src"] | ||
| } | ||
| } |
| describe('addProperty', () => { | ||
| it('should ...', () => { | ||
| const unexpected = 'a' | ||
| expect(unexpected).toEqual('a') | ||
| }) | ||
| }) |
Sorry, the diff of this file is not supported yet
26754
8.67%35
6.06%432
19.01%37
2.78%