-
Notifications
You must be signed in to change notification settings - Fork 0
test: intacct import settings watchers #1010
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
Changes from 7 commits
fdda416
2497372
7f330b7
b712977
e9542a9
d002950
45bfa0f
202ec33
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| /* eslint-disable dot-notation */ | ||
| import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing'; | ||
| import { AbstractControl, FormArray, FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms'; | ||
| import { provideRouter, Router, RouterModule } from '@angular/router'; | ||
|
|
@@ -12,17 +13,13 @@ import { TrackingService } from 'src/app/core/services/integration/tracking.serv | |
| import { IntegrationsToastService } from 'src/app/core/services/common/integrations-toast.service'; | ||
| import { StorageService } from 'src/app/core/services/common/storage.service'; | ||
| import { SiWorkspaceService } from 'src/app/core/services/si/si-core/si-workspace.service'; | ||
| import { HelperService } from 'src/app/core/services/common/helper.service'; | ||
|
|
||
| import { configuration, costCodeFieldValue, costTypeFieldValue, fyleFields, groupedDestinationAttributes, importSettings, importSettingsWithProject, intacctImportCodeConfig, locationEntityMapping, sageIntacctFields, sageIntacctFieldsSortedByPriority, settingsWithDependentFields } from '../../intacct.fixture'; | ||
| import { IntacctCategoryDestination, IntacctOnboardingState, ToastSeverity } from 'src/app/core/models/enum/enum.model'; | ||
| import { IntacctCategoryDestination, IntacctOnboardingState, IntacctUpdateEvent, Page, ProgressPhase, ToastSeverity, TrackingApp } from 'src/app/core/models/enum/enum.model'; | ||
| import { SharedModule } from 'src/app/shared/shared.module'; | ||
| import { Org } from 'src/app/core/models/org/org.model'; | ||
| import { HttpClientTestingModule } from '@angular/common/http/testing'; | ||
| import { LocationEntityMapping } from 'src/app/core/models/intacct/db/location-entity-mapping.model'; | ||
| import { IntacctSharedModule } from '../intacct-shared.module'; | ||
| import { ExpenseField } from 'src/app/core/models/intacct/db/expense-field.model'; | ||
| import { ImportSettingGet } from 'src/app/core/models/intacct/intacct-configuration/import-settings.model'; | ||
| import { IntacctConfiguration } from 'src/app/core/models/db/configuration.model'; | ||
|
|
||
| describe('IntacctImportSettingsComponent', () => { | ||
|
|
@@ -37,6 +34,7 @@ describe('IntacctImportSettingsComponent', () => { | |
| let storageService: jasmine.SpyObj<StorageService>; | ||
| let siWorkspaceService: jasmine.SpyObj<SiWorkspaceService>; | ||
| let router: Router; | ||
| let routerUrlSpy: jasmine.Spy<(this: Router) => string>; | ||
|
|
||
| beforeEach(async () => { | ||
| const siImportSettingServiceSpy = jasmine.createSpyObj('SiImportSettingService', ['getImportSettings', 'postImportSettings', 'getImportCodeFieldConfig']); | ||
|
|
@@ -77,7 +75,7 @@ describe('IntacctImportSettingsComponent', () => { | |
| router = TestBed.inject(Router); | ||
|
|
||
| spyOn(router, 'navigate'); | ||
| spyOnProperty(router, 'url').and.returnValue('/onboarding'); | ||
| routerUrlSpy = spyOnProperty(router, 'url').and.returnValue('/onboarding'); | ||
| siImportSettingService.getImportSettings.and.returnValue(of(importSettings)); | ||
| siImportSettingService.getImportCodeFieldConfig.and.returnValue(of(intacctImportCodeConfig)); | ||
| siMappingsService.getSageIntacctFields.and.returnValue(of(sageIntacctFields)); | ||
|
|
@@ -98,7 +96,7 @@ describe('IntacctImportSettingsComponent', () => { | |
| expect(component).toBeTruthy(); | ||
| }); | ||
|
|
||
| describe('Form Initialization', () => { | ||
| describe('Component Initialization', () => { | ||
| beforeEach(() => { | ||
| component.ngOnInit(); | ||
| fixture.detectChanges(); | ||
|
|
@@ -203,4 +201,189 @@ describe('IntacctImportSettingsComponent', () => { | |
| }); | ||
| }); | ||
| }); | ||
| describe('Save', () => { | ||
| it('should successfully save import settings during onboarding', fakeAsync(() => { | ||
| siWorkspaceService.getIntacctOnboardingState.and.returnValue(IntacctOnboardingState.IMPORT_SETTINGS); | ||
| routerUrlSpy.and.returnValue('/onboarding'); | ||
| siImportSettingService.postImportSettings.and.returnValue(of(importSettings)); | ||
|
|
||
| component.ngOnInit(); | ||
| tick(); | ||
|
|
||
| component.save(); | ||
| tick(); | ||
|
|
||
| expect(siImportSettingService.postImportSettings).toHaveBeenCalled(); | ||
| expect(toastService.displayToastMessage).toHaveBeenCalledWith(ToastSeverity.SUCCESS, 'Import settings saved successfully'); | ||
| expect(trackingService.integrationsOnboardingCompletion).toHaveBeenCalledWith( | ||
| TrackingApp.INTACCT, | ||
| IntacctOnboardingState.IMPORT_SETTINGS, | ||
| 3, | ||
| jasmine.any(Object) | ||
| ); | ||
| expect(siWorkspaceService.setIntacctOnboardingState).toHaveBeenCalledWith(IntacctOnboardingState.ADVANCED_CONFIGURATION); | ||
| expect(router.navigate).toHaveBeenCalledWith(['/integrations/intacct/onboarding/advanced_settings']); | ||
| expect(component.saveInProgress).toBeFalse(); | ||
| })); | ||
|
|
||
| it('should successfully save import settings post-onboarding', fakeAsync(() => { | ||
| siWorkspaceService.getIntacctOnboardingState.and.returnValue(IntacctOnboardingState.COMPLETE); | ||
| routerUrlSpy.and.returnValue('/settings'); | ||
| siImportSettingService.postImportSettings.and.returnValue(of(importSettings)); | ||
|
|
||
| component.ngOnInit(); | ||
| tick(); | ||
|
|
||
| component.save(); | ||
| tick(); | ||
|
|
||
| expect(siImportSettingService.postImportSettings).toHaveBeenCalled(); | ||
| expect(toastService.displayToastMessage).toHaveBeenCalledWith(ToastSeverity.SUCCESS, 'Import settings saved successfully'); | ||
| expect(trackingService.intacctUpdateEvent).toHaveBeenCalledWith( | ||
| IntacctUpdateEvent.ADVANCED_SETTINGS_INTACCT, | ||
| { | ||
| phase: ProgressPhase.POST_ONBOARDING, | ||
| oldState: component.importSettings, | ||
| newState: importSettings | ||
| } | ||
| ); | ||
| expect(router.navigate).not.toHaveBeenCalled(); | ||
| expect(component.saveInProgress).toBeFalse(); | ||
| })); | ||
|
|
||
| it('should handle error when saving import settings', fakeAsync(() => { | ||
| siImportSettingService.postImportSettings.and.returnValue(throwError(() => new Error())); | ||
|
|
||
| component.ngOnInit(); | ||
| tick(); | ||
|
|
||
| component.save(); | ||
| tick(); | ||
|
|
||
| expect(siImportSettingService.postImportSettings).toHaveBeenCalled(); | ||
| expect(toastService.displayToastMessage).toHaveBeenCalledWith( | ||
| ToastSeverity.ERROR, | ||
| 'Error saving import settings, please try again later' | ||
| ); | ||
| expect(component.saveInProgress).toBeFalse(); | ||
| })); | ||
|
|
||
| it('should track time spent when saving settings', fakeAsync(() => { | ||
| const mockStartTime = new Date(); | ||
| component['sessionStartTime'] = mockStartTime; | ||
| siImportSettingService.postImportSettings.and.returnValue(of(importSettings)); | ||
|
|
||
| component.ngOnInit(); | ||
| tick(); | ||
|
|
||
| component.save(); | ||
| tick(); | ||
|
|
||
| expect(trackingService.trackTimeSpent).toHaveBeenCalledWith( | ||
| TrackingApp.INTACCT, | ||
| Page.IMPORT_SETTINGS_INTACCT, | ||
| mockStartTime | ||
| ); | ||
| })); | ||
| }); | ||
|
|
||
| describe('Watchers', () => { | ||
| describe('Import Settings Watcher', () => { | ||
| beforeEach(() => { | ||
| component.ngOnInit(); | ||
| fixture.detectChanges(); | ||
| }); | ||
|
|
||
| it('should trigger custom field dialog when a source field is set to custom_field', () => { | ||
| const expenseFieldArray = component.importSettingsForm.get('expenseFields') as FormArray; | ||
| spyOn(component as any, 'addCustomField').and.callThrough(); | ||
|
|
||
| const firstControl = expenseFieldArray.at(0); | ||
| firstControl.patchValue({ | ||
| source_field: 'custom_field', | ||
| destination_field: 'TEST_FIELD', | ||
| import_to_fyle: true | ||
| }); | ||
|
|
||
| expect(component['addCustomField']).toHaveBeenCalled(); | ||
| expect(component.showDialog).toBeTrue(); | ||
JustARatherRidiculouslyLongUsername marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| expect(component.customFieldControl).toEqual(firstControl); | ||
| }); | ||
|
|
||
| it('should update validators when importTaxCodes value changes', () => { | ||
| const taxCodesControl = component.importSettingsForm.get('sageIntacctTaxCodes'); | ||
|
|
||
| component.importSettingsForm.patchValue({ | ||
| importTaxCodes: true | ||
| }); | ||
| expect(taxCodesControl?.hasValidator(Validators.required)).toBeTrue(); | ||
|
|
||
| component.importSettingsForm.patchValue({ | ||
| importTaxCodes: false | ||
| }); | ||
| expect(taxCodesControl?.hasValidator(Validators.required)).toBeFalse(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Cost Codes and Cost Types Watcher', () => { | ||
| beforeEach(() => { | ||
| fixture.detectChanges(); | ||
| }); | ||
|
|
||
| it('should handle isDependentImportEnabled changes', () => { | ||
| const costCodesControl = component.importSettingsForm.get('costCodes'); | ||
| const costTypesControl = component.importSettingsForm.get('costTypes'); | ||
|
|
||
| component.importSettingsForm.patchValue({ | ||
| isDependentImportEnabled: true | ||
| }); | ||
|
|
||
| expect(costCodesControl?.enabled).toBeTrue(); | ||
| expect(costTypesControl?.enabled).toBeTrue(); | ||
| expect(costCodesControl?.hasValidator(Validators.required)).toBeTrue(); | ||
| expect(costTypesControl?.hasValidator(Validators.required)).toBeTrue(); | ||
|
|
||
| component.importSettingsForm.patchValue({ | ||
| isDependentImportEnabled: false | ||
| }); | ||
|
|
||
| expect(costCodesControl?.enabled).toBeFalse(); | ||
| expect(costTypesControl?.enabled).toBeFalse(); | ||
| expect(costCodesControl?.hasValidator(Validators.required)).toBeFalse(); | ||
| expect(costTypesControl?.hasValidator(Validators.required)).toBeFalse(); | ||
| }); | ||
|
|
||
| it('should handle custom field selection for cost codes', () => { | ||
| spyOn(component as any, 'addCustomField').and.callThrough(); | ||
|
|
||
| component.importSettingsForm.patchValue({ | ||
| costCodes: { | ||
| attribute_type: 'custom_field', | ||
| source_field: 'custom_field' | ||
| } | ||
| }); | ||
|
|
||
| expect(component['addCustomField']).toHaveBeenCalled(); | ||
| expect(component.customFieldForDependentField).toBeTrue(); | ||
|
Comment on lines
+359
to
+369
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Avoid spying on private methods in tests Similar to the previous comment, you are spying on the private method |
||
| expect(component.customFieldControl).toBe(component.importSettingsForm.get('costCodes')!); | ||
| expect(component.importSettingsForm.get('costCodes')?.value.source_field).toBeNull(); | ||
| }); | ||
|
|
||
| it('should handle custom field selection for cost types', () => { | ||
| spyOn(component as any, 'addCustomField').and.callThrough(); | ||
|
|
||
| component.importSettingsForm.patchValue({ | ||
| costTypes: { | ||
| attribute_type: 'custom_field', | ||
| source_field: 'custom_field' | ||
| } | ||
| }); | ||
|
|
||
| expect(component['addCustomField']).toHaveBeenCalled(); | ||
| expect(component.customFieldForDependentField).toBeTrue(); | ||
| expect(component.customFieldControl).toBe(component.importSettingsForm.get('costTypes')!); | ||
| expect(component.importSettingsForm.get('costTypes')?.value.source_field).toBeNull(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Avoid accessing private properties directly in tests
In the test
'should track time spent when saving settings', you're accessing the private propertysessionStartTimeusingcomponent['sessionStartTime']. Accessing private properties directly can violate encapsulation principles. Consider exposing a public method or property to set or retrievesessionStartTime, or adjust the test to avoid direct manipulation of private members.