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