|
| 1 | +import { Observable, of, throwError } from 'rxjs'; |
| 2 | +import { Action } from '@ngrx/store'; |
| 3 | +import { User } from '../models/user'; |
| 4 | +import { UserEffects } from './user.effects'; |
| 5 | +import { TestBed } from '@angular/core/testing'; |
| 6 | +import { UserService } from '../services/user.service'; |
| 7 | +import { provideMockActions } from '@ngrx/effects/testing'; |
| 8 | +import { HttpClientTestingModule } from '@angular/common/http/testing'; |
| 9 | +import { LoadUser, UserActionTypes } from './user.actions'; |
| 10 | + |
| 11 | +describe('UserEffects', () => { |
| 12 | + let actions$: Observable<Action>; |
| 13 | + let effects: UserEffects; |
| 14 | + let service: UserService; |
| 15 | + const userInfo: User = { |
| 16 | + name: 'Unknown Name', |
| 17 | + |
| 18 | + roles: [], |
| 19 | + groups: [], |
| 20 | + id: 'dummy_tenant_id_load', |
| 21 | + tenant_id: null, |
| 22 | + deleted: null |
| 23 | + }; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + TestBed.configureTestingModule({ |
| 27 | + providers: [UserEffects, provideMockActions(() => actions$)], |
| 28 | + imports: [HttpClientTestingModule], |
| 29 | + }); |
| 30 | + |
| 31 | + effects = TestBed.inject(UserEffects); |
| 32 | + service = TestBed.inject(UserService); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should be created', async () => { |
| 36 | + expect(effects).toBeTruthy(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('action type is LOAD_USER_SUCCESS when service is executed successfully', async () => { |
| 40 | + const userId = 'dummy_id_load'; |
| 41 | + const serviceSpy = spyOn(service, 'loadUser'); |
| 42 | + |
| 43 | + actions$ = of(new LoadUser(userId)); |
| 44 | + serviceSpy.and.returnValue(of(userInfo)); |
| 45 | + |
| 46 | + effects.loadUserInfo$.subscribe((action) => { |
| 47 | + expect(action.type).toEqual(UserActionTypes.LOAD_USER_SUCCESS); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('action type is LOAD_USER_FAIL when service fail in execution', async () => { |
| 52 | + const userId = 'dummy_id_load'; |
| 53 | + const serviceSpy = spyOn(service, 'loadUser'); |
| 54 | + |
| 55 | + actions$ = of(new LoadUser(userId)); |
| 56 | + serviceSpy.and.returnValue(throwError({ error: { message: 'fail!' } })); |
| 57 | + |
| 58 | + effects.loadUserInfo$.subscribe((action) => { |
| 59 | + expect(action.type).toEqual(UserActionTypes.LOAD_USER_FAIL); |
| 60 | + }); |
| 61 | + }); |
| 62 | +}); |
0 commit comments