diff --git a/src/app/fyle/add-edit-expense/add-edit-expense-2.spec.ts b/src/app/fyle/add-edit-expense/add-edit-expense-2.spec.ts index 335cf0b4d8..ac9ac1e645 100644 --- a/src/app/fyle/add-edit-expense/add-edit-expense-2.spec.ts +++ b/src/app/fyle/add-edit-expense/add-edit-expense-2.spec.ts @@ -93,6 +93,7 @@ import { FileObject } from 'src/app/core/models/file-obj.model'; import { CustomField } from 'src/app/core/models/custom_field.model'; import { ExpensesService } from 'src/app/core/services/platform/v1/spender/expenses.service'; import { expenseDuplicateSet2 } from 'src/app/core/mock-data/platform/v1/expense-duplicate-sets.data'; +import { cloneDeep } from 'lodash'; const properties = { cssClass: 'fy-modal', @@ -459,9 +460,7 @@ export function TestCases2(getTestBed) { expect(categoriesService.getCategoryByName).toHaveBeenCalledOnceWith( unflattenedTxnWithExtractedData.tx.extracted_data.category ); - expect(dateService.getUTCDate).toHaveBeenCalledWith( - new Date(unflattenedTxnWithExtractedData.tx.extracted_data.date) - ); + expect(dateService.getUTCDate).not.toHaveBeenCalled(); expect(component.isIncompleteExpense).toBeTrue(); done(); }); @@ -484,7 +483,26 @@ export function TestCases2(getTestBed) { component.getEditExpenseObservable().subscribe((res) => { expect(res).toEqual(unflattenedTxnWithExtractedData2); expect(transactionService.getETxnUnflattened).toHaveBeenCalledTimes(1); - expect(dateService.getUTCDate).toHaveBeenCalledTimes(2); + expect(dateService.getUTCDate).not.toHaveBeenCalled(); + done(); + }); + }); + + it('should update txn date with extracted date if txn date is not defined in original expense', (done) => { + const mockedTxn = cloneDeep(unflattenedTxnWithExtractedData2); + const extractedDate = new Date('2023-01-24'); + + mockedTxn.tx.txn_dt = null; + mockedTxn.tx.extracted_data.invoice_dt = null; + mockedTxn.tx.extracted_data.date = extractedDate; + + transactionService.getETxnUnflattened.and.returnValue(of(mockedTxn)); + dateService.getUTCDate.and.returnValue(extractedDate); + + component.getEditExpenseObservable().subscribe((res) => { + expect(res).toEqual(mockedTxn); + expect(dateService.getUTCDate).toHaveBeenCalledOnceWith(mockedTxn.tx.extracted_data.date); + expect(mockedTxn.tx.txn_dt).toEqual(extractedDate); done(); }); }); diff --git a/src/app/fyle/add-edit-expense/add-edit-expense.page.ts b/src/app/fyle/add-edit-expense/add-edit-expense.page.ts index caa7c64a4c..986baf1c6b 100644 --- a/src/app/fyle/add-edit-expense/add-edit-expense.page.ts +++ b/src/app/fyle/add-edit-expense/add-edit-expense.page.ts @@ -2562,11 +2562,11 @@ export class AddEditExpensePage implements OnInit { etxn.tx.currency = etxn.tx.extracted_data.currency; } - if (etxn.tx.extracted_data.date) { + if (etxn.tx.extracted_data.date && !etxn.tx.txn_dt) { etxn.tx.txn_dt = this.dateService.getUTCDate(new Date(etxn.tx.extracted_data.date)); } - if (etxn.tx.extracted_data.invoice_dt) { + if (etxn.tx.extracted_data.invoice_dt && !etxn.tx.txn_dt) { etxn.tx.txn_dt = this.dateService.getUTCDate(new Date(etxn.tx.extracted_data.invoice_dt)); }