Skip to content

Commit

Permalink
fix(select): update selected after cd run is finished (#1299)
Browse files Browse the repository at this point in the history
  • Loading branch information
yggg committed Mar 14, 2019
1 parent b3cebda commit 2a1f113
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
20 changes: 13 additions & 7 deletions src/framework/theme/components/select/select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,19 @@ export class NbSelectComponent<T> implements OnInit, AfterViewInit, AfterContent
this.createOverlay();
}

ngAfterContentInit() {
if (this.queue) {
// Call 'writeValue' when current change detection run is finished.
// When writing is finished, change detection starts again, since
// microtasks queue is empty.
// Prevents ExpressionChangedAfterItHasBeenCheckedError.
Promise.resolve().then(() => {
this.writeValue(this.queue);
this.queue = null;
});
}
}

ngAfterViewInit() {
this.triggerStrategy = this.createTriggerStrategy();

Expand All @@ -329,13 +342,6 @@ export class NbSelectComponent<T> implements OnInit, AfterViewInit, AfterContent
this.subscribeOnSelectionChange();
}

ngAfterContentInit() {
if (this.queue) {
this.writeValue(this.queue);
this.cd.detectChanges();
}
}

ngOnDestroy() {
this.alive = false;

Expand Down
33 changes: 31 additions & 2 deletions src/framework/theme/components/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Component, EventEmitter, Input, Output, ViewChild } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ComponentFixture, fakeAsync, flush, TestBed } from '@angular/core/testing';

import { NbSelectModule } from './select.module';
import { NbThemeModule } from '../../theme.module';
Expand Down Expand Up @@ -75,6 +75,24 @@ export class NbSelectTestComponent {
groups = TEST_GROUPS;
}

@Component({
template: `
<nb-layout>
<nb-layout-column>
<nb-select [selected]="selected">
<nb-option *ngFor="let option of options" [value]="option">{{ option }}</nb-option>
</nb-select>
</nb-layout-column>
</nb-layout>
`,
})
export class NbSelectWithInitiallySelectedOptionComponent {
@Input() selected = 1;
@Input() options = [ 1, 2, 3 ];

@ViewChild(NbSelectComponent) select: NbSelectComponent<number>;
}

describe('Component: NbSelectComponent', () => {
let fixture: ComponentFixture<NbSelectTestComponent>;
let overlayContainerService: NbOverlayContainerAdapter;
Expand All @@ -96,7 +114,7 @@ describe('Component: NbSelectComponent', () => {
NbLayoutModule,
NbSelectModule,
],
declarations: [NbSelectTestComponent],
declarations: [ NbSelectTestComponent, NbSelectWithInitiallySelectedOptionComponent ],
});

fixture = TestBed.createComponent(NbSelectTestComponent);
Expand Down Expand Up @@ -221,4 +239,15 @@ describe('Component: NbSelectComponent', () => {
expect(button.textContent).toContain('1 noitpO');
})
});

it('should select initially specified value without errors', fakeAsync(() => {
const selectFixture = TestBed.createComponent(NbSelectWithInitiallySelectedOptionComponent);
selectFixture.detectChanges();
flush();

const selectedOption = selectFixture.componentInstance.select.options.find(o => o.selected);
expect(selectedOption.value).toEqual(selectFixture.componentInstance.selected);
const selectButton = selectFixture.nativeElement.querySelector('nb-select button') as HTMLElement;
expect(selectButton.textContent).toEqual(selectedOption.value.toString());
}));
});

0 comments on commit 2a1f113

Please sign in to comment.