From 6ea8216cb1b848a4a145810c0463f5756f6b64c9 Mon Sep 17 00:00:00 2001 From: connorpgpmcelroy <74015088+connorpgpmcelroy@users.noreply.github.com> Date: Thu, 17 Aug 2023 08:13:05 +0100 Subject: [PATCH 1/2] Set initial service codes for location and add unit tests (#3154) * Set initial service codes for location and add unit tests * Incorporate CCD Toolkit JudicialUser field fixes EUI-8601/8687/8732/8738 into Manage Cases release for EUI-8633 --------- Co-authored-by: Daniel Lam Co-authored-by: Daniel Lam --- .../staff-select-location.component.spec.ts | 41 +++++++++++++++---- .../staff-select-location.component.ts | 15 ++++--- .../models/location-by-service-code-model.ts | 1 + 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/staff-administrator/components/staff-add-edit-user/staff-add-edit-user-form/staff-select-location/staff-select-location.component.spec.ts b/src/staff-administrator/components/staff-add-edit-user/staff-add-edit-user-form/staff-select-location/staff-select-location.component.spec.ts index 355d98d3cf..2f980a8418 100644 --- a/src/staff-administrator/components/staff-add-edit-user/staff-add-edit-user-form/staff-select-location/staff-select-location.component.spec.ts +++ b/src/staff-administrator/components/staff-add-edit-user/staff-add-edit-user-form/staff-select-location/staff-select-location.component.spec.ts @@ -1,6 +1,6 @@ import { ComponentFixture, fakeAsync, flush, TestBed, tick } from '@angular/core/testing'; -import { FormControl } from '@angular/forms'; +import { FormControl, ReactiveFormsModule } from '@angular/forms'; import { MatAutocompleteModule } from '@angular/material/autocomplete'; import { RefDataService } from '@hmcts/rpx-xui-common-lib'; import { of } from 'rxjs'; @@ -17,7 +17,8 @@ describe('StaffSelectLocationComponent', () => { refDataServiceMock.getLocationsByServiceCodes.and.returnValue(of([])); await TestBed.configureTestingModule({ - imports: [MatAutocompleteModule], + imports: [MatAutocompleteModule, + ReactiveFormsModule], declarations: [StaffSelectLocationComponent], providers: [ { provide: RefDataService, useValue: refDataServiceMock } @@ -262,8 +263,11 @@ describe('StaffSelectLocationComponent', () => { })); it('should get an array when search term is not an empty string', fakeAsync(() => { + // obsCount added as observable should always run initially + let obsCount = 0; component.filteredList$.subscribe((result) => { - expect(Array.isArray(result)).toBe(true); + obsCount > 0 ? expect(Array.isArray(result)).toBe(true) : expect(Array.isArray(result)).toBe(false); + obsCount++; }); component.searchTermFormControl.setValue('123'); @@ -273,9 +277,10 @@ describe('StaffSelectLocationComponent', () => { it('should filter out locations based on searchTerm', fakeAsync(() => { refDataServiceMock.getLocationsByServiceCodes.and.returnValue(of([dummyLocations[0], dummyLocations[1]])); - component.locationsControl.setValue([dummyLocations[0], dummyLocations[1]]); + let obsCount = 0; component.filteredList$.subscribe((result) => { - expect(result).toEqual([dummyLocations[0]]); + obsCount > 0 ? expect(result).toEqual([dummyLocations[0]]) : expect(Array.isArray(result)).toBe(false); + obsCount++; }); component.searchTermFormControl.setValue(dummyLocations[0].venue_name); @@ -285,14 +290,36 @@ describe('StaffSelectLocationComponent', () => { it('should fill locations with correct service codes', fakeAsync(() => { refDataServiceMock.getLocationsByServiceCodes.and.returnValue(of([dummyLocations[0], dummyLocations[1]])); + let obsCount = 0; component.filteredList$.subscribe((result) => { - expect(result).toEqual([dummyLocations[0]]); - expect(result[0].serviceCodes).toEqual(['BFA1', 'AAA7']); + if (obsCount > 1) { + expect(result).toEqual([dummyLocations[0]]); + expect(result[0].serviceCodes).toEqual(['BFA1', 'AAA7']); + } + obsCount++; }); component.searchTermFormControl.setValue(dummyLocations[0].venue_name); tick(); flush(); })); + + it('should correctly set service codes for locations in formControl', fakeAsync(() => { + refDataServiceMock.getLocationsByServiceCodes.and.returnValue(of([dummyLocations[0], dummyLocations[1]])); + const mockLocationInControl: any = dummyLocations[0]; + // also ensures we are checking numbers as well as strings + mockLocationInControl.location_id = parseInt(mockLocationInControl.epimms_id); + component.locationsControl.setValue([mockLocationInControl]); + let obsCount = 0; + component.filteredList$.subscribe((result) => { + obsCount > 0 ? expect(result).toEqual([dummyLocations[0]]) : expect(result).toEqual(false); + expect(component.locationsControl.value[0].serviceCodes[0]).toEqual('BFA1'); + obsCount++; + }); + + component.searchTermFormControl.setValue(dummyLocations[0].venue_name); + tick(); + flush(); + })); }); }); }); diff --git a/src/staff-administrator/components/staff-add-edit-user/staff-add-edit-user-form/staff-select-location/staff-select-location.component.ts b/src/staff-administrator/components/staff-add-edit-user/staff-add-edit-user-form/staff-select-location/staff-select-location.component.ts index 63c7ae71b0..46b6b45f03 100644 --- a/src/staff-administrator/components/staff-add-edit-user/staff-add-edit-user-form/staff-select-location/staff-select-location.component.ts +++ b/src/staff-administrator/components/staff-add-edit-user/staff-add-edit-user-form/staff-select-location/staff-select-location.component.ts @@ -2,7 +2,7 @@ import { Component, Input, OnInit } from '@angular/core'; import { FormControl } from '@angular/forms'; import { RefDataService } from '@hmcts/rpx-xui-common-lib'; import { combineLatest, iif, Observable, of } from 'rxjs'; -import { map, switchMap, tap } from 'rxjs/operators'; +import { map, startWith, switchMap, tap } from 'rxjs/operators'; import { LocationByEpimmsModelWithServiceCodes } from '../../../../models/location-by-service-code-model'; import { StaffUserLocation } from '../../../../models/staff-user-location.model'; @@ -22,6 +22,8 @@ export class StaffSelectLocationComponent implements OnInit { public autocompleteSelectedLocation: LocationByEpimmsModelWithServiceCodes | false; private fullLocations: LocationByEpimmsModelWithServiceCodes[]; + private initialLocationServicesSet = false; + public get selectedLocations(): StaffUserLocation[] { return this.locationsControl?.value; } @@ -30,7 +32,7 @@ export class StaffSelectLocationComponent implements OnInit { public ngOnInit() { this.filteredList$ = combineLatest([ - this.searchTermFormControl.valueChanges, + this.searchTermFormControl.valueChanges.pipe(startWith('')), this.serviceCodes$ ]).pipe( tap(([term]: [string, string[]]) => { @@ -39,7 +41,7 @@ export class StaffSelectLocationComponent implements OnInit { } }), switchMap(([term, serviceCodes]: [string, string[]]) => iif( - () => (!!term && term.length >= 0), + () => ((!!term && term.length >= 0) || !this.initialLocationServicesSet), this.refDataService.getLocationsByServiceCodes( serviceCodes ).pipe( @@ -64,7 +66,7 @@ export class StaffSelectLocationComponent implements OnInit { private setLocationServiceCodes(locations: LocationByEpimmsModelWithServiceCodes[]): LocationByEpimmsModelWithServiceCodes[] { locations.map((location) => { - const currentId = location.epimms_id; + const currentId = location.epimms_id.toString(); const serviceCodes = location.serviceCodes; location.serviceCodes = this.getAllServiceCodes(serviceCodes, currentId); }); @@ -72,11 +74,12 @@ export class StaffSelectLocationComponent implements OnInit { // note: we could edit location types to produce less code - i.e. making them the same const fixedSelectedLocations = this.locationsControl.value; fixedSelectedLocations.forEach((location) => { - const currentId = location.location_id; - const serviceCodes = location.service_codes; + const currentId = location.location_id.toString(); + const serviceCodes = location.service_codes ? location.service_codes : []; location.service_codes = this.getAllServiceCodes(serviceCodes, currentId); }); this.locationsControl.setValue(fixedSelectedLocations); + this.initialLocationServicesSet = true; return locations; } diff --git a/src/staff-administrator/models/location-by-service-code-model.ts b/src/staff-administrator/models/location-by-service-code-model.ts index 6b75385a03..16a802985d 100644 --- a/src/staff-administrator/models/location-by-service-code-model.ts +++ b/src/staff-administrator/models/location-by-service-code-model.ts @@ -1,5 +1,6 @@ export interface LocationByEpimmsModelWithServiceCodes { epimms_id: string; + location_id?: string; site_name?: string; court_name?: string; open_for_public?: string; From dfc0c040543b8c8a570e175348e8a05d3f4fd35d Mon Sep 17 00:00:00 2001 From: codaimaster <55559010+codaimaster@users.noreply.github.com> Date: Thu, 17 Aug 2023 12:30:05 +0100 Subject: [PATCH 2/2] upgrade toolkit for the new patch (#3229) --- package.json | 2 +- yarn.lock | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 690e318cf2..41a3b94c1a 100644 --- a/package.json +++ b/package.json @@ -85,7 +85,7 @@ "@angular/platform-browser-dynamic": "^11.2.14", "@angular/router": "^11.2.14", "@edium/fsm": "^2.1.2", - "@hmcts/ccd-case-ui-toolkit": "6.19.1-RetryCaseRetrievals.2", + "@hmcts/ccd-case-ui-toolkit": "6.19.3-RetryCaseRetrievals.1", "@hmcts/ccpay-web-component": "5.2.8", "@hmcts/frontend": "0.0.39-alpha", "@hmcts/media-viewer": "2.9.3", diff --git a/yarn.lock b/yarn.lock index d6b72ddc8a..4e118b3f78 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2386,12 +2386,12 @@ __metadata: languageName: node linkType: hard -"@hmcts/ccd-case-ui-toolkit@npm:6.19.1-RetryCaseRetrievals.2": - version: 6.19.1-RetryCaseRetrievals.2 - resolution: "@hmcts/ccd-case-ui-toolkit@npm:6.19.1-RetryCaseRetrievals.2" +"@hmcts/ccd-case-ui-toolkit@npm:6.19.3-RetryCaseRetrievals.1": + version: 6.19.3-RetryCaseRetrievals.1 + resolution: "@hmcts/ccd-case-ui-toolkit@npm:6.19.3-RetryCaseRetrievals.1" dependencies: tslib: ^2.0.0 - checksum: bab9f194f43f1ba2dc60685d576a8460fbf07cb69d13945cd6f628bf377ded6886fd1760f319f35b4f1afd845a799cf562bbb0470c6d1df98aa223ffef6deff6 + checksum: 804b26964c596540b05e0c88baadda0f07a4d3a9aaa5ca6782d3d9879ae01f7e6cfb59e4892483385a9d4a8428e709b06f64efa0b99518039ade9e81ced3175c languageName: node linkType: hard @@ -19270,7 +19270,7 @@ __metadata: "@angular/platform-browser-dynamic": ^11.2.14 "@angular/router": ^11.2.14 "@edium/fsm": ^2.1.2 - "@hmcts/ccd-case-ui-toolkit": 6.19.1-RetryCaseRetrievals.2 + "@hmcts/ccd-case-ui-toolkit": 6.19.3-RetryCaseRetrievals.1 "@hmcts/ccpay-web-component": 5.2.8 "@hmcts/frontend": 0.0.39-alpha "@hmcts/media-viewer": 2.9.3