Authentication support for cdt
[appc/cdt.git] / src / app / shared / components / navigation / navigation.component.spec.ts
1 /*
2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved.
5 ===================================================================
6
7 Unless otherwise specified, all software contained herein is licensed
8 under the Apache License, Version 2.0 (the License);
9 you may not use this software except in compliance with the License.
10 You may obtain a copy of the License at
11
12     http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19
20 ============LICENSE_END============================================
21 */
22
23 import { async, ComponentFixture, TestBed, fakeAsync, tick, inject } from '@angular/core/testing';
24 import { RouterTestingModule } from "@angular/router/testing";
25 import { BaseRequestOptions, Response, ResponseOptions, Http } from '@angular/http';
26 import { MockBackend, MockConnection } from '@angular/http/testing';
27 import { Router, ActivatedRoute } from "@angular/router";
28 import { Observable } from 'rxjs/Observable';
29 import 'rxjs/add/observable/empty';
30 import 'rxjs/add/observable/of';
31
32 import { NavigationComponent } from './navigation.component';
33 import { EmitterService } from '../../services/emitter.service';
34
35 describe('NavigationComponent', () => {
36     let component: NavigationComponent;
37     let fixture: ComponentFixture<NavigationComponent>;
38
39     beforeEach(async(() => {
40         TestBed.configureTestingModule({
41             declarations: [NavigationComponent],
42             imports: [RouterTestingModule.withRoutes([])],
43             providers: [EmitterService, MockBackend, BaseRequestOptions, {
44                     provide: Http,
45                     useFactory: (backend: MockBackend, defaultOptions: BaseRequestOptions) => {
46                         return new Http(backend, defaultOptions);
47                     },
48                     deps: [MockBackend, BaseRequestOptions],
49                 }]
50         })
51             .compileComponents();
52     }));
53
54     beforeEach(() => {
55         fixture = TestBed.createComponent(NavigationComponent);
56         component = fixture.componentInstance;
57         fixture.detectChanges();
58     });
59
60     it('should create', () => {
61         expect(component).toBeTruthy();
62     });
63
64     it('should set userLoggedIn on ngOnInit', () => {
65         component.userId = 'testingId';
66
67         component.ngOnInit();
68     });
69
70     describe('Should test ngOnChanges', () => {
71         it('should validate on ngOnChanges subscribe return', () => {
72             let spy = spyOn(EmitterService, 'get').and.returnValue(Observable.of('you object'));
73             component.id = 'userLogin';
74
75             component.ngOnChanges();
76
77             expect(spy).toHaveBeenCalled();
78         });
79
80         it('should validate on ngOnChanges if subscribe return null or undefined', inject([Router],(router: Router) => {
81             let spy = spyOn(EmitterService, 'get').and.returnValue(Observable.of(''));
82             let spy1 = spyOn(component, 'logout');
83             component.id = 'userLogin';
84             component.ngOnChanges();
85
86             expect(spy).toHaveBeenCalled();
87             
88             expect(spy1).toHaveBeenCalled()
89         }));
90     });
91
92     it('should go to /vnfs/list if url = vnfs and userId is not null or undefined', inject([Router],(router: Router) => {
93         let navigateSpy = spyOn(router, 'navigate');
94         sessionStorage['userId'] = 'testingId';
95         let testUrl = 'vnfs';
96
97         component.gotoDetail(testUrl);
98     }));
99
100     it('should go to /vnfs if url = vnfs and userId is null or undefined', inject([Router],(router: Router) => {
101         let navigateSpy = spyOn(router, 'navigate');
102         sessionStorage['userId'] = '';
103         let testUrl = 'vnfs';
104
105         component.gotoDetail(testUrl);
106     }));
107
108     it('should go to passed url if url != vnfs', inject([Router],(router: Router) => {
109         let navigateSpy = spyOn(router, 'navigate');
110         let testUrl = 'test';
111
112         component.gotoDetail(testUrl);
113     }));
114
115     it('should logout', inject([Router],(router: Router) => {
116         let navigateSpy = spyOn(router, 'navigate');
117
118         component.logout();
119     }));
120 });