Skip to content

Commit

Permalink
feat(cb2-9782): feature flag ADR for dev, int, preprod, prod (#1319)
Browse files Browse the repository at this point in the history
  • Loading branch information
shivangidas authored Dec 15, 2023
1 parent da337a7 commit 9e075a9
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 11 deletions.
3 changes: 1 addition & 2 deletions setenv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ require('dotenv').config({
// read the command line arguments passed with yargs
const environment = argv.environment;
const isProduction = environment === 'prod';
const isDevelop = environment === 'dev';
const targetPath = isProduction ? `./src/environments/environment.prod.ts` : `./src/environments/environment.deploy.ts`;
const cypressPath = 'cypress.env.json';

// we have access to our environment variables
// in the process.env object thanks to dotenv
const environmentFileContent = `export const environment = {
production: ${isProduction},
isDevelop: ${isDevelop},
TARGET_ENV: '${environment}',
RemoveAADFullAccessRole: ${process.env['RemoveAADFullAccessRole']},
EnableDevTools: ${process.env['EnableDevTools']},
VTM_CLIENT_ID: "${process.env['VTM_CLIENT_ID']}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { SharedModule } from '@shared/shared.module';
import { State, initialAppState } from '@store/index';
import { updateEditingTechRecord } from '@store/technical-records';
import { of } from 'rxjs';
import { FeatureToggleService } from '@services/feature-toggle-service/feature-toggle-service';
import { TechRecordSummaryComponent } from './tech-record-summary.component';

global.scrollTo = jest.fn();
Expand All @@ -28,6 +29,7 @@ describe('TechRecordSummaryComponent', () => {
let fixture: ComponentFixture<TechRecordSummaryComponent>;
let store: MockStore<State>;
let techRecordService: TechnicalRecordService;
let featureToggleService: FeatureToggleService;

beforeEach(async () => {
await TestBed.configureTestingModule({
Expand All @@ -43,6 +45,7 @@ describe('TechRecordSummaryComponent', () => {
},
},
TechnicalRecordService,
FeatureToggleService,
],
})
.overrideComponent(LettersComponent, {
Expand All @@ -58,6 +61,7 @@ describe('TechRecordSummaryComponent', () => {
fixture = TestBed.createComponent(TechRecordSummaryComponent);
store = TestBed.inject(MockStore);
techRecordService = TestBed.inject(TechnicalRecordService);
featureToggleService = TestBed.inject(FeatureToggleService);
component = fixture.componentInstance;
});

Expand Down Expand Up @@ -164,6 +168,45 @@ describe('TechRecordSummaryComponent', () => {
checkHeadingAndForm();
expect(component.vehicleType).toEqual(VehicleTypes.TRL);
});
it('should show adr section if ADR is enabled', () => {
component.isEditing = false;
jest.spyOn(featureToggleService, 'isFeatureEnabled').mockReturnValue(true);
jest
.spyOn(techRecordService, 'techRecord$', 'get')
.mockReturnValue(
of({
systemNumber: 'foo',
createdTimestamp: 'bar',
vin: 'testVin',
techRecord_vehicleType: VehicleTypes.HGV,
techRecord_adrDetails_dangerousGoods: true,
techRecord_adrDetails_applicantDetails_name: 'Test',
} as V3TechRecordModel),
);
fixture.detectChanges();

checkHeadingAndForm();
expect(component.adr).toBeDefined();
});
it('should not show adr section if ADR is disabled', () => {
component.isEditing = false;
jest.spyOn(featureToggleService, 'isFeatureEnabled').mockReturnValue(false);
jest
.spyOn(techRecordService, 'techRecord$', 'get')
.mockReturnValue(
of({
systemNumber: 'foo',
createdTimestamp: 'bar',
vin: 'testVin',
techRecord_vehicleType: VehicleTypes.TRL,
techRecord_adrDetails_applicantDetails_name: 'Test',
} as V3TechRecordModel),
);
fixture.detectChanges();

checkHeadingAndForm();
expect(component.adr).toBeUndefined();
});
});

describe('handleFormState', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
} from '@models/vehicle-tech-record.model';
import { Store } from '@ngrx/store';
import { AxlesService } from '@services/axles/axles.service';
import { FeatureToggleService } from '@services/feature-toggle-service/feature-toggle-service';
import { LoadingService } from '@services/loading/loading.service';
import { ReferenceDataService } from '@services/reference-data/reference-data.service';
import { RouterService } from '@services/router/router.service';
Expand Down Expand Up @@ -72,6 +73,7 @@ export class TechRecordSummaryComponent implements OnInit, OnDestroy {
middleIndex = 0;
isEditing = false;
scrollPosition: [number, number] = [0, 0];
isADREnabled = false;

private destroy$ = new Subject<void>();

Expand All @@ -86,9 +88,11 @@ export class TechRecordSummaryComponent implements OnInit, OnDestroy {
private viewportScroller: ViewportScroller,
private store: Store,
private loading: LoadingService,
private featureToggleService: FeatureToggleService,
) { }

ngOnInit(): void {
this.isADREnabled = this.featureToggleService.isFeatureEnabled('adrToggle');
this.technicalRecordService.techRecord$
.pipe(
map((record) => {
Expand Down Expand Up @@ -166,7 +170,8 @@ export class TechRecordSummaryComponent implements OnInit, OnDestroy {
return [];
}
return (
vehicleTemplateMap.get(this.vehicleType)?.filter((template) => template.name !== (this.isEditing ? 'audit' : 'reasonForCreationSection')) ?? []
vehicleTemplateMap.get(this.vehicleType)?.filter((template) => template.name !== (this.isEditing ? 'audit' : 'reasonForCreationSection'))
.filter((template) => template.name !== (this.isADREnabled ? '' : 'adrSection')) ?? []
);
}

Expand Down Expand Up @@ -200,12 +205,13 @@ export class TechRecordSummaryComponent implements OnInit, OnDestroy {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return [...commonCustomSections, this.psvBrakes!.form];
case VehicleTypes.HGV:
return [...commonCustomSections, this.adr.form];
return this.isADREnabled ? [...commonCustomSections, this.adr.form] : commonCustomSections;
case VehicleTypes.TRL:
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return [...commonCustomSections, this.trlBrakes!.form, this.letters.form, this.adr.form];
return this.isADREnabled ? [...commonCustomSections, this.trlBrakes!.form, this.letters.form, this.adr.form]
: [...commonCustomSections, this.trlBrakes!.form, this.letters.form];

Check warning on line 212 in src/app/features/tech-record/components/tech-record-summary/tech-record-summary.component.ts

View workflow job for this annotation

GitHub Actions / lint (18.16.0)

Forbidden non-null assertion
case VehicleTypes.LGV:
return [this.adr.form];
return this.isADREnabled ? [this.adr.form] : [];
default:
return [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TestBed } from '@angular/core/testing';
import { Store } from '@ngrx/store';
import { of } from 'rxjs';
import { FeatureToggleService } from './feature-toggle-service';
import { environment } from '../../../environments/environment';

describe('feature toggle service', () => {
let service: FeatureToggleService;
Expand Down Expand Up @@ -41,6 +42,22 @@ describe('feature toggle service', () => {
});
});

describe('getConfig', () => {
it('should return the correct feature toggle config', () => {
environment.TARGET_ENV = 'dev';
expect(service.getConfig()).toBe('assets/featureToggle.json');

environment.TARGET_ENV = 'integration';
expect(service.getConfig()).toBe('assets/featureToggle.int.json');

environment.TARGET_ENV = 'preprod';
expect(service.getConfig()).toBe('assets/featureToggle.preprod.json');

environment.TARGET_ENV = 'prop';
expect(service.getConfig()).toBe('assets/featureToggle.prod.json');
});
});

describe('isFeatureEnabled', () => {
it('should return false if there is no config', () => {
service.config = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface FeatureConfig {
})
export class FeatureToggleService {
config: FeatureConfig | null = null;
configPath = environment.isDevelop ? 'assets/featureToggle.json' : 'assets/featureToggle.prod.json';
configPath = this.getConfig();

constructor(private http: HttpClient) {}

Expand All @@ -22,6 +22,19 @@ export class FeatureToggleService {
return this.config = await lastValueFrom(this.http.get<FeatureConfig>(this.configPath).pipe(take(1)));
}

getConfig() {
switch (environment.TARGET_ENV) {
case 'dev':
return 'assets/featureToggle.json';
case 'integration':
return 'assets/featureToggle.int.json';
case 'preprod':
return 'assets/featureToggle.preprod.json';
default:
return 'assets/featureToggle.prod.json';
}
}

isFeatureEnabled(key: string) {
if (this.config && has(this.config, key)) {
return get(this.config, key, false);
Expand Down
5 changes: 5 additions & 0 deletions src/assets/featureToggle.int.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"testToggle": false,
"adrToggle": true
}

3 changes: 2 additions & 1 deletion src/assets/featureToggle.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"testToggle": true
"testToggle": true,
"adrToggle": true
}
5 changes: 5 additions & 0 deletions src/assets/featureToggle.preprod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"testToggle": false,
"adrToggle": false
}

3 changes: 2 additions & 1 deletion src/assets/featureToggle.prod.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"testToggle": false
"testToggle": false,
"adrToggle": false
}
2 changes: 1 addition & 1 deletion src/environments/environment.prod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const environment = {
production: true,
isDevelop: false,
TARGET_ENV: 'prod',
RemoveAADFullAccessRole: true,
EnableDevTools: false,
VTM_CLIENT_ID: '',
Expand Down
2 changes: 1 addition & 1 deletion src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

export const environment = {
production: false,
isDevelop: false,
TARGET_ENV: 'dev',
RemoveAADFullAccessRole: true,
EnableDevTools: true,
VTM_CLIENT_ID: '',
Expand Down

0 comments on commit 9e075a9

Please sign in to comment.