Skip to content

Commit

Permalink
fix(module: input-item): fix input value can't be 0 or '' (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
nuonuoge authored and fisherspy committed Dec 25, 2018
1 parent d3d3478 commit 59e0ea8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
15 changes: 14 additions & 1 deletion components/input-item/input-item.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,27 @@ import { async, ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core
import { InputItemModule } from './input-item.module';
import { createFakeEvent, dispatchFakeEvent } from '../core/testing';
import { InputItem } from './input-item.component';
import { FormsModule } from '@angular/forms';

describe('InputComponent', () => {
let component: TestInputComponent;
let fixture: ComponentFixture<TestInputComponent>;
let inputItem;
let inputEle;
let inputModel;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [TestInputComponent],
imports: [InputItemModule]
imports: [InputItemModule, FormsModule]
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(TestInputComponent);
component = fixture.componentInstance;
inputItem = fixture.debugElement.query(By.css('InputItem'));
inputModel = fixture.debugElement.query(By.css('.input-item-1'));
fixture.detectChanges();
});

Expand Down Expand Up @@ -351,6 +354,14 @@ describe('InputComponent', () => {
const fakeInput = inputItem.nativeElement.querySelector('.fake-input');
expect(fakeInput.classList).toContain('focus');
});

it('should ngModel work', () => {
const inputModelEle = inputModel.nativeElement.querySelector('input');
inputModelEle.value = 'test-ng-model';
inputModelEle.dispatchEvent(new Event('input'));
fixture.detectChanges();
expect(component.modelValue).toBe('test-ng-model');
});
});

@Component({
Expand Down Expand Up @@ -382,12 +393,14 @@ describe('InputComponent', () => {
>
<span (click)="clickTitle()">标题</span>
</InputItem>
<InputItem class="input-item-1" [(ngModel)]="modelValue"></InputItem>
<div class="am-list-content" click = "blurFocus()">click to focus</div>
`
})
export class TestInputComponent {
type: string = 'text';
value;
modelValue;
defaultValue: string = '';
placeholder: string = '';
editable: boolean = true;
Expand Down
5 changes: 3 additions & 2 deletions components/input-item/input-item.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,10 @@ export class InputItem implements OnInit, OnChanges, ControlValueAccessor {
}

writeValue(value: any): void {
if (value) {
this._value = value;
if (typeof value === undefined || value === null) {
this._value = '';
}
this._value = value;
}

registerOnChange(fn: (_: any) => void): void {
Expand Down

0 comments on commit 59e0ea8

Please sign in to comment.