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 { 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],
30 DialogOverviewExampleDialog
34 HttpClientTestingModule,
37 BrowserAnimationsModule
40 TestBed.overrideModule(BrowserDynamicTestingModule,{
42 entryComponents:[DialogOverviewExampleDialog]
49 fixture = TestBed.createComponent(FormFieldsComponent);
50 component = fixture.componentInstance;
51 fixture.detectChanges();
52 _formfieldservice=TestBed.get(FormFieldsService);
55 it('should create', () => {
56 expect(component).toBeTruthy();
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");
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))
71 expect(spy).toHaveBeenCalled();
74 it('should test isFirst method',()=>{
75 expect(component.isFirst(0)).toBe(true);
76 expect(component.isFirst(2)).toBe(false);
79 it('should test isLast method1 ', () =>{
80 component.formFieldsListObj.length = 12;
81 expect(component.isLast(15)).toEqual(false);
84 it('should test isLast method2 ', () =>{
85 component.formFieldsListObj.length = 12;
86 expect(component.isLast(11)).toEqual(true);
90 it('should test ngDoCheck method for if condition',()=>{
91 component.sqlAsDefaultValue=true;
92 component.ngDoCheck();
93 expect(component.showDefaultSQLOption).toBe(true);
96 it('should test ngDoCheck method for else condition',()=>{
97 component.sqlAsDefaultValue!=true;
98 component.ngDoCheck();
99 expect(component.showDefaultSQLOption).toBe(false);
102 it('should test setDisplayMode method',()=>{
103 component.setDisplayMode("setDisplayMode");
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();
113 it('should test add method',()=>{
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");
135 it('should test close method1',()=>{
136 component.showDialog=true;
138 expect(component.showDialog).toBe(false);
139 expect(component.closable).toBe(false);
142 it('should test close method2',()=>{
143 component.showDialog=false;
145 expect(component.showDialog).toBe(true);
146 expect(component.closable).toBe(false);
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);
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);
163 it('should test addToList method',()=>{
164 component.addToList("attrib")
166 expect(component.predefinedValueList.pop()).toEqual({id:'attrib', name:'attrib',selected:false});
170 it('should test deleteFromList method',()=>{
171 component.predefinedValueList=[{"id":"attrib"}]
172 component.deleteFromList("attrib");
175 it('should test save method1',()=>{
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));
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);
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));
211 expect(spy1).toHaveBeenCalled();
212 expect(spy2).toHaveBeenCalled();
213 expect(component.finalPOSTObj["visible"]).toBe(false)
214 expect(component.finalPOSTObj["fieldDefalultSQL"]).toEqual(component.fieldDefaultSQL);
217 it('should test saveFormFieldGroups method',()=>{
218 component.saveFormFieldGroups();
221 it('should test openSnackBar method',()=>{
222 component.openSnackBar('message','action');
225 it('should test deleteGroup method',()=>{
226 component.Groups=[{"name":"groupName"}];
227 component.deleteGroup("groupName");
230 it('should test deleteFormGroup method',()=>{
231 component.Groups=[{"formFieldList":{"id":"formFieldId"}}]
232 //component.Groups=[{"elements":{"formFieldList":{"id":"formFieldId"}}}]
233 component.deleteFromGroup("formgroup");
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();
245 it('should test createGroup method',()=>{
246 component.openDialog()
249 it('should test createGroup method',()=>{
250 let spy=spyOn(component,'openDialog');
251 component.createGroup();
252 expect(spy).toHaveBeenCalled();