780b3e79093f5f146344856228e8c89305fde33c
[portal/sdk.git] /
1 import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2
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';
13
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}];
24
25   beforeEach(async(() => {
26     TestBed.configureTestingModule({
27       schemas:[CUSTOM_ELEMENTS_SCHEMA],
28       providers:[MatDialog,DialogOverviewExampleDialog],
29       declarations: [
30          FormFieldsComponent,
31         DialogOverviewExampleDialog
32       ],
33       imports:[
34         FormsModule,
35         HttpClientTestingModule,
36         MatDialogModule,
37         MatSnackBarModule,
38        BrowserAnimationsModule,
39       ]
40     })
41     TestBed.overrideModule(BrowserDynamicTestingModule,{
42       set:{
43         entryComponents:[DialogOverviewExampleDialog]
44       }
45     })
46     .compileComponents();
47   }));
48
49   beforeEach(() => {
50     fixture = TestBed.createComponent(FormFieldsComponent);
51     component = fixture.componentInstance;
52     fixture.detectChanges();
53     _formfieldservice=TestBed.get(FormFieldsService);
54   });
55
56   it('should create', () => {
57     expect(component).toBeTruthy();
58   });
59
60   it('should test moveUpward method',()=>{
61     component.postDownwardObj={"indrijeet":"kumar"};
62     component.reportId1="reportId1";
63     component.formFieldsListObj=[{"id":123},
64                                  {"id":321}];
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();
75   })
76
77   it('should test moveDownward method',()=>{
78     component.postDownwardObj={"indrijeet":"kumar"};
79     component.reportId1="reportId1";
80     component.formFieldsListObj=[{"id":123},
81                                  {"id":321}];
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();
92   })
93
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"); 
99   })
100
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();
106   })
107
108   it('should test isFirst method',()=>{
109     expect(component.isFirst(0)).toBe(true);
110     expect(component.isFirst(2)).toBe(false);
111   });
112
113   it('should test isLast method1 ', () =>{
114     component.formFieldsListObj.length = 12;
115     expect(component.isLast(15)).toEqual(false);
116   });
117
118   it('should test isLast method2 ', () =>{
119     component.formFieldsListObj.length = 12;
120     expect(component.isLast(11)).toEqual(true);
121   });
122
123  
124   it('should test ngDoCheck method for if condition',()=>{
125     component.sqlAsDefaultValue=true;
126     component.ngDoCheck();
127     expect(component.showDefaultSQLOption).toBe(true);
128  });
129
130  it('should test ngDoCheck method for else condition',()=>{
131   component.sqlAsDefaultValue!=true;
132   component.ngDoCheck();
133   expect(component.showDefaultSQLOption).toBe(false);
134 });
135
136    it('should test setDisplayMode method',()=>{
137       component.setDisplayMode("setDisplayMode");
138   });
139
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();   
145   })
146
147  it('should test add method',()=>{
148    component.add();
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");
167  });
168
169  it('should test close method1',()=>{
170    component.showDialog=true;
171    component.close();
172    expect(component.showDialog).toBe(false);
173    expect(component.closable).toBe(false);
174  });
175
176  it('should test close method2',()=>{
177   component.showDialog=false;
178   component.close();
179   expect(component.showDialog).toBe(true);
180   expect(component.closable).toBe(false);
181 });
182
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);
188  })
189
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);
195 });
196
197 it('should test addToList method',()=>{
198   component.addToList("attrib")
199  {
200   expect(component.predefinedValueList.pop()).toEqual({id:'attrib', name:'attrib',selected:false});
201   }
202 });
203
204 it('should test deleteFromList method',()=>{
205 component.predefinedValueList=[{"id":"attrib"}]
206 component.deleteFromList("attrib");
207 });
208
209 it('should test save method1',()=>{
210
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));
217   component.save();
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);
234   
235 });
236
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));
244   component.save();
245   expect(spy1).toHaveBeenCalled();
246   expect(spy2).toHaveBeenCalled();
247   expect(component.finalPOSTObj["visible"]).toBe(false)
248   expect(component.finalPOSTObj["fieldDefalultSQL"]).toEqual(component.fieldDefaultSQL);
249 })
250
251 it('should test saveFormFieldGroups method',()=>{
252  component.saveFormFieldGroups();
253 })
254
255 it('should test openSnackBar method',()=>{
256   component.openSnackBar('message','action');
257 })
258
259 it('should test deleteGroup method',()=>{
260   component.Groups=[{"name":"groupName"}];
261   component.deleteGroup("groupName");
262 })
263
264 it('should test deleteFormGroup method',()=>{
265   component.Groups=[{"formFieldList":{"id":"formFieldId"}}]
266   //component.Groups=[{"elements":{"formFieldList":{"id":"formFieldId"}}}]
267   component.deleteFromGroup("formgroup");
268 })
269
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();
277 })
278
279 it('should test createGroup method',()=>{
280   component.openDialog()
281 })
282
283   // it('should test openDialog method',()=>{
284   //   var result={"Name":"name"};
285   //   component.Groups=null;
286   //   const dialogRef = component.dialog.open(DialogOverviewExampleDialog, {
287   //     width: '400px',
288   //     height: '600px',
289   //     data: component.formFieldsListObj
290   //   });
291   //   spyOn(dialogRef,'afterClosed').and.returnValue(result);
292   //   component.openDialog();
293   // })
294
295 it('should test createGroup method',()=>{
296   let spy=spyOn(component,'openDialog');
297   component.createGroup();
298   expect(spy).toHaveBeenCalled();
299 })
300
301 })