Skip to content

Commit

Permalink
Merge branch 'EXUI-266' of github.com:hmcts/rpx-xui-webapp into EXUI-266
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Elliott authored and Tom Elliott committed Aug 17, 2023
2 parents 36b51ea + d2fe453 commit 42c559c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 }
Expand Down Expand Up @@ -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');
Expand All @@ -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);
Expand All @@ -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();
}));
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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;
}
Expand All @@ -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[]]) => {
Expand All @@ -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(
Expand All @@ -64,19 +66,20 @@ 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);
});
// EUI-8051 - as well as setting the correct service codes for new locations, we need to edit them for existing locations
// 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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface LocationByEpimmsModelWithServiceCodes {
epimms_id: string;
location_id?: string;
site_name?: string;
court_name?: string;
open_for_public?: string;
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 42c559c

Please sign in to comment.