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

fix: Fix tests for tasks page stats changes [2] #2928

Merged
merged 21 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/app/core/mock-data/report-stats.data.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { ReportsStatsResponsePlatform } from '../models/platform/v1/report-stats-response.model';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefix the word Platform. Reason - We prefix the word in case of file names. So helps in maintaining consistency.

import { ReportStats } from '../models/report-stats.model';

export const expectedReportStats: ReportStats = {
draft: {
count: 6,
count: 2,
total_amount: 93165.91,
failed_amount: null,
failed_count: null,
processing_amount: null,
processing_count: null,
reimbursable_amount: null,
total_amount: 93165.91,
},
report: {
count: 45,
count: 2,
total_amount: 5177243929.65219,
failed_amount: null,
failed_count: null,
Expand Down Expand Up @@ -95,3 +96,13 @@ export const expectedEmptyReportStats: ReportStats = {
reimbursable_amount: null,
},
};

export const expectedSentBackResponse: ReportsStatsResponsePlatform = {
total_amount: 4500,
count: 2,
failed_amount: null,
failed_count: null,
processing_amount: null,
processing_count: null,
reimbursable_amount: null,
};
14 changes: 7 additions & 7 deletions src/app/core/mock-data/task.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ export const teamReportTaskSample = {
};

export const sentBackReportTaskSample = {
amount: '44.53',
count: 1,
header: 'Report sent back!',
subheader: '1 report worth ₹44.53 was sent back by your approver',
amount: '4.5K',
count: 2,
header: 'Reports sent back!',
subheader: '2 reports worth ₹4.5K were sent back by your approver',
icon: TaskIcon.REPORT,
ctas: [
{
content: 'View Report',
content: 'View Reports',
event: TASKEVENT.openSentBackReport,
},
],
Expand Down Expand Up @@ -100,10 +100,10 @@ export const unreportedExpenseTaskSample2 = {
};

export const unsubmittedReportTaskSample = {
amount: '0.00',
amount: '93.17K',
count: 2,
header: 'Unsubmitted reports',
subheader: '2 reports remain in draft state',
subheader: '2 reports worth ₹93.17K remain in draft state',
icon: TaskIcon.REPORT,
ctas: [
{
Expand Down
153 changes: 75 additions & 78 deletions src/app/core/services/tasks.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ import {
commuteDetailsResponseData3,
} from '../mock-data/commute-details-response.data';
import { orgSettingsPendingRestrictions } from '../mock-data/org-settings.data';
import { SpenderReportsService } from './platform/v1/spender/reports.service';
import { ApproverReportsService } from './platform/v1/approver/reports.service';
import { ReportsStatsResponsePlatform } from '../models/platform/v1/report-stats-response.model';
import {
expectedEmptyReportStats,
expectedReportStats,
expectedSentBackResponse,
} from '../mock-data/report-stats.data';

describe('TasksService', () => {
let tasksService: TasksService;
Expand All @@ -67,14 +75,15 @@ describe('TasksService', () => {
let humanizeCurrencyPipe: jasmine.SpyObj<HumanizeCurrencyPipe>;
let expensesService: jasmine.SpyObj<ExpensesService>;
let employeesService: jasmine.SpyObj<EmployeesService>;
let spenderReportsService: jasmine.SpyObj<SpenderReportsService>;
let approverReportsService: jasmine.SpyObj<ApproverReportsService>;
let orgSettingsService: jasmine.SpyObj<OrgSettingsService>;
const mockTaskClearSubject = new Subject();
const homeCurrency = 'INR';

beforeEach(() => {
const reportServiceSpy = jasmine.createSpyObj('ReportService', [
'getReportAutoSubmissionDetails',
'getReportStatsData',
'getAllExtendedReports',
]);
const expensesServiceSpy = jasmine.createSpyObj('ExpensesService', ['getExpenseStats', 'getDuplicateSets']);
Expand All @@ -88,6 +97,8 @@ describe('TasksService', () => {
const humanizeCurrencyPipeSpy = jasmine.createSpyObj('HumanizeCurrencyPipe', ['transform']);
const orgSettingsServiceSpy = jasmine.createSpyObj('OrgSettingsService', ['get']);
const employeesServiceSpy = jasmine.createSpyObj('EmployeesService', ['getCommuteDetails']);
const spenderReportsServiceSpy = jasmine.createSpyObj('SpenderReportsService', ['getReportsStats']);
const approverReportsServiceSpy = jasmine.createSpyObj('ApproverReportsService', ['getReportsStats']);

TestBed.configureTestingModule({
providers: [
Expand Down Expand Up @@ -132,6 +143,14 @@ describe('TasksService', () => {
provide: OrgSettingsService,
useValue: orgSettingsServiceSpy,
},
{
provide: SpenderReportsService,
useValue: spenderReportsServiceSpy,
},
{
provide: ApproverReportsService,
useValue: approverReportsServiceSpy,
},
],
});
tasksService = TestBed.inject(TasksService);
Expand All @@ -148,6 +167,8 @@ describe('TasksService', () => {
expensesService = TestBed.inject(ExpensesService) as jasmine.SpyObj<ExpensesService>;
employeesService = TestBed.inject(EmployeesService) as jasmine.SpyObj<EmployeesService>;
orgSettingsService = TestBed.inject(OrgSettingsService) as jasmine.SpyObj<OrgSettingsService>;
spenderReportsService = TestBed.inject(SpenderReportsService) as jasmine.SpyObj<SpenderReportsService>;
approverReportsService = TestBed.inject(ApproverReportsService) as jasmine.SpyObj<ApproverReportsService>;
orgSettingsService.get.and.returnValue(of(orgSettingsPendingRestrictions));
});

Expand All @@ -171,22 +192,16 @@ describe('TasksService', () => {
});
});

function setupUnsibmittedReportsResponse() {
reportService.getReportStatsData
.withArgs({
scalar: true,
aggregates: 'count(rp_id),sum(rp_amount)',
rp_state: 'in.(DRAFT)',
})
.and.returnValue(of(unsubmittedReportsResponse));
}

it('should be able to fetch unsubmitted reports', (done) => {
setupUnsibmittedReportsResponse();
spenderReportsService.getReportsStats.and.returnValue(of(expectedReportStats.draft));
currencyService.getHomeCurrency.and.returnValue(of(homeCurrency));
humanizeCurrencyPipe.transform
.withArgs(unsubmittedReportsResponse[0].aggregates[1].function_value, homeCurrency, true)
.and.returnValue('0.00');
.withArgs(expectedReportStats.draft.total_amount, homeCurrency, true)
.and.returnValue('93.17K');
humanizeCurrencyPipe.transform
.withArgs(expectedReportStats.draft.total_amount, homeCurrency)
.and.returnValue('₹93.17K');
humanizeCurrencyPipe.transform.and.returnValue('93.1K');
tasksService.getUnsubmittedReportsTasks().subscribe((unsubmittedReportsTasks) => {
expect(unsubmittedReportsTasks).toEqual([unsubmittedReportTaskSample]);
done();
Expand Down Expand Up @@ -228,22 +243,20 @@ describe('TasksService', () => {
});

it('should be able to fetch Sent Back Report Tasks', (done) => {
reportService.getReportStatsData
spenderReportsService.getReportsStats
.withArgs({
scalar: true,
aggregates: 'count(rp_id),sum(rp_amount)',
rp_state: 'in.(APPROVER_INQUIRY)',
state: 'eq.APPROVER_INQUIRY',
})
.and.returnValue(of(sentBackResponse));
.and.returnValue(of(expectedSentBackResponse));

currencyService.getHomeCurrency.and.returnValue(of(homeCurrency));

humanizeCurrencyPipe.transform
.withArgs(sentBackResponse[0].aggregates[1].function_value, homeCurrency, true)
.and.returnValue('44.53');
.withArgs(expectedSentBackResponse.total_amount, homeCurrency, true)
.and.returnValue('4.5K');
humanizeCurrencyPipe.transform
.withArgs(sentBackResponse[0].aggregates[1].function_value, homeCurrency)
.and.returnValue('₹44.53');
.withArgs(expectedSentBackResponse.total_amount, homeCurrency)
.and.returnValue('₹4.5K');

tasksService.getSentBackReportTasks().subscribe((sentBackReportsTasks) => {
expect(sentBackReportsTasks).toEqual([sentBackReportTaskSample]);
Expand All @@ -256,25 +269,18 @@ describe('TasksService', () => {
currencyService.getHomeCurrency.and.returnValue(of(homeCurrency));

humanizeCurrencyPipe.transform
.withArgs(teamReportResponse[0].aggregates[1].function_value, homeCurrency, true)
.withArgs(expectedReportStats.report.total_amount, homeCurrency, true)
.and.returnValue('733.48K');
humanizeCurrencyPipe.transform
.withArgs(teamReportResponse[0].aggregates[1].function_value, homeCurrency)
.withArgs(expectedReportStats.report.total_amount, homeCurrency)
.and.returnValue('₹733.48K');

reportService.getReportStatsData
.withArgs(
{
approved_by: 'cs.{' + extendedOrgUserResponse.ou.id + '}',
rp_approval_state: ['in.(APPROVAL_PENDING)'],
rp_state: ['in.(APPROVER_PENDING)'],
sequential_approval_turn: ['in.(true)'],
aggregates: 'count(rp_id),sum(rp_amount)',
scalar: true,
},
false
)
.and.returnValue(of(teamReportResponse));
approverReportsService.getReportsStats
.withArgs({
next_approver_user_ids: `cs.[${extendedOrgUserResponse.us.id}]`,
state: 'eq.APPROVER_PENDING',
})
.and.returnValue(of(expectedReportStats.report));

tasksService.getTeamReportsTasks().subscribe((teamReportsTasks) => {
expect(teamReportsTasks).toEqual([teamReportTaskSample]);
Expand Down Expand Up @@ -615,19 +621,19 @@ describe('TasksService', () => {

const tasks2 = tasksService.mapAggregateToTeamReportTask(
{
totalAmount: 0,
totalCount: 0,
},
total_amount: 0,
count: 0,
} as ReportsStatsResponsePlatform,
homeCurrency
);

expect(tasks2).toEqual([]);

const tasks3 = tasksService.mapAggregateToUnsubmittedReportTask(
{
totalAmount: 0,
totalCount: 0,
},
total_amount: 0,
count: 0,
} as ReportsStatsResponsePlatform,
homeCurrency
);

Expand All @@ -645,9 +651,9 @@ describe('TasksService', () => {

const tasks5 = tasksService.mapSentBackReportsToTasks(
{
totalAmount: 0,
totalCount: 0,
},
total_amount: 0,
count: 0,
} as ReportsStatsResponsePlatform,
homeCurrency
);

Expand Down Expand Up @@ -690,30 +696,21 @@ describe('TasksService', () => {
function setupData() {
currencyService.getHomeCurrency.and.returnValue(of(homeCurrency));
advanceRequestService.getAdvanceRequestStats.and.returnValue(of(sentBackAdvancesResponse));
setupUnsibmittedReportsResponse();
spenderReportsService.getReportsStats.and.returnValue(of(expectedReportStats.draft));
getUnreportedExpenses();
reportService.getReportStatsData
spenderReportsService.getReportsStats
.withArgs({
scalar: true,
aggregates: 'count(rp_id),sum(rp_amount)',
rp_state: 'in.(APPROVER_INQUIRY)',
state: 'eq.APPROVER_INQUIRY',
})
.and.returnValue(of(sentBackResponse));
.and.returnValue(of(expectedSentBackResponse));
authService.getEou.and.returnValue(new Promise((resolve) => resolve(extendedOrgUserResponse)));
currencyService.getHomeCurrency.and.returnValue(of(homeCurrency));
reportService.getReportStatsData
.withArgs(
{
approved_by: 'cs.{' + extendedOrgUserResponse.ou.id + '}',
rp_approval_state: ['in.(APPROVAL_PENDING)'],
rp_state: ['in.(APPROVER_PENDING)'],
sequential_approval_turn: ['in.(true)'],
aggregates: 'count(rp_id),sum(rp_amount)',
scalar: true,
},
false
)
.and.returnValue(of(teamReportResponse));
approverReportsService.getReportsStats
.withArgs({
next_approver_user_ids: `cs.[${extendedOrgUserResponse.us.id}]`,
state: 'eq.APPROVER_PENDING',
})
.and.returnValue(of(expectedReportStats.report));
expensesService.getDuplicateSets.and.returnValue(of(expenseDuplicateSets));
expensesService.getExpenseStats
.withArgs({
Expand All @@ -732,7 +729,7 @@ describe('TasksService', () => {
tasksService.getTasks().subscribe((tasks) => {
expect(tasks.map((task) => task.header)).toEqual([
'34 Potential Duplicates',
'Report sent back!',
'Reports sent back!',
'Incomplete expenses',
'Unsubmitted reports',
'Expenses are ready to report',
Expand Down Expand Up @@ -766,7 +763,7 @@ describe('TasksService', () => {
tasksService.getTasks(true).subscribe((tasks) => {
expect(tasks.map((task) => task.header)).toEqual([
'34 Potential Duplicates',
'Report sent back!',
'Reports sent back!',
'Incomplete expenses',
'Reports to be approved',
'Advances sent back!',
Expand Down Expand Up @@ -851,9 +848,9 @@ describe('TasksService', () => {

const sentBackReportTask = tasksService.mapSentBackReportsToTasks(
{
totalCount: 2,
totalAmount: sentBackResponse[0].aggregates[1].function_value,
},
count: 2,
total_amount: sentBackResponse[0].aggregates[1].function_value,
} as ReportsStatsResponsePlatform,
homeCurrency
);

Expand Down Expand Up @@ -907,16 +904,16 @@ describe('TasksService', () => {
]);
});

it('should generate proper content in all cases of unsibmitted report tasks', () => {
it('should generate proper content in all cases of unsubmitted report tasks', () => {
humanizeCurrencyPipe.transform
.withArgs(unsubmittedReportsResponse[0].aggregates[1].function_value, homeCurrency, true)
.and.returnValue('0.00');

const tasks = tasksService.mapAggregateToUnsubmittedReportTask(
{
totalAmount: unsubmittedReportsResponse[0].aggregates[1].function_value,
totalCount: 1,
},
total_amount: unsubmittedReportsResponse[0].aggregates[1].function_value,
count: 1,
} as ReportsStatsResponsePlatform,
homeCurrency
);

Expand Down Expand Up @@ -947,9 +944,9 @@ describe('TasksService', () => {

const tasks = tasksService.mapAggregateToTeamReportTask(
{
totalAmount: teamReportResponse[0].aggregates[1].function_value,
totalCount: 1,
},
total_amount: teamReportResponse[0].aggregates[1].function_value,
count: 1,
} as ReportsStatsResponsePlatform,
homeCurrency
);

Expand Down
2 changes: 1 addition & 1 deletion src/app/core/services/tasks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ export class TasksService {
return [
{
amount: this.humanizeCurrency.transform(aggregate.total_amount, homeCurrency, true),
count: aggregate.total_amount,
count: aggregate.count,
header: `Report${aggregate.count === 1 ? '' : 's'} sent back!`,
subheader: `${aggregate.count} report${aggregate.count === 1 ? '' : 's'}${this.getAmountString(
aggregate.total_amount,
Expand Down
Loading