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: intacct dashboard #981

Merged
merged 12 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,30 +1,148 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { ComponentFixture, discardPeriodicTasks, fakeAsync, flush, TestBed, tick } from '@angular/core/testing';
import { IntacctDashboardComponent } from './intacct-dashboard.component';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { AccountingExportService } from 'src/app/core/services/common/accounting-export.service';
import { DashboardService } from 'src/app/core/services/common/dashboard.service';
import { ExportLogService } from 'src/app/core/services/si/export-log/export-log.service';
import { WorkspaceService } from 'src/app/core/services/common/workspace.service';
import { UserService } from 'src/app/core/services/misc/user.service';
import { SiExportSettingService } from 'src/app/core/services/si/si-configuration/si-export-setting.service';
import { MinimalUser } from 'src/app/core/models/db/user.model';
import { of } from 'rxjs';
import { AccountingExportSummary, AccountingExportSummaryModel } from 'src/app/core/models/db/accounting-export-summary.model';
import { mockAccountingExportSummary, mockCompletedTasksWithFailures, mockConfiguration, mockErrors, mockExportableAccountingExportIds, mockExportSettingGet, mockExportSettings, mockTasksInProgress } from '../../intacct.fixture';
import { SharedModule } from 'src/app/shared/shared.module';
import { Error } from 'src/app/core/models/db/error.model';
import { AccountingErrorType, AppName, CCCImportState, IntacctCategoryDestination, ReimbursableImportState, TaskLogState } from 'src/app/core/models/enum/enum.model';
JustARatherRidiculouslyLongUsername marked this conversation as resolved.
Show resolved Hide resolved

describe('IntacctDashboardComponent', () => {

xdescribe('DashboardComponent', () => {
let component: IntacctDashboardComponent;
let fixture: ComponentFixture<IntacctDashboardComponent>;
let dashboardServiceSpy: jasmine.SpyObj<DashboardService>;
let accountingExportServiceSpy: jasmine.SpyObj<AccountingExportService>;
let userServiceSpy: jasmine.SpyObj<UserService>;
let workspaceServiceSpy: jasmine.SpyObj<WorkspaceService>;
let intacctExportSettingServiceSpy: jasmine.SpyObj<SiExportSettingService>;
let exportLogServiceSpy: jasmine.SpyObj<ExportLogService>;

beforeEach(async () => {
const localStorageDump = {
email: '[email protected]',
org_id: '2'
};
localStorage.setItem('user', JSON.stringify(localStorageDump));
const dashboardServiceSpyObj = jasmine.createSpyObj('DashboardService', ['getExportErrors', 'triggerAccountingExport', 'getAllTasks', 'getExportableAccountingExportIds']);
const accountingExportServiceSpyObj = jasmine.createSpyObj('AccountingExportService', ['getAccountingExportSummary', 'importExpensesFromFyle']);
const userServiceSpyObj = jasmine.createSpyObj('UserService', ['getUserProfile']);
const workspaceServiceSpyObj = jasmine.createSpyObj('WorkspaceService', ['getConfiguration', 'getWorkspaceId', 'setOnboardingState']);
const intacctExportSettingServiceSpyObj = jasmine.createSpyObj('SiExportSettingService', ['getExportSettings']);
const exportLogServiceSpyObj = jasmine.createSpyObj('ExportLogService', ['getExportLogs']);

await TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
declarations: [ IntacctDashboardComponent ]
})
.compileComponents();
imports: [HttpClientTestingModule, SharedModule],
declarations: [IntacctDashboardComponent],
providers: [
{ provide: DashboardService, useValue: dashboardServiceSpyObj },
{ provide: AccountingExportService, useValue: accountingExportServiceSpyObj },
{ provide: UserService, useValue: userServiceSpyObj },
{ provide: WorkspaceService, useValue: workspaceServiceSpyObj },
{ provide: SiExportSettingService, useValue: intacctExportSettingServiceSpyObj },
{ provide: ExportLogService, useValue: exportLogServiceSpyObj }
]
}).compileComponents();

dashboardServiceSpy = TestBed.inject(DashboardService) as jasmine.SpyObj<DashboardService>;
accountingExportServiceSpy = TestBed.inject(AccountingExportService) as jasmine.SpyObj<AccountingExportService>;
userServiceSpy = TestBed.inject(UserService) as jasmine.SpyObj<UserService>;
workspaceServiceSpy = TestBed.inject(WorkspaceService) as jasmine.SpyObj<WorkspaceService>;
intacctExportSettingServiceSpy = TestBed.inject(SiExportSettingService) as jasmine.SpyObj<SiExportSettingService>;
exportLogServiceSpy = TestBed.inject(ExportLogService) as jasmine.SpyObj<ExportLogService>;

userServiceSpy.getUserProfile.and.returnValue({ full_name: 'John Doe' } as MinimalUser);
dashboardServiceSpy.getExportErrors.and.returnValue(of([]));
accountingExportServiceSpy.getAccountingExportSummary.and.returnValue(of(mockAccountingExportSummary as unknown as AccountingExportSummary));
dashboardServiceSpy.getExportableAccountingExportIds.and.returnValue(of(mockExportableAccountingExportIds));
dashboardServiceSpy.getAllTasks.and.returnValue(of(mockCompletedTasksWithFailures));
workspaceServiceSpy.getConfiguration.and.returnValue(of(mockConfiguration));
intacctExportSettingServiceSpy.getExportSettings.and.returnValue(of(mockExportSettingGet));
dashboardServiceSpy.triggerAccountingExport.and.returnValue(of({}));
accountingExportServiceSpy.importExpensesFromFyle.and.returnValue(of({}));


fixture = TestBed.createComponent(IntacctDashboardComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});


it('should initialize correctly', fakeAsync(() => {
component.getExportErrors$ = of(mockErrors as Error[]);
spyOn(AccountingExportSummaryModel, 'parseAPIResponseToAccountingSummary');
JustARatherRidiculouslyLongUsername marked this conversation as resolved.
Show resolved Hide resolved

tick();
fixture.detectChanges();

expect(component.isLoading).toBeFalse();
expect(component.errors).toEqual({
[AccountingErrorType.EMPLOYEE_MAPPING]: [mockErrors[0]],
[AccountingErrorType.CATEGORY_MAPPING]: [mockErrors[1]],
[AccountingErrorType.ACCOUNTING_ERROR]: [mockErrors[2]]
});

expect(component.reimbursableImportState).toEqual(ReimbursableImportState.PROCESSING);
expect(component.cccImportState).toEqual(CCCImportState.PAID);

expect(AccountingExportSummaryModel.parseAPIResponseToAccountingSummary).toHaveBeenCalledOnceWith(mockAccountingExportSummary);
expect(component.destinationFieldMap).toEqual({
JustARatherRidiculouslyLongUsername marked this conversation as resolved.
Show resolved Hide resolved
"EMPLOYEE": "EMPLOYEE",
"CATEGORY": IntacctCategoryDestination.EXPENSE_TYPE
});
expect(component.exportableAccountingExportIds).toEqual([1, 2, 3]);
expect(component.failedExpenseGroupCount).toBe(1);
expect(component.isImportInProgress).toBeFalse();
}));

it('should initialize correctly when an export is in progress', fakeAsync(() => {
dashboardServiceSpy.getAllTasks.and.returnValue(of(mockTasksInProgress));
spyOn<any>(component, 'pollExportStatus');

tick();
fixture.detectChanges();

expect(component.isImportInProgress).toBeFalse();
expect(component.isExportInProgress).toBeTrue();

// eslint-disable-next-line dot-notation
expect(component['pollExportStatus']).toHaveBeenCalledOnceWith([1, 2, 3]);
}));
JustARatherRidiculouslyLongUsername marked this conversation as resolved.
Show resolved Hide resolved

it('should handle export correctly', fakeAsync(() => {
dashboardServiceSpy.getAllTasks.and.returnValue(of(mockTasksInProgress));
component.exportableAccountingExportIds = [1, 2];

component.export();
tick(3000);

expect(component.isExportInProgress).toBeTrue();
expect(component.processedCount).toBe(1);
expect(component.exportProgressPercentage).toBe(50);

// Simulate export completion
dashboardServiceSpy.getAllTasks.and.returnValue(of(mockCompletedTasksWithFailures));
dashboardServiceSpy.getExportErrors.and.returnValue(of([]));

tick(3000);

expect(component.isExportInProgress).toBeFalse();
expect(component.failedExpenseGroupCount).toBe(1);

// The failed expense group should be marked as exportable again
expect(component.exportableAccountingExportIds).toEqual([2]);
expect(component.exportProgressPercentage).toBe(0);
expect(component.processedCount).toBe(0);

fixture.detectChanges();
flush();

discardPeriodicTasks();
}));
});
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,21 @@ export class IntacctDashboardComponent implements OnInit {
interval(3000).pipe(
switchMap(() => from(this.dashboardService.getAllTasks([], exportableAccountingExportIds, this.accountingExportType, AppName.INTACCT))),
takeWhile((response: IntacctTaskResponse) =>
response.results.filter(task =>
(task.status === TaskLogState.IN_PROGRESS || task.status === TaskLogState.ENQUEUED)
).length > 0, true)
response.results.filter(task =>
(task.status === TaskLogState.IN_PROGRESS || task.status === TaskLogState.ENQUEUED)
).length > 0, true
)
).subscribe((res: IntacctTaskResponse) => {
this.processedCount = res.results.filter((task: { status: string; type: TaskLogType; expense_group: number; }) => (task.status !== 'IN_PROGRESS' && task.status !== 'ENQUEUED') && (task.type !== TaskLogType.FETCHING_EXPENSES && task.type !== TaskLogType.CREATING_AP_PAYMENT && task.type !== TaskLogType.CREATING_REIMBURSEMENT) && exportableAccountingExportIds.includes(task.expense_group)).length;
this.processedCount = res.results.filter(
(task: { status: string; type: TaskLogType; expense_group: number; }) =>
(task.status !== 'IN_PROGRESS' && task.status !== 'ENQUEUED') &&
(
task.type !== TaskLogType.FETCHING_EXPENSES &&
task.type !== TaskLogType.CREATING_AP_PAYMENT &&
task.type !== TaskLogType.CREATING_REIMBURSEMENT
) &&
exportableAccountingExportIds.includes(task.expense_group)
).length;
this.exportProgressPercentage = Math.round((this.processedCount / exportableAccountingExportIds.length) * 100);

if (res.results.filter(task => (task.status === TaskLogState.IN_PROGRESS || task.status === TaskLogState.ENQUEUED)).length === 0) {
Expand Down Expand Up @@ -187,7 +197,9 @@ export class IntacctDashboardComponent implements OnInit {

this.isLoading = false;

const queuedTasks: IntacctTaskLog[] = responses[2].results.filter((task: IntacctTaskLog) => task.status === TaskLogState.ENQUEUED || task.status === TaskLogState.IN_PROGRESS);
const queuedTasks: IntacctTaskLog[] = responses[2].results.filter(
(task: IntacctTaskLog) => task.status === TaskLogState.ENQUEUED || task.status === TaskLogState.IN_PROGRESS
);
this.failedExpenseGroupCount = responses[2].results.filter((task: IntacctTaskLog) => task.status === TaskLogState.FAILED || task.status === TaskLogState.FATAL).length;

this.exportableAccountingExportIds = responses[4].exportable_expense_group_ids;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ComponentFixture, TestBed } from '@angular/core/testing';

import { IntacctOnboardingComponent } from './intacct-onboarding.component';

xdescribe('OnboardingComponent', () => {
describe('IntacctOnboardingComponent', () => {
let component: IntacctOnboardingComponent;
let fixture: ComponentFixture<IntacctOnboardingComponent>;

Expand Down
Loading