Created
May 16, 2019 06:40
-
-
Save mohamedaboelmagd/3866ae339ab2b23486be5e3eba501fdb to your computer and use it in GitHub Desktop.
This testing doesn't work
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { environment } from 'environments/environment'; | |
import { HttpClientTestingModule } from '@angular/common/http/testing'; | |
import { TestBed } from '@angular/core/testing'; | |
import * as fromModels from '@models'; | |
import { Actions } from '@ngrx/effects'; | |
import { cold, hot } from 'jasmine-marbles'; | |
import { empty, Observable, of, BehaviorSubject } from 'rxjs'; | |
import * as fromActions from '../actions/trending.actions'; | |
import * as fromEffects from './trending.effects'; | |
import { AngularFireAuth, AngularFireAuthModule } from '@angular/fire/auth'; | |
import { | |
AngularFirestore, | |
AngularFirestoreModule, | |
DocumentChangeAction | |
} from '@angular/fire/firestore'; | |
import { AngularFireModule } from '@angular/fire'; | |
import { ApiService } from '@services/api.service'; | |
import { AngularFireFunctions } from '@angular/fire/functions'; | |
import { StoreModule, combineReducers, Store } from '@ngrx/store'; | |
import * as fromRoot from '../../../../../../store'; | |
import * as fromReducer from '../../store/reducers'; | |
import { map, concatMap, tap, switchMap } from 'rxjs/operators'; | |
export class TestActions extends Actions { | |
constructor() { | |
super(empty()); | |
} | |
set stream(source: Observable<any>) { | |
this.source = source; | |
} | |
} | |
export function getActions() { | |
return new TestActions(); | |
} | |
describe('--- TrendingEffects ---', () => { | |
let actions$: TestActions; | |
let service: ApiService; | |
let effects: fromEffects.TrendingEffects; | |
let store: Store<fromReducer.IFeatureState>; | |
const latestBadges: any[] = [ | |
{ | |
type: 'added', | |
doc: { | |
id: '1', | |
data: () => { | |
return { | |
name: 'name', | |
description: 'de', | |
criteria: 'cd', | |
photo: 'stringify', | |
privacy: 'PUBLIC' | |
}; | |
} | |
} | |
}, | |
{ | |
type: 'added', | |
doc: { | |
id: '2', | |
data: () => { | |
return { | |
name: 'name 2', | |
description: 'de 2', | |
criteria: 'cd 2', | |
photo: 'stringify 2', | |
privacy: 'PUBLIC' | |
}; | |
} | |
} | |
} | |
]; | |
const MockApiService = { | |
getAll: () => of(latestBadges) | |
}; | |
const MockAngularFirestore = { | |
collection: (name: string) => ({ | |
stateChanges: (): Observable<DocumentChangeAction<any>[]> => | |
of(latestBadges as DocumentChangeAction<any>[]) | |
}) | |
}; | |
beforeEach(() => { | |
TestBed.configureTestingModule({ | |
imports: [ | |
HttpClientTestingModule, | |
AngularFireModule.initializeApp(environment.firebaseConfig), | |
StoreModule.forRoot({ | |
...fromRoot.reducers, | |
trending_feature: combineReducers(fromReducer.reducers) | |
}) | |
], | |
providers: [ | |
ApiService, | |
// AngularFirestore, | |
AngularFireFunctions, | |
// AngularFireAuth, | |
fromEffects.TrendingEffects, | |
{ | |
provide: Actions, | |
useFactory: getActions | |
}, | |
// { | |
// provide: ApiService, | |
// useValue: MockApiService | |
// }, | |
{ | |
provide: AngularFirestore, | |
useValue: MockAngularFirestore | |
} | |
] | |
}); | |
actions$ = TestBed.get(Actions); | |
service = TestBed.get(ApiService); | |
effects = TestBed.get(fromEffects.TrendingEffects); | |
store = TestBed.get(Store); | |
// effects = new fromEffects.TrendingEffects(actions$, service, store); | |
spyOn(service, 'getAll').and.returnValue(of(latestBadges)); | |
spyOn(store, 'dispatch').and.callThrough(); | |
}); | |
describe('loadTrending$', () => { | |
it('should return a collection from loadTrendingSuccess', () => { | |
const action = new fromActions.LoadTrending(); | |
const step1 = new fromActions.TrendingAdded(latestBadges[0].doc.data()); | |
const step2 = new fromActions.TrendingAdded(latestBadges[1].doc.data()); | |
const completion = new fromActions.LoadTrendingSuccess(); | |
actions$.stream = hot('-a', { a: action }); | |
const expected = cold('--b-c', { | |
b: step1, | |
c: step2 | |
}); | |
expect(effects.loadTrending$).toBeObservable(expected); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { Actions, Effect, ofType } from '@ngrx/effects'; | |
import { ApiService } from '@services/api.service'; | |
import { of } from 'rxjs'; | |
import { catchError, switchMap, tap, concatMap } from 'rxjs/operators'; | |
import * as fromActions from '../actions'; | |
import * as fromStore from '../../store'; | |
import { Store } from '@ngrx/store'; | |
import * as fromModels from '@models'; | |
import { DocumentChangeAction } from '@angular/fire/firestore'; | |
/* | |
Note that: | |
apiService.getAll(): Observable<DocumentChangeAction<any>[]> { | |
return this.angularFirestore.collection(this.path).stateChanges(); | |
} | |
*/ | |
@Injectable() | |
export class TrendingEffects { | |
constructor( | |
private actions$: Actions, | |
private apiService: ApiService, | |
private store: Store<fromStore.IFeatureState> | |
) {} | |
@Effect() | |
loadTrending$ = this.actions$.pipe( | |
ofType(fromActions.LOAD_TRENDING), | |
switchMap(() => { | |
this.apiService.path = `latestBadges`; | |
return this.apiService.getAll().pipe( | |
tap(actions => { | |
if (actions) { | |
this.store.dispatch(new fromActions.LoadTrendingSuccess()); | |
} | |
}), | |
concatMap( | |
(actions: DocumentChangeAction<fromModels.IBadge>[]) => actions | |
), | |
concatMap((action: DocumentChangeAction<fromModels.IBadge>) => { | |
return [ | |
{ | |
type: `[Trending] ${action.type}`, | |
payload: { | |
id: action.payload.doc.id, | |
...action.payload.doc.data() | |
} | |
} | |
]; | |
}), | |
catchError(err => of(new fromActions.LoadTrendingFailure(err))) | |
); | |
}) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment