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
fix: TT-97 add some test to feature
  • Loading branch information
PaulRC-ioet committed Jan 18, 2021
commit d3dc13f15cd114e07c46c8bec44c21408c46326e
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { MockStore, provideMockStore } from '@ngrx/store/testing';

import { NgxPaginationModule } from 'ngx-pagination';
import { UsersListComponent } from './users-list.component';
import { UserActionTypes, UserState, LoadUsers } from '../../store';
import { UserActionTypes, UserState, LoadUsers, GrantRoleUser, RevokeRoleUser } from '../../store';
import { ActionsSubject } from '@ngrx/store';
import { DataTablesModule } from 'angular-datatables';
import { of } from 'rxjs';

describe('UsersListComponent', () => {
let component: UsersListComponent;
Expand All @@ -29,13 +30,15 @@ describe('UsersListComponent', () => {
message: '',
};

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [NgxPaginationModule, DataTablesModule],
declarations: [UsersListComponent],
providers: [provideMockStore({ initialState: state }), { provide: ActionsSubject, useValue: actionSub }],
}).compileComponents();
}));
beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
imports: [NgxPaginationModule, DataTablesModule],
declarations: [UsersListComponent],
providers: [provideMockStore({ initialState: state }), { provide: ActionsSubject, useValue: actionSub }],
}).compileComponents();
})
);

beforeEach(() => {
fixture = TestBed.createComponent(UsersListComponent);
Expand Down Expand Up @@ -68,9 +71,76 @@ describe('UsersListComponent', () => {

expect(component.users).toEqual(state.data);
});
/*
TODO: block commented on purpose so that when the tests pass and the Feature toggle is removed,
the table will be rendered again with dtInstance and not with dtOptions

it('When Component is created, should call the feature toggle method', () => {
spyOn(component, 'isFeatureToggleActivated').and.returnValue(of(true));

component.ngOnInit();

expect(component.isFeatureToggleActivated).toHaveBeenCalled();
expect(component.isFlagOn).toBe(true);
});

const actionsParams = [
{ actionType: UserActionTypes.GRANT_USER_ROLE_SUCCESS },
{ actionType: UserActionTypes.REVOKE_USER_ROLE_SUCCESS },
];

actionsParams.map((param) => {
it(`When action ${param.actionType} is dispatched should triggered load Users action`, () => {
spyOn(store, 'dispatch');

const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject;
const action = {
type: param.actionType,
payload: state.data,
};

actionSubject.next(action);

expect(store.dispatch).toHaveBeenCalledWith(new LoadUsers());
});
});

const grantRoleTypes = [
{ roleId: 'admin', roleValue: 'time-tracker-admin' },
{ roleId: 'test', roleValue: 'time-tracker-tester' },
];

grantRoleTypes.map((param) => {
it(`When user switchRole to ${param.roleId} and don't have any role, should grant ${param.roleValue} Role`, () => {
const roleId = param.roleId;
const roleValue = param.roleValue;
const userRoles = [];
const userId = 'userId';

spyOn(store, 'dispatch');

component.switchRole(userId, userRoles, roleId, roleValue);

expect(store.dispatch).toHaveBeenCalledWith(new GrantRoleUser(userId, roleId));
});
});

const revokeRoleTypes = [
{ roleId: 'admin', roleValue: 'time-tracker-admin', userRoles: ['time-tracker-admin'] },
{ roleId: 'test', roleValue: 'time-tracker-tester', userRoles: ['time-tracker-tester'] },
];

revokeRoleTypes.map((param) => {
it(`When user switchRole to ${param.roleId} and have that rol asigned, should revoke ${param.roleValue} Role`, () => {
const roleId = param.roleId;
const roleValue = param.roleValue;
const userRoles = param.userRoles;
const userId = 'userId';

spyOn(store, 'dispatch');

component.switchRole(userId, userRoles, roleId, roleValue);

expect(store.dispatch).toHaveBeenCalledWith(new RevokeRoleUser(userId, roleId));
});
});

it('on success load users, the data of roles should be an array and role null', () => {
const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject;
Expand Down Expand Up @@ -117,7 +187,11 @@ describe('UsersListComponent', () => {
});
});

it('on success load users, the datatable should be reloaded', async () => {
/*
TODO: block commented on purpose so that when the tests pass and the Feature toggle is removed,
the table will be rendered again with dtInstance and not with dtOptions

it('on success load users, the datatable should be reloaded', async () => {
const actionSubject = TestBed.inject(ActionsSubject);
const action = {
type: UserActionTypes.LOAD_USERS_SUCCESS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
private featureManagerService: FeatureManagerService
) {
this.isLoading$ = store.pipe(delay(0), select(getIsLoading));
this.isFeatureToggleAactivated().subscribe((flag) => {
this.isFlagOn = flag;
});
}

ngOnInit(): void {
this.isFeatureToggleActivated().subscribe((flag) => {
this.isFlagOn = flag;
});
this.store.dispatch(new LoadUsers());
this.loadUsersSubscription = this.actionsSubject$
.pipe(filter((action: any) => action.type === UserActionTypes.LOAD_USERS_SUCCESS))
Expand Down Expand Up @@ -83,7 +83,7 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
: this.store.dispatch(new GrantRoleUser(userId, roleId));
}

isFeatureToggleAactivated() {
isFeatureToggleActivated() {
return this.featureManagerService.isToggleEnabledForUser('ui-list-test-users').pipe(
map((enabled) => {
if (enabled === true) {
Expand Down