mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +00:00
Merge branch 'add-referrer-to-pageview-event-7.0' into add-referrer-to-pageview-event-7.2
This commit is contained in:
@@ -10,7 +10,7 @@ describe(`BrowserReferrerService`, () => {
|
|||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
routeService = {
|
routeService = {
|
||||||
getPreviousUrl: () => observableOf('')
|
getHistory: () => observableOf([])
|
||||||
} as any;
|
} as any;
|
||||||
service = new BrowserReferrerService(
|
service = new BrowserReferrerService(
|
||||||
{ referrer: documentReferrer },
|
{ referrer: documentReferrer },
|
||||||
@@ -20,12 +20,9 @@ describe(`BrowserReferrerService`, () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe(`getReferrer`, () => {
|
describe(`getReferrer`, () => {
|
||||||
let prevUrl: string;
|
describe(`when the history is an empty`, () => {
|
||||||
|
|
||||||
describe(`when getPreviousUrl is an empty string`, () => {
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
prevUrl = '';
|
spyOn(routeService, 'getHistory').and.returnValue(observableOf([]));
|
||||||
spyOn(routeService, 'getPreviousUrl').and.returnValue(observableOf(prevUrl));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`should return document.referrer`, (done: DoneFn) => {
|
it(`should return document.referrer`, (done: DoneFn) => {
|
||||||
@@ -36,13 +33,31 @@ describe(`BrowserReferrerService`, () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe(`when getPreviousUrl is not empty`, () => {
|
describe(`when the history only contains the current route`, () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
prevUrl = '/some/local/route';
|
spyOn(routeService, 'getHistory').and.returnValue(observableOf(['/current/route']));
|
||||||
spyOn(routeService, 'getPreviousUrl').and.returnValue(observableOf(prevUrl));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`should return the value emitted by getPreviousUrl combined with the origin from HardRedirectService`, (done: DoneFn) => {
|
it(`should return document.referrer`, (done: DoneFn) => {
|
||||||
|
service.getReferrer().subscribe((emittedReferrer: string) => {
|
||||||
|
expect(emittedReferrer).toBe(documentReferrer);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe(`when the history contains multiple routes`, () => {
|
||||||
|
const prevUrl = '/the/route/we/need';
|
||||||
|
beforeEach(() => {
|
||||||
|
spyOn(routeService, 'getHistory').and.returnValue(observableOf([
|
||||||
|
'/first/route',
|
||||||
|
'/second/route',
|
||||||
|
prevUrl,
|
||||||
|
'/current/route'
|
||||||
|
]));
|
||||||
|
});
|
||||||
|
|
||||||
|
it(`should return the last route before the current one combined with the origin from HardRedirectService`, (done: DoneFn) => {
|
||||||
service.getReferrer().subscribe((emittedReferrer: string) => {
|
service.getReferrer().subscribe((emittedReferrer: string) => {
|
||||||
expect(emittedReferrer).toBe(origin + prevUrl);
|
expect(emittedReferrer).toBe(origin + prevUrl);
|
||||||
done();
|
done();
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
import { ReferrerService } from './referrer.service';
|
import { ReferrerService } from './referrer.service';
|
||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
import { map } from 'rxjs/operators';
|
import { map } from 'rxjs/operators';
|
||||||
import { isEmpty } from '../../shared/empty.util';
|
import { isEmpty, hasNoValue } from '../../shared/empty.util';
|
||||||
import { URLCombiner } from '../url-combiner/url-combiner';
|
import { URLCombiner } from '../url-combiner/url-combiner';
|
||||||
import { Inject, Injectable } from '@angular/core';
|
import { Inject, Injectable } from '@angular/core';
|
||||||
import { DOCUMENT } from '@angular/common';
|
import { DOCUMENT } from '@angular/common';
|
||||||
@@ -33,13 +33,19 @@ export class BrowserReferrerService extends ReferrerService {
|
|||||||
* in the store yet, document.referrer will be used
|
* in the store yet, document.referrer will be used
|
||||||
*/
|
*/
|
||||||
public getReferrer(): Observable<string> {
|
public getReferrer(): Observable<string> {
|
||||||
return this.routeService.getPreviousUrl().pipe(
|
return this.routeService.getHistory().pipe(
|
||||||
map((prevUrl: string) => {
|
map((history: string[]) => {
|
||||||
// if we don't have anything in the history yet, return document.referrer
|
const currentURL = history[history.length - 1];
|
||||||
// (note that that may be empty too, e.g. if you've just opened a new browser tab)
|
// if the current URL isn't set yet, or the only URL in the history is the current one,
|
||||||
if (isEmpty(prevUrl)) {
|
// return document.referrer (note that that may be empty too, e.g. if you've just opened a
|
||||||
|
// new browser tab)
|
||||||
|
if (hasNoValue(currentURL) || history.every((url: string) => url === currentURL)) {
|
||||||
return this.document.referrer;
|
return this.document.referrer;
|
||||||
} else {
|
} else {
|
||||||
|
// reverse the history
|
||||||
|
const reversedHistory = [...history].reverse();
|
||||||
|
// and find the first URL that differs from the current one
|
||||||
|
const prevUrl = reversedHistory.find((url: string) => url !== currentURL);
|
||||||
return new URLCombiner(this.hardRedirectService.getCurrentOrigin(), prevUrl).toString();
|
return new URLCombiner(this.hardRedirectService.getCurrentOrigin(), prevUrl).toString();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user