Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: removing unnecessary optionals and increasing branch cov - 1 #2503

Merged
merged 7 commits into from
Oct 16, 2023
51 changes: 51 additions & 0 deletions src/app/core/mock-data/expense-fields-map.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1174,3 +1174,54 @@ export const txnFieldsMap2: Partial<ExpenseFieldsObj> = {
field: 'org_category_id',
},
};

export const txnFieldsFlightData: Partial<ExpenseFieldsObj> = {
...txnFieldsData2,
flight_journey_travel_class: {
id: 2,
created_at: new Date('2018-01-31T23:50:27.221Z'),
created_by: {
user_id: 'SYSTEM',
org_user_id: null,
org_id: null,
roles: [],
scopes: [],
allowed_cidrs: [],
cluster_domain: null,
proxy_org_user_id: null,
tpa_id: null,
tpa_name: null,
name: null,
},
updated_at: new Date('2023-01-13T11:45:00.655Z'),
updated_by: {
user_id: 'usEyHSLj6aHw',
org_user_id: 'ou9KVQTZWIFk',
org_id: 'orNVthTo2Zyo',
roles: ['FYLER', 'ADMIN', 'APPROVER', 'VERIFIER', 'PAYMENT_PROCESSOR', 'FINANCE', 'SUPER_ADMIN', 'HOP'],
scopes: [],
allowed_cidrs: [],
cluster_domain: '"https://staging.fyle.tech"',
proxy_org_user_id: null,
tpa_id: null,
tpa_name: null,
name: 'ou9KVQTZWIFk',
},
org_id: 'orNVthTo2Zyo',
column_name: 'flight_journey_travel_class',
field_name: 'Flight Travel Class\\/',
seq: 1,
type: 'SELECT',
is_custom: false,
is_enabled: true,
is_mandatory: true,
placeholder: 'TEST',
default_value: null,
options: ['economy', 'business'],
org_category_ids: [
17283, 49621, 83060, 87409, 87441, 101148, 101149, 101151, 102676, 102757, 105705, 106574, 110168, 110597, 114563,
],
code: null,
roles_editable: ['FYLER', 'APPROVER', 'TRAVEL_ADMIN', 'VERIFIER', 'PAYMENT_PROCESSOR', 'FINANCE', 'ADMIN'],
},
};
12 changes: 12 additions & 0 deletions src/app/core/mock-data/unflattened-expense.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,18 @@ export const draftUnflattendedTxn3 = {
},
};

export const draftUnflattendedTxn4: UnflattenedTransaction = {
...unflattenedExpData,
tx: {
...unflattenedExpData.tx,
id: null,
source: 'MOBILE',
state: 'DRAFT',
org_category_id: 212690,
fyle_category: null,
},
};

export const unflattenedTxnDataPerDiem = {
tx: {
// TODO: Enum for state and source
Expand Down
148 changes: 133 additions & 15 deletions src/app/core/services/merge-expenses.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ import { projectsV1Data } from '../test-data/projects.spec.data';
import { corporateCardExpenseData } from '../mock-data/corporate-card-expense.data';
import { customInputData } from '../test-data/custom-inputs.spec.data';
import * as dayjs from 'dayjs';
import { orgCategoryData1 } from '../mock-data/org-category.data';
import { expectedOrgCategoryByName2, orgCategoryData1 } from '../mock-data/org-category.data';
import { taxGroupData } from '../mock-data/tax-group.data';

describe('MergeExpensesService', () => {
Expand Down Expand Up @@ -412,6 +412,15 @@ describe('MergeExpensesService', () => {
});
});

it('should return the project options with label as project name if id matches with option', (done) => {
projectService.getAllActive.and.returnValue(of(projectsV1Data));
// @ts-ignore
mergeExpensesService.formatProjectOptions({ ...projectOptionsData, value: 257528 }).subscribe((res) => {
expect(res).toEqual({ label: 'Customer Mapped Project', value: 257528 });
done();
});
});

it('should return the project options when project is not present', (done) => {
projectService.getAllActive.and.returnValue(of([]));
// @ts-ignore
Expand Down Expand Up @@ -447,7 +456,7 @@ describe('MergeExpensesService', () => {
});

describe('getCorporateCardTransactions(): ', () => {
it('should return the corportate card transactions', (done) => {
it('should return the corporate card transactions', (done) => {
const params = {
queryParams: {
group_id: ['in.(,)'],
Expand All @@ -468,6 +477,29 @@ describe('MergeExpensesService', () => {
done();
});
});

it('should return the corporate card transactions if expenses are undefined', (done) => {
const params = {
queryParams: {
group_id: ['in.(,)'],
},
offset: 0,
limit: 1,
};

customInputsService.getAll.withArgs(true).and.returnValue(of(customInputData));
corporateCreditCardExpenseService.getv2CardTransactions
.withArgs(params)
.and.returnValue(of(corporateCardExpenseData));

const mockExpense = [undefined, undefined];
mergeExpensesService.getCorporateCardTransactions(mockExpense).subscribe((res) => {
expect(res).toEqual(corporateCardExpenseData.data);
expect(corporateCreditCardExpenseService.getv2CardTransactions).toHaveBeenCalledOnceWith(params);
expect(customInputsService.getAll).toHaveBeenCalledOnceWith(true);
done();
});
});
});

it('should return empty list if there are no expenses', (done) => {
Expand Down Expand Up @@ -669,6 +701,16 @@ describe('MergeExpensesService', () => {
it('should return null when options are not passed', () => {
expect(mergeExpensesService.getFieldValue(optionsData13)).toBeNull();
});

it('should return null when optionsData is null', () => {
expect(mergeExpensesService.getFieldValue(null)).toBeNull();
});

it('should return undefined if option is empty array', () => {
expect(
mergeExpensesService.getFieldValue({ ...optionsData13, areSameValues: true, options: [] })
).toBeUndefined();
});
});

it('generateLocationOptions(): should return the location options', (done) => {
Expand Down Expand Up @@ -782,12 +824,32 @@ describe('MergeExpensesService', () => {
});
});

it('getCategoryName(): should return the category name', () => {
categoriesService.getAll.and.returnValue(of(orgCategoryData1));
const categoryId = '201952';
mergeExpensesService.getCategoryName(categoryId).subscribe((res) => {
expect(res).toEqual('Food');
expect(categoriesService.getAll).toHaveBeenCalledTimes(1);
describe('getCategoryName():', () => {
it('should return the category name', () => {
categoriesService.getAll.and.returnValue(of(orgCategoryData1));
const categoryId = '201952';
mergeExpensesService.getCategoryName(categoryId).subscribe((res) => {
expect(res).toEqual('Food');
expect(categoriesService.getAll).toHaveBeenCalledTimes(1);
});
});

it('should return undefined if category id does not match with params', () => {
categoriesService.getAll.and.returnValue(of(orgCategoryData1));
const categoryId = '201951';
mergeExpensesService.getCategoryName(categoryId).subscribe((res) => {
expect(res).toEqual(undefined);
expect(categoriesService.getAll).toHaveBeenCalledTimes(1);
});
});

it('should return undefined if category id is undefined', () => {
categoriesService.getAll.and.returnValue(of([expectedOrgCategoryByName2]));
const categoryId = '201951';
mergeExpensesService.getCategoryName(categoryId).subscribe((res) => {
expect(res).toEqual(undefined);
expect(categoriesService.getAll).toHaveBeenCalledTimes(1);
});
});
});

Expand Down Expand Up @@ -815,6 +877,16 @@ describe('MergeExpensesService', () => {
expect(res).toEqual(mergeExpensesOptionData4[0].value);
});

it('should return the field value on change when value is untouched and optionsData is undefined', () => {
const res = mergeExpensesService.getFieldValueOnChange(
undefined,
null,
mergeExpensesOptionData4[0].value,
mergeExpensesOptionData4[0].value
);
expect(res).toEqual(mergeExpensesOptionData4[0].value);
});

it('should return the field value on change when value is touched', () => {
const res = mergeExpensesService.getFieldValueOnChange(
mergeExpensesOptionData4[0],
Expand All @@ -826,13 +898,29 @@ describe('MergeExpensesService', () => {
});
});

it('formatTaxGroupOption(): should return the formatted tax group option', (done) => {
taxGroupService.get.and.returnValue(of(taxGroupData));
// @ts-ignore
mergeExpensesService.formatTaxGroupOption(optionsData11.options[0]).subscribe((res) => {
expect(res).toEqual(mergeExpensesOptionData5[0]);
expect(taxGroupService.get).toHaveBeenCalledTimes(1);
done();
describe('formatTaxGroupOption():', () => {
it('formatTaxGroupOption(): should return the formatted tax group option', (done) => {
taxGroupService.get.and.returnValue(of(taxGroupData));
// @ts-ignore
mergeExpensesService.formatTaxGroupOption(optionsData11.options[0]).subscribe((res) => {
expect(res).toEqual(mergeExpensesOptionData5[0]);
expect(taxGroupService.get).toHaveBeenCalledTimes(1);
done();
});
});

it('formatTaxGroupOption(): should return the formatted tax group option with label as undefined if id does not matches with options', (done) => {
taxGroupService.get.and.returnValue(of(taxGroupData));
const mockOptions = { ...optionsData11.options[0], value: 'tgp6JA6tgoZ1' };
// @ts-ignore
mergeExpensesService.formatTaxGroupOption(mockOptions).subscribe((res) => {
expect(res).toEqual({
label: undefined,
value: 'tgp6JA6tgoZ1',
});
expect(taxGroupService.get).toHaveBeenCalledTimes(1);
done();
});
});
});

Expand Down Expand Up @@ -912,4 +1000,34 @@ describe('MergeExpensesService', () => {
const mockDate = '2021-03-10T05:31:00.000Z';
expect(mergeExpensesService.setFormattedDate(mockDate)).toEqual(dayjs(mockDate).format('MMM DD, YYYY'));
});

describe('formatCategoryOption():', () => {
beforeEach(() => {
categoriesService.getAll.and.returnValue(of(orgCategoryData1));
categoriesService.filterRequired.and.returnValue(orgCategoryData1);
});

it('should return the formatted category option', (done) => {
// @ts-ignore
mergeExpensesService.formatCategoryOption(mergeExpensesOptionData4[0]).subscribe((res) => {
expect(res).toEqual(mergeExpensesOptionData4[0]);
expect(categoriesService.getAll).toHaveBeenCalledTimes(1);
expect(categoriesService.filterRequired).toHaveBeenCalledOnceWith(orgCategoryData1);
done();
});
});

it('should return the formatted category option with label as Unspecified if id does not matches with options', (done) => {
// @ts-ignore
mergeExpensesService.formatCategoryOption({ ...mergeExpensesOptionData4[0], value: 201951 }).subscribe((res) => {
expect(res).toEqual({
label: 'Unspecified',
value: 201951,
});
expect(categoriesService.getAll).toHaveBeenCalledTimes(1);
expect(categoriesService.filterRequired).toHaveBeenCalledOnceWith(orgCategoryData1);
done();
});
});
});
});
10 changes: 5 additions & 5 deletions src/app/core/services/merge-expenses.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class MergeExpensesService {
switchMap(() => {
const CCCGroupIds = expenses.map((expense) => expense?.tx_corporate_credit_card_expense_group_id);

if (CCCGroupIds?.length > 0) {
if (CCCGroupIds.length > 0) {
const queryParams = {
group_id: ['in.(' + CCCGroupIds + ')'],
};
Expand Down Expand Up @@ -297,7 +297,7 @@ export class MergeExpensesService {
return from(expenses).pipe(
filter((expense) => !!expense.tx_project_id),
map((expense) => ({
label: expense.tx_project_id?.toString(),
label: expense.tx_project_id.toString(),
value: expense.tx_project_id,
})),
mergeMap((option) => this.formatProjectOptions(option)),
Expand Down Expand Up @@ -395,7 +395,7 @@ export class MergeExpensesService {
return from(expenses).pipe(
filter((expense) => !!expense.tx_locations[locationIndex]),
map((expense) => ({
label: expense.tx_locations[locationIndex]?.formatted_address,
label: expense.tx_locations[locationIndex].formatted_address,
value: expense.tx_locations[locationIndex],
})),
reduce((acc: MergeExpensesOption<Location>[], curr) => {
Expand Down Expand Up @@ -562,7 +562,7 @@ export class MergeExpensesService {
getCategoryName(categoryId: string): Observable<string> {
return this.categoriesService.getAll().pipe(
map((categories) => {
const category = categories.find((category) => category?.id?.toString() === categoryId);
const category = categories.find((category) => category.id?.toString() === categoryId);
return category?.name;
})
);
Expand Down Expand Up @@ -641,7 +641,7 @@ export class MergeExpensesService {

getFieldValue<T>(optionsData: MergeExpensesOptionsData<T>): T {
if (optionsData?.areSameValues) {
return optionsData?.options[0]?.value;
return optionsData.options[0]?.value;
} else {
return null;
}
Expand Down
Loading
Loading