import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { FormFieldsComponent } from './form-fields.component'; import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { HttpClientTestingModule } from '@angular/common/http/testing'; import { MatDatepickerModule, MatDialogModule, MatSnackBarModule } from '@angular/material'; import { CommonModule } from '@angular/common'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { FormFieldsService } from './form-fields.service'; import 'rxjs/add/observable/of'; import { Observable } from 'rxjs/Observable'; describe('FormFieldsComponent', () => { let component: FormFieldsComponent; let fixture: ComponentFixture; let _formfieldservice: FormFieldsService; let fieldDefaultSQL : string; beforeEach(async(() => { TestBed.configureTestingModule({ schemas:[CUSTOM_ELEMENTS_SCHEMA], declarations: [ FormFieldsComponent ], imports:[ FormsModule, HttpClientTestingModule, MatDialogModule, MatSnackBarModule, BrowserAnimationsModule, MatDatepickerModule ] }) .compileComponents(); })); beforeEach(() => { fixture = TestBed.createComponent(FormFieldsComponent); component = fixture.componentInstance; fixture.detectChanges(); _formfieldservice=TestBed.get(FormFieldsService); }); it('should create', () => { expect(component).toBeTruthy(); }); it('should test isFirst method',()=>{ expect(component.isFirst(0)).toBe(true); expect(component.isFirst(2)).toBe(false); }); it('should test isLast method1 ', () =>{ component.formFieldsListObj.length = 12; expect(component.isLast(15)).toEqual(false); }); it('should test isLast method2 ', () =>{ component.formFieldsListObj.length = 12; expect(component.isLast(11)).toEqual(true); }); it('should test ngDoCheck method for if condition',()=>{ component.sqlAsDefaultValue=true; component.ngDoCheck(); expect(component.showDefaultSQLOption).toBe(true); }); it('should test ngDoCheck method for else condition',()=>{ component.sqlAsDefaultValue!=true; component.ngDoCheck(); expect(component.showDefaultSQLOption).toBe(false); }); it('should test setDisplayMode method',()=>{ component.setDisplayMode("setDisplayMode"); }); it('should test edit method',()=>{ component.fieldDefaultSQL!=null; let spy=spyOn(_formfieldservice,'getFormFieldData').and.returnValue(Observable.of('you object')); component.edit("Id"); expect(spy).toHaveBeenCalled(); }) it('should test add method',()=>{ component.add(); expect(component.showDialog).toBe(true); expect(component.closable).toBe(true); expect(component.validationType).toEqual(""); expect(component.visible).toEqual("YES"); expect(component.message).toBe(""); expect(component.groupFormField).toBe(false); expect(component.fieldType).toBe(""); expect(component.fieldSQL).toBe(""); expect(component.fieldName).toBe(""); expect(component.fieldId).toBe(""); expect(component.fieldDefaultSQL).toBe(''); expect(component.errorStackTrace).toBe(""); expect(component.errorMessage).toBe(""); expect(component.defaultValue).toBe(""); expect(component.predefinedValueList).toEqual([]); expect(component.showDialog).toBe(true); expect(component.closable).toBe(true); expect(component.mode).toEqual("Add"); }); it('should test close method1',()=>{ component.showDialog=true; component.close(); expect(component.showDialog).toBe(false); expect(component.closable).toBe(false); }); it('should test close method2',()=>{ component.showDialog=false; component.close(); expect(component.showDialog).toBe(true); expect(component.closable).toBe(false); }); it('should test loseValidateModal method1',()=>{ component.showDialog1=true; component.closeValidateModal(); expect(component.showDialog1).toBe(false); expect(component.showVerifySpinner).toBe(false); }) it('should test loseValidateModal method2',()=>{ component.showDialog1=false; component.closeValidateModal(); expect(component.showDialog1).toBe(true); expect(component.showVerifySpinner).toBe(false); }); it('should test addToList method',()=>{ component.addToList("attrib") { expect(component.predefinedValueList.pop()).toEqual({id:'attrib', name:'attrib',selected:false}); } }); it('should test deleteFromList method',()=>{ component.deleteFromList("attrib"); }); it('should test save method1',()=>{ component.visible="YES"; component.defaultValue=false; component.save(); expect(component.showSpinner).toBe(true); expect(component.showDialog).toBe(false); expect(component.finalPOSTObj["validationType"]).toEqual(component.validationType); expect(component.finalPOSTObj["visible"]).toBe(true); expect(component.finalPOSTObj["orderSeq"]).toEqual(component.orderSeq); expect(component.finalPOSTObj["message"]).toEqual(component.message); expect(component.finalPOSTObj["groupFormField"]).toEqual(component.groupFormField); expect(component.finalPOSTObj["fieldType"]).toEqual(component.fieldType); expect(component.finalPOSTObj["fieldSQL"]).toEqual(component.fieldSQL); expect(component.finalPOSTObj["fieldName"]).toEqual(component.fieldName); expect(component.finalPOSTObj["fieldId"]).toEqual(component.fieldId); expect(component.finalPOSTObj["errorStackTrace"]).toEqual(component.errorStackTrace); expect(component.finalPOSTObj["errorMessage"]).toEqual(component.errorMessage); expect(component.finalPOSTObj["defaultValue"]).toEqual(component.defaultValue); expect(component.finalPOSTObj["fieldDefaultSQL"]).toBe(fieldDefaultSQL); expect(component.finalPOSTObj["predefinedValueList"]).toEqual(component.predefinedValueList); }); it('should test save method for else part',()=>{ component.visible!="YES"; component.defaultValue!=false; component.save(); expect(component.finalPOSTObj["visible"]).toBe(false) expect(component.finalPOSTObj["fieldDefalultSQL"]).toEqual(component.fieldDefaultSQL); }) it('should test saveFormFieldGroups method',()=>{ component.saveFormFieldGroups(); }) it('should test openSnackBar method',()=>{ component.openSnackBar('message','action'); }) it('should test deleteGroup method',()=>{ component.deleteGroup("string"); }) it('should test deleteFormGroup method',()=>{ component.deleteFromGroup("formgroup"); }) it('should test delete method',()=>{ component.delete("delete"); }) it('should test verify method',()=>{ component.verify("Default"); component.verify("value"); }) // it('should test createGroup method',()=>{ // component.openDialog() // }) });