diff --git a/src/app/integrations/intacct/intacct.component.spec.ts b/src/app/integrations/intacct/intacct.component.spec.ts index 138779464..498fce260 100644 --- a/src/app/integrations/intacct/intacct.component.spec.ts +++ b/src/app/integrations/intacct/intacct.component.spec.ts @@ -1,5 +1,5 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { Router, NavigationEnd, RouterModule } from '@angular/router'; +import { Router, NavigationEnd, provideRouter } from '@angular/router'; import { of } from 'rxjs'; import { IntacctComponent } from './intacct.component'; import { HelperService } from 'src/app/core/services/common/helper.service'; @@ -8,9 +8,11 @@ import { WindowService } from 'src/app/core/services/common/window.service'; import { AppcuesService } from 'src/app/core/services/integration/appcues.service'; import { UserService } from 'src/app/core/services/misc/user.service'; import { SiWorkspaceService } from 'src/app/core/services/si/si-core/si-workspace.service'; -import { AppName, AppUrl, IntacctOnboardingState } from 'src/app/core/models/enum/enum.model'; +import { AppUrl, IntacctOnboardingState } from 'src/app/core/models/enum/enum.model'; import { mockUser, testOnboardingState, workspaceResponse } from './intacct.fixture'; import { IntacctWorkspace } from 'src/app/core/models/intacct/db/workspaces.model'; +import { SharedModule } from 'src/app/shared/shared.module'; +import { HttpClientTestingModule } from '@angular/common/http/testing'; describe('IntacctComponent', () => { let component: IntacctComponent; @@ -19,16 +21,15 @@ describe('IntacctComponent', () => { let workspaceServiceSpy: jasmine.SpyObj; let helperServiceSpy: jasmine.SpyObj; let storageServiceSpy: jasmine.SpyObj; - let routerSpy: jasmine.SpyObj; let windowServiceMock: Partial; let appcuesServiceSpy: jasmine.SpyObj; + let router: Router; beforeEach(async () => { const userSpy = jasmine.createSpyObj('UserService', ['getUserProfile']); const workspaceSpy = jasmine.createSpyObj('SiWorkspaceService', ['getWorkspace', 'postWorkspace', 'syncFyleDimensions', 'syncIntacctDimensions']); const helperSpy = jasmine.createSpyObj('HelperService', ['setBaseApiURL']); const storageSpy = jasmine.createSpyObj('StorageService', ['set']); - const routerSpyObj = jasmine.createSpyObj('Router', ['navigateByUrl', 'events']); const appcuesSpy = jasmine.createSpyObj('AppcuesService', ['initialiseAppcues']); windowServiceMock = { @@ -42,16 +43,16 @@ describe('IntacctComponent', () => { }; await TestBed.configureTestingModule({ - declarations: [ IntacctComponent ], - imports: [RouterModule], + declarations: [IntacctComponent], + imports: [SharedModule, HttpClientTestingModule], providers: [ { provide: HelperService, useValue: helperSpy }, { provide: AppcuesService, useValue: appcuesSpy }, - { provide: Router, useValue: routerSpyObj }, { provide: StorageService, useValue: storageSpy }, { provide: UserService, useValue: userSpy }, { provide: SiWorkspaceService, useValue: workspaceSpy }, - { provide: WindowService, useValue: windowServiceMock } + { provide: WindowService, useValue: windowServiceMock }, + provideRouter([]) ] }).compileComponents(); @@ -59,10 +60,11 @@ describe('IntacctComponent', () => { workspaceServiceSpy = TestBed.inject(SiWorkspaceService) as jasmine.SpyObj; helperServiceSpy = TestBed.inject(HelperService) as jasmine.SpyObj; storageServiceSpy = TestBed.inject(StorageService) as jasmine.SpyObj; - routerSpy = TestBed.inject(Router) as jasmine.SpyObj; - (routerSpy.events as any) = of(new NavigationEnd(0, '', '')); + router = TestBed.inject(Router); appcuesServiceSpy = TestBed.inject(AppcuesService) as jasmine.SpyObj; + spyOn(router, 'navigateByUrl'); + spyOnProperty(router, 'events').and.returnValue(of(new NavigationEnd(0, '', ''))); userServiceSpy.getUserProfile.and.returnValue(mockUser); workspaceServiceSpy.getWorkspace.and.returnValue(of(workspaceResponse)); workspaceServiceSpy.syncFyleDimensions.and.returnValue(of()); @@ -85,7 +87,7 @@ describe('IntacctComponent', () => { expect(storageServiceSpy.set).toHaveBeenCalledWith('onboarding-state', IntacctOnboardingState.CONNECTION); expect(workspaceServiceSpy.syncFyleDimensions).toHaveBeenCalled(); expect(workspaceServiceSpy.syncIntacctDimensions).toHaveBeenCalled(); - expect(routerSpy.navigateByUrl).toHaveBeenCalledWith('/integrations/intacct/onboarding/landing'); + expect(router.navigateByUrl).toHaveBeenCalledWith('/integrations/intacct/onboarding/landing'); }); it('should create a new workspace if none exists', () => { @@ -97,18 +99,17 @@ describe('IntacctComponent', () => { expect(workspaceServiceSpy.postWorkspace).toHaveBeenCalled(); expect(storageServiceSpy.set).toHaveBeenCalledWith('workspaceId', 1); expect(storageServiceSpy.set).toHaveBeenCalledWith('onboarding-state', IntacctOnboardingState.CONNECTION); - expect(routerSpy.navigateByUrl).toHaveBeenCalledWith('/integrations/intacct/onboarding/landing'); + expect(router.navigateByUrl).toHaveBeenCalledWith('/integrations/intacct/onboarding/landing'); }); it('should navigate to correct route based on onboarding state', () => { - Object.entries(testOnboardingState).forEach(([state, route ]) => { - routerSpy.navigateByUrl.calls.reset(); + Object.entries(testOnboardingState).forEach(([state, route]) => { const testWorkspace: IntacctWorkspace = { ...workspaceResponse[0], onboarding_state: state as IntacctOnboardingState }; workspaceServiceSpy.getWorkspace.and.returnValue(of([testWorkspace])); fixture.detectChanges(); - expect(routerSpy.navigateByUrl).toHaveBeenCalledWith(route); + expect(router.navigateByUrl).toHaveBeenCalledWith(route); fixture = TestBed.createComponent(IntacctComponent); component = fixture.componentInstance; @@ -119,7 +120,7 @@ describe('IntacctComponent', () => { component.windowReference.location.pathname = '/some/other/path'; fixture.detectChanges(); - expect(routerSpy.navigateByUrl).not.toHaveBeenCalled(); + expect(router.navigateByUrl).not.toHaveBeenCalled(); }); it('should initialise Appcues', () => { diff --git a/src/app/integrations/qbo/qbo.component.spec.ts b/src/app/integrations/qbo/qbo.component.spec.ts index 1a8f699d8..24f32eb11 100644 --- a/src/app/integrations/qbo/qbo.component.spec.ts +++ b/src/app/integrations/qbo/qbo.component.spec.ts @@ -1,5 +1,5 @@ import { ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing'; -import { Router } from '@angular/router'; +import { provideRouter, Router } from '@angular/router'; import { of } from 'rxjs'; import { QboComponent } from './qbo.component'; import { HelperService } from 'src/app/core/services/common/helper.service'; @@ -10,22 +10,23 @@ import { WorkspaceService } from 'src/app/core/services/common/workspace.service import { QboHelperService } from 'src/app/core/services/qbo/qbo-core/qbo-helper.service'; import { QBOOnboardingState, AppUrl } from 'src/app/core/models/enum/enum.model'; import { mockUser, mockWorkspace, testOnboardingState } from './qbo.fixture'; +import { SharedModule } from 'src/app/shared/shared.module'; +import { HttpClientTestingModule } from '@angular/common/http/testing'; describe('QboComponent', () => { let component: QboComponent; let fixture: ComponentFixture; let helperServiceSpy: jasmine.SpyObj; let qboHelperServiceSpy: jasmine.SpyObj; - let routerSpy: jasmine.SpyObj; let storageServiceSpy: jasmine.SpyObj; let userServiceSpy: jasmine.SpyObj; let workspaceServiceSpy: jasmine.SpyObj; let windowServiceMock: Partial; + let router: Router; beforeEach(async () => { const helperSpy = jasmine.createSpyObj('HelperService', ['setBaseApiURL']); const qboHelperSpy = jasmine.createSpyObj('QboHelperService', ['syncFyleDimensions', 'syncQBODimensions']); - const routerSpyObj = jasmine.createSpyObj('Router', ['navigateByUrl']); const storageSpy = jasmine.createSpyObj('StorageService', ['set']); const userSpy = jasmine.createSpyObj('IntegrationsUserService', ['getUserProfile']); const workspaceSpy = jasmine.createSpyObj('WorkspaceService', ['getWorkspace', 'postWorkspace']); @@ -46,24 +47,26 @@ describe('QboComponent', () => { }; await TestBed.configureTestingModule({ - declarations: [ QboComponent ], + declarations: [QboComponent], + imports: [SharedModule, HttpClientTestingModule], providers: [ { provide: HelperService, useValue: helperSpy }, { provide: QboHelperService, useValue: qboHelperSpy }, - { provide: Router, useValue: routerSpyObj }, { provide: StorageService, useValue: storageSpy }, { provide: IntegrationsUserService, useValue: userSpy }, { provide: WorkspaceService, useValue: workspaceSpy }, - { provide: WindowService, useValue: windowServiceMock } + { provide: WindowService, useValue: windowServiceMock }, + provideRouter([]) ] }).compileComponents(); helperServiceSpy = TestBed.inject(HelperService) as jasmine.SpyObj; qboHelperServiceSpy = TestBed.inject(QboHelperService) as jasmine.SpyObj; - routerSpy = TestBed.inject(Router) as jasmine.SpyObj; storageServiceSpy = TestBed.inject(StorageService) as jasmine.SpyObj; userServiceSpy = TestBed.inject(IntegrationsUserService) as jasmine.SpyObj; workspaceServiceSpy = TestBed.inject(WorkspaceService) as jasmine.SpyObj; + router = TestBed.inject(Router); + spyOn(router, 'navigateByUrl'); userServiceSpy.getUserProfile.and.returnValue(mockUser); qboHelperServiceSpy.syncFyleDimensions.and.returnValue(of(null)); @@ -91,7 +94,7 @@ describe('QboComponent', () => { expect(storageServiceSpy.set).toHaveBeenCalledWith('onboarding-state', QBOOnboardingState.CONNECTION); expect(qboHelperServiceSpy.syncFyleDimensions).toHaveBeenCalled(); expect(qboHelperServiceSpy.syncQBODimensions).toHaveBeenCalled(); - expect(routerSpy.navigateByUrl).toHaveBeenCalledWith('/integrations/qbo/onboarding/landing'); + expect(router.navigateByUrl).toHaveBeenCalledWith('/integrations/qbo/onboarding/landing'); })); it('should create a new workspace if none exists', fakeAsync(() => { @@ -104,12 +107,11 @@ describe('QboComponent', () => { expect(workspaceServiceSpy.postWorkspace).toHaveBeenCalled(); expect(storageServiceSpy.set).toHaveBeenCalledWith('workspaceId', '1'); expect(storageServiceSpy.set).toHaveBeenCalledWith('onboarding-state', QBOOnboardingState.CONNECTION); - expect(routerSpy.navigateByUrl).toHaveBeenCalledWith('/integrations/qbo/onboarding/landing'); + expect(router.navigateByUrl).toHaveBeenCalledWith('/integrations/qbo/onboarding/landing'); })); it('should navigate to correct route based on onboarding state', fakeAsync(() => { - testOnboardingState.forEach(({ state, route }) => { - routerSpy.navigateByUrl.calls.reset(); + testOnboardingState.forEach(({ state }) => { const testWorkspace = { ...mockWorkspace, onboarding_state: state }; workspaceServiceSpy.getWorkspace.and.returnValue(of([testWorkspace])); @@ -125,6 +127,6 @@ describe('QboComponent', () => { fixture.detectChanges(); tick(); - expect(routerSpy.navigateByUrl).toHaveBeenCalled(); + expect(router.navigateByUrl).toHaveBeenCalled(); })); }); \ No newline at end of file