Skip to content

Commit

Permalink
fix: check if bindLabel exist while searching for selected items
Browse files Browse the repository at this point in the history
  • Loading branch information
varnastadeus committed Jan 9, 2018
1 parent 3cf5436 commit 1835187
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/ng-select/items-list.ts
Expand Up @@ -52,7 +52,7 @@ export class ItemsList {
}
const index = this.items.findIndex(x => x.value === value);
return index > -1 ? this.items[index] :
this.items.find(item => item.label === this.resolveNested(value, this._bindLabel))
this.items.find(item => item.label && item.label === this.resolveNested(value, this._bindLabel));
}

unselect(item: NgOption) {
Expand Down
27 changes: 22 additions & 5 deletions src/ng-select/ng-select.component.spec.ts
Expand Up @@ -99,11 +99,28 @@ describe('NgSelectComponent', function () {
fixture.componentInstance.cities = cities;
tickAndDetectChanges(fixture);

expect(fixture.componentInstance.select.selectedItems).toEqual([jasmine.objectContaining(
{
value: cities[0]
}
)]);
expect(fixture.componentInstance.select.selectedItems).toEqual([jasmine.objectContaining({
value: cities[0]
})]);
}));

it('should set items correctly if there is no bindLabel', fakeAsync(() => {
const fixture = createTestingModule(
NgSelectModelChangesTestCmp,
`<ng-select
[items]="cities"
[clearable]="true"
[(ngModel)]="selectedCity">
</ng-select>`);

const cities = [{ id: 7, name: 'Pailgis' }];
fixture.componentInstance.selectedCity = { id: 7, name: 'Pailgis' };
tickAndDetectChanges(fixture);
fixture.componentInstance.cities = [{ id: 1, name: 'Vilnius' }, { id: 2, name: 'Kaunas' }];
tickAndDetectChanges(fixture);
expect(fixture.componentInstance.select.selectedItems[0]).toEqual(jasmine.objectContaining({
value: cities[0]
}));
}));

it('should bind ngModel even if items are empty', fakeAsync(() => {
Expand Down

0 comments on commit 1835187

Please sign in to comment.