From 69c5ce789e84689e25546f5b8cb7c8528cfe6fb2 Mon Sep 17 00:00:00 2001 From: Karthik Jeeyar Date: Wed, 5 Dec 2018 00:26:00 +0530 Subject: [PATCH] fix(whitespace-issue): add validation to create label field (#2811) * fix(whitespace-issue): add validation to create label field * fix tslint errors * fix(whitespaceissue): Add test cases * fix(whitespaceissue): fix test cases errors * change indentation in test cases --- .../label-selector.component.spec.ts | 71 +++++++++++++++++++ .../label-selector.component.ts | 7 +- 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/app/components_ngrx/label-selector/label-selector.component.spec.ts diff --git a/src/app/components_ngrx/label-selector/label-selector.component.spec.ts b/src/app/components_ngrx/label-selector/label-selector.component.spec.ts new file mode 100644 index 000000000..668fa90e3 --- /dev/null +++ b/src/app/components_ngrx/label-selector/label-selector.component.spec.ts @@ -0,0 +1,71 @@ + +import { CommonModule } from '@angular/common'; +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { async, ComponentFixture, inject, TestBed } from '@angular/core/testing'; +import { HttpModule } from '@angular/http'; +import { Store, StoreModule } from '@ngrx/store'; +import { WidgetsModule } from 'ngx-widgets'; +import { AppState } from './../../../app/states/app.state'; +import { LabelService } from './../../services/label.service'; +import { SelectDropdownModule } from './../../widgets/select-dropdown/select-dropdown.module'; +import { LabelSelectorComponent } from './label-selector.component'; + +import { HttpClientService } from './../../shared/http-module/http.service'; + +let componentInstance: LabelSelectorComponent; +let fixture: ComponentFixture; +let store: Store; + +describe('LabelSelectorComponent', () => { + + beforeEach(async(() => { + TestBed.configureTestingModule({ + imports: [ + WidgetsModule, + CommonModule, + HttpModule, + HttpClientTestingModule, + SelectDropdownModule, + StoreModule.forRoot({}) + ], + declarations: [ + LabelSelectorComponent + ], + providers: [ LabelService, HttpClientService, Store] + }).compileComponents() + .then(() => { + let store = TestBed.get(Store); + spyOn(store, 'dispatch').and.callThrough(); + fixture = TestBed.createComponent(LabelSelectorComponent); + componentInstance = fixture.componentInstance; + fixture.detectChanges(); + }); + })); + + it(`should create label selector component`, async(() => { + expect(componentInstance).toBeTruthy(); + })); + + it('should recognize a label name field', async(() => { + fixture.detectChanges(); + const labelnameInput = fixture.componentInstance.labelnameInput; + expect(labelnameInput).toBeDefined(); + })); + + it('should remove the white spaces', async(() => { + let labelnameInput = fixture.componentInstance.labelnameInput; + labelnameInput.nativeElement.input = ' '; + fixture.detectChanges(); + expect(labelnameInput.nativeElement.value.length).toBe(0); + })); + + it('should disable the create label button', async(() => { + let labelnameInput = fixture.componentInstance.labelnameInput; + labelnameInput.nativeElement.input = ' '; + fixture.detectChanges(); + // check if button is disabled + expect(fixture.componentInstance['createDisabled']).toBeTruthy(); + expect(labelnameInput.nativeElement.value.length).toBe(0); + })); + +}); diff --git a/src/app/components_ngrx/label-selector/label-selector.component.ts b/src/app/components_ngrx/label-selector/label-selector.component.ts index 8bd8e829a..d9649c676 100644 --- a/src/app/components_ngrx/label-selector/label-selector.component.ts +++ b/src/app/components_ngrx/label-selector/label-selector.component.ts @@ -210,6 +210,11 @@ export class LabelSelectorComponent implements OnInit { } onAddLabelInput(val) { - this.createDisabled = val === ''; + if (val.trim().length === 0 && val.length) { + this.labelnameInput.nativeElement.value = ''; + this.createDisabled = true; + } else { + this.createDisabled = false; + } } }