Skip to content

Commit

Permalink
fix: Delegated Accounts Linting (#2429)
Browse files Browse the repository at this point in the history
* fixed linting

* resovled comments

* test: Code Coverage: Delegated Accounts Page (#2434)

* coverage at 100

* resolved comments

---------

Co-authored-by: Jay Budhadev <[email protected]>

---------

Co-authored-by: Jay Budhadev <[email protected]>
  • Loading branch information
jayfyle and Jay Budhadev authored Sep 25, 2023
1 parent edbd69d commit 7d556ec
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 42 deletions.
173 changes: 160 additions & 13 deletions src/app/fyle/delegated-accounts/delegated-accounts.page.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,173 @@
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { ComponentFixture, TestBed, fakeAsync, tick, waitForAsync } from '@angular/core/testing';
import { IonicModule } from '@ionic/angular';

import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { of } from 'rxjs';
import { getElementRef } from 'src/app/core/dom-helpers';
import { apiEouRes, eouRes2, eouRes3 } from 'src/app/core/mock-data/extended-org-user.data';
import { orgData1 } from 'src/app/core/mock-data/org.data';
import { LoaderService } from 'src/app/core/services/loader.service';
import { OrgUserService } from 'src/app/core/services/org-user.service';
import { OrgService } from 'src/app/core/services/org.service';
import { RecentLocalStorageItemsService } from 'src/app/core/services/recent-local-storage-items.service';
import { DelegatedAccountsPage } from './delegated-accounts.page';

xdescribe('DelegatedAccountsPage', () => {
describe('DelegatedAccountsPage', () => {
let component: DelegatedAccountsPage;
let fixture: ComponentFixture<DelegatedAccountsPage>;
let orgUserService: jasmine.SpyObj<OrgUserService>;
let orgService: jasmine.SpyObj<OrgService>;
let router: jasmine.SpyObj<Router>;
let loaderService: jasmine.SpyObj<LoaderService>;
let activatedRoute: jasmine.SpyObj<ActivatedRoute>;
let recentLocalStorageItemsService: jasmine.SpyObj<RecentLocalStorageItemsService>;

beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [DelegatedAccountsPage],
imports: [IonicModule.forRoot()],
}).compileComponents();
beforeEach(waitForAsync(() => {
const orgUserServiceSpy = jasmine.createSpyObj('OrgUserService', [
'switchToDelegator',
'switchToDelegatee',
'findDelegatedAccounts',
'excludeByStatus',
]);
const orgServiceSpy = jasmine.createSpyObj('OrgService', ['getCurrentOrg']);
const routerSpy = jasmine.createSpyObj('Router', ['navigate']);
const loaderServiceSpy = jasmine.createSpyObj('LoaderService', ['hideLoader', 'showLoader']);
const recentLocalStorageItemsServiceSpy = jasmine.createSpyObj('RecentLocalStorageItemsService', [
'clearRecentLocalStorageCache',
]);

fixture = TestBed.createComponent(DelegatedAccountsPage);
component = fixture.componentInstance;
fixture.detectChanges();
})
);
TestBed.configureTestingModule({
declarations: [DelegatedAccountsPage],
imports: [IonicModule.forRoot(), FormsModule],
providers: [
{
provide: ActivatedRoute,
useValue: {
snapshot: {
params: {
switchToOwn: true,
},
},
},
},
{
provide: OrgUserService,
useValue: orgUserServiceSpy,
},
{
provide: OrgService,
useValue: orgServiceSpy,
},
{
provide: Router,
useValue: routerSpy,
},
{
provide: LoaderService,
useValue: loaderServiceSpy,
},
{
provide: RecentLocalStorageItemsService,
useValue: recentLocalStorageItemsServiceSpy,
},
],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
}).compileComponents();

fixture = TestBed.createComponent(DelegatedAccountsPage);
component = fixture.componentInstance;

orgUserService = TestBed.inject(OrgUserService) as jasmine.SpyObj<OrgUserService>;
orgService = TestBed.inject(OrgService) as jasmine.SpyObj<OrgService>;
router = TestBed.inject(Router) as jasmine.SpyObj<Router>;
loaderService = TestBed.inject(LoaderService) as jasmine.SpyObj<LoaderService>;
recentLocalStorageItemsService = TestBed.inject(
RecentLocalStorageItemsService
) as jasmine.SpyObj<RecentLocalStorageItemsService>;
activatedRoute = TestBed.inject(ActivatedRoute) as jasmine.SpyObj<ActivatedRoute>;

fixture.detectChanges();
}));

it('should create', () => {
expect(component).toBeTruthy();
});

it('switchToDelegatee(): should switch delegatee', fakeAsync(() => {
loaderService.showLoader.and.resolveTo();
loaderService.hideLoader.and.resolveTo();
recentLocalStorageItemsService.clearRecentLocalStorageCache.and.returnValue(null);
orgUserService.switchToDelegator.and.returnValue(of(apiEouRes));

component.switchToDelegatee(eouRes2);
tick(500);

expect(loaderService.showLoader).toHaveBeenCalledTimes(1);
expect(loaderService.hideLoader).toHaveBeenCalledTimes(1);
expect(recentLocalStorageItemsService.clearRecentLocalStorageCache).toHaveBeenCalledTimes(1);
expect(orgUserService.switchToDelegator).toHaveBeenCalledOnceWith(eouRes2.ou);
expect(router.navigate).toHaveBeenCalledOnceWith(['/', 'enterprise', 'my_dashboard']);
}));

describe('ionViewWillEnter():', () => {
it('should switch to own account if enabled', fakeAsync(() => {
loaderService.showLoader.and.resolveTo(null);
loaderService.hideLoader.and.resolveTo(null);
recentLocalStorageItemsService.clearRecentLocalStorageCache.and.returnValue(null);
orgUserService.switchToDelegatee.and.returnValue(of(apiEouRes));

component.ionViewWillEnter();
tick(500);

expect(loaderService.showLoader).toHaveBeenCalledTimes(1);
expect(loaderService.hideLoader).toHaveBeenCalledTimes(1);
expect(recentLocalStorageItemsService.clearRecentLocalStorageCache).toHaveBeenCalledTimes(1);
expect(orgUserService.switchToDelegatee).toHaveBeenCalledTimes(1);
expect(router.navigate).toHaveBeenCalledOnceWith(['/', 'enterprise', 'my_dashboard']);
}));

it('should allow user to search and select a delegatee account', fakeAsync(() => {
component.searchDelegatees = getElementRef(fixture, '.delegated--search-input');
const input = component.searchDelegatees.nativeElement as HTMLInputElement;
activatedRoute.snapshot.params.switchToOwn = null;
orgUserService.findDelegatedAccounts.and.returnValue(of([apiEouRes, eouRes2, eouRes3]));
orgService.getCurrentOrg.and.returnValue(of(orgData1[0]));
orgUserService.excludeByStatus.and.returnValue([eouRes2, eouRes3]);

component.ionViewWillEnter();
tick(500);

input.value = '[email protected]';
input.dispatchEvent(new Event('keyup'));
tick(500);

expect(component.delegatedAccList).toEqual([eouRes2, eouRes3]);
expect(orgUserService.findDelegatedAccounts).toHaveBeenCalledTimes(1);
expect(orgService.getCurrentOrg).toHaveBeenCalledTimes(1);
expect(orgUserService.excludeByStatus).toHaveBeenCalledWith([apiEouRes, eouRes2, eouRes3], 'DISABLED');
}));

it('should set delegatee acc list to empty array if no accounts are provided', fakeAsync(() => {
component.searchDelegatees = getElementRef(fixture, '.delegated--search-input');
const input = component.searchDelegatees.nativeElement as HTMLInputElement;
activatedRoute.snapshot.params.switchToOwn = null;
orgUserService.findDelegatedAccounts.and.returnValue(of([]));
orgService.getCurrentOrg.and.returnValue(of(orgData1[0]));
orgUserService.excludeByStatus.and.returnValue(null);

component.ionViewWillEnter();
tick(500);

input.value = '[email protected]';
input.dispatchEvent(new Event('keyup'));
tick(500);

expect(component.delegatedAccList).toEqual([]);
expect(orgUserService.findDelegatedAccounts).toHaveBeenCalledTimes(1);
expect(orgService.getCurrentOrg).toHaveBeenCalledTimes(1);
expect(orgUserService.excludeByStatus).toHaveBeenCalledWith([], 'DISABLED');
}));
});
});
48 changes: 19 additions & 29 deletions src/app/fyle/delegated-accounts/delegated-accounts.page.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { forkJoin, from, fromEvent, throwError } from 'rxjs';
import { OrgUserService } from 'src/app/core/services/org-user.service';
import { Component, ElementRef, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { forkJoin, from, fromEvent } from 'rxjs';
import { concatMap, distinctUntilChanged, finalize, map, shareReplay, startWith, switchMap } from 'rxjs/operators';
import { ExtendedOrgUser } from 'src/app/core/models/extended-org-user.model';
import { LoaderService } from 'src/app/core/services/loader.service';
import {
concatMap,
finalize,
catchError,
map,
startWith,
distinctUntilChanged,
switchMap,
tap,
shareReplay,
} from 'rxjs/operators';
import { globalCacheBusterNotifier } from 'ts-cacheable';
import { RecentLocalStorageItemsService } from 'src/app/core/services/recent-local-storage-items.service';
import { OrgUserService } from 'src/app/core/services/org-user.service';
import { OrgService } from 'src/app/core/services/org.service';
import { RecentLocalStorageItemsService } from 'src/app/core/services/recent-local-storage-items.service';
import { globalCacheBusterNotifier } from 'ts-cacheable';
import { User } from 'src/app/core/models/user.model';

@Component({
selector: 'app-delegated-accounts',
templateUrl: './delegated-accounts.page.html',
styleUrls: ['./delegated-accounts.page.scss'],
})
export class DelegatedAccountsPage implements OnInit {
@ViewChild('searchDelegatees') searchDelegatees: ElementRef;
export class DelegatedAccountsPage {
@ViewChild('searchDelegatees') searchDelegatees: ElementRef<HTMLInputElement>;

delegatedAccList;

Expand All @@ -41,7 +33,7 @@ export class DelegatedAccountsPage implements OnInit {
private recentLocalStorageItemsService: RecentLocalStorageItemsService
) {}

switchToDelegatee(eou) {
switchToDelegatee(eou: ExtendedOrgUser): void {
from(this.loaderService.showLoader('Switching Account'))
.pipe(
concatMap(() => {
Expand All @@ -58,11 +50,9 @@ export class DelegatedAccountsPage implements OnInit {
});
}

ngOnInit() {}

ionViewWillEnter() {
ionViewWillEnter(): void {
this.searchInput = '';
const switchToOwn = this.activatedRoute.snapshot.params.switchToOwn;
const switchToOwn = this.activatedRoute.snapshot.params.switchToOwn as string;

if (switchToOwn) {
from(this.loaderService.showLoader('Switching Account'))
Expand All @@ -87,18 +77,18 @@ export class DelegatedAccountsPage implements OnInit {
this.currentOrg = res.currentOrg;
});

fromEvent(this.searchDelegatees.nativeElement, 'keyup')
fromEvent<{ srcElement: { value: string } }>(this.searchDelegatees.nativeElement, 'keyup')
.pipe(
map((event: any) => event.srcElement.value),
map((event) => event.srcElement.value),
startWith(''),
distinctUntilChanged(),
switchMap((searchText) =>
delegatedAccList$.pipe(
map(({ delegatedAcc }) => this.orgUserService.excludeByStatus(delegatedAcc, 'DISABLED')),
map((delegatees) =>
delegatees?.filter((delegatee) =>
map((delegatees: ExtendedOrgUser[]) =>
delegatees?.filter((delegatee: ExtendedOrgUser) =>
Object.values(delegatee.us).some(
(delegateeProp) =>
(delegateeProp: User) =>
delegateeProp &&
delegateeProp.toString() &&
delegateeProp.toString().toLowerCase().includes(searchText.toLowerCase())
Expand Down

0 comments on commit 7d556ec

Please sign in to comment.