Skip to content

Commit

Permalink
test: some branches in view per diem page (#2524)
Browse files Browse the repository at this point in the history
* test: some branches in view per diem page

* minor
  • Loading branch information
suyashpatil78 authored Oct 18, 2023
1 parent 8b0ee2d commit bf50817
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 34 deletions.
57 changes: 53 additions & 4 deletions src/app/fyle/view-per-diem/view-per-diem.page.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ describe('ViewPerDiemPage', () => {
it('should get policy details for team expenses', () => {
component.view = ExpenseView.team;
policyService.getApproverExpensePolicyViolations.and.returnValue(
of(ApproverExpensePolicyStatesData.data[0].individual_desired_states),
of(ApproverExpensePolicyStatesData.data[0].individual_desired_states)
);
component.getPolicyDetails('txRNWeQRXhso');
expect(policyService.getApproverExpensePolicyViolations).toHaveBeenCalledOnceWith('txRNWeQRXhso');
Expand All @@ -211,7 +211,7 @@ describe('ViewPerDiemPage', () => {
it('should get policy details for individual expenses', () => {
component.view = ExpenseView.individual;
policyService.getSpenderExpensePolicyViolations.and.returnValue(
of(expensePolicyStatesData.data[0].individual_desired_states),
of(expensePolicyStatesData.data[0].individual_desired_states)
);
component.getPolicyDetails('txVTmNOp5JEa');
expect(policyService.getSpenderExpensePolicyViolations).toHaveBeenCalledOnceWith('txVTmNOp5JEa');
Expand Down Expand Up @@ -297,7 +297,7 @@ describe('ViewPerDiemPage', () => {
.pipe(
finalize(() => {
expect(loaderService.hideLoader).toHaveBeenCalledTimes(1);
}),
})
)
.subscribe((extendedPerDiem) => {
expect(transactionService.getExpenseV2).toHaveBeenCalledOnceWith('tx3qwe4ty');
Expand Down Expand Up @@ -327,6 +327,48 @@ describe('ViewPerDiemPage', () => {
});
});

it('should call dependentFieldsService.getDependentFieldValuesForBaseField with undefined if "txnFields.project_id" and "txnFields.cost_center_id" is array containing undefined', fakeAsync(() => {
expenseFieldsService.getAllMap.and.returnValue(
of({ ...expenseFieldsMapResponse4, project_id: [undefined], cost_center_id: [undefined] })
);
component.ionViewWillEnter();
tick(100);

component.projectDependentCustomProperties$.subscribe((projectDependentCustomProperties) => {
expect(dependentFieldsService.getDependentFieldValuesForBaseField).toHaveBeenCalledOnceWith(
expenseData1.tx_custom_properties,
undefined
);
expect(projectDependentCustomProperties).toEqual(customInputData1);
});

component.costCenterDependentCustomProperties$.subscribe((costCenterDependentCustomProperties) => {
expect(dependentFieldsService.getDependentFieldValuesForBaseField).not.toHaveBeenCalledOnceWith(
expenseData1.tx_custom_properties,
undefined
);
expect(costCenterDependentCustomProperties).toEqual(customInputData1);
});
}));

it('should set projectDependentCustomProperties$ and costCenterDependentCustomProperties$ to undefined if "txnFields.project_id" and "txnFields.cost_center_id" is undefined', fakeAsync(() => {
expenseFieldsService.getAllMap.and.returnValue(
of({ ...expenseFieldsMapResponse4, project_id: undefined, cost_center_id: undefined })
);
component.ionViewWillEnter();
tick(100);

component.projectDependentCustomProperties$.subscribe((projectDependentCustomProperties) => {
expect(dependentFieldsService.getDependentFieldValuesForBaseField).not.toHaveBeenCalled();
expect(projectDependentCustomProperties).toEqual(undefined);
});

component.costCenterDependentCustomProperties$.subscribe((costCenterDependentCustomProperties) => {
expect(dependentFieldsService.getDependentFieldValuesForBaseField).not.toHaveBeenCalled();
expect(costCenterDependentCustomProperties).toEqual(undefined);
});
}));

it('should set paymentMode and paymentMode icon correctly if account type is ADVANCE', fakeAsync(() => {
const mockExpense = cloneDeep(expenseData1);
mockExpense.source_account_type = AccountType.ADVANCE;
Expand Down Expand Up @@ -373,6 +415,13 @@ describe('ViewPerDiemPage', () => {
expect(component.isProjectShown).toBeFalse();
}));

it('should set isProjectShown to undefined if orgSettings is undefined', fakeAsync(() => {
orgSettingsService.get.and.returnValue(of(undefined));
component.ionViewWillEnter();
tick(100);
expect(component.isProjectShown).toBeUndefined();
}));

it('should set orgSettings and isNewReportsFlowEnabled', fakeAsync(() => {
component.ionViewWillEnter();
tick(100);
Expand All @@ -391,7 +440,7 @@ describe('ViewPerDiemPage', () => {
expect(customInputsService.fillCustomProperties).toHaveBeenCalledOnceWith(
expenseData1.tx_org_category_id,
expenseData1.tx_custom_properties,
true,
true
);
// Called twice because of the two custom fields
expect(customInputsService.getCustomPropertyDisplayValue).toHaveBeenCalledTimes(2);
Expand Down
57 changes: 27 additions & 30 deletions src/app/fyle/view-per-diem/view-per-diem.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class ViewPerDiemPage {
private trackingService: TrackingService,
private expenseFieldsService: ExpenseFieldsService,
private orgSettingsService: OrgSettingsService,
private dependentFieldsService: DependentFieldsService,
private dependentFieldsService: DependentFieldsService
) {}

get ExpenseView(): typeof ExpenseView {
Expand Down Expand Up @@ -172,10 +172,10 @@ export class ViewPerDiemPage {

this.extendedPerDiem$ = this.updateFlag$.pipe(
switchMap(() =>
from(this.loaderService.showLoader()).pipe(switchMap(() => this.transactionService.getExpenseV2(id))),
from(this.loaderService.showLoader()).pipe(switchMap(() => this.transactionService.getExpenseV2(id)))
),
finalize(() => from(this.loaderService.hideLoader())),
shareReplay(1),
shareReplay(1)
);

this.txnFields$ = this.expenseFieldsService.getAllMap().pipe(shareReplay(1));
Expand All @@ -185,31 +185,30 @@ export class ViewPerDiemPage {
txnFields: this.txnFields$.pipe(take(1)),
}).pipe(
filter(
({ extendedPerDiem, txnFields }) => extendedPerDiem.tx_custom_properties && txnFields.project_id?.length > 0,
({ extendedPerDiem, txnFields }) => extendedPerDiem.tx_custom_properties && txnFields.project_id?.length > 0
),
switchMap(({ extendedPerDiem, txnFields }) =>
this.dependentFieldsService.getDependentFieldValuesForBaseField(
extendedPerDiem.tx_custom_properties,
txnFields.project_id[0]?.id,
),
),
txnFields.project_id[0]?.id
)
)
);

this.costCenterDependentCustomProperties$ = forkJoin({
extendedPerDiem: this.extendedPerDiem$.pipe(take(1)),
txnFields: this.txnFields$.pipe(take(1)),
}).pipe(
filter(
({ extendedPerDiem, txnFields }) =>
extendedPerDiem.tx_custom_properties && txnFields.cost_center_id?.length > 0,
({ extendedPerDiem, txnFields }) => extendedPerDiem.tx_custom_properties && txnFields.cost_center_id?.length > 0
),
switchMap(({ extendedPerDiem, txnFields }) =>
this.dependentFieldsService.getDependentFieldValuesForBaseField(
extendedPerDiem.tx_custom_properties,
txnFields.cost_center_id[0]?.id,
),
txnFields.cost_center_id[0]?.id
)
),
shareReplay(1),
shareReplay(1)
);

this.extendedPerDiem$.subscribe((extendedPerDiem) => {
Expand All @@ -232,11 +231,11 @@ export class ViewPerDiemPage {
forkJoin([this.txnFields$, this.extendedPerDiem$.pipe(take(1))])
.pipe(
map(([expenseFieldsMap, extendedPerDiem]) => {
this.projectFieldName = expenseFieldsMap?.project_id && expenseFieldsMap?.project_id[0]?.field_name;
const isProjectMandatory = expenseFieldsMap?.project_id && expenseFieldsMap?.project_id[0]?.is_mandatory;
this.projectFieldName = expenseFieldsMap?.project_id && expenseFieldsMap.project_id[0]?.field_name;
const isProjectMandatory = expenseFieldsMap?.project_id && expenseFieldsMap.project_id[0]?.is_mandatory;
this.isProjectShown =
this.orgSettings?.projects?.enabled && (!!extendedPerDiem.tx_project_name || isProjectMandatory);
}),
})
)
.subscribe(noop);

Expand All @@ -250,45 +249,43 @@ export class ViewPerDiemPage {

this.perDiemCustomFields$ = this.extendedPerDiem$.pipe(
switchMap((res) =>
this.customInputsService.fillCustomProperties(res.tx_org_category_id, res.tx_custom_properties, true),
this.customInputsService.fillCustomProperties(res.tx_org_category_id, res.tx_custom_properties, true)
),
map((res) =>
res.map((customProperties) => {
customProperties.displayValue = this.customInputsService.getCustomPropertyDisplayValue(customProperties);
return customProperties;
}),
),
})
)
);

this.perDiemRate$ = this.extendedPerDiem$.pipe(
switchMap((res) => {
const perDiemRateId = parseInt(res.tx_per_diem_rate_id, 10);
return this.perDiemService.getRate(perDiemRateId);
}),
})
);

this.view = this.activatedRoute.snapshot.params.view as ExpenseView;

this.canFlagOrUnflag$ = this.extendedPerDiem$.pipe(
filter(() => this.view === ExpenseView.team),
map(
(etxn) =>
['COMPLETE', 'POLICY_APPROVED', 'APPROVER_PENDING', 'APPROVED', 'PAYMENT_PENDING'].indexOf(etxn.tx_state) >
-1,
),
map((etxn) =>
['COMPLETE', 'POLICY_APPROVED', 'APPROVER_PENDING', 'APPROVED', 'PAYMENT_PENDING'].includes(etxn.tx_state)
)
);

this.canDelete$ = this.extendedPerDiem$.pipe(
filter(() => this.view === ExpenseView.team),
switchMap((etxn) =>
this.reportService.getTeamReport(etxn.tx_report_id).pipe(map((report) => ({ report, etxn }))),
this.reportService.getTeamReport(etxn.tx_report_id).pipe(map((report) => ({ report, etxn })))
),
map(({ report, etxn }) => {
if (report.rp_num_transactions === 1) {
return false;
}
return ['PAYMENT_PENDING', 'PAYMENT_PROCESSING', 'PAID'].indexOf(etxn.tx_state) < 0;
}),
})
);

if (id) {
Expand All @@ -303,13 +300,13 @@ export class ViewPerDiemPage {
this.comments$ = this.statusService.find('transactions', id);

this.isCriticalPolicyViolated$ = this.extendedPerDiem$.pipe(
map((res) => this.isNumber(res.tx_policy_amount) && res.tx_policy_amount < 0.0001),
map((res) => this.isNumber(res.tx_policy_amount) && res.tx_policy_amount < 0.0001)
);

this.getPolicyDetails(id);

this.isAmountCapped$ = this.extendedPerDiem$.pipe(
map((res) => this.isNumber(res.tx_admin_amount) || this.isNumber(res.tx_policy_amount)),
map((res) => this.isNumber(res.tx_admin_amount) || this.isNumber(res.tx_policy_amount))
);

this.extendedPerDiem$.subscribe((etxn) => {
Expand Down Expand Up @@ -384,12 +381,12 @@ export class ViewPerDiemPage {
concatMap(() =>
etxn.tx_manual_flag
? this.transactionService.manualUnflag(etxn.tx_id)
: this.transactionService.manualFlag(etxn.tx_id),
: this.transactionService.manualFlag(etxn.tx_id)
),
finalize(() => {
this.updateFlag$.next(null);
this.loaderService.hideLoader();
}),
})
)
.subscribe(noop);
}
Expand Down

0 comments on commit bf50817

Please sign in to comment.