|
1 | 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; |
| 2 | +import { MockStore, provideMockStore } from '@ngrx/store/testing'; |
2 | 3 |
|
| 4 | +import { NgxPaginationModule } from 'ngx-pagination'; |
3 | 5 | import { UsersListComponent } from './users-list.component'; |
| 6 | +import { UserActionTypes, UserState, LoadUsers } from '../../store'; |
| 7 | +import { ActionsSubject } from '@ngrx/store'; |
| 8 | +import { DataTablesModule } from 'angular-datatables'; |
| 9 | +import { act } from '@ngrx/effects'; |
4 | 10 |
|
5 | 11 | describe('UsersListComponent', () => { |
6 | 12 | let component: UsersListComponent; |
7 | 13 | let fixture: ComponentFixture<UsersListComponent>; |
| 14 | + let store: MockStore<UserState>; |
| 15 | + const actionSub: ActionsSubject = new ActionsSubject(); |
| 16 | + |
| 17 | + const state = { |
| 18 | + data: [{ name: 'name', email: 'email', role: 'role', id: 'id', tenand_id: 'tenand id', deleted: 'delete' }], |
| 19 | + isLoading: false, |
| 20 | + message: '', |
| 21 | + }; |
8 | 22 |
|
9 | 23 | beforeEach(async(() => { |
10 | 24 | TestBed.configureTestingModule({ |
11 | | - declarations: [ UsersListComponent ] |
12 | | - }) |
13 | | - .compileComponents(); |
| 25 | + imports: [NgxPaginationModule, DataTablesModule], |
| 26 | + declarations: [UsersListComponent], |
| 27 | + providers: [provideMockStore({ initialState: state }), { provide: ActionsSubject, useValue: actionSub }], |
| 28 | + }).compileComponents(); |
14 | 29 | })); |
15 | 30 |
|
16 | 31 | beforeEach(() => { |
17 | 32 | fixture = TestBed.createComponent(UsersListComponent); |
18 | 33 | component = fixture.componentInstance; |
| 34 | + store = TestBed.inject(MockStore); |
| 35 | + store.setState(state); |
19 | 36 | fixture.detectChanges(); |
20 | 37 | }); |
21 | 38 |
|
22 | 39 | it('should create', () => { |
23 | 40 | expect(component).toBeTruthy(); |
24 | 41 | }); |
| 42 | + |
| 43 | + it('when the component is initialized the load User action is triggered', () => { |
| 44 | + spyOn(store, 'dispatch'); |
| 45 | + |
| 46 | + component.ngOnInit(); |
| 47 | + |
| 48 | + expect(store.dispatch).toHaveBeenCalledWith(new LoadUsers()); |
| 49 | + }); |
| 50 | + |
| 51 | + it('on success load users, the user list should be populated', () => { |
| 52 | + const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject; |
| 53 | + const action = { |
| 54 | + type: UserActionTypes.LOAD_USERS_SUCCESS, |
| 55 | + payload: state.data, |
| 56 | + }; |
| 57 | + |
| 58 | + actionSubject.next(action); |
| 59 | + |
| 60 | + expect(component.users).toEqual(state.data); |
| 61 | + }); |
| 62 | + |
| 63 | + it('on success load users, the datatable should be reloaded', async () => { |
| 64 | + const actionSubject = TestBed.inject(ActionsSubject); |
| 65 | + const action = { |
| 66 | + type: UserActionTypes.LOAD_USERS_SUCCESS, |
| 67 | + payload: state.data, |
| 68 | + }; |
| 69 | + spyOn(component.dtElement.dtInstance, 'then'); |
| 70 | + |
| 71 | + actionSubject.next(action); |
| 72 | + |
| 73 | + expect(component.dtElement.dtInstance.then).toHaveBeenCalled(); |
| 74 | + }); |
| 75 | + |
| 76 | + afterEach(() => { |
| 77 | + component.dtTrigger.unsubscribe(); |
| 78 | + component.loadUsersSubscription.unsubscribe(); |
| 79 | + fixture.destroy(); |
| 80 | + }); |
25 | 81 | }); |
0 commit comments