8cf379d04f2a06f9030c460584e47ead8ec5b836
[so.git] / so-monitoring / so-monitoring-ui / src / main / frontend / src / app / login / login.component.spec.ts
1 /**
2  ============LICENSE_START=======================================================
3  Copyright (C) 2019 Samsung. All rights reserved.
4  ================================================================================
5  Licensed under the Apache License, Version 2.0 (the "License");
6  you may not use this file except in compliance with the License.
7  You may obtain a copy of the License at
8
9  http://www.apache.org/licenses/LICENSE-2.0
10
11  Unless required by applicable law or agreed to in writing, software
12  distributed under the License is distributed on an "AS IS" BASIS,
13  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  See the License for the specific language governing permissions and
15  limitations under the License.
16
17  SPDX-License-Identifier: Apache-2.0
18  ============LICENSE_END=========================================================
19
20  @authors: k.kazak@samsung.com
21  **/
22
23 import {async, ComponentFixture, inject, TestBed} from '@angular/core/testing';
24
25 import {LoginComponent} from './login.component';
26 import {AuthenticationService} from "../authentication.service";
27 import {CUSTOM_ELEMENTS_SCHEMA} from "@angular/core";
28 import {FormsModule, ReactiveFormsModule} from '@angular/forms';
29 import {RouterTestingModule} from "@angular/router/testing";
30 import {ActivatedRoute, Router, RouterModule} from "@angular/router";
31
32 describe('LoginComponent', () => {
33   // Create SPY Object for Jasmine tests to mock DataService
34   let spyDataService: jasmine.SpyObj<AuthenticationService>;
35   let component: LoginComponent;
36   let fixture: ComponentFixture<LoginComponent>;
37   let router: Router;
38
39   beforeEach(async(() => {
40     spyDataService = jasmine.createSpyObj('AuthenticationService', ['login', 'logout']);
41
42     TestBed.configureTestingModule({
43       providers: [LoginComponent,
44         {provide: AuthenticationService, useValue: spyDataService},
45         {provide: ActivatedRoute, useValue: { snapshot: {queryParams: { returnUrl: 'test'}}}}
46       ],
47       imports: [RouterTestingModule, ReactiveFormsModule, FormsModule, RouterModule.forRoot([])],
48       declarations: [LoginComponent],
49       schemas: [
50         CUSTOM_ELEMENTS_SCHEMA
51       ]
52     });
53
54     fixture = TestBed.createComponent(LoginComponent);
55     component = fixture.componentInstance;
56     router = TestBed.get(Router);
57   }));
58
59
60   it('should create', inject([LoginComponent],
61     (component: LoginComponent) => {
62       expect(component).toBeTruthy();
63     }));
64
65   it('should logout and route to test directory', inject([LoginComponent],
66     (component: LoginComponent) => {
67       component.ngOnInit();
68       expect(component.returnUrl).toBe('test');
69     }));
70
71   it('should logout and route to root directory', inject([LoginComponent],
72     (component: LoginComponent) => {
73       router.initialNavigation();
74       component.ngOnInit();
75       expect(component.returnUrl).toBe('test');
76     }));
77
78   it('should submit without success', inject([LoginComponent],
79     (component: LoginComponent) => {
80       component.ngOnInit();
81       expect(component.loginForm.valid).toBe(false);
82       component.onSubmit();
83       expect(component.submitted).toBe(true);
84     }));
85
86   it('should submit without success', inject([LoginComponent],
87     (component: LoginComponent) => {
88       component.ngOnInit();
89       expect(component.loginForm.valid).toBe(false);
90       spyDataService.login.and.returnValue(Promise.resolve());
91
92       let compiled = fixture.debugElement.nativeElement;
93       let username = compiled.querySelector('input[type="text"]');
94       let password = compiled.querySelector('input[type="password"]');
95
96       fixture.detectChanges();
97
98       // Change value
99       username.value = 'test';
100       password.value = 'password';
101
102       // dispatch input event
103       dispatchEvent(new Event('input'));
104
105       component.onSubmit();
106       expect(component.submitted).toBe(true);
107     }));
108 });