1+ /* eslint-disable dot-notation */
12import { ComponentFixture , fakeAsync , TestBed , tick } from '@angular/core/testing' ;
2- import { AbstractControl , FormArray , FormBuilder , ReactiveFormsModule , Validators } from '@angular/forms' ;
3+ import { AbstractControl , FormArray , FormBuilder , ReactiveFormsModule } from '@angular/forms' ;
34import { provideRouter , Router , RouterModule } from '@angular/router' ;
45import { of , throwError } from 'rxjs' ;
56
@@ -12,17 +13,13 @@ import { TrackingService } from 'src/app/core/services/integration/tracking.serv
1213import { IntegrationsToastService } from 'src/app/core/services/common/integrations-toast.service' ;
1314import { StorageService } from 'src/app/core/services/common/storage.service' ;
1415import { SiWorkspaceService } from 'src/app/core/services/si/si-core/si-workspace.service' ;
15- import { HelperService } from 'src/app/core/services/common/helper.service' ;
16-
1716import { configuration , costCodeFieldValue , costTypeFieldValue , fyleFields , groupedDestinationAttributes , importSettings , importSettingsWithProject , intacctImportCodeConfig , locationEntityMapping , sageIntacctFields , sageIntacctFieldsSortedByPriority , settingsWithDependentFields } from '../../intacct.fixture' ;
18- import { IntacctCategoryDestination , IntacctOnboardingState , ToastSeverity } from 'src/app/core/models/enum/enum.model' ;
17+ import { IntacctCategoryDestination , IntacctOnboardingState , IntacctUpdateEvent , Page , ProgressPhase , ToastSeverity , TrackingApp } from 'src/app/core/models/enum/enum.model' ;
1918import { SharedModule } from 'src/app/shared/shared.module' ;
2019import { Org } from 'src/app/core/models/org/org.model' ;
2120import { HttpClientTestingModule } from '@angular/common/http/testing' ;
22- import { LocationEntityMapping } from 'src/app/core/models/intacct/db/location-entity-mapping.model' ;
2321import { IntacctSharedModule } from '../intacct-shared.module' ;
2422import { ExpenseField } from 'src/app/core/models/intacct/db/expense-field.model' ;
25- import { ImportSettingGet } from 'src/app/core/models/intacct/intacct-configuration/import-settings.model' ;
2623import { IntacctConfiguration } from 'src/app/core/models/db/configuration.model' ;
2724
2825describe ( 'IntacctImportSettingsComponent' , ( ) => {
@@ -37,6 +34,7 @@ describe('IntacctImportSettingsComponent', () => {
3734 let storageService : jasmine . SpyObj < StorageService > ;
3835 let siWorkspaceService : jasmine . SpyObj < SiWorkspaceService > ;
3936 let router : Router ;
37+ let routerUrlSpy : jasmine . Spy < ( this : Router ) => string > ;
4038
4139 beforeEach ( async ( ) => {
4240 const siImportSettingServiceSpy = jasmine . createSpyObj ( 'SiImportSettingService' , [ 'getImportSettings' , 'postImportSettings' , 'getImportCodeFieldConfig' ] ) ;
@@ -77,7 +75,7 @@ describe('IntacctImportSettingsComponent', () => {
7775 router = TestBed . inject ( Router ) ;
7876
7977 spyOn ( router , 'navigate' ) ;
80- spyOnProperty ( router , 'url' ) . and . returnValue ( '/onboarding' ) ;
78+ routerUrlSpy = spyOnProperty ( router , 'url' ) . and . returnValue ( '/onboarding' ) ;
8179 siImportSettingService . getImportSettings . and . returnValue ( of ( importSettings ) ) ;
8280 siImportSettingService . getImportCodeFieldConfig . and . returnValue ( of ( intacctImportCodeConfig ) ) ;
8381 siMappingsService . getSageIntacctFields . and . returnValue ( of ( sageIntacctFields ) ) ;
@@ -98,7 +96,7 @@ describe('IntacctImportSettingsComponent', () => {
9896 expect ( component ) . toBeTruthy ( ) ;
9997 } ) ;
10098
101- describe ( 'Form Initialization' , ( ) => {
99+ describe ( 'Component Initialization' , ( ) => {
102100 beforeEach ( ( ) => {
103101 component . ngOnInit ( ) ;
104102 fixture . detectChanges ( ) ;
@@ -203,4 +201,90 @@ describe('IntacctImportSettingsComponent', () => {
203201 } ) ;
204202 } ) ;
205203 } ) ;
204+
205+ describe ( 'Save' , ( ) => {
206+ it ( 'should successfully save import settings during onboarding' , fakeAsync ( ( ) => {
207+ siWorkspaceService . getIntacctOnboardingState . and . returnValue ( IntacctOnboardingState . IMPORT_SETTINGS ) ;
208+ routerUrlSpy . and . returnValue ( '/onboarding' ) ;
209+ siImportSettingService . postImportSettings . and . returnValue ( of ( importSettings ) ) ;
210+
211+ component . ngOnInit ( ) ;
212+ tick ( ) ;
213+
214+ component . save ( ) ;
215+ tick ( ) ;
216+
217+ expect ( siImportSettingService . postImportSettings ) . toHaveBeenCalled ( ) ;
218+ expect ( toastService . displayToastMessage ) . toHaveBeenCalledWith ( ToastSeverity . SUCCESS , 'Import settings saved successfully' ) ;
219+ expect ( trackingService . integrationsOnboardingCompletion ) . toHaveBeenCalledWith (
220+ TrackingApp . INTACCT ,
221+ IntacctOnboardingState . IMPORT_SETTINGS ,
222+ 3 ,
223+ jasmine . any ( Object )
224+ ) ;
225+ expect ( siWorkspaceService . setIntacctOnboardingState ) . toHaveBeenCalledWith ( IntacctOnboardingState . ADVANCED_CONFIGURATION ) ;
226+ expect ( router . navigate ) . toHaveBeenCalledWith ( [ '/integrations/intacct/onboarding/advanced_settings' ] ) ;
227+ expect ( component . saveInProgress ) . toBeFalse ( ) ;
228+ } ) ) ;
229+
230+ it ( 'should successfully save import settings post-onboarding' , fakeAsync ( ( ) => {
231+ siWorkspaceService . getIntacctOnboardingState . and . returnValue ( IntacctOnboardingState . COMPLETE ) ;
232+ routerUrlSpy . and . returnValue ( '/settings' ) ;
233+ siImportSettingService . postImportSettings . and . returnValue ( of ( importSettings ) ) ;
234+
235+ component . ngOnInit ( ) ;
236+ tick ( ) ;
237+
238+ component . save ( ) ;
239+ tick ( ) ;
240+
241+ expect ( siImportSettingService . postImportSettings ) . toHaveBeenCalled ( ) ;
242+ expect ( toastService . displayToastMessage ) . toHaveBeenCalledWith ( ToastSeverity . SUCCESS , 'Import settings saved successfully' ) ;
243+ expect ( trackingService . intacctUpdateEvent ) . toHaveBeenCalledWith (
244+ IntacctUpdateEvent . ADVANCED_SETTINGS_INTACCT ,
245+ {
246+ phase : ProgressPhase . POST_ONBOARDING ,
247+ oldState : component . importSettings ,
248+ newState : importSettings
249+ }
250+ ) ;
251+ expect ( router . navigate ) . not . toHaveBeenCalled ( ) ;
252+ expect ( component . saveInProgress ) . toBeFalse ( ) ;
253+ } ) ) ;
254+
255+ it ( 'should handle error when saving import settings' , fakeAsync ( ( ) => {
256+ siImportSettingService . postImportSettings . and . returnValue ( throwError ( ( ) => new Error ( ) ) ) ;
257+
258+ component . ngOnInit ( ) ;
259+ tick ( ) ;
260+
261+ component . save ( ) ;
262+ tick ( ) ;
263+
264+ expect ( siImportSettingService . postImportSettings ) . toHaveBeenCalled ( ) ;
265+ expect ( toastService . displayToastMessage ) . toHaveBeenCalledWith (
266+ ToastSeverity . ERROR ,
267+ 'Error saving import settings, please try again later'
268+ ) ;
269+ expect ( component . saveInProgress ) . toBeFalse ( ) ;
270+ } ) ) ;
271+
272+ it ( 'should track time spent when saving settings' , fakeAsync ( ( ) => {
273+ const mockStartTime = new Date ( ) ;
274+ component [ 'sessionStartTime' ] = mockStartTime ;
275+ siImportSettingService . postImportSettings . and . returnValue ( of ( importSettings ) ) ;
276+
277+ component . ngOnInit ( ) ;
278+ tick ( ) ;
279+
280+ component . save ( ) ;
281+ tick ( ) ;
282+
283+ expect ( trackingService . trackTimeSpent ) . toHaveBeenCalledWith (
284+ TrackingApp . INTACCT ,
285+ Page . IMPORT_SETTINGS_INTACCT ,
286+ mockStartTime
287+ ) ;
288+ } ) ) ;
289+ } ) ;
206290} ) ;
0 commit comments