From 073bcdd88bdc7b18e14f0ed35bffd57a834287d8 Mon Sep 17 00:00:00 2001 From: pbardy2000 <146740183+pbardy2000@users.noreply.github.com> Date: Mon, 16 Sep 2024 14:03:31 +0100 Subject: [PATCH 1/2] fix(cb2-14000): handle response type text correctly --- .../tech-record-amend-vin.component.ts | 2 +- .../tech-record-generate-letter.component.ts | 2 +- .../tech-record-search-tyres.component.ts | 2 +- .../tech-record-title.component.ts | 4 +- .../vehicle-technical-record.component.ts | 2 +- .../__tests__/custom-async-validators.spec.ts | 387 ++++++++++-------- .../dynamic-forms/dynamic-form.service.ts | 2 +- src/app/services/http/http.service.ts | 29 +- .../technical-records/batch-create.reducer.ts | 3 +- 9 files changed, 236 insertions(+), 197 deletions(-) diff --git a/src/app/features/tech-record/components/tech-record-amend-vin/tech-record-amend-vin.component.ts b/src/app/features/tech-record/components/tech-record-amend-vin/tech-record-amend-vin.component.ts index bb8cf26bd..b16bf6274 100644 --- a/src/app/features/tech-record/components/tech-record-amend-vin/tech-record-amend-vin.component.ts +++ b/src/app/features/tech-record/components/tech-record-amend-vin/tech-record-amend-vin.component.ts @@ -60,7 +60,7 @@ export class AmendVinComponent implements OnDestroy, OnInit { } get currentVrm(): string | undefined { - return this.techRecord?.techRecord_vehicleType !== 'trl' ? (this.techRecord?.primaryVrm ?? '') : undefined; + return this.techRecord?.techRecord_vehicleType !== 'trl' ? this.techRecord?.primaryVrm ?? '' : undefined; } isFormValid(): boolean { diff --git a/src/app/features/tech-record/components/tech-record-generate-letter/tech-record-generate-letter.component.ts b/src/app/features/tech-record/components/tech-record-generate-letter/tech-record-generate-letter.component.ts index e9021f344..4a5fd5f34 100644 --- a/src/app/features/tech-record/components/tech-record-generate-letter/tech-record-generate-letter.component.ts +++ b/src/app/features/tech-record/components/tech-record-generate-letter/tech-record-generate-letter.component.ts @@ -80,7 +80,7 @@ export class GenerateLetterComponent implements OnInit { get emailAddress(): string | undefined { return this.techRecord?.techRecord_vehicleType === 'trl' - ? (this.techRecord?.techRecord_applicantDetails_emailAddress ?? '') + ? this.techRecord?.techRecord_applicantDetails_emailAddress ?? '' : undefined; } diff --git a/src/app/features/tech-record/components/tech-record-search-tyres/tech-record-search-tyres.component.ts b/src/app/features/tech-record/components/tech-record-search-tyres/tech-record-search-tyres.component.ts index 47f8fecdb..1159eaa37 100644 --- a/src/app/features/tech-record/components/tech-record-search-tyres/tech-record-search-tyres.component.ts +++ b/src/app/features/tech-record/components/tech-record-search-tyres/tech-record-search-tyres.component.ts @@ -107,7 +107,7 @@ export class TechRecordSearchTyresComponent implements OnInit { } get currentVrm(): string | undefined { return this.vehicleTechRecord?.techRecord_vehicleType !== 'trl' - ? (this.vehicleTechRecord?.primaryVrm ?? '') + ? this.vehicleTechRecord?.primaryVrm ?? '' : undefined; } get paginatedFields(): ReferenceDataTyre[] { diff --git a/src/app/features/tech-record/components/tech-record-title/tech-record-title.component.ts b/src/app/features/tech-record/components/tech-record-title/tech-record-title.component.ts index 6200fe693..21056f613 100644 --- a/src/app/features/tech-record/components/tech-record-title/tech-record-title.component.ts +++ b/src/app/features/tech-record/components/tech-record-title/tech-record-title.component.ts @@ -47,11 +47,11 @@ export class TechRecordTitleComponent implements OnInit { } get currentVrm(): string | undefined { - return this.vehicle?.techRecord_vehicleType !== 'trl' ? (this.vehicle?.primaryVrm ?? '') : undefined; + return this.vehicle?.techRecord_vehicleType !== 'trl' ? this.vehicle?.primaryVrm ?? '' : undefined; } get otherVrms(): string[] | undefined { - return this.vehicle?.techRecord_vehicleType !== 'trl' ? (this.vehicle?.secondaryVrms ?? []) : undefined; + return this.vehicle?.techRecord_vehicleType !== 'trl' ? this.vehicle?.secondaryVrms ?? [] : undefined; } get vehicleTypes(): typeof VehicleTypes { diff --git a/src/app/features/tech-record/components/vehicle-technical-record/vehicle-technical-record.component.ts b/src/app/features/tech-record/components/vehicle-technical-record/vehicle-technical-record.component.ts index 2ae3b7c69..286b2f24c 100644 --- a/src/app/features/tech-record/components/vehicle-technical-record/vehicle-technical-record.component.ts +++ b/src/app/features/tech-record/components/vehicle-technical-record/vehicle-technical-record.component.ts @@ -85,7 +85,7 @@ export class VehicleTechnicalRecordComponent implements OnInit, OnDestroy { } get currentVrm(): string | undefined { - return this.techRecord?.techRecord_vehicleType !== 'trl' ? (this.techRecord?.primaryVrm ?? '') : undefined; + return this.techRecord?.techRecord_vehicleType !== 'trl' ? this.techRecord?.primaryVrm ?? '' : undefined; } get roles(): typeof Roles { diff --git a/src/app/forms/validators/custom-async-validator/__tests__/custom-async-validators.spec.ts b/src/app/forms/validators/custom-async-validator/__tests__/custom-async-validators.spec.ts index 2b3f5adda..dbafd9ef9 100644 --- a/src/app/forms/validators/custom-async-validator/__tests__/custom-async-validators.spec.ts +++ b/src/app/forms/validators/custom-async-validator/__tests__/custom-async-validators.spec.ts @@ -550,89 +550,101 @@ describe('requiredIfNotResultAndSiblingEquals', () => { }); }); - it('should be required when result is "pass" and "bar is "x" and ' + - 'validator specifies requiredIfNotResultAndSiblingEquals "fail" when sibling "bar" is "x"', async () => { - form.controls['foo'].patchValue(''); - form.controls['bar'].patchValue('x'); - - const testResult = { testTypes: [{ testResult: resultOfTestEnum.pass }] } as TestResultModel; - - store.overrideSelector(testResultInEdit, testResult); - - const result = await firstValueFrom( - CustomAsyncValidators.requiredIfNotResultAndSiblingEquals( - store, - resultOfTestEnum.fail, - 'bar', - 'x' - )(form.controls['foo']) as Observable - ); - - expect(result).toEqual({ requiredIfNotResultAndSiblingEquals: true }); - }); - - it('should pass validation when result is "fail" and "bar is "x" and ' + - 'validator specifies requiredIfNotResultAndSiblingEquals "fail" when sibling "bar" is "x"', async () => { - form.controls['foo'].patchValue(''); - form.controls['bar'].patchValue('x'); - - const testResult = { testTypes: [{ testResult: resultOfTestEnum.fail }] } as TestResultModel; - - store.overrideSelector(testResultInEdit, testResult); - - const result = await firstValueFrom( - CustomAsyncValidators.requiredIfNotResultAndSiblingEquals( - store, - resultOfTestEnum.fail, - 'bar', - 'x' - )(form.controls['foo']) as Observable - ); - - expect(result).toBeNull(); - }); - - it('should pass validation when result is "pass" and "bar is "y" and ' + - 'validator specifies requiredIfNotResultAndSiblingEquals "fail" when sibling "bar" is "x"', async () => { - form.controls['foo'].patchValue(''); - form.controls['bar'].patchValue('y'); - - const testResult = { testTypes: [{ testResult: resultOfTestEnum.pass }] } as TestResultModel; - - store.overrideSelector(testResultInEdit, testResult); - - const result = await firstValueFrom( - CustomAsyncValidators.requiredIfNotResultAndSiblingEquals( - store, - resultOfTestEnum.fail, - 'bar', - 'x' - )(form.controls['foo']) as Observable - ); - - expect(result).toBeNull(); - }); - - it('should be required when result is "pass" and "bar is "x" and ' + - 'validator specifies requiredIfNotResultAndSiblingEquals "fail"/"abandoned" when sibling "bar" is "x"', async () => { - form.controls['foo'].patchValue(''); - form.controls['bar'].patchValue('x'); - - const testResult = { testTypes: [{ testResult: resultOfTestEnum.pass }] } as TestResultModel; - - store.overrideSelector(testResultInEdit, testResult); - - const result = await firstValueFrom( - CustomAsyncValidators.requiredIfNotResultAndSiblingEquals( - store, - [resultOfTestEnum.fail, resultOfTestEnum.abandoned], - 'bar', - 'x' - )(form.controls['foo']) as Observable - ); - - expect(result).toEqual({ requiredIfNotResultAndSiblingEquals: true }); - }); + it( + 'should be required when result is "pass" and "bar is "x" and ' + + 'validator specifies requiredIfNotResultAndSiblingEquals "fail" when sibling "bar" is "x"', + async () => { + form.controls['foo'].patchValue(''); + form.controls['bar'].patchValue('x'); + + const testResult = { testTypes: [{ testResult: resultOfTestEnum.pass }] } as TestResultModel; + + store.overrideSelector(testResultInEdit, testResult); + + const result = await firstValueFrom( + CustomAsyncValidators.requiredIfNotResultAndSiblingEquals( + store, + resultOfTestEnum.fail, + 'bar', + 'x' + )(form.controls['foo']) as Observable + ); + + expect(result).toEqual({ requiredIfNotResultAndSiblingEquals: true }); + } + ); + + it( + 'should pass validation when result is "fail" and "bar is "x" and ' + + 'validator specifies requiredIfNotResultAndSiblingEquals "fail" when sibling "bar" is "x"', + async () => { + form.controls['foo'].patchValue(''); + form.controls['bar'].patchValue('x'); + + const testResult = { testTypes: [{ testResult: resultOfTestEnum.fail }] } as TestResultModel; + + store.overrideSelector(testResultInEdit, testResult); + + const result = await firstValueFrom( + CustomAsyncValidators.requiredIfNotResultAndSiblingEquals( + store, + resultOfTestEnum.fail, + 'bar', + 'x' + )(form.controls['foo']) as Observable + ); + + expect(result).toBeNull(); + } + ); + + it( + 'should pass validation when result is "pass" and "bar is "y" and ' + + 'validator specifies requiredIfNotResultAndSiblingEquals "fail" when sibling "bar" is "x"', + async () => { + form.controls['foo'].patchValue(''); + form.controls['bar'].patchValue('y'); + + const testResult = { testTypes: [{ testResult: resultOfTestEnum.pass }] } as TestResultModel; + + store.overrideSelector(testResultInEdit, testResult); + + const result = await firstValueFrom( + CustomAsyncValidators.requiredIfNotResultAndSiblingEquals( + store, + resultOfTestEnum.fail, + 'bar', + 'x' + )(form.controls['foo']) as Observable + ); + + expect(result).toBeNull(); + } + ); + + it( + 'should be required when result is "pass" and "bar is "x" and ' + + 'validator specifies requiredIfNotResultAndSiblingEquals "fail"/"abandoned" when sibling "bar" is "x"', + async () => { + form.controls['foo'].patchValue(''); + form.controls['bar'].patchValue('x'); + + const testResult = { testTypes: [{ testResult: resultOfTestEnum.pass }] } as TestResultModel; + + store.overrideSelector(testResultInEdit, testResult); + + const result = await firstValueFrom( + CustomAsyncValidators.requiredIfNotResultAndSiblingEquals( + store, + [resultOfTestEnum.fail, resultOfTestEnum.abandoned], + 'bar', + 'x' + )(form.controls['foo']) as Observable + ); + + expect(result).toEqual({ requiredIfNotResultAndSiblingEquals: true }); + } + ); }); describe('hide if equals with condition', () => { @@ -652,103 +664,118 @@ describe('hide if equals with condition', () => { }); }); - it('"bar" should be hidden when "foo" is "x" and testTypeId is "1" and ' + - 'validator specifies hideIfEqualsWithCondition for current field equals "x" with the condition that the ' + - '"testTypeId" field has a value in "1,2,3,4"', async () => { - form.controls['foo'].patchValue('x'); - - const testResult = { testTypes: [{ testTypeId: '1' }] } as TestResultModel; - - store.overrideSelector(testResultInEdit, testResult); - - await firstValueFrom( - CustomAsyncValidators.hideIfEqualsWithCondition(store, 'bar', 'x', { - field: 'testTypeId', - operator: operatorEnum.Equals, - value: ['1', '2', '3', '4'], - })(form.controls['foo']) as Observable - ); - - expect((form.controls['bar'] as CustomFormControl).meta.hide).toBe(true); - }); - - it('"bar" should not be hidden when "foo" is "x" and testTypeId is "1" and ' + - 'validator specifies hideIfEqualsWithCondition for current field equals "y" with ' + - 'the condition that the "testTypeId" field has a value in "1,2,3,4"', async () => { - form.controls['foo'].patchValue('x'); - - const testResult = { testTypes: [{ testTypeId: '1' }] } as TestResultModel; - - store.overrideSelector(testResultInEdit, testResult); - - await firstValueFrom( - CustomAsyncValidators.hideIfEqualsWithCondition(store, 'bar', 'y', { - field: 'testTypeId', - operator: operatorEnum.Equals, - value: ['1', '2', '3', '4'], - })(form.controls['foo']) as Observable - ); - - expect((form.controls['bar'] as CustomFormControl).meta.hide).toBe(false); - }); - - it('"bar" should not be hidden when "foo" is "x" and testTypeId is "5" and ' + - 'validator specifies hideIfEqualsWithCondition for current field equals "x" with ' + - 'the condition that the "testTypeId" field has a value in "1,2,3,4"', async () => { - form.controls['foo'].patchValue('x'); - - const testResult = { testTypes: [{ testTypeId: '5' }] } as TestResultModel; - - store.overrideSelector(testResultInEdit, testResult); - - await firstValueFrom( - CustomAsyncValidators.hideIfEqualsWithCondition(store, 'bar', 'x', { - field: 'testTypeId', - operator: operatorEnum.Equals, - value: ['1', '2', '3', '4'], - })(form.controls['foo']) as Observable - ); - - expect((form.controls['bar'] as CustomFormControl).meta.hide).toBe(false); - }); - - it('"bar" should be hidden when "foo" is "x" and testTypeId is "1" and "odometerReading" is 100 and ' + - 'validator specifies hideIfEqualsWithCondition for current field equals "x" with the condition that the ' + - '"testTypeId" field has a value in "1,2,3,4" and "odometerReading" is 100', async () => { - form.controls['foo'].patchValue('x'); - - const testResult = { odometerReading: 100, testTypes: [{ testTypeId: '1' }] } as TestResultModel; - - store.overrideSelector(testResultInEdit, testResult); - - await firstValueFrom( - CustomAsyncValidators.hideIfEqualsWithCondition(store, 'bar', 'x', [ - { field: 'testTypeId', operator: operatorEnum.Equals, value: ['1', '2', '3', '4'] }, - { field: 'odometerReading', operator: operatorEnum.Equals, value: 100 }, - ])(form.controls['foo']) as Observable - ); - - expect((form.controls['bar'] as CustomFormControl).meta.hide).toBe(true); - }); - - it('"bar" should not be hidden when "foo" is "x" and testTypeId is "1" and "odometerReading" is 101 and ' + - 'validator specifies hideIfEqualsWithCondition for current field equals "x" with the condition that the "testTypeId" ' + - 'field has a value in "1,2,3,4" and "odometerReading" is 100', async () => { - form.controls['foo'].patchValue('x'); - - const testResult = { odometerReading: 101, testTypes: [{ testTypeId: '1' }] } as TestResultModel; - - store.overrideSelector(testResultInEdit, testResult); - - await firstValueFrom( - CustomAsyncValidators.hideIfEqualsWithCondition(store, 'bar', 'x', [ - { field: 'testTypeId', operator: operatorEnum.Equals, value: ['1', '2', '3', '4'] }, - { field: 'odometerReading', operator: operatorEnum.Equals, value: 100 }, - ])(form.controls['foo']) as Observable - ); - - expect((form.controls['bar'] as CustomFormControl).meta.hide).toBe(false); - }); + it( + '"bar" should be hidden when "foo" is "x" and testTypeId is "1" and ' + + 'validator specifies hideIfEqualsWithCondition for current field equals "x" with the condition that the ' + + '"testTypeId" field has a value in "1,2,3,4"', + async () => { + form.controls['foo'].patchValue('x'); + + const testResult = { testTypes: [{ testTypeId: '1' }] } as TestResultModel; + + store.overrideSelector(testResultInEdit, testResult); + + await firstValueFrom( + CustomAsyncValidators.hideIfEqualsWithCondition(store, 'bar', 'x', { + field: 'testTypeId', + operator: operatorEnum.Equals, + value: ['1', '2', '3', '4'], + })(form.controls['foo']) as Observable + ); + + expect((form.controls['bar'] as CustomFormControl).meta.hide).toBe(true); + } + ); + + it( + '"bar" should not be hidden when "foo" is "x" and testTypeId is "1" and ' + + 'validator specifies hideIfEqualsWithCondition for current field equals "y" with ' + + 'the condition that the "testTypeId" field has a value in "1,2,3,4"', + async () => { + form.controls['foo'].patchValue('x'); + + const testResult = { testTypes: [{ testTypeId: '1' }] } as TestResultModel; + + store.overrideSelector(testResultInEdit, testResult); + + await firstValueFrom( + CustomAsyncValidators.hideIfEqualsWithCondition(store, 'bar', 'y', { + field: 'testTypeId', + operator: operatorEnum.Equals, + value: ['1', '2', '3', '4'], + })(form.controls['foo']) as Observable + ); + + expect((form.controls['bar'] as CustomFormControl).meta.hide).toBe(false); + } + ); + + it( + '"bar" should not be hidden when "foo" is "x" and testTypeId is "5" and ' + + 'validator specifies hideIfEqualsWithCondition for current field equals "x" with ' + + 'the condition that the "testTypeId" field has a value in "1,2,3,4"', + async () => { + form.controls['foo'].patchValue('x'); + + const testResult = { testTypes: [{ testTypeId: '5' }] } as TestResultModel; + + store.overrideSelector(testResultInEdit, testResult); + + await firstValueFrom( + CustomAsyncValidators.hideIfEqualsWithCondition(store, 'bar', 'x', { + field: 'testTypeId', + operator: operatorEnum.Equals, + value: ['1', '2', '3', '4'], + })(form.controls['foo']) as Observable + ); + + expect((form.controls['bar'] as CustomFormControl).meta.hide).toBe(false); + } + ); + + it( + '"bar" should be hidden when "foo" is "x" and testTypeId is "1" and "odometerReading" is 100 and ' + + 'validator specifies hideIfEqualsWithCondition for current field equals "x" with the condition that the ' + + '"testTypeId" field has a value in "1,2,3,4" and "odometerReading" is 100', + async () => { + form.controls['foo'].patchValue('x'); + + const testResult = { odometerReading: 100, testTypes: [{ testTypeId: '1' }] } as TestResultModel; + + store.overrideSelector(testResultInEdit, testResult); + + await firstValueFrom( + CustomAsyncValidators.hideIfEqualsWithCondition(store, 'bar', 'x', [ + { field: 'testTypeId', operator: operatorEnum.Equals, value: ['1', '2', '3', '4'] }, + { field: 'odometerReading', operator: operatorEnum.Equals, value: 100 }, + ])(form.controls['foo']) as Observable + ); + + expect((form.controls['bar'] as CustomFormControl).meta.hide).toBe(true); + } + ); + + it( + '"bar" should not be hidden when "foo" is "x" and testTypeId is "1" and "odometerReading" is 101 and ' + + 'validator specifies hideIfEqualsWithCondition for current field equals "x" with the condition that the "testTypeId" ' + + 'field has a value in "1,2,3,4" and "odometerReading" is 100', + async () => { + form.controls['foo'].patchValue('x'); + + const testResult = { odometerReading: 101, testTypes: [{ testTypeId: '1' }] } as TestResultModel; + + store.overrideSelector(testResultInEdit, testResult); + + await firstValueFrom( + CustomAsyncValidators.hideIfEqualsWithCondition(store, 'bar', 'x', [ + { field: 'testTypeId', operator: operatorEnum.Equals, value: ['1', '2', '3', '4'] }, + { field: 'odometerReading', operator: operatorEnum.Equals, value: 100 }, + ])(form.controls['foo']) as Observable + ); + + expect((form.controls['bar'] as CustomFormControl).meta.hide).toBe(false); + } + ); }); describe('requiredWhenCarryingDangerousGoods', () => { diff --git a/src/app/services/dynamic-forms/dynamic-form.service.ts b/src/app/services/dynamic-forms/dynamic-form.service.ts index 81bf222c0..7419fcf95 100644 --- a/src/app/services/dynamic-forms/dynamic-form.service.ts +++ b/src/app/services/dynamic-forms/dynamic-form.service.ts @@ -245,7 +245,7 @@ export class DynamicFormService { // If an anchor link is provided, use that, otherwise determine target element from customId or name const defaultAnchorLink = meta?.customId ?? meta?.name; const anchorLink = - typeof data === 'object' && data !== null ? (data.anchorLink ?? defaultAnchorLink) : defaultAnchorLink; + typeof data === 'object' && data !== null ? data.anchorLink ?? defaultAnchorLink : defaultAnchorLink; // If typeof data is an array, assume we're passing the service multiple global errors const globalErrors = Array.isArray(data) diff --git a/src/app/services/http/http.service.ts b/src/app/services/http/http.service.ts index 40e2003ce..4c83791a7 100644 --- a/src/app/services/http/http.service.ts +++ b/src/app/services/http/http.service.ts @@ -152,7 +152,7 @@ export class HttpService { paragraphId: number, user: { name?: string; email?: string } ) { - return this.http.post( + return this.http.post( `${environment.VTM_API_URI}/v3/technical-records/letter/${vehicleRecord.systemNumber}/${vehicleRecord.createdTimestamp}`, { vtmUsername: user.name, @@ -161,7 +161,8 @@ export class HttpService { recipientEmailAddress: vehicleRecord.techRecord_applicantDetails_emailAddress ? vehicleRecord.techRecord_applicantDetails_emailAddress : user.email, - } + }, + { responseType: 'text' } ); } @@ -337,7 +338,9 @@ export class HttpService { } return this.http.get( - `${environment.VTM_API_URI}/reference/${encodeURIComponent(String(resourceType))}/${encodeURIComponent(String(resourceKey))}` + `${environment.VTM_API_URI}/reference/${encodeURIComponent( + String(resourceType) + )}/${encodeURIComponent(String(resourceKey))}` ); } @@ -359,7 +362,9 @@ export class HttpService { } return this.http.delete( - `${environment.VTM_API_URI}/reference/${encodeURIComponent(String(resourceType))}/${encodeURIComponent(String(resourceKey))}`, + `${environment.VTM_API_URI}/reference/${encodeURIComponent( + String(resourceType) + )}/${encodeURIComponent(String(resourceKey))}`, body ); } @@ -378,7 +383,9 @@ export class HttpService { } return this.http.get( - `${environment.VTM_API_URI}/reference/lookup/${encodeURIComponent(String(resourceType))}/${encodeURIComponent(String(resourceKey))}` + `${environment.VTM_API_URI}/reference/lookup/${encodeURIComponent( + String(resourceType) + )}/${encodeURIComponent(String(resourceKey))}` ); } @@ -396,7 +403,9 @@ export class HttpService { } return this.http.get( - `${environment.VTM_API_URI}/reference/lookup/tyres/${encodeURIComponent(String(searchKey))}/${encodeURIComponent(String(param))}` + `${environment.VTM_API_URI}/reference/lookup/tyres/${encodeURIComponent( + String(searchKey) + )}/${encodeURIComponent(String(param))}` ); } @@ -414,7 +423,9 @@ export class HttpService { } return this.http.post( - `${environment.VTM_API_URI}/reference/${encodeURIComponent(String(resourceType))}/${encodeURIComponent(String(resourceKey))}`, + `${environment.VTM_API_URI}/reference/${encodeURIComponent( + String(resourceType) + )}/${encodeURIComponent(String(resourceKey))}`, body ); } @@ -433,7 +444,9 @@ export class HttpService { } return this.http.put( - `${environment.VTM_API_URI}/reference/${encodeURIComponent(String(resourceType))}/${encodeURIComponent(String(resourceKey))}`, + `${environment.VTM_API_URI}/reference/${encodeURIComponent( + String(resourceType) + )}/${encodeURIComponent(String(resourceKey))}`, body ); } diff --git a/src/app/store/technical-records/batch-create.reducer.ts b/src/app/store/technical-records/batch-create.reducer.ts index d104eb57b..1dab192a3 100644 --- a/src/app/store/technical-records/batch-create.reducer.ts +++ b/src/app/store/technical-records/batch-create.reducer.ts @@ -73,8 +73,7 @@ function vehicleRecordsToBatchRecordMapper( changes: { vin: techRecord.vin, systemNumber: techRecord.systemNumber, - trailerIdOrVrm: - techRecord.techRecord_vehicleType !== 'trl' ? (techRecord.primaryVrm ?? '') : techRecord.trailerId, + trailerIdOrVrm: techRecord.techRecord_vehicleType !== 'trl' ? techRecord.primaryVrm ?? '' : techRecord.trailerId, vehicleType: techRecord.techRecord_vehicleType, status: (techRecord.techRecord_statusCode as StatusCodes) ?? undefined, created, From db63e4d6a40edb2168641c35fc76598422865821 Mon Sep 17 00:00:00 2001 From: pbardy2000 <146740183+pbardy2000@users.noreply.github.com> Date: Mon, 16 Sep 2024 14:07:32 +0100 Subject: [PATCH 2/2] fix(cb2-14000): fix linting --- .../tech-record-amend-vin.component.ts | 2 +- .../tech-record-generate-letter.component.ts | 2 +- .../tech-record-search-tyres.component.ts | 2 +- .../tech-record-title.component.ts | 4 +- .../vehicle-technical-record.component.ts | 2 +- .../__tests__/custom-async-validators.spec.ts | 387 ++++++++---------- .../dynamic-forms/dynamic-form.service.ts | 2 +- .../technical-records/batch-create.reducer.ts | 3 +- 8 files changed, 189 insertions(+), 215 deletions(-) diff --git a/src/app/features/tech-record/components/tech-record-amend-vin/tech-record-amend-vin.component.ts b/src/app/features/tech-record/components/tech-record-amend-vin/tech-record-amend-vin.component.ts index b16bf6274..bb8cf26bd 100644 --- a/src/app/features/tech-record/components/tech-record-amend-vin/tech-record-amend-vin.component.ts +++ b/src/app/features/tech-record/components/tech-record-amend-vin/tech-record-amend-vin.component.ts @@ -60,7 +60,7 @@ export class AmendVinComponent implements OnDestroy, OnInit { } get currentVrm(): string | undefined { - return this.techRecord?.techRecord_vehicleType !== 'trl' ? this.techRecord?.primaryVrm ?? '' : undefined; + return this.techRecord?.techRecord_vehicleType !== 'trl' ? (this.techRecord?.primaryVrm ?? '') : undefined; } isFormValid(): boolean { diff --git a/src/app/features/tech-record/components/tech-record-generate-letter/tech-record-generate-letter.component.ts b/src/app/features/tech-record/components/tech-record-generate-letter/tech-record-generate-letter.component.ts index 4a5fd5f34..e9021f344 100644 --- a/src/app/features/tech-record/components/tech-record-generate-letter/tech-record-generate-letter.component.ts +++ b/src/app/features/tech-record/components/tech-record-generate-letter/tech-record-generate-letter.component.ts @@ -80,7 +80,7 @@ export class GenerateLetterComponent implements OnInit { get emailAddress(): string | undefined { return this.techRecord?.techRecord_vehicleType === 'trl' - ? this.techRecord?.techRecord_applicantDetails_emailAddress ?? '' + ? (this.techRecord?.techRecord_applicantDetails_emailAddress ?? '') : undefined; } diff --git a/src/app/features/tech-record/components/tech-record-search-tyres/tech-record-search-tyres.component.ts b/src/app/features/tech-record/components/tech-record-search-tyres/tech-record-search-tyres.component.ts index 1159eaa37..47f8fecdb 100644 --- a/src/app/features/tech-record/components/tech-record-search-tyres/tech-record-search-tyres.component.ts +++ b/src/app/features/tech-record/components/tech-record-search-tyres/tech-record-search-tyres.component.ts @@ -107,7 +107,7 @@ export class TechRecordSearchTyresComponent implements OnInit { } get currentVrm(): string | undefined { return this.vehicleTechRecord?.techRecord_vehicleType !== 'trl' - ? this.vehicleTechRecord?.primaryVrm ?? '' + ? (this.vehicleTechRecord?.primaryVrm ?? '') : undefined; } get paginatedFields(): ReferenceDataTyre[] { diff --git a/src/app/features/tech-record/components/tech-record-title/tech-record-title.component.ts b/src/app/features/tech-record/components/tech-record-title/tech-record-title.component.ts index 21056f613..6200fe693 100644 --- a/src/app/features/tech-record/components/tech-record-title/tech-record-title.component.ts +++ b/src/app/features/tech-record/components/tech-record-title/tech-record-title.component.ts @@ -47,11 +47,11 @@ export class TechRecordTitleComponent implements OnInit { } get currentVrm(): string | undefined { - return this.vehicle?.techRecord_vehicleType !== 'trl' ? this.vehicle?.primaryVrm ?? '' : undefined; + return this.vehicle?.techRecord_vehicleType !== 'trl' ? (this.vehicle?.primaryVrm ?? '') : undefined; } get otherVrms(): string[] | undefined { - return this.vehicle?.techRecord_vehicleType !== 'trl' ? this.vehicle?.secondaryVrms ?? [] : undefined; + return this.vehicle?.techRecord_vehicleType !== 'trl' ? (this.vehicle?.secondaryVrms ?? []) : undefined; } get vehicleTypes(): typeof VehicleTypes { diff --git a/src/app/features/tech-record/components/vehicle-technical-record/vehicle-technical-record.component.ts b/src/app/features/tech-record/components/vehicle-technical-record/vehicle-technical-record.component.ts index 286b2f24c..2ae3b7c69 100644 --- a/src/app/features/tech-record/components/vehicle-technical-record/vehicle-technical-record.component.ts +++ b/src/app/features/tech-record/components/vehicle-technical-record/vehicle-technical-record.component.ts @@ -85,7 +85,7 @@ export class VehicleTechnicalRecordComponent implements OnInit, OnDestroy { } get currentVrm(): string | undefined { - return this.techRecord?.techRecord_vehicleType !== 'trl' ? this.techRecord?.primaryVrm ?? '' : undefined; + return this.techRecord?.techRecord_vehicleType !== 'trl' ? (this.techRecord?.primaryVrm ?? '') : undefined; } get roles(): typeof Roles { diff --git a/src/app/forms/validators/custom-async-validator/__tests__/custom-async-validators.spec.ts b/src/app/forms/validators/custom-async-validator/__tests__/custom-async-validators.spec.ts index dbafd9ef9..2b3f5adda 100644 --- a/src/app/forms/validators/custom-async-validator/__tests__/custom-async-validators.spec.ts +++ b/src/app/forms/validators/custom-async-validator/__tests__/custom-async-validators.spec.ts @@ -550,101 +550,89 @@ describe('requiredIfNotResultAndSiblingEquals', () => { }); }); - it( - 'should be required when result is "pass" and "bar is "x" and ' + - 'validator specifies requiredIfNotResultAndSiblingEquals "fail" when sibling "bar" is "x"', - async () => { - form.controls['foo'].patchValue(''); - form.controls['bar'].patchValue('x'); - - const testResult = { testTypes: [{ testResult: resultOfTestEnum.pass }] } as TestResultModel; - - store.overrideSelector(testResultInEdit, testResult); - - const result = await firstValueFrom( - CustomAsyncValidators.requiredIfNotResultAndSiblingEquals( - store, - resultOfTestEnum.fail, - 'bar', - 'x' - )(form.controls['foo']) as Observable - ); - - expect(result).toEqual({ requiredIfNotResultAndSiblingEquals: true }); - } - ); - - it( - 'should pass validation when result is "fail" and "bar is "x" and ' + - 'validator specifies requiredIfNotResultAndSiblingEquals "fail" when sibling "bar" is "x"', - async () => { - form.controls['foo'].patchValue(''); - form.controls['bar'].patchValue('x'); - - const testResult = { testTypes: [{ testResult: resultOfTestEnum.fail }] } as TestResultModel; - - store.overrideSelector(testResultInEdit, testResult); - - const result = await firstValueFrom( - CustomAsyncValidators.requiredIfNotResultAndSiblingEquals( - store, - resultOfTestEnum.fail, - 'bar', - 'x' - )(form.controls['foo']) as Observable - ); - - expect(result).toBeNull(); - } - ); - - it( - 'should pass validation when result is "pass" and "bar is "y" and ' + - 'validator specifies requiredIfNotResultAndSiblingEquals "fail" when sibling "bar" is "x"', - async () => { - form.controls['foo'].patchValue(''); - form.controls['bar'].patchValue('y'); - - const testResult = { testTypes: [{ testResult: resultOfTestEnum.pass }] } as TestResultModel; - - store.overrideSelector(testResultInEdit, testResult); - - const result = await firstValueFrom( - CustomAsyncValidators.requiredIfNotResultAndSiblingEquals( - store, - resultOfTestEnum.fail, - 'bar', - 'x' - )(form.controls['foo']) as Observable - ); - - expect(result).toBeNull(); - } - ); - - it( - 'should be required when result is "pass" and "bar is "x" and ' + - 'validator specifies requiredIfNotResultAndSiblingEquals "fail"/"abandoned" when sibling "bar" is "x"', - async () => { - form.controls['foo'].patchValue(''); - form.controls['bar'].patchValue('x'); - - const testResult = { testTypes: [{ testResult: resultOfTestEnum.pass }] } as TestResultModel; - - store.overrideSelector(testResultInEdit, testResult); - - const result = await firstValueFrom( - CustomAsyncValidators.requiredIfNotResultAndSiblingEquals( - store, - [resultOfTestEnum.fail, resultOfTestEnum.abandoned], - 'bar', - 'x' - )(form.controls['foo']) as Observable - ); - - expect(result).toEqual({ requiredIfNotResultAndSiblingEquals: true }); - } - ); + it('should be required when result is "pass" and "bar is "x" and ' + + 'validator specifies requiredIfNotResultAndSiblingEquals "fail" when sibling "bar" is "x"', async () => { + form.controls['foo'].patchValue(''); + form.controls['bar'].patchValue('x'); + + const testResult = { testTypes: [{ testResult: resultOfTestEnum.pass }] } as TestResultModel; + + store.overrideSelector(testResultInEdit, testResult); + + const result = await firstValueFrom( + CustomAsyncValidators.requiredIfNotResultAndSiblingEquals( + store, + resultOfTestEnum.fail, + 'bar', + 'x' + )(form.controls['foo']) as Observable + ); + + expect(result).toEqual({ requiredIfNotResultAndSiblingEquals: true }); + }); + + it('should pass validation when result is "fail" and "bar is "x" and ' + + 'validator specifies requiredIfNotResultAndSiblingEquals "fail" when sibling "bar" is "x"', async () => { + form.controls['foo'].patchValue(''); + form.controls['bar'].patchValue('x'); + + const testResult = { testTypes: [{ testResult: resultOfTestEnum.fail }] } as TestResultModel; + + store.overrideSelector(testResultInEdit, testResult); + + const result = await firstValueFrom( + CustomAsyncValidators.requiredIfNotResultAndSiblingEquals( + store, + resultOfTestEnum.fail, + 'bar', + 'x' + )(form.controls['foo']) as Observable + ); + + expect(result).toBeNull(); + }); + + it('should pass validation when result is "pass" and "bar is "y" and ' + + 'validator specifies requiredIfNotResultAndSiblingEquals "fail" when sibling "bar" is "x"', async () => { + form.controls['foo'].patchValue(''); + form.controls['bar'].patchValue('y'); + + const testResult = { testTypes: [{ testResult: resultOfTestEnum.pass }] } as TestResultModel; + + store.overrideSelector(testResultInEdit, testResult); + + const result = await firstValueFrom( + CustomAsyncValidators.requiredIfNotResultAndSiblingEquals( + store, + resultOfTestEnum.fail, + 'bar', + 'x' + )(form.controls['foo']) as Observable + ); + + expect(result).toBeNull(); + }); + + it('should be required when result is "pass" and "bar is "x" and ' + + 'validator specifies requiredIfNotResultAndSiblingEquals "fail"/"abandoned" when sibling "bar" is "x"', async () => { + form.controls['foo'].patchValue(''); + form.controls['bar'].patchValue('x'); + + const testResult = { testTypes: [{ testResult: resultOfTestEnum.pass }] } as TestResultModel; + + store.overrideSelector(testResultInEdit, testResult); + + const result = await firstValueFrom( + CustomAsyncValidators.requiredIfNotResultAndSiblingEquals( + store, + [resultOfTestEnum.fail, resultOfTestEnum.abandoned], + 'bar', + 'x' + )(form.controls['foo']) as Observable + ); + + expect(result).toEqual({ requiredIfNotResultAndSiblingEquals: true }); + }); }); describe('hide if equals with condition', () => { @@ -664,118 +652,103 @@ describe('hide if equals with condition', () => { }); }); - it( - '"bar" should be hidden when "foo" is "x" and testTypeId is "1" and ' + - 'validator specifies hideIfEqualsWithCondition for current field equals "x" with the condition that the ' + - '"testTypeId" field has a value in "1,2,3,4"', - async () => { - form.controls['foo'].patchValue('x'); - - const testResult = { testTypes: [{ testTypeId: '1' }] } as TestResultModel; - - store.overrideSelector(testResultInEdit, testResult); - - await firstValueFrom( - CustomAsyncValidators.hideIfEqualsWithCondition(store, 'bar', 'x', { - field: 'testTypeId', - operator: operatorEnum.Equals, - value: ['1', '2', '3', '4'], - })(form.controls['foo']) as Observable - ); - - expect((form.controls['bar'] as CustomFormControl).meta.hide).toBe(true); - } - ); - - it( - '"bar" should not be hidden when "foo" is "x" and testTypeId is "1" and ' + - 'validator specifies hideIfEqualsWithCondition for current field equals "y" with ' + - 'the condition that the "testTypeId" field has a value in "1,2,3,4"', - async () => { - form.controls['foo'].patchValue('x'); - - const testResult = { testTypes: [{ testTypeId: '1' }] } as TestResultModel; - - store.overrideSelector(testResultInEdit, testResult); - - await firstValueFrom( - CustomAsyncValidators.hideIfEqualsWithCondition(store, 'bar', 'y', { - field: 'testTypeId', - operator: operatorEnum.Equals, - value: ['1', '2', '3', '4'], - })(form.controls['foo']) as Observable - ); - - expect((form.controls['bar'] as CustomFormControl).meta.hide).toBe(false); - } - ); - - it( - '"bar" should not be hidden when "foo" is "x" and testTypeId is "5" and ' + - 'validator specifies hideIfEqualsWithCondition for current field equals "x" with ' + - 'the condition that the "testTypeId" field has a value in "1,2,3,4"', - async () => { - form.controls['foo'].patchValue('x'); - - const testResult = { testTypes: [{ testTypeId: '5' }] } as TestResultModel; - - store.overrideSelector(testResultInEdit, testResult); - - await firstValueFrom( - CustomAsyncValidators.hideIfEqualsWithCondition(store, 'bar', 'x', { - field: 'testTypeId', - operator: operatorEnum.Equals, - value: ['1', '2', '3', '4'], - })(form.controls['foo']) as Observable - ); - - expect((form.controls['bar'] as CustomFormControl).meta.hide).toBe(false); - } - ); - - it( - '"bar" should be hidden when "foo" is "x" and testTypeId is "1" and "odometerReading" is 100 and ' + - 'validator specifies hideIfEqualsWithCondition for current field equals "x" with the condition that the ' + - '"testTypeId" field has a value in "1,2,3,4" and "odometerReading" is 100', - async () => { - form.controls['foo'].patchValue('x'); - - const testResult = { odometerReading: 100, testTypes: [{ testTypeId: '1' }] } as TestResultModel; - - store.overrideSelector(testResultInEdit, testResult); - - await firstValueFrom( - CustomAsyncValidators.hideIfEqualsWithCondition(store, 'bar', 'x', [ - { field: 'testTypeId', operator: operatorEnum.Equals, value: ['1', '2', '3', '4'] }, - { field: 'odometerReading', operator: operatorEnum.Equals, value: 100 }, - ])(form.controls['foo']) as Observable - ); - - expect((form.controls['bar'] as CustomFormControl).meta.hide).toBe(true); - } - ); - - it( - '"bar" should not be hidden when "foo" is "x" and testTypeId is "1" and "odometerReading" is 101 and ' + - 'validator specifies hideIfEqualsWithCondition for current field equals "x" with the condition that the "testTypeId" ' + - 'field has a value in "1,2,3,4" and "odometerReading" is 100', - async () => { - form.controls['foo'].patchValue('x'); - - const testResult = { odometerReading: 101, testTypes: [{ testTypeId: '1' }] } as TestResultModel; - - store.overrideSelector(testResultInEdit, testResult); - - await firstValueFrom( - CustomAsyncValidators.hideIfEqualsWithCondition(store, 'bar', 'x', [ - { field: 'testTypeId', operator: operatorEnum.Equals, value: ['1', '2', '3', '4'] }, - { field: 'odometerReading', operator: operatorEnum.Equals, value: 100 }, - ])(form.controls['foo']) as Observable - ); - - expect((form.controls['bar'] as CustomFormControl).meta.hide).toBe(false); - } - ); + it('"bar" should be hidden when "foo" is "x" and testTypeId is "1" and ' + + 'validator specifies hideIfEqualsWithCondition for current field equals "x" with the condition that the ' + + '"testTypeId" field has a value in "1,2,3,4"', async () => { + form.controls['foo'].patchValue('x'); + + const testResult = { testTypes: [{ testTypeId: '1' }] } as TestResultModel; + + store.overrideSelector(testResultInEdit, testResult); + + await firstValueFrom( + CustomAsyncValidators.hideIfEqualsWithCondition(store, 'bar', 'x', { + field: 'testTypeId', + operator: operatorEnum.Equals, + value: ['1', '2', '3', '4'], + })(form.controls['foo']) as Observable + ); + + expect((form.controls['bar'] as CustomFormControl).meta.hide).toBe(true); + }); + + it('"bar" should not be hidden when "foo" is "x" and testTypeId is "1" and ' + + 'validator specifies hideIfEqualsWithCondition for current field equals "y" with ' + + 'the condition that the "testTypeId" field has a value in "1,2,3,4"', async () => { + form.controls['foo'].patchValue('x'); + + const testResult = { testTypes: [{ testTypeId: '1' }] } as TestResultModel; + + store.overrideSelector(testResultInEdit, testResult); + + await firstValueFrom( + CustomAsyncValidators.hideIfEqualsWithCondition(store, 'bar', 'y', { + field: 'testTypeId', + operator: operatorEnum.Equals, + value: ['1', '2', '3', '4'], + })(form.controls['foo']) as Observable + ); + + expect((form.controls['bar'] as CustomFormControl).meta.hide).toBe(false); + }); + + it('"bar" should not be hidden when "foo" is "x" and testTypeId is "5" and ' + + 'validator specifies hideIfEqualsWithCondition for current field equals "x" with ' + + 'the condition that the "testTypeId" field has a value in "1,2,3,4"', async () => { + form.controls['foo'].patchValue('x'); + + const testResult = { testTypes: [{ testTypeId: '5' }] } as TestResultModel; + + store.overrideSelector(testResultInEdit, testResult); + + await firstValueFrom( + CustomAsyncValidators.hideIfEqualsWithCondition(store, 'bar', 'x', { + field: 'testTypeId', + operator: operatorEnum.Equals, + value: ['1', '2', '3', '4'], + })(form.controls['foo']) as Observable + ); + + expect((form.controls['bar'] as CustomFormControl).meta.hide).toBe(false); + }); + + it('"bar" should be hidden when "foo" is "x" and testTypeId is "1" and "odometerReading" is 100 and ' + + 'validator specifies hideIfEqualsWithCondition for current field equals "x" with the condition that the ' + + '"testTypeId" field has a value in "1,2,3,4" and "odometerReading" is 100', async () => { + form.controls['foo'].patchValue('x'); + + const testResult = { odometerReading: 100, testTypes: [{ testTypeId: '1' }] } as TestResultModel; + + store.overrideSelector(testResultInEdit, testResult); + + await firstValueFrom( + CustomAsyncValidators.hideIfEqualsWithCondition(store, 'bar', 'x', [ + { field: 'testTypeId', operator: operatorEnum.Equals, value: ['1', '2', '3', '4'] }, + { field: 'odometerReading', operator: operatorEnum.Equals, value: 100 }, + ])(form.controls['foo']) as Observable + ); + + expect((form.controls['bar'] as CustomFormControl).meta.hide).toBe(true); + }); + + it('"bar" should not be hidden when "foo" is "x" and testTypeId is "1" and "odometerReading" is 101 and ' + + 'validator specifies hideIfEqualsWithCondition for current field equals "x" with the condition that the "testTypeId" ' + + 'field has a value in "1,2,3,4" and "odometerReading" is 100', async () => { + form.controls['foo'].patchValue('x'); + + const testResult = { odometerReading: 101, testTypes: [{ testTypeId: '1' }] } as TestResultModel; + + store.overrideSelector(testResultInEdit, testResult); + + await firstValueFrom( + CustomAsyncValidators.hideIfEqualsWithCondition(store, 'bar', 'x', [ + { field: 'testTypeId', operator: operatorEnum.Equals, value: ['1', '2', '3', '4'] }, + { field: 'odometerReading', operator: operatorEnum.Equals, value: 100 }, + ])(form.controls['foo']) as Observable + ); + + expect((form.controls['bar'] as CustomFormControl).meta.hide).toBe(false); + }); }); describe('requiredWhenCarryingDangerousGoods', () => { diff --git a/src/app/services/dynamic-forms/dynamic-form.service.ts b/src/app/services/dynamic-forms/dynamic-form.service.ts index 7419fcf95..81bf222c0 100644 --- a/src/app/services/dynamic-forms/dynamic-form.service.ts +++ b/src/app/services/dynamic-forms/dynamic-form.service.ts @@ -245,7 +245,7 @@ export class DynamicFormService { // If an anchor link is provided, use that, otherwise determine target element from customId or name const defaultAnchorLink = meta?.customId ?? meta?.name; const anchorLink = - typeof data === 'object' && data !== null ? data.anchorLink ?? defaultAnchorLink : defaultAnchorLink; + typeof data === 'object' && data !== null ? (data.anchorLink ?? defaultAnchorLink) : defaultAnchorLink; // If typeof data is an array, assume we're passing the service multiple global errors const globalErrors = Array.isArray(data) diff --git a/src/app/store/technical-records/batch-create.reducer.ts b/src/app/store/technical-records/batch-create.reducer.ts index 1dab192a3..d104eb57b 100644 --- a/src/app/store/technical-records/batch-create.reducer.ts +++ b/src/app/store/technical-records/batch-create.reducer.ts @@ -73,7 +73,8 @@ function vehicleRecordsToBatchRecordMapper( changes: { vin: techRecord.vin, systemNumber: techRecord.systemNumber, - trailerIdOrVrm: techRecord.techRecord_vehicleType !== 'trl' ? techRecord.primaryVrm ?? '' : techRecord.trailerId, + trailerIdOrVrm: + techRecord.techRecord_vehicleType !== 'trl' ? (techRecord.primaryVrm ?? '') : techRecord.trailerId, vehicleType: techRecord.techRecord_vehicleType, status: (techRecord.techRecord_statusCode as StatusCodes) ?? undefined, created,