d53e63cee4de9a42c9a20ec3173fd55dd9ee6529
[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 } from '@angular/material';
8 import { CommonModule } from '@angular/common';
9 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
10 import { FormFieldsService } from './form-fields.service';
11 import 'rxjs/add/observable/of';
12 import { Observable } from 'rxjs/Observable';
13 import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing';
14
15 describe('FormFieldsComponent', () => {
16   let component: FormFieldsComponent;
17   let fixture: ComponentFixture<FormFieldsComponent>;
18   let _formfieldservice: FormFieldsService;
19    var responseFormFields=[{"orderSeq":3}];
20    var formFieldGroupsJSON={"formFieldGroupsJSON":'{"data":"cachedRegions"}'};
21    var formFieldsListObj=[{"formFieldsListObj":[ {"id":1}]}];
22   var responseDeleted={"responseDeleted":[{"message":"Formfield  Deleted"}]}
23   var responseDefaultSQL={"data":[{"elemensts":"indrijeet"}]};
24   var responsePost={"message":true};
25   var stubData=[{m:true}];
26   var stubData1=[{n:{"id":1}}]
27
28   beforeEach(async(() => {
29     TestBed.configureTestingModule({
30       schemas:[CUSTOM_ELEMENTS_SCHEMA],
31       declarations: [
32          FormFieldsComponent,
33         DialogOverviewExampleDialog
34       ],
35       imports:[
36         FormsModule,
37         HttpClientTestingModule,
38         MatDialogModule,
39         MatSnackBarModule,
40        BrowserAnimationsModule
41       ]
42     })
43     TestBed.overrideModule(BrowserDynamicTestingModule,{
44       set:{
45         entryComponents:[DialogOverviewExampleDialog]
46       }
47     })
48     .compileComponents();
49   }));
50
51   beforeEach(() => {
52     fixture = TestBed.createComponent(FormFieldsComponent);
53     component = fixture.componentInstance;
54     fixture.detectChanges();
55     _formfieldservice=TestBed.get(FormFieldsService);
56   });
57
58   it('should create', () => {
59     expect(component).toBeTruthy();
60   });
61
62    //The blow test case is wrong.
63   // it('should test moveUpward method',()=>{
64   //    component.formFieldsListObj[n]["id"]=1;
65   //   let spy=spyOn(_formfieldservice,'getFormFieldData').and.returnValue(Observable.of(stubData1))
66   //   component.moveUpward(1);
67   //    component.formFieldsListObj[n]["id"]=1;
68   // })
69
70
71   it('should test verify method',()=>{
72     let spy=spyOn(_formfieldservice,'verifySQL').and.returnValue(Observable.of(responseDefaultSQL))
73   component.verify("Default");
74   expect(component.validateResponseString).toEqual( responseDefaultSQL["data"]["elements"])
75   component.verify("value");
76   
77   })
78
79   it('should test ngOnInit method',()=>{
80     let spy=spyOn(_formfieldservice,'getListOfFormFields').and.returnValue(Observable.of(responseFormFields))
81     let spy1=spyOn(_formfieldservice,'getFormFieldGroupsData').and.returnValue(Observable.of(formFieldGroupsJSON))
82     component.ngOnInit();
83     expect(spy).toHaveBeenCalled();
84   })
85
86   it('should test isFirst method',()=>{
87     expect(component.isFirst(0)).toBe(true);
88     expect(component.isFirst(2)).toBe(false);
89   });
90
91   it('should test isLast method1 ', () =>{
92     component.formFieldsListObj.length = 12;
93     expect(component.isLast(15)).toEqual(false);
94   });
95
96   it('should test isLast method2 ', () =>{
97     component.formFieldsListObj.length = 12;
98     expect(component.isLast(11)).toEqual(true);
99   });
100
101  
102   it('should test ngDoCheck method for if condition',()=>{
103     component.sqlAsDefaultValue=true;
104     component.ngDoCheck();
105     expect(component.showDefaultSQLOption).toBe(true);
106  });
107
108  it('should test ngDoCheck method for else condition',()=>{
109   component.sqlAsDefaultValue!=true;
110   component.ngDoCheck();
111   expect(component.showDefaultSQLOption).toBe(false);
112 });
113
114    it('should test setDisplayMode method',()=>{
115       component.setDisplayMode("setDisplayMode");
116   });
117
118   it('should test edit method',()=>{
119     component.fieldDefaultSQL!=null;
120     let spy=spyOn(_formfieldservice,'getFormFieldData').and.returnValue(Observable.of('you object'));
121     component.edit("Id");
122     expect(spy).toHaveBeenCalled();   
123   })
124
125  it('should test add method',()=>{
126    component.add();
127    expect(component.showDialog).toBe(true);
128    expect(component.closable).toBe(true);
129    expect(component.validationType).toEqual("");
130    expect(component.visible).toEqual("YES");
131    expect(component.message).toBe("");
132    expect(component.groupFormField).toBe(false);
133    expect(component.fieldType).toBe("");
134    expect(component.fieldSQL).toBe("");
135    expect(component.fieldName).toBe("");
136    expect(component.fieldId).toBe("");
137    expect(component.fieldDefaultSQL).toBe('');
138    expect(component.errorStackTrace).toBe("");
139    expect(component.errorMessage).toBe("");
140    expect(component.defaultValue).toBe(false);
141    expect(component.predefinedValueList).toEqual([]);
142    expect(component.showDialog).toBe(true);
143    expect(component.closable).toBe(true);
144    expect(component.mode).toEqual("Add");
145  });
146
147  it('should test close method1',()=>{
148    component.showDialog=true;
149    component.close();
150    expect(component.showDialog).toBe(false);
151    expect(component.closable).toBe(false);
152  });
153
154  it('should test close method2',()=>{
155   component.showDialog=false;
156   component.close();
157   expect(component.showDialog).toBe(true);
158   expect(component.closable).toBe(false);
159 });
160
161  it('should test loseValidateModal method1',()=>{
162    component.showDialog1=true;
163     component.closeValidateModal();
164     expect(component.showDialog1).toBe(false);
165     expect(component.showVerifySpinner).toBe(false);
166  })
167
168  it('should test loseValidateModal method2',()=>{
169   component.showDialog1=false;
170    component.closeValidateModal();
171    expect(component.showDialog1).toBe(true);
172    expect(component.showVerifySpinner).toBe(false);
173 });
174
175 it('should test addToList method',()=>{
176   component.addToList("attrib")
177   {
178   expect(component.predefinedValueList.pop()).toEqual({id:'attrib', name:'attrib',selected:false});
179   }
180 });
181
182 it('should test deleteFromList method',()=>{
183 component.deleteFromList("attrib");
184 });
185
186 it('should test save method1',()=>{
187
188   component.visible="YES";
189   component.defaultValue=false;
190   component.mode ="Edit";
191   responseFormFields["m"]=1;
192   spyOn(_formfieldservice,'postFormFieldData').and.returnValue(Observable.of(responsePost));
193   spyOn(_formfieldservice,'getListOfFormFields').and.returnValue(Observable.of(stubData));
194   component.save();
195   // expect(component.showSpinner).toBe(true);
196   // expect(component.showDialog).toBe(false);
197   expect(component.finalPOSTObj["validationType"]).toEqual(component.validationType);
198   expect(component.finalPOSTObj["visible"]).toBe(true);
199   expect(component.finalPOSTObj["orderSeq"]).toEqual(component.orderSeq);
200   expect(component.finalPOSTObj["message"]).toEqual(component.message);
201   expect(component.finalPOSTObj["groupFormField"]).toEqual(component.groupFormField);
202   expect(component.finalPOSTObj["fieldType"]).toEqual(component.fieldType);
203   expect(component.finalPOSTObj["fieldSQL"]).toEqual(component.fieldSQL);
204   expect(component.finalPOSTObj["fieldName"]).toEqual(component.fieldName);
205   expect(component.finalPOSTObj["fieldId"]).toEqual(component.fieldId);
206   expect(component.finalPOSTObj["errorStackTrace"]).toEqual(component.errorStackTrace);
207   expect(component.finalPOSTObj["errorMessage"]).toEqual(component.errorMessage);
208   expect(component.finalPOSTObj["defaultValue"]).toEqual(component.defaultValue);
209     expect(component.finalPOSTObj["fieldDefaultSQL"]).toBe(null); 
210   expect(component.finalPOSTObj["predefinedValueList"]).toEqual(component.predefinedValueList);
211   
212 });
213
214 it('should test save method for else part',()=>{
215   component.visible!="YES";
216   component.defaultValue!=false;
217   component.mode ="Edited";
218   responseFormFields["m"]=1;
219   spyOn(_formfieldservice,'addFormFieldData').and.returnValue(Observable.of(responsePost));
220   spyOn(_formfieldservice,'getListOfFormFields').and.returnValue(Observable.of(stubData));
221   component.save();
222   expect(component.finalPOSTObj["visible"]).toBe(false)
223   expect(component.finalPOSTObj["fieldDefalultSQL"]).toEqual(component.fieldDefaultSQL);
224 })
225
226 it('should test saveFormFieldGroups method',()=>{
227  component.saveFormFieldGroups();
228 })
229
230 it('should test openSnackBar method',()=>{
231   component.openSnackBar('message','action');
232 })
233
234 it('should test deleteGroup method',()=>{
235   component.deleteGroup("string");
236 })
237
238 it('should test deleteFormGroup method',()=>{
239   component.deleteFromGroup("formgroup");
240 })
241
242 it('should test delete method',()=>{
243   responseDeleted["message"]="Formfield  Deleted";
244   let spy=spyOn(_formfieldservice,'deleteFormField').and.returnValue(Observable.of(responseDeleted));
245   spyOn(_formfieldservice,'getListOfFormFields').and.returnValue(Observable.of('you object'))
246   component.delete("delete");
247 })
248
249
250
251 it('should test createGroup method',()=>{
252   component.openDialog()
253 })
254
255 })