Skip to content

Commit e998883

Browse files
committed
feat: #569 add test for user effects and selectors
1 parent e2caaea commit e998883

File tree

5 files changed

+71
-6
lines changed

5 files changed

+71
-6
lines changed

src/app/modules/users/components/users-list/users-list.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
<td class="col-sm-3">{{ user.name }}</td>
1515
<td class="col-sm-3">{{ user.role }}</td>
1616
<td class="col-sm-3 text-center custom-control custom-switch">
17-
<input type="checkbox" class="custom-control-input" id="{{user.name}}" [(ngModel)]="user.role" (click)="changeSwitch()"/>
18-
<label class="custom-control-label" for="{{user.name}}"></label>
17+
<input type="checkbox" class="custom-control-input" id="{{ user.id }}" />
18+
<label class="custom-control-label" for="{{ user.id }}"></label>
1919
</td>
2020
</tr>
2121
</tbody>

src/app/modules/users/components/users-list/users-list.component.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,4 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
5252
this.dtTrigger.next();
5353
}
5454
}
55-
56-
changeSwitch() {
57-
console.log('ENTROOO');
58-
}
5955
}

src/app/modules/users/store/user.actions.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import * as actions from './user.actions';
22

33
describe('UserActions', () => {
4+
it('LoadUsers type is UserActionTypes.LOAD_USERS', () => {
5+
const action = new actions.LoadUsers();
6+
expect(action.type).toEqual(actions.UserActionTypes.LOAD_USERS);
7+
});
8+
49
it('LoadUsersSuccess type is UserActionTypes.LOAD_USERS_SUCCESS', () => {
510
const action = new actions.LoadUsersSuccess([]);
611
expect(action.type).toEqual(actions.UserActionTypes.LOAD_USERS_SUCCESS);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { TestBed } from '@angular/core/testing';
2+
import { provideMockActions } from '@ngrx/effects/testing';
3+
import { Action } from '@ngrx/store';
4+
import { Observable, of, throwError } from 'rxjs';
5+
import { UsersService } from '../services/users.service';
6+
import { UserActionTypes } from './user.actions';
7+
import { UserEffects } from './user.effects';
8+
import { HttpClientTestingModule } from '@angular/common/http/testing';
9+
import { ToastrModule, ToastrService } from 'ngx-toastr';
10+
11+
describe('UserEffects', () => {
12+
let actions$: Observable<Action>;
13+
let effects: UserEffects;
14+
let service: UsersService;
15+
let toastrService;
16+
const user = { id: 'id', name: 'name', email: 'email' };
17+
18+
beforeEach(() => {
19+
TestBed.configureTestingModule({
20+
providers: [UserEffects, provideMockActions(() => actions$)],
21+
imports: [HttpClientTestingModule, ToastrModule.forRoot()],
22+
declarations: [],
23+
});
24+
effects = TestBed.inject(UserEffects);
25+
service = TestBed.inject(UsersService);
26+
toastrService = TestBed.inject(ToastrService);
27+
});
28+
29+
it('should be created', async () => {
30+
expect(effects).toBeTruthy();
31+
});
32+
33+
it('should return a list of users when the action LOAD_USERS is called', async () => {
34+
actions$ = of({ type: UserActionTypes.LOAD_USERS });
35+
const serviceSpy = spyOn(service, 'loadUsers');
36+
serviceSpy.and.returnValue(of(user));
37+
38+
effects.loadUsers$.subscribe((action) => {
39+
expect(action.type).toEqual(UserActionTypes.LOAD_USERS_SUCCESS);
40+
});
41+
});
42+
43+
it('should return an error when the action LOAD_USERS call fails', async () => {
44+
actions$ = of({ type: UserActionTypes.LOAD_USERS });
45+
const serviceSpy = spyOn(service, 'loadUsers');
46+
serviceSpy.and.returnValue(throwError({ error: { message: 'fail!' } }));
47+
spyOn(toastrService, 'error');
48+
49+
effects.loadUsers$.subscribe((action) => {
50+
expect(toastrService.error).toHaveBeenCalled();
51+
expect(action.type).toEqual(UserActionTypes.LOAD_USERS_FAIL);
52+
});
53+
});
54+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as selectors from './user.selectors';
2+
3+
describe('UserSelectors', () => {
4+
it('should select is Loading', () => {
5+
const isLoadingValue = true;
6+
const userState = { isLoading: isLoadingValue };
7+
8+
expect(selectors.getIsLoading.projector(userState)).toBe(isLoadingValue);
9+
});
10+
});

0 commit comments

Comments
 (0)