Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: #569 add test for user effects and selectors
  • Loading branch information
PaulRC-ioet committed Nov 24, 2020
commit e998883c0e2318080c3b6c96b5fae9f5d4b8be24
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<td class="col-sm-3">{{ user.name }}</td>
<td class="col-sm-3">{{ user.role }}</td>
<td class="col-sm-3 text-center custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="{{user.name}}" [(ngModel)]="user.role" (click)="changeSwitch()"/>
<label class="custom-control-label" for="{{user.name}}"></label>
<input type="checkbox" class="custom-control-input" id="{{ user.id }}" />
<label class="custom-control-label" for="{{ user.id }}"></label>
</td>
</tr>
</tbody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,4 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
this.dtTrigger.next();
}
}

changeSwitch() {
console.log('ENTROOO');
}
}
5 changes: 5 additions & 0 deletions src/app/modules/users/store/user.actions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import * as actions from './user.actions';

describe('UserActions', () => {
it('LoadUsers type is UserActionTypes.LOAD_USERS', () => {
const action = new actions.LoadUsers();
expect(action.type).toEqual(actions.UserActionTypes.LOAD_USERS);
});

it('LoadUsersSuccess type is UserActionTypes.LOAD_USERS_SUCCESS', () => {
const action = new actions.LoadUsersSuccess([]);
expect(action.type).toEqual(actions.UserActionTypes.LOAD_USERS_SUCCESS);
Expand Down
54 changes: 54 additions & 0 deletions src/app/modules/users/store/user.effects.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { TestBed } from '@angular/core/testing';
import { provideMockActions } from '@ngrx/effects/testing';
import { Action } from '@ngrx/store';
import { Observable, of, throwError } from 'rxjs';
import { UsersService } from '../services/users.service';
import { UserActionTypes } from './user.actions';
import { UserEffects } from './user.effects';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ToastrModule, ToastrService } from 'ngx-toastr';

describe('UserEffects', () => {
let actions$: Observable<Action>;
let effects: UserEffects;
let service: UsersService;
let toastrService;
const user = { id: 'id', name: 'name', email: 'email' };

beforeEach(() => {
TestBed.configureTestingModule({
providers: [UserEffects, provideMockActions(() => actions$)],
imports: [HttpClientTestingModule, ToastrModule.forRoot()],
declarations: [],
});
effects = TestBed.inject(UserEffects);
service = TestBed.inject(UsersService);
toastrService = TestBed.inject(ToastrService);
});

it('should be created', async () => {
expect(effects).toBeTruthy();
});

it('should return a list of users when the action LOAD_USERS is called', async () => {
actions$ = of({ type: UserActionTypes.LOAD_USERS });
const serviceSpy = spyOn(service, 'loadUsers');
serviceSpy.and.returnValue(of(user));

effects.loadUsers$.subscribe((action) => {
expect(action.type).toEqual(UserActionTypes.LOAD_USERS_SUCCESS);
});
});

it('should return an error when the action LOAD_USERS call fails', async () => {
actions$ = of({ type: UserActionTypes.LOAD_USERS });
const serviceSpy = spyOn(service, 'loadUsers');
serviceSpy.and.returnValue(throwError({ error: { message: 'fail!' } }));
spyOn(toastrService, 'error');

effects.loadUsers$.subscribe((action) => {
expect(toastrService.error).toHaveBeenCalled();
expect(action.type).toEqual(UserActionTypes.LOAD_USERS_FAIL);
});
});
});
10 changes: 10 additions & 0 deletions src/app/modules/users/store/user.selectors.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as selectors from './user.selectors';

describe('UserSelectors', () => {
it('should select is Loading', () => {
const isLoadingValue = true;
const userState = { isLoading: isLoadingValue };

expect(selectors.getIsLoading.projector(userState)).toBe(isLoadingValue);
});
});