Skip to content

Commit

Permalink
fix: fixed getCategoryById duplicate calls in add-edit expense page (#…
Browse files Browse the repository at this point in the history
…3056)

* fix: fixed getCategoryById duplicate calls in add-edit expense page

* fixed unit tests
  • Loading branch information
devendrafyle authored Jun 7, 2024
1 parent 18e62ed commit 75e3a03
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 2 additions & 4 deletions src/app/fyle/add-edit-expense/add-edit-expense-3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1067,14 +1067,13 @@ export function TestCases3(getTestBed) {
component.recentlyUsedCategories$ = of(recentUsedCategoriesRes);
component.etxn$ = of(unflattenedPaidExp);
component.initialFetch = true;
categoriesService.getCategoryById.and.returnValue(of(orgCategoryData1[0]));
component.selectedCategory$ = of(orgCategoryData1[0]);

fixture.detectChanges();
component.getCategoryOnEdit(orgCategoryData1[0]).subscribe((res) => {
expect(res).toEqual(orgCategoryPaginated1[0]);
expect(orgUserSettingsService.get).toHaveBeenCalledTimes(1);
expect(orgSettingsService.get).toHaveBeenCalledTimes(1);
expect(categoriesService.getCategoryById).toHaveBeenCalledOnceWith(unflattenedDraftExp.tx.org_category_id);
done();
});
});
Expand All @@ -1089,14 +1088,13 @@ export function TestCases3(getTestBed) {
tx: { ...unflattenedPaidExp.tx, fyle_category: undefined, state: 'DRAFT' },
});
component.initialFetch = true;
categoriesService.getCategoryById.and.returnValue(of(orgCategoryData1[0]));
component.selectedCategory$ = of(orgCategoryData1[0]);

fixture.detectChanges();
component.getCategoryOnEdit(orgCategoryData1[0]).subscribe((res) => {
expect(res).toEqual(orgCategoryPaginated1[0]);
expect(orgUserSettingsService.get).toHaveBeenCalledTimes(1);
expect(orgSettingsService.get).toHaveBeenCalledTimes(1);
expect(categoriesService.getCategoryById).toHaveBeenCalledOnceWith(unflattenedDraftExp.tx.org_category_id);
done();
});
});
Expand Down
8 changes: 5 additions & 3 deletions src/app/fyle/add-edit-expense/add-edit-expense.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ export class AddEditExpensePage implements OnInit {

activeCategories$: Observable<OrgCategory[]>;

selectedCategory$: Observable<OrgCategory>;

constructor(
private activatedRoute: ActivatedRoute,
private accountsService: AccountsService,
Expand Down Expand Up @@ -1655,7 +1657,7 @@ export class AddEditExpensePage implements OnInit {
setupFormInit(): void {
const selectedProject$ = this.getSelectedProjects();

const selectedCategory$ = this.getSelectedCategory();
this.selectedCategory$ = this.getSelectedCategory().pipe(shareReplay(1));

const selectedReport$ = this.getSelectedReport();

Expand All @@ -1682,7 +1684,7 @@ export class AddEditExpensePage implements OnInit {
etxn: this.etxn$,
paymentMode: selectedPaymentMode$,
project: selectedProject$,
category: selectedCategory$,
category: this.selectedCategory$,
report: selectedReport$,
costCenter: selectedCostCenter$,
customExpenseFields: customExpenseFields$,
Expand Down Expand Up @@ -2058,7 +2060,7 @@ export class AddEditExpensePage implements OnInit {
}) => {
const isExpenseCategoryUnspecified = etxn.tx.fyle_category?.toLowerCase() === 'unspecified';
if (this.initialFetch && etxn.tx.org_category_id && !isExpenseCategoryUnspecified) {
return this.categoriesService.getCategoryById(etxn.tx.org_category_id).pipe(
return this.selectedCategory$.pipe(
map((selectedCategory) => ({
orgUserSettings,
orgSettings,
Expand Down

0 comments on commit 75e3a03

Please sign in to comment.