Skip to content

Commit

Permalink
Merge branch 'master' into feature/EUI-6646
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbenjamin-hmcts committed Aug 17, 2023
2 parents ced9a12 + 6ea8216 commit 86e5c4d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
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

0 comments on commit 86e5c4d

Please sign in to comment.