Skip to content

Commit

Permalink
test: scrollInputIntoView method coverage in all pages (#2484)
Browse files Browse the repository at this point in the history
* test: coverage of scrollInputIntoView method

* minor
  • Loading branch information
suyashpatil78 authored Oct 13, 2023
1 parent 0c4e7b5 commit b625a93
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 173 deletions.
72 changes: 46 additions & 26 deletions src/app/fyle/add-edit-expense/add-edit-expense-1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ export function TestCases1(getTestBed) {
popupService = TestBed.inject(PopupService) as jasmine.SpyObj<PopupService>;
navController = TestBed.inject(NavController) as jasmine.SpyObj<NavController>;
corporateCreditCardExpenseService = TestBed.inject(
CorporateCreditCardExpenseService,
CorporateCreditCardExpenseService
) as jasmine.SpyObj<CorporateCreditCardExpenseService>;
trackingService = TestBed.inject(TrackingService) as jasmine.SpyObj<TrackingService>;
recentLocalStorageItemsService = TestBed.inject(
RecentLocalStorageItemsService,
RecentLocalStorageItemsService
) as jasmine.SpyObj<RecentLocalStorageItemsService>;
recentlyUsedItemsService = TestBed.inject(RecentlyUsedItemsService) as jasmine.SpyObj<RecentlyUsedItemsService>;
tokenService = TestBed.inject(TokenService) as jasmine.SpyObj<TokenService>;
Expand Down Expand Up @@ -215,6 +215,26 @@ export function TestCases1(getTestBed) {
expect(component).toBeTruthy();
});

it('scrollInputIntoView(): should scroll input into view on keydown', () => {
const inputElement = document.createElement('input');
spyOn(inputElement, 'scrollIntoView');
spyOn(component, 'getActiveElement').and.returnValue(inputElement);

component.scrollInputIntoView();

expect(inputElement.scrollIntoView).toHaveBeenCalledOnceWith({
block: 'center',
});

expect(component.getActiveElement).toHaveBeenCalledTimes(1);
});

it('getActiveElement(): should return active element in DOM', () => {
const result = component.getActiveElement();

expect(result).toEqual(document.activeElement);
});

describe('goBack():', () => {
it('should go back to the report if redirected from the report page', () => {
component.isRedirectedFromReport = true;
Expand Down Expand Up @@ -586,7 +606,7 @@ export function TestCases1(getTestBed) {
expect(transactionService.delete).toHaveBeenCalledOnceWith(expenseData1.tx_id);
expect(trackingService.deleteExpense).toHaveBeenCalledOnceWith({ Type: 'Marked Personal' });
expect(corporateCreditCardExpenseService.markPersonal).toHaveBeenCalledOnceWith(
component.corporateCreditCardExpenseGroupId,
component.corporateCreditCardExpenseGroupId
);
done();
});
Expand Down Expand Up @@ -615,7 +635,7 @@ export function TestCases1(getTestBed) {
expect(transactionService.delete).toHaveBeenCalledOnceWith(expenseData1.tx_id);
expect(trackingService.deleteExpense).toHaveBeenCalledOnceWith({ Type: 'Dismiss as Card Payment' });
expect(corporateCreditCardExpenseService.dismissCreditTransaction).toHaveBeenCalledOnceWith(
expenseData1.tx_corporate_credit_card_expense_group_id,
expenseData1.tx_corporate_credit_card_expense_group_id
);
done();
});
Expand Down Expand Up @@ -645,7 +665,7 @@ export function TestCases1(getTestBed) {
result.componentProps.deleteMethod().subscribe((res) => {
expect(res).toEqual(UndoMergeData2);
expect(transactionService.removeCorporateCardExpense).toHaveBeenCalledOnceWith(
activatedRoute.snapshot.params.id,
activatedRoute.snapshot.params.id
);
done();
});
Expand Down Expand Up @@ -675,14 +695,14 @@ export function TestCases1(getTestBed) {
expect(transactionService.getRemoveCardExpenseDialogBody).toHaveBeenCalledTimes(1);
expect(component.getRemoveCCCExpModalParams).toHaveBeenCalledOnceWith(header, body, ctaText, ctaLoadingText);
expect(popoverController.create).toHaveBeenCalledOnceWith(
component.getRemoveCCCExpModalParams(header, body, ctaText, ctaLoadingText),
component.getRemoveCCCExpModalParams(header, body, ctaText, ctaLoadingText)
);
expect(trackingService.unlinkCorporateCardExpense).toHaveBeenCalledTimes(1),
expect(component.goBack).toHaveBeenCalledOnceWith();
expect(component.showSnackBarToast).toHaveBeenCalledOnceWith(
{ message: 'Successfully removed the card details from the expense.' },
'information',
['msb-info'],
['msb-info']
);
expect(trackingService.showToastMessage).toHaveBeenCalledOnceWith({
ToastContent: 'Successfully removed the card details from the expense.',
Expand Down Expand Up @@ -712,7 +732,7 @@ export function TestCases1(getTestBed) {
expect(transactionService.getRemoveCardExpenseDialogBody).toHaveBeenCalledTimes(1);
expect(component.getRemoveCCCExpModalParams).toHaveBeenCalledOnceWith(header, body, ctaText, ctaLoadingText);
expect(popoverController.create).toHaveBeenCalledOnceWith(
component.getRemoveCCCExpModalParams(header, body, ctaText, ctaLoadingText),
component.getRemoveCCCExpModalParams(header, body, ctaText, ctaLoadingText)
);
expect(trackingService.unlinkCorporateCardExpense).toHaveBeenCalledTimes(1);

Expand All @@ -725,7 +745,7 @@ export function TestCases1(getTestBed) {
expect(component.showSnackBarToast).toHaveBeenCalledOnceWith(
{ message: 'Successfully removed the card details from the expense.' },
'information',
['msb-info'],
['msb-info']
);
expect(trackingService.showToastMessage).toHaveBeenCalledOnceWith({
ToastContent: 'Successfully removed the card details from the expense.',
Expand Down Expand Up @@ -755,7 +775,7 @@ export function TestCases1(getTestBed) {
expect(transactionService.getRemoveCardExpenseDialogBody).toHaveBeenCalledTimes(1);
expect(component.getRemoveCCCExpModalParams).toHaveBeenCalledOnceWith(header, body, ctaText, ctaLoadingText);
expect(popoverController.create).toHaveBeenCalledOnceWith(
component.getRemoveCCCExpModalParams(header, body, ctaText, ctaLoadingText),
component.getRemoveCCCExpModalParams(header, body, ctaText, ctaLoadingText)
);
expect(trackingService.unlinkCorporateCardExpense).not.toHaveBeenCalled();
expect(component.showSnackBarToast).not.toHaveBeenCalled();
Expand Down Expand Up @@ -784,13 +804,13 @@ export function TestCases1(getTestBed) {
expect(transactionService.getRemoveCardExpenseDialogBody).toHaveBeenCalledTimes(1);
expect(component.getRemoveCCCExpModalParams).toHaveBeenCalledOnceWith(header, body, ctaText, ctaLoadingText);
expect(popoverController.create).toHaveBeenCalledOnceWith(
component.getRemoveCCCExpModalParams(header, body, ctaText, ctaLoadingText),
component.getRemoveCCCExpModalParams(header, body, ctaText, ctaLoadingText)
);
expect(trackingService.unlinkCorporateCardExpense).toHaveBeenCalledTimes(1);
expect(component.showSnackBarToast).toHaveBeenCalledOnceWith(
{ message: 'Successfully removed the card details from the expense.' },
'information',
['msb-info'],
['msb-info']
);
expect(trackingService.showToastMessage).toHaveBeenCalledOnceWith({
ToastContent: 'Successfully removed the card details from the expense.',
Expand All @@ -815,7 +835,7 @@ export function TestCases1(getTestBed) {
tick(500);

expect(popoverController.create).toHaveBeenCalledOnceWith(
component.getMarkDismissModalParams(getMarkDismissModalParamsData1, true),
component.getMarkDismissModalParams(getMarkDismissModalParamsData1, true)
);
expect(router.navigate).toHaveBeenCalledOnceWith(['/', 'enterprise', 'my_expenses']);
expect(component.showSnackBarToast).toHaveBeenCalledOnceWith({ message: 'Dismissed expense' }, 'information', [
Expand All @@ -841,13 +861,13 @@ export function TestCases1(getTestBed) {
tick(500);

expect(popoverController.create).toHaveBeenCalledOnceWith(
component.getMarkDismissModalParams(getMarkDismissModalParamsData2, true),
component.getMarkDismissModalParams(getMarkDismissModalParamsData2, true)
);
expect(router.navigate).toHaveBeenCalledOnceWith(['/', 'enterprise', 'my_expenses']);
expect(component.showSnackBarToast).toHaveBeenCalledOnceWith(
{ message: 'Marked expense as Personal' },
'information',
['msb-info'],
['msb-info']
);
expect(trackingService.showToastMessage).toHaveBeenCalledOnceWith({
ToastContent: 'Marked expense as Personal',
Expand All @@ -871,13 +891,13 @@ export function TestCases1(getTestBed) {
tick(500);

expect(popoverController.create).toHaveBeenCalledOnceWith(
component.getMarkDismissModalParams(getMarkDismissModalParamsData2, true),
component.getMarkDismissModalParams(getMarkDismissModalParamsData2, true)
);
expect(router.navigate).toHaveBeenCalledOnceWith(['/', 'enterprise', 'my_expenses']);
expect(component.showSnackBarToast).toHaveBeenCalledOnceWith(
{ message: 'Marked expense as Personal' },
'information',
['msb-info'],
['msb-info']
);
expect(trackingService.showToastMessage).toHaveBeenCalledOnceWith({
ToastContent: 'Marked expense as Personal',
Expand Down Expand Up @@ -972,7 +992,7 @@ export function TestCases1(getTestBed) {
of({
...orgSettingsData,
expense_settings: { ...orgSettingsData.expense_settings, split_expense_settings: { enabled: true } },
}),
})
);
component.costCenters$ = of(expectedCCdata);
projectsService.getAllActive.and.returnValue(of(projectsV1Data));
Expand Down Expand Up @@ -1001,7 +1021,7 @@ export function TestCases1(getTestBed) {
expect(projectsService.getAllActive).toHaveBeenCalledTimes(1);
expect(launchDarklyService.getVariation).toHaveBeenCalledOnceWith(
'show_project_mapped_categories_in_split_expense',
false,
false
);
});

Expand All @@ -1025,7 +1045,7 @@ export function TestCases1(getTestBed) {
of({
...orgSettingsData,
expense_settings: { ...orgSettingsData.expense_settings, split_expense_settings: { enabled: false } },
}),
})
);
component.costCenters$ = of(expectedCCdata);
projectsService.getAllActive.and.returnValue(of(projectsV1Data));
Expand All @@ -1047,7 +1067,7 @@ export function TestCases1(getTestBed) {
expect(projectsService.getAllActive).toHaveBeenCalledTimes(1);
expect(launchDarklyService.getVariation).toHaveBeenCalledOnceWith(
'show_project_mapped_categories_in_split_expense',
false,
false
);
done();
});
Expand All @@ -1058,7 +1078,7 @@ export function TestCases1(getTestBed) {
of({
...orgSettingsData,
expense_settings: { ...orgSettingsData.expense_settings, split_expense_settings: { enabled: false } },
}),
})
);
component.costCenters$ = of(expectedCCdata);
projectsService.getAllActive.and.returnValue(of(projectsV1Data));
Expand All @@ -1078,7 +1098,7 @@ export function TestCases1(getTestBed) {
expect(projectsService.getAllActive).toHaveBeenCalledTimes(1);
expect(launchDarklyService.getVariation).toHaveBeenCalledOnceWith(
'show_project_mapped_categories_in_split_expense',
false,
false
);
done();
});
Expand All @@ -1089,7 +1109,7 @@ export function TestCases1(getTestBed) {
of({
...orgSettingsData,
expense_settings: { ...orgSettingsData.expense_settings, split_expense_settings: { enabled: false } },
}),
})
);
component.costCenters$ = of(expectedCCdata);
projectsService.getAllActive.and.returnValue(of(projectsV1Data));
Expand All @@ -1109,7 +1129,7 @@ export function TestCases1(getTestBed) {
expect(projectsService.getAllActive).toHaveBeenCalledTimes(1);
expect(launchDarklyService.getVariation).toHaveBeenCalledOnceWith(
'show_project_mapped_categories_in_split_expense',
false,
false
);
done();
});
Expand Down Expand Up @@ -1158,7 +1178,7 @@ export function TestCases1(getTestBed) {
it('should return empty array if cost centers are not enabled', (done) => {
component.orgUserSettings$ = of(orgUserSettingsData);
orgSettingsService.get.and.returnValue(
of({ ...orgSettingsRes, cost_centers: { ...orgSettingsRes.cost_centers, enabled: false } }),
of({ ...orgSettingsRes, cost_centers: { ...orgSettingsRes.cost_centers, enabled: false } })
);
orgUserSettingsService.getAllowedCostCenters.and.returnValue(of(costCenterApiRes1));
fixture.detectChanges();
Expand Down
6 changes: 5 additions & 1 deletion src/app/fyle/add-edit-expense/add-edit-expense.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,14 +475,18 @@ export class AddEditExpensePage implements OnInit {

@HostListener('keydown')
scrollInputIntoView(): void {
const el = document.activeElement;
const el = this.getActiveElement();
if (el && el instanceof HTMLInputElement) {
el.scrollIntoView({
block: 'center',
});
}
}

getActiveElement(): Element {
return document.activeElement;
}

getFormValues(): FormValue {
return this.fg.value as FormValue;
}
Expand Down
20 changes: 20 additions & 0 deletions src/app/fyle/add-edit-mileage/add-edit-mileage-1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,26 @@ export function TestCases1(getTestBed) {
expect(component.isExpandedView).toBeFalse();
});

it('scrollInputIntoView(): should scroll input into view on keydown', () => {
const inputElement = document.createElement('input');
spyOn(inputElement, 'scrollIntoView');
spyOn(component, 'getActiveElement').and.returnValue(inputElement);

component.scrollInputIntoView();

expect(inputElement.scrollIntoView).toHaveBeenCalledOnceWith({
block: 'center',
});

expect(component.getActiveElement).toHaveBeenCalledTimes(1);
});

it('getActiveElement(): should return active element in DOM', () => {
const result = component.getActiveElement();

expect(result).toEqual(document.activeElement);
});

describe('openCommentsModal():', () => {
it('should add comment to the expense and track the event', fakeAsync(() => {
modalProperties.getModalDefaultProperties.and.returnValue(properties);
Expand Down
6 changes: 5 additions & 1 deletion src/app/fyle/add-edit-mileage/add-edit-mileage.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,18 @@ export class AddEditMileagePage implements OnInit {

@HostListener('keydown')
scrollInputIntoView(): void {
const el = document.activeElement;
const el = this.getActiveElement();
if (el && el instanceof HTMLInputElement) {
el.scrollIntoView({
block: 'center',
});
}
}

getActiveElement(): Element {
return document.activeElement;
}

getFormValues(): Partial<FormValue> {
return this.fg.value as Partial<FormValue>;
}
Expand Down
32 changes: 26 additions & 6 deletions src/app/fyle/add-edit-per-diem/add-edit-per-diem-1.page.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,26 @@ export function TestCases1(getTestBed) {
expect(component).toBeTruthy();
});

it('scrollInputIntoView(): should scroll input into view on keydown', () => {
const inputElement = document.createElement('input');
spyOn(inputElement, 'scrollIntoView');
spyOn(component, 'getActiveElement').and.returnValue(inputElement);

component.scrollInputIntoView();

expect(inputElement.scrollIntoView).toHaveBeenCalledOnceWith({
block: 'center',
});

expect(component.getActiveElement).toHaveBeenCalledTimes(1);
});

it('getActiveElement(): should return active element in DOM', () => {
const result = component.getActiveElement();

expect(result).toEqual(document.activeElement);
});

describe('ngOnInit()', () => {
it('should set isRedirectedFromReport and canRemoveFromReport to true if remove_from_report is true', () => {
activatedRoute.snapshot.params = {
Expand Down Expand Up @@ -504,7 +524,7 @@ export function TestCases1(getTestBed) {
of({
defaultPerDiemCategory: perDiemCategory,
perDiemCategories: [perDiemCategory],
}),
})
);
const mockTxnFieldData = cloneDeep(txnFieldsData2);
expenseFieldsService.filterByOrgCategoryId.and.returnValue(of(mockTxnFieldData));
Expand All @@ -514,7 +534,7 @@ export function TestCases1(getTestBed) {
expect(expenseFieldsService.filterByOrgCategoryId).toHaveBeenCalledOnceWith(
expenseFieldsMapResponse,
fields,
perDiemCategory,
perDiemCategory
);
expect(res).toEqual(mockTxnFieldData);
done();
Expand All @@ -529,7 +549,7 @@ export function TestCases1(getTestBed) {
of({
defaultPerDiemCategory: perDiemCategory,
perDiemCategories: [perDiemCategory],
}),
})
);
expenseFieldsService.filterByOrgCategoryId.and.returnValue(of(mockTxnFieldData));
expenseFieldsService.getDefaultTxnFieldValues.and.returnValue(defaultTxnFieldValuesData2);
Expand All @@ -547,7 +567,7 @@ export function TestCases1(getTestBed) {
expect(expenseFieldsService.filterByOrgCategoryId).toHaveBeenCalledOnceWith(
expenseFieldsMapResponse,
fields,
perDiemCategory,
perDiemCategory
);
expect(expenseFieldsService.getDefaultTxnFieldValues).toHaveBeenCalledOnceWith(mockTxnFieldData);
expect(component.fg.controls.costCenter.value).toEqual(15818);
Expand All @@ -559,7 +579,7 @@ export function TestCases1(getTestBed) {
accountsService.getEMyAccounts.and.returnValue(of(accountsData));
orgSettingsService.get.and.returnValue(of(orgSettingsCCCDisabled));
orgUserSettingsService.getAllowedPaymentModes.and.returnValue(
of([AccountType.PERSONAL, AccountType.CCC, AccountType.COMPANY]),
of([AccountType.PERSONAL, AccountType.CCC, AccountType.COMPANY])
);
paymentModesService.checkIfPaymentModeConfigurationsIsEnabled.and.returnValue(of(true));
accountsService.getPaymentModes.and.returnValue(paymentModesData);
Expand All @@ -575,7 +595,7 @@ export function TestCases1(getTestBed) {
expect(accountsService.getPaymentModes).toHaveBeenCalledOnceWith(
accountsData,
[AccountType.PERSONAL, AccountType.CCC, AccountType.COMPANY],
paymentModesConfig,
paymentModesConfig
);
done();
});
Expand Down
Loading

0 comments on commit b625a93

Please sign in to comment.