From fbda1d2ade35c81601f8e67b99c5ba1379666182 Mon Sep 17 00:00:00 2001 From: Sergey Andrievskiy Date: Mon, 22 Apr 2019 13:45:25 +0300 Subject: [PATCH] fix(select): emit array when resetting multiple select (#1399) --- .../components/select/select.component.ts | 2 +- .../theme/components/select/select.spec.ts | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/framework/theme/components/select/select.component.ts b/src/framework/theme/components/select/select.component.ts index 037aed36e1..d0dd92876e 100644 --- a/src/framework/theme/components/select/select.component.ts +++ b/src/framework/theme/components/select/select.component.ts @@ -416,7 +416,7 @@ export class NbSelectComponent implements OnInit, AfterViewInit, AfterContent this.selectionModel = []; this.hide(); this.button.nativeElement.focus(); - this.emitSelected(null); + this.emitSelected(this.multiple ? [] : null); } /** diff --git a/src/framework/theme/components/select/select.spec.ts b/src/framework/theme/components/select/select.spec.ts index 20bd5ca331..5e7c774815 100644 --- a/src/framework/theme/components/select/select.spec.ts +++ b/src/framework/theme/components/select/select.spec.ts @@ -360,6 +360,31 @@ describe('Component: NbSelectComponent', () => { expect(overlayContainer.querySelectorAll('nb-option.selected').length).toBe(0); }); + it('should emit selectionChange with empty array when reset option selected in multiple select', () => { + select.multiple = true; + setSelectedAndOpen(['Option 1', 'Option 2']); + + const selectionChangeSpy = createSpy('selectionChangeSpy'); + select.selectedChange.subscribe(selectionChangeSpy); + + const option = overlayContainer.querySelector('nb-option'); + option.dispatchEvent(new Event('click')); + + expect(selectionChangeSpy).toHaveBeenCalledWith([]); + }); + + it('should emit selectionChange with null when reset option selected in single select', () => { + setSelectedAndOpen('Option 1'); + + const selectionChangeSpy = createSpy('selectionChangeSpy'); + select.selectedChange.subscribe(selectionChangeSpy); + + const option = overlayContainer.querySelector('nb-option'); + option.dispatchEvent(new Event('click')); + + expect(selectionChangeSpy).toHaveBeenCalledWith(null); + }); + it('should deselect only clicked item in multiple select', () => { select.multiple = true; setSelectedAndOpen(['Option 1', 'Option 2']);