diff --git a/src/app/shared/log-in/log-in.component.spec.ts b/src/app/shared/log-in/log-in.component.spec.ts index adb7187757..43004b79c7 100644 --- a/src/app/shared/log-in/log-in.component.spec.ts +++ b/src/app/shared/log-in/log-in.component.spec.ts @@ -19,9 +19,9 @@ import {APP_BASE_HREF} from '@angular/common'; import {HostWindowService} from '../host-window.service'; import {HostWindowServiceStub} from '../testing/host-window-service-stub'; import {RouterStub} from '../testing/router-stub'; -import {NavigationEnd, Router} from '@angular/router'; -import {Observable, of as observableOf} from 'rxjs'; -import {RouterEventsStub} from '../testing/router-events-stub'; +import {Router} from '@angular/router'; +import {RouteService} from '../services/route.service'; +import {routeServiceStub} from '../testing/route-service-stub'; describe('LogInComponent', () => { @@ -58,6 +58,7 @@ describe('LogInComponent', () => { {provide: AuthService, useClass: AuthServiceStub}, {provide: APP_BASE_HREF, useValue: '/'}, {provide: Router, useClass: RouterStub}, + {provide: RouteService, useValue: routeServiceStub }, {provide: HostWindowService, useValue: new HostWindowServiceStub(900) } ], schemas: [ @@ -164,11 +165,6 @@ describe('LogInComponent on small screen', () => { let page: Page; let user: EPerson; - const navEvents = observableOf( - new NavigationEnd(0, 'http://localhost:3000/home', 'http://localhost:3000/home'), - new NavigationEnd(1, 'http://localhost:3000/login', 'http://localhost:3000/login') - ); - const authState = { authenticated: false, loaded: false, @@ -196,7 +192,8 @@ describe('LogInComponent on small screen', () => { providers: [ {provide: AuthService, useClass: AuthServiceStub}, {provide: APP_BASE_HREF, useValue: '/'}, - {provide: Router, useValue: new RouterEventsStub(navEvents)}, + {provide: Router, useClass: RouterStub}, + {provide: RouteService, useValue: routeServiceStub }, {provide: HostWindowService, useValue: new HostWindowServiceStub(300) } ], schemas: [ @@ -231,12 +228,13 @@ describe('LogInComponent on small screen', () => { })); it('should set the redirect url on init', () => { - const authService: AuthService = TestBed.get(AuthService); spyOn(authService, 'setRedirectUrl'); - fixture.detectChanges(); - expect(authService.setRedirectUrl).toHaveBeenCalledWith('http://localhost:3000/home'); + // set FormControl values + component.form.controls.email.setValue('user'); + component.form.controls.password.setValue('password'); + expect(authService.setRedirectUrl).toHaveBeenCalledWith('collection/123'); }); diff --git a/src/app/shared/log-in/log-in.component.ts b/src/app/shared/log-in/log-in.component.ts index 0821ff1678..5097b2a0c7 100644 --- a/src/app/shared/log-in/log-in.component.ts +++ b/src/app/shared/log-in/log-in.component.ts @@ -1,9 +1,9 @@ -import {filter, map, pairwise, take, takeWhile, tap} from 'rxjs/operators'; +import {filter, map, pairwise, take, takeUntil, takeWhile, tap} from 'rxjs/operators'; import { Component, OnDestroy, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { select, Store } from '@ngrx/store'; -import { Observable } from 'rxjs'; +import {Observable, Subject} from 'rxjs'; import { AuthenticateAction, ResetAuthenticationMessagesAction @@ -17,11 +17,12 @@ import { } from '../../core/auth/selectors'; import { CoreState } from '../../core/core.reducers'; -import {isEmpty, isNotEmpty} from '../empty.util'; +import {isNotEmpty} from '../empty.util'; import { fadeOut } from '../animations/fade'; import {AuthService, LOGIN_ROUTE} from '../../core/auth/auth.service'; -import {NavigationEnd, Router, RoutesRecognized} from '@angular/router'; +import {Router} from '@angular/router'; import {HostWindowService} from '../host-window.service'; +import {RouteService} from '../services/route.service'; /** * /users/sign-in @@ -89,6 +90,7 @@ export class LogInComponent implements OnDestroy, OnInit { * @constructor * @param {AuthService} authService * @param {FormBuilder} formBuilder + * @param {RouteService} routeService * @param {Router} router * @param {HostWindowService} windowService * @param {Store} store @@ -96,6 +98,7 @@ export class LogInComponent implements OnDestroy, OnInit { constructor( private authService: AuthService, private formBuilder: FormBuilder, + private routeService: RouteService, private router: Router, private windowService: HostWindowService, private store: Store @@ -106,18 +109,19 @@ export class LogInComponent implements OnDestroy, OnInit { * Lifecycle hook that is called after data-bound properties of a directive are initialized. * @method ngOnInit */ - public ngOnInit() { // set isAuthenticated + public ngOnInit() { + // set isAuthenticated this.isAuthenticated = this.store.pipe(select(isAuthenticated)); // for mobile login, set the redirect url to the previous route this.windowService.isXs().pipe(take(1)) .subscribe((isMobile) => { if (isMobile) { - this.router.events.pipe( - filter((e: any) => e instanceof NavigationEnd), - pairwise() - ).subscribe((e: any) => { - this.setRedirectUrl(e[0].urlAfterRedirects); + this.routeService.getHistory().pipe( + take(1) + ).subscribe((history) => { + const previousIndex = history.length - 2; + this.setRedirectUrl(history[previousIndex]); }); } }); @@ -211,7 +215,7 @@ export class LogInComponent implements OnDestroy, OnInit { } /** - * Sets the redirect url if not LOGIN_ROUTE + * Sets the redirect url if not equal to LOGIN_ROUTE * @param url */ private setRedirectUrl(url: string) { diff --git a/src/app/shared/testing/route-service-stub.ts b/src/app/shared/testing/route-service-stub.ts index e3cdd9d8c6..22e039a815 100644 --- a/src/app/shared/testing/route-service-stub.ts +++ b/src/app/shared/testing/route-service-stub.ts @@ -27,6 +27,9 @@ export const routeServiceStub: any = { }, getRouteDataValue: (param) => { return observableOf({}) + }, + getHistory: () => { + return observableOf(['/home','collection/123','/login']) } /* tslint:enable:no-empty */ }; diff --git a/src/app/shared/testing/router-events-stub.ts b/src/app/shared/testing/router-events-stub.ts deleted file mode 100644 index 313f6a2b4e..0000000000 --- a/src/app/shared/testing/router-events-stub.ts +++ /dev/null @@ -1,24 +0,0 @@ -import {Observable, of as observableOf} from 'rxjs'; -export class RouterEventsStub { - url: string; - routeReuseStrategy = {shouldReuseRoute: {}}; - //noinspection TypeScriptUnresolvedFunction - navigate = jasmine.createSpy('navigate'); - parseUrl = jasmine.createSpy('parseUrl'); - events = new Observable((observer) => { - this.eventArr.forEach((e) => { - observer.next(e); - }); - observer.complete(); - }); - eventArr: any; - - // Stub constructor takes array of event objects. - constructor( events: any = observableOf({})) { - this.eventArr = events; - } - - navigateByUrl(url): void { - this.url = url; - } -}