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
Next Next commit
test: #309 Removing flaky tests
  • Loading branch information
Juan Gabriel Guzman committed Jun 2, 2020
commit 61978ffd365c84c06e7fd3ef4948f39b937fe2d5
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,18 @@ describe('CustomerTableListComponent', () => {
expect(component.customers).toEqual(state.data);
});

// it('on success load customer and the datatable was already initialized, then the datatable should be destroyed ' +
// 'before reloading the customer data', () => {
// const actionSubject = TestBed.get(ActionsSubject) as ActionsSubject;
// component.isDtInitialized = true;
// const action = {
// type: CustomerManagementActionTypes.LOAD_CUSTOMERS_SUCCESS,
// payload: state.data
// };
// const dtApi: DataTables.Api = jasmine.createSpyObj<DataTables.Api>('dtApi', ['destroy']);
// component.dtElement.dtInstance = Promise.resolve(dtApi);
// spyOn(component.dtElement.dtInstance, 'then');
//
// actionSubject.next(action);
//
// expect(component.dtElement.dtInstance.then).toHaveBeenCalled();
// // TODO Improve this test. This is not testing the datatable is destroyed
// // expect(dtApi.destroy).toHaveBeenCalled();
// });
it('on success load customer, the datatable should be reloaded', async () => {
const actionSubject = TestBed.inject(ActionsSubject);
const action = {
type: CustomerManagementActionTypes.LOAD_CUSTOMERS_SUCCESS,
payload: state.data
};
spyOn(component.dtElement.dtInstance, 'then');

actionSubject.next(action);

expect(component.dtElement.dtInstance.then).toHaveBeenCalled();
});

afterEach(() => {
component.dtTrigger.unsubscribe();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component, EventEmitter, Input, OnDestroy, OnInit, Output, ViewChild} from '@angular/core';
import {Component, EventEmitter, Input, OnDestroy, OnInit, Output, ViewChild, AfterViewInit} from '@angular/core';
import {ActionsSubject, Store} from '@ngrx/store';

import {Subject, Subscription} from 'rxjs';
Expand All @@ -17,7 +17,7 @@ import {DataTableDirective} from 'angular-datatables';
templateUrl: './customer-list.component.html',
styleUrls: ['./customer-list.component.scss'],
})
export class CustomerListComponent implements OnInit, OnDestroy {
export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit {

@Input() showCustomerForm: boolean;
@Output() changeValueShowCustomerForm = new EventEmitter<boolean>();
Expand All @@ -27,7 +27,6 @@ export class CustomerListComponent implements OnInit, OnDestroy {
dtTrigger: Subject<any> = new Subject();
@ViewChild(DataTableDirective, {static: false})
dtElement: DataTableDirective;
isDtInitialized = false;
loadCustomersSubscription: Subscription;
changeCustomerSubscription: Subscription;

Expand Down Expand Up @@ -62,6 +61,10 @@ export class CustomerListComponent implements OnInit, OnDestroy {
this.store.dispatch(new LoadCustomers());
}

ngAfterViewInit(): void {
this.rerenderDataTable();
}

ngOnDestroy() {
this.loadCustomersSubscription.unsubscribe();
this.changeCustomerSubscription.unsubscribe();
Expand All @@ -78,16 +81,14 @@ export class CustomerListComponent implements OnInit, OnDestroy {
this.store.dispatch(new DeleteCustomer(customerId));
}

/* istanbul ignore next */
private rerenderDataTable(): void {
if (this.isDtInitialized) {
this.dtElement.dtInstance.then((dtInstance: DataTables.Api) => {
dtInstance.destroy();
if (this.dtElement && this.dtElement.dtInstance) {
this.dtElement.dtInstance.then((dtInstances: DataTables.Api) => {
dtInstances.destroy();
this.dtTrigger.next();
});
} else {
this.dtTrigger.next();
this.isDtInitialized = true;
}
}
}
50 changes: 26 additions & 24 deletions src/app/modules/login/services/azure.ad.b2c.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@ import { UserAgentApplication, Account } from 'msal';
describe('AzureAdB2CService', () => {
let service: AzureAdB2CService;

const account: Account = {
accountIdentifier: 'abc',
homeAccountIdentifier: 'abc',
userName: 'abc',
name: 'abc',
idToken: {
iss: ' http://hostname.com/12345/v0/',
},
idTokenClaims: {},
sid: 'abc',
environment: 'abc',
};
let account: Account;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [],
});
service = TestBed.inject(AzureAdB2CService);
account = {
accountIdentifier: 'abc',
homeAccountIdentifier: 'abc',
userName: 'abc',
name: 'abc',
idToken: {
iss: ' http://hostname.com/12345/v0/',
},
idTokenClaims: {},
sid: 'abc',
environment: 'abc',
};
});

it('should be created', inject([AzureAdB2CService], (apiService: AzureAdB2CService) => {
Expand Down Expand Up @@ -54,23 +55,24 @@ describe('AzureAdB2CService', () => {
expect(name).toEqual(account.name);
});

// it('isAdmin false when extension_role !== time-tracker-admin', () => {
// spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(account);
it('isAdmin false when extension_role !== time-tracker-admin', async () => {
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(account);

const isAdmin = service.isAdmin();

// const isAdmin = service.isAdmin();
expect(isAdmin).toEqual(false);
});

// expect(isAdmin).toEqual(false);
// });
it('isAdmin when extension_role === time-tracker-admin', async () => {
const adminAccount = {...account};
adminAccount.idToken.extension_role = 'time-tracker-admin';

// it('isAdmin when extension_role === time-tracker-admin', () => {
// const adminAccount = account;
// adminAccount.idToken.extension_role = 'time-tracker-admin';
// spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(adminAccount);
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(adminAccount);

// const isAdmin = service.isAdmin();
const isAdmin = service.isAdmin();

// expect(isAdmin).toBeTruthy();
// });
expect(isAdmin).toBeTruthy();
});

it('isLogin returns true if UserAgentApplication has a defined Account', () => {
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(account);
Expand Down
1 change: 1 addition & 0 deletions src/app/modules/login/services/azure.ad.b2c.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class AzureAdB2CService {
}

isAdmin() {
// console.log("Account: " ,this.msal.getAccount());
return this.msal.getAccount()?.idToken?.extension_role === 'time-tracker-admin';
}

Expand Down