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
9 http://www.apache.org/licenses/LICENSE-2.0
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.
17 SPDX-License-Identifier: Apache-2.0
18 ============LICENSE_END=========================================================
20 @authors: k.kazak@samsung.com
23 import {async, ComponentFixture, inject, TestBed} from '@angular/core/testing';
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";
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>;
39 beforeEach(async(() => {
40 spyDataService = jasmine.createSpyObj('AuthenticationService', ['login', 'logout']);
42 TestBed.configureTestingModule({
43 providers: [LoginComponent,
44 {provide: AuthenticationService, useValue: spyDataService},
45 {provide: ActivatedRoute, useValue: { snapshot: {queryParams: { returnUrl: 'test'}}}}
47 imports: [RouterTestingModule, ReactiveFormsModule, FormsModule, RouterModule.forRoot([])],
48 declarations: [LoginComponent],
50 CUSTOM_ELEMENTS_SCHEMA
54 fixture = TestBed.createComponent(LoginComponent);
55 component = fixture.componentInstance;
56 router = TestBed.get(Router);
60 it('should create', inject([LoginComponent],
61 (component: LoginComponent) => {
62 expect(component).toBeTruthy();
65 it('should logout and route to test directory', inject([LoginComponent],
66 (component: LoginComponent) => {
68 expect(component.returnUrl).toBe('test');
71 it('should logout and route to root directory', inject([LoginComponent],
72 (component: LoginComponent) => {
73 router.initialNavigation();
75 expect(component.returnUrl).toBe('test');
78 it('should submit without success', inject([LoginComponent],
79 (component: LoginComponent) => {
81 expect(component.loginForm.valid).toBe(false);
83 expect(component.submitted).toBe(true);
86 it('should submit without success', inject([LoginComponent],
87 (component: LoginComponent) => {
89 expect(component.loginForm.valid).toBe(false);
90 spyDataService.login.and.returnValue(Promise.resolve());
92 let compiled = fixture.debugElement.nativeElement;
93 let username = compiled.querySelector('input[type="text"]');
94 let password = compiled.querySelector('input[type="password"]');
96 fixture.detectChanges();
99 username.value = 'test';
100 password.value = 'password';
102 // dispatch input event
103 dispatchEvent(new Event('input'));
105 component.onSubmit();
106 expect(component.submitted).toBe(true);