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: qbo skipped export log component #995

Merged
merged 13 commits into from
Oct 8, 2024
Original file line number Diff line number Diff line change
@@ -1,16 +1,53 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

/* eslint-disable max-lines */
/* eslint-disable dot-notation */
import { ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing';
import { FormBuilder, ReactiveFormsModule } from '@angular/forms';
import { of } from 'rxjs';
import { QboCompleteExportLogComponent } from './qbo-complete-export-log.component';
import { ExportLogService } from 'src/app/core/services/common/export-log.service';
import { PaginatorService } from 'src/app/core/services/common/paginator.service';
import { WindowService } from 'src/app/core/services/common/window.service';
import { UserService } from 'src/app/core/services/misc/user.service';
import { AppName, PaginatorPage, TaskLogState } from 'src/app/core/models/enum/enum.model';
import { AccountingExportModel } from 'src/app/core/models/db/accounting-export.model';
import { mockExpenseGroupResponse, mockUser } from 'src/app/integrations/qbo/qbo.fixture';

xdescribe('QboCompleteExportLogComponent', () => {
describe('QboCompleteExportLogComponent', () => {
let component: QboCompleteExportLogComponent;
let fixture: ComponentFixture<QboCompleteExportLogComponent>;
let exportLogService: jasmine.SpyObj<ExportLogService>;
let paginatorService: jasmine.SpyObj<PaginatorService>;
let windowService: jasmine.SpyObj<WindowService>;
let userService: jasmine.SpyObj<UserService>;

beforeEach(async () => {
const exportLogServiceSpy = jasmine.createSpyObj('ExportLogService', ['getExpenseGroups']);
const paginatorServiceSpy = jasmine.createSpyObj('PaginatorService', ['storePageSize', 'getPageSize']);
const windowServiceSpy = jasmine.createSpyObj('WindowService', ['openInNewTab']);
const userServiceSpy = jasmine.createSpyObj('UserService', ['getUserProfile']);

await TestBed.configureTestingModule({
declarations: [ QboCompleteExportLogComponent ]
})
.compileComponents();
declarations: [ QboCompleteExportLogComponent ],
imports: [ ReactiveFormsModule ],
providers: [
FormBuilder,
{ provide: ExportLogService, useValue: exportLogServiceSpy },
{ provide: PaginatorService, useValue: paginatorServiceSpy },
{ provide: WindowService, useValue: windowServiceSpy },
{ provide: UserService, useValue: userServiceSpy }
]
}).compileComponents();

exportLogService = TestBed.inject(ExportLogService) as jasmine.SpyObj<ExportLogService>;
paginatorService = TestBed.inject(PaginatorService) as jasmine.SpyObj<PaginatorService>;
windowService = TestBed.inject(WindowService) as jasmine.SpyObj<WindowService>;
userService = TestBed.inject(UserService) as jasmine.SpyObj<UserService>;
});

beforeEach(() => {
userService.getUserProfile.and.returnValue(mockUser);
paginatorService.getPageSize.and.returnValue({ limit: 10, offset: 0 });
exportLogService.getExpenseGroups.and.returnValue(of());

fixture = TestBed.createComponent(QboCompleteExportLogComponent);
component = fixture.componentInstance;
Expand All @@ -20,4 +57,104 @@ xdescribe('QboCompleteExportLogComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});
});

it('should initialize with correct default values', () => {
expect(component.appName).toBe(AppName.QBO);
expect(component.totalCount).toBe(0);
expect(component.offset).toBe(0);
expect(component.currentPage).toBe(1);
expect(component.isDateSelected).toBeFalse();
expect(component['org_id']).toBe(mockUser.org_id);
});

it('should open expense in Fyle', () => {
const expenseId = 'txGDE32dCf';
component.openExpenseinFyle(expenseId);
expect(windowService.openInNewTab).toHaveBeenCalledWith(
AccountingExportModel.getFyleExpenseUrl(expenseId)
);
});

it('should handle simple search', fakeAsync(() => {
const query = 'test query';
component.handleSimpleSearch(query);
tick(1000);
expect(component.searchQuery).toBe(query);
expect(component.offset).toBe(0);
expect(component.currentPage).toBe(1);
expect(exportLogService.getExpenseGroups).toHaveBeenCalled();
}));

it('should handle page size changes', () => {
const newLimit = 20;
component.pageSizeChanges(newLimit);
expect(component.isLoading).toBeTrue();
expect(component.currentPage).toBe(1);
expect(component.limit).toBe(newLimit);
expect(paginatorService.storePageSize).toHaveBeenCalledWith(PaginatorPage.EXPORT_LOG, newLimit);
expect(exportLogService.getExpenseGroups).toHaveBeenCalled();
});

it('should handle page changes', () => {
const newOffset = 10;
component.pageChanges(newOffset);
expect(component.isLoading).toBeTrue();
expect(component.offset).toBe(newOffset);
expect(component.currentPage).toBe(2);
expect(exportLogService.getExpenseGroups).toHaveBeenCalled();
});

it('should setup form correctly', () => {
expect(component.exportLogForm).toBeDefined();
expect(component.exportLogForm.get('searchOption')).toBeDefined();
expect(component.exportLogForm.get('dateRange')).toBeDefined();
expect(component.exportLogForm.get('start')).toBeDefined();
expect(component.exportLogForm.get('end')).toBeDefined();
});

it('should handle date range changes', fakeAsync(() => {
const dateRange = [new Date(), new Date()];
component.exportLogForm.get('start')?.setValue(dateRange);
tick(10);
expect(component.selectedDateFilter).toBeDefined();
expect(component.isDateSelected).toBeTrue();
expect(exportLogService.getExpenseGroups).toHaveBeenCalled();
}));

it('should handle null date range', fakeAsync(() => {
component.exportLogForm.get('start')?.setValue(null);
tick();
expect(component.selectedDateFilter).toBeNull();
expect(component.isDateSelected).toBeFalse();
expect(exportLogService.getExpenseGroups).toHaveBeenCalled();
}));

it('should parse expense group API response correctly', () => {
exportLogService.getExpenseGroups.and.returnValue(of(mockExpenseGroupResponse));
component['getAccountingExports'](10, 0);

expect(component.totalCount).toBe(mockExpenseGroupResponse.count);
expect(component.filteredAccountingExports.length).toBe(mockExpenseGroupResponse.results.length);
expect(component.accountingExports.length).toBe(mockExpenseGroupResponse.results.length);

// Check if the first expense group is parsed correctly
const firstExpenseGroup = mockExpenseGroupResponse.results[0];
expect(firstExpenseGroup.expenses.length).toBe(component.filteredAccountingExports[0].expenses.length);

// Check if the expense details are parsed correctly
const firstExpense = firstExpenseGroup.expenses[0];
expect(firstExpense.expense_number).toBe(mockExpenseGroupResponse.results[0].expenses[0].expense_number);
expect(firstExpense.amount).toBe(mockExpenseGroupResponse.results[0].expenses[0].amount);
expect(firstExpense.currency).toBe(mockExpenseGroupResponse.results[0].expenses[0].currency);
expect(firstExpense.category).toBe(mockExpenseGroupResponse.results[0].expenses[0].category);
expect(firstExpense.expense_id).toBe(mockExpenseGroupResponse.results[0].expenses[0].expense_id);

// Check if the response logs are parsed correctly
expect(firstExpenseGroup.response_logs).toEqual(mockExpenseGroupResponse.results[0].response_logs);

// Check if the dates are parsed correctly
expect(firstExpenseGroup.created_at).toEqual(new Date(mockExpenseGroupResponse.results[0].created_at));
expect(firstExpenseGroup.exported_at).toEqual(new Date(mockExpenseGroupResponse.results[0].exported_at));
expect(firstExpenseGroup.updated_at).toEqual(new Date(mockExpenseGroupResponse.results[0].updated_at));
});
});
Original file line number Diff line number Diff line change
@@ -1,23 +1,140 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

/* eslint-disable max-lines */
/* eslint-disable dot-notation */
import { ComponentFixture, TestBed, fakeAsync, flush, tick } from '@angular/core/testing';
import { FormBuilder, ReactiveFormsModule } from '@angular/forms';
import { of } from 'rxjs';
import { QboSkippedExportLogComponent } from './qbo-skipped-export-log.component';
import { UserService } from 'src/app/core/services/misc/user.service';
import { ExportLogService } from 'src/app/core/services/common/export-log.service';
import { AccountingExportService } from 'src/app/core/services/common/accounting-export.service';
import { WindowService } from 'src/app/core/services/common/window.service';
import { PaginatorService } from 'src/app/core/services/common/paginator.service';
import { mockSkippedExpenseGroup, mockSkippedExpenseGroupWithDateRange, mockPaginator } from 'src/app/integrations/qbo/qbo.fixture';
import { PaginatorPage } from 'src/app/core/models/enum/enum.model';

xdescribe('QboSkippedExportLogComponent', () => {
describe('QboSkippedExportLogComponent', () => {
let component: QboSkippedExportLogComponent;
let fixture: ComponentFixture<QboSkippedExportLogComponent>;
let exportLogService: jasmine.SpyObj<ExportLogService>;
let userService: jasmine.SpyObj<UserService>;
let paginatorService: jasmine.SpyObj<PaginatorService>;

beforeEach(async () => {
const exportLogServiceSpy = jasmine.createSpyObj('ExportLogService', ['getSkippedExpenses']);
const userServiceSpy = jasmine.createSpyObj('UserService', ['getUserProfile']);
const paginatorServiceSpy = jasmine.createSpyObj('PaginatorService', ['getPageSize', 'storePageSize']);

await TestBed.configureTestingModule({
declarations: [ QboSkippedExportLogComponent ]
})
.compileComponents();
declarations: [ QboSkippedExportLogComponent ],
imports: [ ReactiveFormsModule ],
providers: [
FormBuilder,
{ provide: ExportLogService, useValue: exportLogServiceSpy },
{ provide: UserService, useValue: userServiceSpy },
{ provide: AccountingExportService, useValue: {} },
{ provide: WindowService, useValue: {} },
{ provide: PaginatorService, useValue: paginatorServiceSpy }
]
}).compileComponents();

exportLogService = TestBed.inject(ExportLogService) as jasmine.SpyObj<ExportLogService>;
userService = TestBed.inject(UserService) as jasmine.SpyObj<UserService>;
paginatorService = TestBed.inject(PaginatorService) as jasmine.SpyObj<PaginatorService>;
});

beforeEach(() => {
fixture = TestBed.createComponent(QboSkippedExportLogComponent);
component = fixture.componentInstance;
userService.getUserProfile.and.returnValue({
org_id: 'or79Cob97KSh',
anishfyle marked this conversation as resolved.
Show resolved Hide resolved
email: '[email protected]',
access_token: 'dummy_access_token',
refresh_token: 'dummy_refresh_token',
full_name: 'Test User',
user_id: 'user123',
org_name: 'Test Org'
});
paginatorService.getPageSize.and.returnValue(mockPaginator);
exportLogService.getSkippedExpenses.and.returnValue(of(mockSkippedExpenseGroup));
fixture.detectChanges();
});

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

it('should initialize with correct data', () => {
expect(component.isLoading).toBeFalse();
expect(component.totalCount).toBe(mockSkippedExpenseGroup.count);
expect(component.expenses.length).toBe(mockSkippedExpenseGroup.results.length);
expect(component.limit).toBe(mockPaginator.limit);
expect(component.offset).toBe(mockPaginator.offset);
});

it('should handle simple search', fakeAsync(() => {
const searchQuery = 'anish';
component.handleSimpleSearch(searchQuery);
tick(1000);
expect(component.searchQuery).toBe(searchQuery);
expect(component.offset).toBe(0);
expect(component.currentPage).toBe(1);
}));

it('should handle page size changes', () => {
const newLimit = 100;
component.pageSizeChanges(newLimit);
expect(component.isLoading).toBeFalse();
expect(component.currentPage).toBe(1);
expect(component.limit).toBe(newLimit);
expect(paginatorService.storePageSize).toHaveBeenCalledWith(PaginatorPage.EXPORT_LOG, newLimit);
});

it('should handle page changes', () => {
const newOffset = 50;
component.limit = 50;
component.pageChanges(newOffset);
expect(component.isLoading).toBeFalse();
expect(component.offset).toBe(newOffset);
expect(component.currentPage).toBe(2);
});

it('should handle date range selection and hide/show calendar', fakeAsync(() => {
const startDate = new Date('2023-06-15');
const endDate = new Date('2023-06-16');

component.skipExportLogForm.controls.start.setValue([startDate, endDate]);
tick();

expect(component.isDateSelected).toBeTrue();
expect(component.selectedDateFilter).toEqual({ startDate, endDate });
expect(component.hideCalendar).toBeTrue();

tick(10);

expect(component.hideCalendar).toBeFalse();
}));

it('should handle date range reset', fakeAsync(() => {
component.skipExportLogForm.controls.start.setValue(null);
tick();
expect(component.isDateSelected).toBeFalse();
expect(component.selectedDateFilter).toBeNull();
}));

it('should filter expenses based on date range', fakeAsync(() => {
const startDate = new Date('2023-06-15');
const endDate = new Date('2023-06-16');
exportLogService.getSkippedExpenses.and.returnValue(of(mockSkippedExpenseGroupWithDateRange));

component.skipExportLogForm.controls.start.setValue([startDate, endDate]);

tick();
tick(10);

expect(component.filteredExpenses.length).toBe(mockSkippedExpenseGroupWithDateRange.results.length);
expect(component.totalCount).toBe(mockSkippedExpenseGroupWithDateRange.count);
expect(component.hideCalendar).toBeFalse();

flush();
}));
});
Loading
Loading