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 } 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';
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}}]
28 beforeEach(async(() => {
29 TestBed.configureTestingModule({
30 schemas:[CUSTOM_ELEMENTS_SCHEMA],
33 DialogOverviewExampleDialog
37 HttpClientTestingModule,
40 BrowserAnimationsModule
43 TestBed.overrideModule(BrowserDynamicTestingModule,{
45 entryComponents:[DialogOverviewExampleDialog]
52 fixture = TestBed.createComponent(FormFieldsComponent);
53 component = fixture.componentInstance;
54 fixture.detectChanges();
55 _formfieldservice=TestBed.get(FormFieldsService);
58 it('should create', () => {
59 expect(component).toBeTruthy();
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;
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");
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))
83 expect(spy).toHaveBeenCalled();
86 it('should test isFirst method',()=>{
87 expect(component.isFirst(0)).toBe(true);
88 expect(component.isFirst(2)).toBe(false);
91 it('should test isLast method1 ', () =>{
92 component.formFieldsListObj.length = 12;
93 expect(component.isLast(15)).toEqual(false);
96 it('should test isLast method2 ', () =>{
97 component.formFieldsListObj.length = 12;
98 expect(component.isLast(11)).toEqual(true);
102 it('should test ngDoCheck method for if condition',()=>{
103 component.sqlAsDefaultValue=true;
104 component.ngDoCheck();
105 expect(component.showDefaultSQLOption).toBe(true);
108 it('should test ngDoCheck method for else condition',()=>{
109 component.sqlAsDefaultValue!=true;
110 component.ngDoCheck();
111 expect(component.showDefaultSQLOption).toBe(false);
114 it('should test setDisplayMode method',()=>{
115 component.setDisplayMode("setDisplayMode");
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();
125 it('should test add method',()=>{
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");
147 it('should test close method1',()=>{
148 component.showDialog=true;
150 expect(component.showDialog).toBe(false);
151 expect(component.closable).toBe(false);
154 it('should test close method2',()=>{
155 component.showDialog=false;
157 expect(component.showDialog).toBe(true);
158 expect(component.closable).toBe(false);
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);
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);
175 it('should test addToList method',()=>{
176 component.addToList("attrib")
178 expect(component.predefinedValueList.pop()).toEqual({id:'attrib', name:'attrib',selected:false});
182 it('should test deleteFromList method',()=>{
183 component.deleteFromList("attrib");
186 it('should test save method1',()=>{
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));
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);
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));
222 expect(component.finalPOSTObj["visible"]).toBe(false)
223 expect(component.finalPOSTObj["fieldDefalultSQL"]).toEqual(component.fieldDefaultSQL);
226 it('should test saveFormFieldGroups method',()=>{
227 component.saveFormFieldGroups();
230 it('should test openSnackBar method',()=>{
231 component.openSnackBar('message','action');
234 it('should test deleteGroup method',()=>{
235 component.deleteGroup("string");
238 it('should test deleteFormGroup method',()=>{
239 component.deleteFromGroup("formgroup");
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");
251 it('should test createGroup method',()=>{
252 component.openDialog()