userlogin-form component test case
[appc/cdt.git] / src / app / vnfs / userlogin-form / userlogin-form.component.spec.ts
1 /*
2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
5
6 Modification Copyright (C) 2018 IBM
7 ===================================================================
8
9 Unless otherwise specified, all software contained herein is licensed
10 under the Apache License, Version 2.0 (the License);
11 you may not use this software except in compliance with the License.
12 You may obtain a copy of the License at
13
14     http://www.apache.org/licenses/LICENSE-2.0
15
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
21
22 ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 ============LICENSE_END============================================
24 */
25
26 /* tslint:disable:no-unused-variable */
27 import { async, ComponentFixture, inject, TestBed } from '@angular/core/testing';
28 import { NO_ERRORS_SCHEMA } from '@angular/core';
29 import { ActivatedRoute } from '@angular/router';
30 import { userloginFormComponent } from './userlogin-form.component';
31 import { FormsModule } from '@angular/forms';
32 import { NotificationService } from './../../shared/services/notification.service';
33 import { ParamShareService } from './../../shared/services/paramShare.service';
34 import { MappingEditorService } from './../../shared/services/mapping-editor.service';
35 import { DialogService } from 'ng2-bootstrap-modal';
36 import { RouterTestingModule } from '@angular/router/testing';
37 import { HttpUtilService } from '.././../shared/services/httpUtil/http-util.service';
38 import { UtilityService } from '.././../shared/services/utilityService/utility.service';
39 import { Router } from '@angular/router';
40 import {NotificationsService} from 'angular2-notifications';
41
42 describe('userloginFormComponent', () => {
43     let component: userloginFormComponent;
44     let fixture: ComponentFixture<userloginFormComponent>;
45     let mockActiveRoute = {
46         snapshot: {
47           queryParams: {
48             returnUrl: '/home',
49           }
50         }
51      };
52     beforeEach(async(() => {
53         TestBed.configureTestingModule({
54             declarations: [userloginFormComponent],
55             schemas: [NO_ERRORS_SCHEMA],
56             imports: [FormsModule, RouterTestingModule,],
57             providers: [UtilityService, ParamShareService, DialogService,NotificationsService, HttpUtilService, MappingEditorService,
58                 {provide: ActivatedRoute, useValue: mockActiveRoute}, 
59                 { provide: Router, useClass: MockRouter }]
60         })
61             .compileComponents();
62     }));
63     beforeEach(() => {
64         fixture = TestBed.createComponent(userloginFormComponent);
65         component = fixture.componentInstance;
66         fixture.detectChanges();
67         // localStorage['userId'] = "testUser"
68         component.userId = 'test Usr';
69     });
70
71     class MockRouter {
72         navigateByUrl(url: string) {
73             return url;
74         }
75
76         navigate(url: string) {
77             return url;
78         }
79     }
80     class MockUtility {
81         randomId() {
82             return 123;
83         }
84     }
85     it('should create', () => {
86         expect(component).toBeTruthy();
87     });
88     it('get the user Id', () => {
89         component.getData();
90         expect(localStorage.userId).toEqual('test Usr');
91     });
92
93     it('should route to myvnfform', inject([Router], (router: Router) => {
94         const spy = spyOn(router, 'navigateByUrl');
95         component.getData();
96         const url = spy.calls.first().args[0];
97
98         // return url is set to '/home' in mockActiveRoute.
99         expect(url.length).toBe(5);
100         expect(url[0]).toEqual('/');
101         expect(url[1]).toEqual('h');
102         expect(localStorage['userId']).toBe('test Usr');
103     }));
104
105     it('test validateUserName function', () => {
106         component.userId = '';
107         component.validateUserName();
108         expect(component.errorMessage).toEqual('');
109         expect(component.invalid).toEqual(true);
110     });
111
112 });
113