1 import { async, ComponentFixture, TestBed } from '@angular/core/testing';
3 import { FormFieldsComponent, DialogOverviewExampleDialog } from './form-fields.component';
4 import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
5 import { FormsModule } from '@angular/forms';
6 import { HttpClientTestingModule } from '@angular/common/http/testing';
7 import { MatDialogModule, MatSnackBarModule, MatDialog } from '@angular/material';
8 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
9 import { FormFieldsService } from './form-fields.service';
10 import 'rxjs/add/observable/of';
11 import { Observable } from 'rxjs/Observable';
12 import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing';
14 describe('FormFieldsComponent', () => {
15 let component: FormFieldsComponent;
16 let fixture: ComponentFixture<FormFieldsComponent>;
17 let _formfieldservice: FormFieldsService;
18 var responseFormFields=[{"orderSeq":3}];
19 var formFieldGroupsJSON={"formFieldGroupsJSON":'{"data":"cachedRegions"}'};
20 var responseDeleted={"responseDeleted":[{"message":"Formfield Deleted"}]}
21 var responseDefaultSQL={"data":[{"elemensts":"indrijeet"}]};
22 var responsePost={"message":true};
23 var stubData=[{m:true}];
25 beforeEach(async(() => {
26 TestBed.configureTestingModule({
27 schemas:[CUSTOM_ELEMENTS_SCHEMA],
28 providers:[MatDialog,DialogOverviewExampleDialog],
31 DialogOverviewExampleDialog
35 HttpClientTestingModule,
38 BrowserAnimationsModule,
41 TestBed.overrideModule(BrowserDynamicTestingModule,{
43 entryComponents:[DialogOverviewExampleDialog]
50 fixture = TestBed.createComponent(FormFieldsComponent);
51 component = fixture.componentInstance;
52 fixture.detectChanges();
53 _formfieldservice=TestBed.get(FormFieldsService);
56 it('should create', () => {
57 expect(component).toBeTruthy();
60 it('should test moveUpward method',()=>{
61 component.postDownwardObj={"indrijeet":"kumar"};
62 component.reportId1="reportId1";
63 component.formFieldsListObj=[{"id":123},
65 var responsePostn={"message":true};
66 var postDownwardObj={"orderSeq":1};
67 var finalListResponse={"orderSeq":1};
68 let spy1=spyOn(_formfieldservice,'getFormFieldData').and.returnValue(Observable.of(postDownwardObj));
69 let spy2=spyOn(_formfieldservice,'postFormFieldData').and.returnValue(Observable.of(responsePostn));
70 let spy3=spyOn(_formfieldservice,'getListOfFormFields').and.returnValue(Observable.of(finalListResponse));
71 component.moveUpward(1);
72 expect(spy1).toHaveBeenCalled();
73 expect(spy2).toHaveBeenCalled();
74 expect(spy3).toHaveBeenCalled();
77 it('should test moveDownward method',()=>{
78 component.postDownwardObj={"indrijeet":"kumar"};
79 component.reportId1="reportId1";
80 component.formFieldsListObj=[{"id":123},
82 var responsePostn={"message":true};
83 var postDownwardObj={"orderSeq":1};
84 var finalListResponse={"orderSeq":1};
85 let spy1=spyOn(_formfieldservice,'getFormFieldData').and.returnValue(Observable.of(postDownwardObj));
86 let spy2=spyOn(_formfieldservice,'postFormFieldData').and.returnValue(Observable.of(responsePostn));
87 let spy3=spyOn(_formfieldservice,'getListOfFormFields').and.returnValue(Observable.of(finalListResponse));
88 component.moveDownward(0);
89 expect(spy1).toHaveBeenCalled();
90 expect(spy2).toHaveBeenCalled();
91 expect(spy3).toHaveBeenCalled();
94 it('should test verify method',()=>{
95 spyOn(_formfieldservice,'verifySQL').and.returnValue(Observable.of(responseDefaultSQL))
96 component.verify("Default");
97 expect(component.validateResponseString).toEqual( responseDefaultSQL["data"]["elements"])
98 component.verify("value");
101 it('should test ngOnInit method',()=>{
102 let spy=spyOn(_formfieldservice,'getListOfFormFields').and.returnValue(Observable.of(responseFormFields))
103 spyOn(_formfieldservice,'getFormFieldGroupsData').and.returnValue(Observable.of(formFieldGroupsJSON))
104 component.ngOnInit();
105 expect(spy).toHaveBeenCalled();
108 it('should test isFirst method',()=>{
109 expect(component.isFirst(0)).toBe(true);
110 expect(component.isFirst(2)).toBe(false);
113 it('should test isLast method1 ', () =>{
114 component.formFieldsListObj.length = 12;
115 expect(component.isLast(15)).toEqual(false);
118 it('should test isLast method2 ', () =>{
119 component.formFieldsListObj.length = 12;
120 expect(component.isLast(11)).toEqual(true);
124 it('should test ngDoCheck method for if condition',()=>{
125 component.sqlAsDefaultValue=true;
126 component.ngDoCheck();
127 expect(component.showDefaultSQLOption).toBe(true);
130 it('should test ngDoCheck method for else condition',()=>{
131 component.sqlAsDefaultValue!=true;
132 component.ngDoCheck();
133 expect(component.showDefaultSQLOption).toBe(false);
136 it('should test setDisplayMode method',()=>{
137 component.setDisplayMode("setDisplayMode");
140 it('should test edit method',()=>{
141 component.fieldDefaultSQL!=null;
142 let spy=spyOn(_formfieldservice,'getFormFieldData').and.returnValue(Observable.of('you object'));
143 component.edit("Id");
144 expect(spy).toHaveBeenCalled();
147 it('should test add method',()=>{
149 expect(component.showDialog).toBe(true);
150 expect(component.closable).toBe(true);
151 expect(component.validationType).toEqual("");
152 expect(component.visible).toEqual("YES");
153 expect(component.message).toBe("");
154 expect(component.groupFormField).toBe(false);
155 expect(component.fieldType).toBe("");
156 expect(component.fieldSQL).toBe("");
157 expect(component.fieldName).toBe("");
158 expect(component.fieldId).toBe("");
159 expect(component.fieldDefaultSQL).toBe('');
160 expect(component.errorStackTrace).toBe("");
161 expect(component.errorMessage).toBe("");
162 expect(component.defaultValue).toBe(false);
163 expect(component.predefinedValueList).toEqual([]);
164 expect(component.showDialog).toBe(true);
165 expect(component.closable).toBe(true);
166 expect(component.mode).toEqual("Add");
169 it('should test close method1',()=>{
170 component.showDialog=true;
172 expect(component.showDialog).toBe(false);
173 expect(component.closable).toBe(false);
176 it('should test close method2',()=>{
177 component.showDialog=false;
179 expect(component.showDialog).toBe(true);
180 expect(component.closable).toBe(false);
183 it('should test loseValidateModal method1',()=>{
184 component.showDialog1=true;
185 component.closeValidateModal();
186 expect(component.showDialog1).toBe(false);
187 expect(component.showVerifySpinner).toBe(false);
190 it('should test loseValidateModal method2',()=>{
191 component.showDialog1=false;
192 component.closeValidateModal();
193 expect(component.showDialog1).toBe(true);
194 expect(component.showVerifySpinner).toBe(false);
197 it('should test addToList method',()=>{
198 component.addToList("attrib")
200 expect(component.predefinedValueList.pop()).toEqual({id:'attrib', name:'attrib',selected:false});
204 it('should test deleteFromList method',()=>{
205 component.predefinedValueList=[{"id":"attrib"}]
206 component.deleteFromList("attrib");
209 it('should test save method1',()=>{
211 component.visible="YES";
212 component.defaultValue=false;
213 component.mode ="Edit";
214 responseFormFields["m"]=1;
215 let spy1=spyOn(_formfieldservice,'postFormFieldData').and.returnValue(Observable.of(responsePost));
216 let spy2=spyOn(_formfieldservice,'getListOfFormFields').and.returnValue(Observable.of(stubData));
218 expect(spy1).toHaveBeenCalled();
219 expect(spy2).toHaveBeenCalled();
220 expect(component.finalPOSTObj["validationType"]).toEqual(component.validationType);
221 expect(component.finalPOSTObj["visible"]).toBe(true);
222 expect(component.finalPOSTObj["orderSeq"]).toEqual(component.orderSeq);
223 expect(component.finalPOSTObj["message"]).toEqual(component.message);
224 expect(component.finalPOSTObj["groupFormField"]).toEqual(component.groupFormField);
225 expect(component.finalPOSTObj["fieldType"]).toEqual(component.fieldType);
226 expect(component.finalPOSTObj["fieldSQL"]).toEqual(component.fieldSQL);
227 expect(component.finalPOSTObj["fieldName"]).toEqual(component.fieldName);
228 expect(component.finalPOSTObj["fieldId"]).toEqual(component.fieldId);
229 expect(component.finalPOSTObj["errorStackTrace"]).toEqual(component.errorStackTrace);
230 expect(component.finalPOSTObj["errorMessage"]).toEqual(component.errorMessage);
231 expect(component.finalPOSTObj["defaultValue"]).toEqual(component.defaultValue);
232 expect(component.finalPOSTObj["fieldDefaultSQL"]).toBe(null);
233 expect(component.finalPOSTObj["predefinedValueList"]).toEqual(component.predefinedValueList);
237 it('should test save method for else part',()=>{
238 component.visible!="YES";
239 component.defaultValue!=false;
240 component.mode ="Edited";
241 responseFormFields["m"]=1;
242 let spy1=spyOn(_formfieldservice,'addFormFieldData').and.returnValue(Observable.of(responsePost));
243 let spy2=spyOn(_formfieldservice,'getListOfFormFields').and.returnValue(Observable.of(stubData));
245 expect(spy1).toHaveBeenCalled();
246 expect(spy2).toHaveBeenCalled();
247 expect(component.finalPOSTObj["visible"]).toBe(false)
248 expect(component.finalPOSTObj["fieldDefalultSQL"]).toEqual(component.fieldDefaultSQL);
251 it('should test saveFormFieldGroups method',()=>{
252 component.saveFormFieldGroups();
255 it('should test openSnackBar method',()=>{
256 component.openSnackBar('message','action');
259 it('should test deleteGroup method',()=>{
260 component.Groups=[{"name":"groupName"}];
261 component.deleteGroup("groupName");
264 it('should test deleteFormGroup method',()=>{
265 component.Groups=[{"formFieldList":{"id":"formFieldId"}}]
266 //component.Groups=[{"elements":{"formFieldList":{"id":"formFieldId"}}}]
267 component.deleteFromGroup("formgroup");
270 it('should test delete method',()=>{
271 responseDeleted["message"]="Formfield Deleted";
272 let spy1=spyOn(_formfieldservice,'deleteFormField').and.returnValue(Observable.of(responseDeleted));
273 let spy2=spyOn(_formfieldservice,'getListOfFormFields').and.returnValue(Observable.of('you object'))
274 component.delete("delete");
275 expect(spy1).toHaveBeenCalled();
276 expect(spy2).toHaveBeenCalled();
279 it('should test createGroup method',()=>{
280 component.openDialog()
283 // it('should test openDialog method',()=>{
284 // var result={"Name":"name"};
285 // component.Groups=null;
286 // const dialogRef = component.dialog.open(DialogOverviewExampleDialog, {
289 // data: component.formFieldsListObj
291 // spyOn(dialogRef,'afterClosed').and.returnValue(result);
292 // component.openDialog();
295 it('should test createGroup method',()=>{
296 let spy=spyOn(component,'openDialog');
297 component.createGroup();
298 expect(spy).toHaveBeenCalled();