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: fix failing tests due to outdated router spies #1004

Merged
merged 1 commit into from
Oct 8, 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
33 changes: 17 additions & 16 deletions src/app/integrations/intacct/intacct.component.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand All @@ -19,16 +21,15 @@ describe('IntacctComponent', () => {
let workspaceServiceSpy: jasmine.SpyObj<SiWorkspaceService>;
let helperServiceSpy: jasmine.SpyObj<HelperService>;
let storageServiceSpy: jasmine.SpyObj<StorageService>;
let routerSpy: jasmine.SpyObj<Router>;
let windowServiceMock: Partial<WindowService>;
let appcuesServiceSpy: jasmine.SpyObj<AppcuesService>;
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 = {
Expand All @@ -42,27 +43,28 @@ 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();

userServiceSpy = TestBed.inject(UserService) as jasmine.SpyObj<UserService>;
workspaceServiceSpy = TestBed.inject(SiWorkspaceService) as jasmine.SpyObj<SiWorkspaceService>;
helperServiceSpy = TestBed.inject(HelperService) as jasmine.SpyObj<HelperService>;
storageServiceSpy = TestBed.inject(StorageService) as jasmine.SpyObj<StorageService>;
routerSpy = TestBed.inject(Router) as jasmine.SpyObj<Router>;
(routerSpy.events as any) = of(new NavigationEnd(0, '', ''));
router = TestBed.inject(Router);
appcuesServiceSpy = TestBed.inject(AppcuesService) as jasmine.SpyObj<AppcuesService>;

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());
Expand All @@ -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', () => {
Expand All @@ -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;
Expand All @@ -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', () => {
Expand Down
26 changes: 14 additions & 12 deletions src/app/integrations/qbo/qbo.component.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<QboComponent>;
let helperServiceSpy: jasmine.SpyObj<HelperService>;
let qboHelperServiceSpy: jasmine.SpyObj<QboHelperService>;
let routerSpy: jasmine.SpyObj<Router>;
let storageServiceSpy: jasmine.SpyObj<StorageService>;
let userServiceSpy: jasmine.SpyObj<IntegrationsUserService>;
let workspaceServiceSpy: jasmine.SpyObj<WorkspaceService>;
let windowServiceMock: Partial<WindowService>;
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']);
Expand All @@ -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<HelperService>;
qboHelperServiceSpy = TestBed.inject(QboHelperService) as jasmine.SpyObj<QboHelperService>;
routerSpy = TestBed.inject(Router) as jasmine.SpyObj<Router>;
storageServiceSpy = TestBed.inject(StorageService) as jasmine.SpyObj<StorageService>;
userServiceSpy = TestBed.inject(IntegrationsUserService) as jasmine.SpyObj<IntegrationsUserService>;
workspaceServiceSpy = TestBed.inject(WorkspaceService) as jasmine.SpyObj<WorkspaceService>;
router = TestBed.inject(Router);
spyOn(router, 'navigateByUrl');

userServiceSpy.getUserProfile.and.returnValue(mockUser);
qboHelperServiceSpy.syncFyleDimensions.and.returnValue(of(null));
Expand Down Expand Up @@ -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(() => {
Expand All @@ -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]));

Expand All @@ -125,6 +127,6 @@ describe('QboComponent', () => {
fixture.detectChanges();
tick();

expect(routerSpy.navigateByUrl).toHaveBeenCalled();
expect(router.navigateByUrl).toHaveBeenCalled();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Consistent update to navigation expectation, but potential logical error in the test.

The change to use router.navigateByUrl instead of routerSpy.navigateByUrl is consistent with the earlier modifications and maintains the new approach of using a real Router instance.

However, there's a potential issue with the test logic:

The test description states "should not navigate if pathname is not /integrations/qbo", but the expectation expect(router.navigateByUrl).toHaveBeenCalled() checks if navigation did occur. This seems contradictory.

Consider updating the expectation to match the test description:

expect(router.navigateByUrl).not.toHaveBeenCalled();

This would correctly verify that no navigation occurs when the pathname is not "/integrations/qbo".

}));
});
Loading