Skip to content

Commit

Permalink
Refactor tech debt + add tests for compliance tab
Browse files Browse the repository at this point in the history
  • Loading branch information
sggerard committed Nov 25, 2024
1 parent cbe86e1 commit 48942e7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
3 changes: 1 addition & 2 deletions frontend/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BrowserModule } from '@angular/platform-browser';
import { NgModule, APP_INITIALIZER, inject } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpClientModule, provideHttpClient } from '@angular/common/http';
import { provideHttpClient } from '@angular/common/http';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { AppRoutingModule } from './app.routes';
import { NgxPaginationModule } from 'ngx-pagination';
Expand Down Expand Up @@ -57,7 +57,6 @@ export function initConfig(configService: ConfigService) {
BrowserAnimationsModule,
BrowserModule,
FormsModule,
HttpClientModule,
ProjectsModule, // <-- module import order matters - https://angular.io/guide/router#module-import-order-matters
EnforcementActionsModule,
AppRoutingModule,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { waitForAsync, ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { ObjectFilterPipe } from '@pipes/object-filter.pipe';
import { OperatorFilterPipe } from '@pipes/operator-filter.pipe';
import { ProjectStatusFilterPipe } from '@pipes/project-status-filter.pipe';
Expand All @@ -11,34 +10,30 @@ import { ConfigService } from '@services/config.service';

import { ComplianceTabContentComponent } from '@projects/project-detail/compliance//compliance-tab-content.component';
import { OrderByPipe } from '@pipes/filters/order-by.pipe';
import { HttpClientModule } from '@angular/common/http';
import { Subscription } from 'rxjs';
import { of } from 'rxjs';
import { ContentService } from '@app/services/content-service';
import { Apollo } from 'apollo-angular';

describe('ComplianceTabContentComponent', () => {
let component: ComplianceTabContentComponent;
let fixture: ComponentFixture<ComplianceTabContentComponent>;
let configService: ConfigService;
let ActivatedRouteStub;

beforeEach(waitForAsync(() => {
// stub activated route
ActivatedRouteStub = {
parent: {
data: {
subscribe: (next: (value) => void) => {
next({project: Project});
const sub = new Subscription();
sub.unsubscribe = jest.fn();
return sub;
}
}
data: of({project: Project})
}
};
const configServiceMock = {
checkFeatureFlag: jest.fn() // Create a mock function
};
TestBed.configureTestingModule({
providers: [
{ provide: ActivatedRoute, useValue: ActivatedRouteStub },
ConfigService,
{ provide: ConfigService, useValue: configServiceMock },
ContentService,
Apollo
],
Expand All @@ -51,8 +46,7 @@ describe('ComplianceTabContentComponent', () => {
OrderByPipe
],
imports: [
RouterTestingModule,
HttpClientModule

]
})
.compileComponents();
Expand All @@ -61,6 +55,7 @@ describe('ComplianceTabContentComponent', () => {
beforeEach(() => {
fixture = TestBed.createComponent(ComplianceTabContentComponent);
component = fixture.componentInstance;
configService = TestBed.inject(ConfigService);
component.project = new Project();
component.project.moreInspectionsLink = null;
component.collections = new CollectionsArray();
Expand All @@ -73,6 +68,9 @@ describe('ComplianceTabContentComponent', () => {
it('should return project data', () => {
expect(component.project).toBeTruthy();
});
it('should check feature flag', () => {
expect(configService.checkFeatureFlag).toHaveBeenCalledWith('nrced-link', 'true');
})
});
describe('parseData(data)', () => {
beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,22 @@ export class ComplianceTabContentComponent implements OnInit, OnDestroy {

NRCEDLinkEnabled: boolean;

constructor(private route: ActivatedRoute, private logger: LoggerService, private configService: ConfigService) { }
constructor(private readonly route: ActivatedRoute, private readonly logger: LoggerService, private readonly configService: ConfigService) { }

ngOnInit(): void {
this.loading = true;
this.sub = this.route.parent.data.subscribe(
(data: { project: Project }) => this.parseData(data),
error => this.logger.log(error),
() => this.loading = false
);

this.sub = this.route.parent.data.subscribe({
next: (data: { project: Project }) => this.parseData(data),
error: (error) => this.logger.log(error),
complete: () => this.loading = false
})

this.NRCEDLinkEnabled = this.configService.checkFeatureFlag("nrced-link","true");
}

parseData(data: {project: Project}): void {
if (data.project && data.project.collections) {
if (data.project?.collections) {
this.project = data.project;
this.collections = data.project.collections.compliance;

Expand Down

0 comments on commit 48942e7

Please sign in to comment.