Skip to content

Commit

Permalink
fix: don't fire addEvent for single select
Browse files Browse the repository at this point in the history
  • Loading branch information
varnastadeus committed Jul 9, 2018
1 parent aaffc90 commit cbfd948
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
30 changes: 23 additions & 7 deletions src/ng-select/ng-select.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2355,7 +2355,7 @@ describe('NgSelectComponent', function () {
});

describe('Output events', () => {
it('fire open event once', fakeAsync(() => {
it('should fire open event once', fakeAsync(() => {
const fixture = createTestingModule(
NgSelectTestCmp,
`<ng-select [items]="cities"
Expand All @@ -2372,7 +2372,7 @@ describe('NgSelectComponent', function () {
expect(fixture.componentInstance.onOpen).toHaveBeenCalledTimes(1);
}));

it('fire close event once', fakeAsync(() => {
it('should fire close event once', fakeAsync(() => {
const fixture = createTestingModule(
NgSelectTestCmp,
`<ng-select [items]="cities"
Expand All @@ -2390,7 +2390,7 @@ describe('NgSelectComponent', function () {
expect(fixture.componentInstance.onClose).toHaveBeenCalledTimes(1);
}));

it('fire change when changed', fakeAsync(() => {
it('should fire change when changed', fakeAsync(() => {
const fixture = createTestingModule(
NgSelectTestCmp,
`<ng-select [items]="cities"
Expand All @@ -2413,7 +2413,7 @@ describe('NgSelectComponent', function () {
expect(fixture.componentInstance.selectedCityId).toBe(fixture.componentInstance.cities[0].id);
}));

it('do not fire change when item not changed', fakeAsync(() => {
it('should not fire change when item not changed', fakeAsync(() => {
const fixture = createTestingModule(
NgSelectTestCmp,
`<ng-select [items]="cities"
Expand All @@ -2431,7 +2431,7 @@ describe('NgSelectComponent', function () {
expect(fixture.componentInstance.onChange).toHaveBeenCalledTimes(1);
}));

it('fire add when item is added', fakeAsync(() => {
it('should fire addEvent when item is added', fakeAsync(() => {
const fixture = createTestingModule(
NgSelectTestCmp,
`<ng-select [items]="cities"
Expand All @@ -2448,7 +2448,23 @@ describe('NgSelectComponent', function () {
expect(fixture.componentInstance.onAdd).toHaveBeenCalledWith(fixture.componentInstance.cities[0]);
}));

it('fire remove when item is removed', fakeAsync(() => {
it('should not fire addEvent for single select', fakeAsync(() => {
const fixture = createTestingModule(
NgSelectTestCmp,
`<ng-select [items]="cities"
(add)="onAdd($event)"
[multiple]="false"
[(ngModel)]="selectedCity">
</ng-select>`);

spyOn(fixture.componentInstance, 'onAdd');

tickAndDetectChanges(fixture);
fixture.componentInstance.select.select(fixture.componentInstance.select.itemsList.items[0]);
expect(fixture.componentInstance.onAdd).not.toHaveBeenCalled();
}));

it('should fire remove when item is removed', fakeAsync(() => {
const fixture = createTestingModule(
NgSelectTestCmp,
`<ng-select [items]="cities"
Expand All @@ -2467,7 +2483,7 @@ describe('NgSelectComponent', function () {
expect(fixture.componentInstance.onRemove).toHaveBeenCalledWith(fixture.componentInstance.cities[0]);
}));

it('fire clear when model is cleared using clear icon', fakeAsync(() => {
it('should fire clear when model is cleared using clear icon', fakeAsync(() => {
const fixture = createTestingModule(
NgSelectTestCmp,
`<ng-select [items]="cities"
Expand Down
11 changes: 7 additions & 4 deletions src/ng-select/ng-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,14 @@ export class NgSelectComponent implements OnDestroy, OnChanges, AfterViewInit, C

select(item: NgOption) {
this.itemsList.select(item);

if (this.clearSearchOnAdd) {
this._clearSearch();
}

this.addEvent.emit(item.value);
if (this.multiple) {
this.addEvent.emit(item.value);
}

if (this.closeOnSelect || this.itemsList.noItemsToSelect) {
this.close();
}
Expand Down Expand Up @@ -633,12 +635,13 @@ export class NgSelectComponent implements OnDestroy, OnChanges, AfterViewInit, C
}
}

const selected = this.selectedItems.map(x => x.value);
if (this.multiple) {
this._onChange(model);
this.changeEvent.emit(this.selectedItems.map(x => x.value));
this.changeEvent.emit(selected);
} else {
this._onChange(isDefined(model[0]) ? model[0] : null);
this.changeEvent.emit(this.selectedItems[0] && this.selectedItems[0].value);
this.changeEvent.emit(selected[0]);
}

this._cd.markForCheck();
Expand Down

0 comments on commit cbfd948

Please sign in to comment.