68085b1e9d849f37b292105dbeb662b1ad805181
[portal/sdk.git] /
1 import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2
3 import { SQLComponent } from './sql.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 { RouterTestingModule } from '@angular/router/testing';
8 import { SqlService } from './sql.service';
9 import 'rxjs/add/observable/of';
10 import { Observable } from 'rxjs/Observable';
11
12
13 describe('SQLComponentComponent', () => {
14   let sqlService: SqlService;
15   let component: SQLComponent;
16   let fixture: ComponentFixture<SQLComponent>;
17   const reportId =  "test";
18   const finalGetObj = {"query":"dummyQuery"};
19   let environment = [
20     {
21      "baseUrl": 'just for test'
22     }
23   ]
24
25   beforeEach(async(() => {
26     TestBed.configureTestingModule({
27       schemas: [CUSTOM_ELEMENTS_SCHEMA],
28       declarations: [ SQLComponent ],
29       imports: [FormsModule, HttpClientTestingModule, RouterTestingModule],
30       providers: [SqlService]
31     })
32     .compileComponents();
33     sqlService = TestBed.get(SqlService);
34     spyOn(sqlService, 'getSQLTabData').and.returnValue(Observable.of(environment));
35   }));
36
37   beforeEach(() => {
38
39     fixture = TestBed.createComponent(SQLComponent); 
40     component = fixture.componentInstance;
41     component.reportId1 = reportId;
42     component.finalGetObj = finalGetObj;
43     fixture.detectChanges();
44
45   });
46
47   it('should create', () => {
48     expect(component).toBeTruthy();
49   });
50
51   it('should test ngOninit subscribe method', () => {
52       spyOn(component, 'ngOnInit').and.callThrough();
53       component.ngOnInit();
54       expect(component.ngOnInit).toHaveBeenCalled();
55   });
56
57   it('should test ngOnInit method', () => {
58       component.ngOnInit();
59       expect(component.showSaveSQLDialog).toEqual(false);
60       expect(component.SQLPostResponse).toEqual(true);
61       expect(component.ValidatePostResponse).toEqual({});
62   });
63
64   it('should test ngOnChanges methods', () => {
65     
66       fixture.detectChanges();
67       component.ngOnChanges();
68       expect(component.showSaveSQLDialog).toEqual(false);
69       expect(component.SQLPostResponse).toEqual(true);
70       expect(component.ValidatePostResponse).toEqual({});
71   });
72
73   it('should test saveSQL methods if condition', () => {
74       component.SQLPostResponse = true;
75       component.saveSQL();
76       expect(component.SQLstatus).toEqual("Success!");
77       expect(component.SQLmessage).toEqual("Your change has been saved! Definition is updated.");
78       expect(component.showSaveSQLDialog).toEqual(component.showSaveSQLDialog);
79       expect(component.SQLclosable).toEqual(true);
80   });
81
82   it('should test saveSQL method else condition',()=>{
83     component.SQLPostResponse = false;
84     component.saveSQL();
85   })
86
87   it('should test validate method',()=>{
88     component.sqlText="sqlText";
89     var response={"data":{"elements":'{"elements":""}'}}
90     let spy=spyOn(sqlService,'postSQLValidateAndSave').and.returnValue(Observable.of(response))
91     component.validate();
92     expect(spy).toHaveBeenCalled();
93   })
94
95   it('should test validate method',()=>{
96     component.sqlText="sqlText";
97     var response={"data":{"elements":'{"query":"query"}'}}
98     let spy=spyOn(sqlService,'postSQLValidateAndSave').and.returnValue(Observable.of(response))
99     component.validate();
100     expect(spy).toHaveBeenCalled();
101   })
102
103   it('should test closeSaveModal method', () => {
104       component.closeSaveModal();
105       expect(component.showSaveSQLDialog).toEqual(component.showSaveSQLDialog);
106       expect(component.SQLclosable).toEqual(false);
107   });
108
109   it('should test closeValidateModal method', () => {
110       component.reportMode = "Create";
111       component.Validatestatus = "SQL Test Run - Failed!";
112       component.closeValidateModal();
113       expect(component.sqlText).toEqual(component.sqlText);
114       expect(component.showValidateSQLDialog).toEqual(component.showValidateSQLDialog);
115       expect(component.Validateclosable).toEqual(false);
116
117       component.reportMode = "Create";
118       component.Validatestatus = "SQL Test Run - Passed!";
119       component.closeValidateModal();
120       expect(component.showValidateSQLDialog).toEqual(component.showValidateSQLDialog);
121       expect(component.Validateclosable).toEqual(false);
122   });
123
124   it('should test SetValidateResponseString method', () => {
125       component.SetValidateResponseString("testing");
126       expect(component.ValidateResponseString).toEqual("testing");
127   });
128
129   it('should test GetValidateResponseString method', () => {
130       component.ValidateResponseString = 'test';
131       expect(component.GetValidateResponseString()).toEqual("test");
132   });
133
134 });