import { waitForAsync, ComponentFixture, TestBed } from '@angular/core/testing'; import { HttpClientTestingModule } from '@angular/common/http/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { Router, Routes } from '@angular/router'; import { Location } from '@angular/common'; import { LoginComponent } from './login.component'; import { LoginModule } from './login.module'; import { USER } from '../../mockup'; import { KeycloakService } from '../../utils/keycloak.service'; import { GraphQLService } from '../../utils/graphql.service'; import { AuthGaurdService } from '../../utils/auth-guard.service'; import { split, lowerCase } from 'lodash'; import { of, Subscription } from 'rxjs'; const routes: Routes = [ { path: '', redirectTo: '/login', pathMatch: 'full' }, { path: 'login', loadChildren: () => import('./login.module').then(m => m.LoginModule) } ]; const graphQLMockService = { query: () => of(USER) }; describe('LoginComponent', () => { let component: LoginComponent; let fixture: ComponentFixture; let router: Router; let location: Location; let keycloak; let authGuard; let graphQL; const mockFn = jest.fn(); beforeEach(waitForAsync(() => { TestBed.configureTestingModule({ imports: [HttpClientTestingModule, RouterTestingModule.withRoutes(routes), LoginModule], providers: [{ provide: GraphQLService, useValue: graphQLMockService }], declarations: [], }).compileComponents(); router = TestBed.inject(Router); location = TestBed.inject(Location); keycloak = TestBed.inject(KeycloakService); authGuard = TestBed.inject(AuthGaurdService); graphQL = TestBed.inject(GraphQLService); })); beforeEach(() => { fixture = TestBed.createComponent(LoginComponent); component = fixture.componentInstance; component.keyCloakSubscription = new Subscription(); component.userConfigDFSubscription = new Subscription(); component.userConfigDPOSubscription = new Subscription(); fixture.detectChanges(); }); it('Should create', () => { expect(component).toBeTruthy(); }); it('Should navigate to login if user does not have token ', () => { component.initLogin = mockFn; fixture.ngZone.run(() => { component.ngOnInit(); }); keycloak.keycloakSetup.next(true); expect(mockFn).toHaveBeenCalled(); }); it('Should navigate to default app if token is present', () => { keycloak.getToken = () => true; fixture.ngZone.run(() => { component.initLogin(); }); const i18n = USER.data.userConfig.applications[0].i18n.translations; const routeName = split(lowerCase(i18n['header']), ' ', 2).join('-'); expect(routeName).toEqual('demand-forecasting'); }); /* it('Should navigate to login if token is not available', () => { keycloak.getToken = () => false; keycloak.login = mockFn; authGuard.isLogin = (url: '') => mockFn; fixture.ngZone.run(() => { component.initLogin(); }); expect(mockFn).toHaveBeenCalled(); }); it('should call ngOnDestroy', () => { component.userConfigDFSubscription.unsubscribe = jest.fn(); component.userConfigDPOSubscription.unsubscribe = jest.fn(); component.keyCloakSubscription.unsubscribe = jest.fn(); component.ngOnDestroy(); expect(component.userConfigDFSubscription.unsubscribe).toHaveBeenCalled(); expect(component.userConfigDPOSubscription.unsubscribe).toHaveBeenCalled(); expect(component.keyCloakSubscription.unsubscribe).toHaveBeenCalled(); }); */ });