Skip to content

Commit 0e31212

Browse files
feat(LocationSelectMenu): Alphabetize options (#2219)
- Add test for LocationSelectMenuComponent
1 parent 35e7824 commit 0e31212

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

src/app/domain/projectFilterValues.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ export class ProjectFilterValues {
105105
this.locationValue.length === 0 ||
106106
project.metadata.locations
107107
?.map((location) => Object.assign(new Location(), location))
108-
.map((location) => location.getLocationOptions())
109-
.flat()
108+
.flatMap((location) => location.getLocationOptions())
110109
.some((locationOption) => this.locationValue.includes(locationOption.name))
111110
);
112111
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { HarnessLoader } from '@angular/cdk/testing';
3+
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
4+
import { MatSelectHarness } from '@angular/material/select/testing';
5+
import { LocationSelectMenuComponent } from './location-select-menu.component';
6+
import { Location } from '../../library/Location';
7+
8+
let component: LocationSelectMenuComponent;
9+
let fixture: ComponentFixture<LocationSelectMenuComponent>;
10+
describe('LocationSelectMenuComponent', () => {
11+
let loader: HarnessLoader;
12+
beforeEach(async () => {
13+
await TestBed.configureTestingModule({
14+
imports: [LocationSelectMenuComponent]
15+
}).compileComponents();
16+
17+
fixture = TestBed.createComponent(LocationSelectMenuComponent);
18+
loader = TestbedHarnessEnvironment.loader(fixture);
19+
component = fixture.componentInstance;
20+
component['viewValueProp'] = 'name';
21+
component.options = [
22+
Object.assign(new Location(), {
23+
level1: 'USA',
24+
level2: 'California'
25+
}),
26+
Object.assign(new Location(), {
27+
level1: 'USA',
28+
level2: 'New York'
29+
}),
30+
Object.assign(new Location(), {
31+
level1: 'Canada',
32+
level2: 'Ontario'
33+
})
34+
];
35+
fixture.detectChanges();
36+
});
37+
38+
it('should show groups and options', async () => {
39+
const select = await loader.getHarness(MatSelectHarness);
40+
await select.open();
41+
const optionGroups = await select.getOptionGroups();
42+
expect(
43+
await Promise.all(optionGroups.map(async (option) => await option.getLabelText()))
44+
).toEqual(['State', 'Country']);
45+
const options = await select.getOptions();
46+
expect(await Promise.all(options.map(async (option) => await option.getText()))).toEqual([
47+
'California, USA',
48+
'New York, USA',
49+
'Ontario, Canada',
50+
'Canada',
51+
'USA'
52+
]);
53+
});
54+
});

src/app/modules/shared/location-select-menu/location-select-menu.component.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,30 @@ export class LocationSelectMenuComponent extends SelectMenuComponent {
2121

2222
ngOnInit(): void {
2323
super.ngOnInit();
24+
this.populateLocationOptions();
25+
this.alphabetizeOptions();
26+
this.keepNonEmptyLabels();
27+
}
28+
29+
private populateLocationOptions(): void {
2430
this.options
2531
.flatMap((option: Location) => option.getLocationOptions())
2632
.forEach((option: LocationOption) => {
2733
if (!this.locationOptions[option.type].some((opt) => opt.name === option.name)) {
2834
this.locationOptions[option.type].push(option);
2935
}
3036
});
37+
}
38+
39+
private alphabetizeOptions(): void {
40+
(Object.keys(this.locationOptions) as LocationType[]).forEach((key: LocationType) =>
41+
this.locationOptions[key].sort((a: LocationOption, b: LocationOption) =>
42+
a.name.localeCompare(b.name)
43+
)
44+
);
45+
}
46+
47+
private keepNonEmptyLabels(): void {
3148
this.labels = Object.keys(this.locationOptions).filter(
3249
(key: LocationType) => this.locationOptions[key].length > 0
3350
) as LocationType[];

0 commit comments

Comments
 (0)