Skip to content

Commit

Permalink
Add tests for workplace info panel
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrome committed Oct 24, 2023
1 parent ab98e1f commit 6eee37a
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { getTestBed } from '@angular/core/testing';
import { ActivatedRoute, Router, RouterModule } from '@angular/router';
import { AuthService } from '@core/services/auth.service';
import { BreadcrumbService } from '@core/services/breadcrumb.service';
import { EstablishmentService } from '@core/services/establishment.service';
import { FeatureFlagsService } from '@shared/services/feature-flags.service';
import { WindowRef } from '@core/services/window.ref';
import { MockAuthService } from '@core/test-utils/MockAuthService';
import { MockBreadcrumbService } from '@core/test-utils/MockBreadcrumbService';
import { MockFeatureFlagsService } from '@core/test-utils/MockFeatureFlagService';
import { SharedModule } from '@shared/shared.module';
import { fireEvent, render } from '@testing-library/angular';
import { of } from 'rxjs';
import { WorkplaceInfoPanelComponent } from './workplace-info-panel.component';
import { Establishment } from '../../../../mockdata/establishment';
import { MockEstablishmentServiceWithNoEmployerType } from '@core/test-utils/MockEstablishmentService';

describe('WorkplaceInfoPanel', () => {
async function setup(employerTypeHasValue = true, owner: string = 'Workplace', establishment = Establishment) {
const { fixture, getByTestId, getByText, queryByTestId, queryByText } = await render(WorkplaceInfoPanelComponent, {
imports: [SharedModule, RouterModule, HttpClientTestingModule],
declarations: [],
providers: [
{
provide: WindowRef,
useClass: WindowRef,
},
{
provide: EstablishmentService,
useFactory: MockEstablishmentServiceWithNoEmployerType.factory(employerTypeHasValue, owner),
},
{
provide: AuthService,
useClass: MockAuthService,
},
{
provide: BreadcrumbService,
useClass: MockBreadcrumbService,
},
{ provide: FeatureFlagsService, useClass: MockFeatureFlagsService },
{
provide: ActivatedRoute,
useValue: {
snapshot: {
data: {
workers: { workers: [], workerCount: 0, trainingCounts: {} },
},
},
queryParams: of({ view: null }),
},
},
],
componentProperties: {
workplace: establishment,
},
});

const component = fixture.componentInstance;

const injector = getTestBed();
const establishmentService = injector.inject(EstablishmentService) as EstablishmentService;

const router = injector.inject(Router) as Router;
const routerSpy = spyOn(router, 'navigate').and.returnValue(Promise.resolve(true));

return {
getByTestId,
queryByTestId,
queryByText,
getByText,
component,
fixture,
establishmentService,
routerSpy,
};
}

it('should render Workplace Info Component', async () => {
const { component } = await setup();
expect(component).toBeTruthy();
});

it('should navigate to the selected workplace', async () => {
const { component, establishmentService, fixture, getByText, routerSpy } = await setup();

component.newHomeDesignParentFlag = true;
component.canViewEstablishment = true;

fixture.detectChanges();

const workplaceNameLink = getByText(component.workplace.name);

spyOn(establishmentService, 'getEstablishment').and.callThrough();

fireEvent.click(workplaceNameLink);
fixture.detectChanges();

expect(workplaceNameLink.getAttribute('href')).toBeTruthy();
expect(routerSpy).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class WorkplaceInfoPanelComponent implements OnInit, OnDestroy {
this.router.navigate(['/workplace', this.workplace.uid, 'type-of-employer']);
} else if (this.newHomeDesignParentFlag) {
this.establishmentService.setIsSelectedWorkplace(true);

Check warning on line 149 in src/app/features/workplace/workplace-info-panel/workplace-info-panel.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/features/workplace/workplace-info-panel/workplace-info-panel.component.ts#L148-L149

Added lines #L148 - L149 were not covered by tests
this.router.navigate(['workplace', this.workplace.uid, 'home', { dashboard: 'dashboard' }]);
this.router.navigate(['/workplace', this.workplace.uid, 'home', { dashboard: 'dashboard' }]);
} else {
this.router.navigate(['/workplace', this.workplace.uid]);
}
Expand Down

0 comments on commit 6eee37a

Please sign in to comment.