mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-18 15:33:04 +00:00
[#1816][CST-6565] Refactored methods & Tests
Test: - New tests for the use case of filtering analytics configuration; - Enhanced initialization of service with new google analytics service mock.
This commit is contained in:
@@ -12,12 +12,13 @@ import { getTestScheduler } from 'jasmine-marbles';
|
|||||||
import { MetadataValue } from '../../core/shared/metadata.models';
|
import { MetadataValue } from '../../core/shared/metadata.models';
|
||||||
import { cloneDeep } from 'lodash';
|
import { cloneDeep } from 'lodash';
|
||||||
import { ConfigurationDataService } from '../../core/data/configuration-data.service';
|
import { ConfigurationDataService } from '../../core/data/configuration-data.service';
|
||||||
import { createSuccessfulRemoteDataObject$ } from '../remote-data.utils';
|
import {createFailedRemoteDataObject$, createSuccessfulRemoteDataObject$} from '../remote-data.utils';
|
||||||
import { ConfigurationProperty } from '../../core/shared/configuration-property.model';
|
import { ConfigurationProperty } from '../../core/shared/configuration-property.model';
|
||||||
|
|
||||||
describe('BrowserKlaroService', () => {
|
describe('BrowserKlaroService', () => {
|
||||||
const trackingIdProp = 'google.analytics.key';
|
const trackingIdProp = 'google.analytics.key';
|
||||||
const trackingIdTestValue = 'mock-tracking-id';
|
const trackingIdTestValue = 'mock-tracking-id';
|
||||||
|
const googleAnalytics = 'google-analytics';
|
||||||
let translateService;
|
let translateService;
|
||||||
let ePersonService;
|
let ePersonService;
|
||||||
let authService;
|
let authService;
|
||||||
@@ -33,11 +34,15 @@ describe('BrowserKlaroService', () => {
|
|||||||
values: values,
|
values: values,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
const googleAnalyticsFilter =
|
||||||
|
(bService: BrowserKlaroService) =>
|
||||||
|
bService.klaroConfig.services.filter(({name}) => name === googleAnalytics);
|
||||||
|
|
||||||
let mockConfig;
|
let mockConfig;
|
||||||
let appName;
|
let appName;
|
||||||
let purpose;
|
let purpose;
|
||||||
let testKey;
|
let testKey;
|
||||||
|
let findByPropertyName;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
user = new EPerson();
|
user = new EPerson();
|
||||||
@@ -52,6 +57,7 @@ describe('BrowserKlaroService', () => {
|
|||||||
getAuthenticatedUserFromStore: observableOf(user)
|
getAuthenticatedUserFromStore: observableOf(user)
|
||||||
});
|
});
|
||||||
configurationDataService = createConfigSuccessSpy(trackingIdTestValue);
|
configurationDataService = createConfigSuccessSpy(trackingIdTestValue);
|
||||||
|
findByPropertyName = cloneDeep(configurationDataService.findByPropertyName);
|
||||||
cookieService = jasmine.createSpyObj('cookieService', {
|
cookieService = jasmine.createSpyObj('cookieService', {
|
||||||
get: '{%22token_item%22:true%2C%22impersonation%22:true%2C%22redirect%22:true%2C%22language%22:true%2C%22klaro%22:true%2C%22has_agreed_end_user%22:true%2C%22google-analytics%22:true}',
|
get: '{%22token_item%22:true%2C%22impersonation%22:true%2C%22redirect%22:true%2C%22language%22:true%2C%22klaro%22:true%2C%22has_agreed_end_user%22:true%2C%22google-analytics%22:true}',
|
||||||
set: () => {
|
set: () => {
|
||||||
@@ -101,6 +107,9 @@ describe('BrowserKlaroService', () => {
|
|||||||
services: [{
|
services: [{
|
||||||
name: appName,
|
name: appName,
|
||||||
purposes: [purpose]
|
purposes: [purpose]
|
||||||
|
},{
|
||||||
|
name: googleAnalytics,
|
||||||
|
purposes: [purpose]
|
||||||
}],
|
}],
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -251,4 +260,46 @@ describe('BrowserKlaroService', () => {
|
|||||||
expect(ePersonService.patch).not.toHaveBeenCalled();
|
expect(ePersonService.patch).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('initialize google analytics configuration', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
configurationDataService.findByPropertyName = findByPropertyName;
|
||||||
|
spyOn((service as any), 'getUser$').and.returnValue(observableOf(user));
|
||||||
|
translateService.get.and.returnValue(observableOf('loading...'));
|
||||||
|
spyOn(service, 'addAppMessages');
|
||||||
|
spyOn((service as any), 'initializeUser');
|
||||||
|
spyOn(service, 'translateConfiguration');
|
||||||
|
});
|
||||||
|
it('should have been initialized with googleAnalytics', () => {
|
||||||
|
const filteredServices = googleAnalyticsFilter(service);
|
||||||
|
expect(filteredServices.length).toBe(1);
|
||||||
|
});
|
||||||
|
it('should filter empty configuration', () => {
|
||||||
|
configurationDataService.findByPropertyName = jasmine.createSpy().and.returnValue(
|
||||||
|
createSuccessfulRemoteDataObject$({
|
||||||
|
... new ConfigurationProperty(),
|
||||||
|
name: googleAnalytics,
|
||||||
|
values: [],
|
||||||
|
}));
|
||||||
|
service.initialize();
|
||||||
|
const filteredServices = googleAnalyticsFilter(service);
|
||||||
|
expect(filteredServices.length).toBe(0);
|
||||||
|
});
|
||||||
|
it('should filter when error', () => {
|
||||||
|
configurationDataService.findByPropertyName = jasmine.createSpy().and.returnValue(
|
||||||
|
createFailedRemoteDataObject$('Erro while loading GA')
|
||||||
|
);
|
||||||
|
service.initialize();
|
||||||
|
const filteredServices = googleAnalyticsFilter(service);
|
||||||
|
expect(filteredServices.length).toBe(0);
|
||||||
|
});
|
||||||
|
it('should filter when invalid payload', () => {
|
||||||
|
configurationDataService.findByPropertyName = jasmine.createSpy().and.returnValue(
|
||||||
|
createSuccessfulRemoteDataObject$(null)
|
||||||
|
);
|
||||||
|
service.initialize();
|
||||||
|
const filteredServices = googleAnalyticsFilter(service);
|
||||||
|
expect(filteredServices.length).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@@ -13,10 +13,8 @@ import { EPersonDataService } from '../../core/eperson/eperson-data.service';
|
|||||||
import { cloneDeep, debounce } from 'lodash';
|
import { cloneDeep, debounce } from 'lodash';
|
||||||
import { ANONYMOUS_STORAGE_NAME_KLARO, klaroConfiguration } from './klaro-configuration';
|
import { ANONYMOUS_STORAGE_NAME_KLARO, klaroConfiguration } from './klaro-configuration';
|
||||||
import { Operation } from 'fast-json-patch';
|
import { Operation } from 'fast-json-patch';
|
||||||
import { getFirstCompletedRemoteData } from '../../core/shared/operators';
|
import { getFirstCompletedRemoteData} from '../../core/shared/operators';
|
||||||
import { ConfigurationDataService } from '../../core/data/configuration-data.service';
|
import { ConfigurationDataService } from '../../core/data/configuration-data.service';
|
||||||
import { RemoteData } from '../../core/data/remote-data';
|
|
||||||
import { ConfigurationProperty } from '../../core/shared/configuration-property.model';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Metadata field to store a user's cookie consent preferences in
|
* Metadata field to store a user's cookie consent preferences in
|
||||||
@@ -80,12 +78,15 @@ export class BrowserKlaroService extends KlaroService {
|
|||||||
this.klaroConfig.translations.en.consentNotice.description = 'cookies.consent.content-notice.description.no-privacy';
|
this.klaroConfig.translations.en.consentNotice.description = 'cookies.consent.content-notice.description.no-privacy';
|
||||||
}
|
}
|
||||||
|
|
||||||
const configurationToHide$: Observable<Pick<typeof klaroConfiguration, 'name'>[]> =
|
const servicesToHide$: Observable<string[]> = this.configService.findByPropertyName(this.GOOGLE_ANALYTICS_KEY).pipe(
|
||||||
this.configService.findByPropertyName(this.GOOGLE_ANALYTICS_KEY)
|
|
||||||
.pipe(
|
|
||||||
getFirstCompletedRemoteData(),
|
getFirstCompletedRemoteData(),
|
||||||
map(remoteData => this.mapInvalidConfiguration(remoteData, this.GOOGLE_ANALYTICS_SERVICE_NAME)),
|
map(remoteData => {
|
||||||
take(1)
|
if (!remoteData.hasSucceeded || !remoteData.payload || isEmpty(remoteData.payload.values)) {
|
||||||
|
return [this.GOOGLE_ANALYTICS_SERVICE_NAME];
|
||||||
|
} else {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
this.translateService.setDefaultLang(environment.defaultLanguage);
|
this.translateService.setDefaultLang(environment.defaultLanguage);
|
||||||
@@ -94,8 +95,8 @@ export class BrowserKlaroService extends KlaroService {
|
|||||||
|
|
||||||
const translationServiceReady$ = this.translateService.get('loading.default').pipe(take(1));
|
const translationServiceReady$ = this.translateService.get('loading.default').pipe(take(1));
|
||||||
|
|
||||||
observableCombineLatest([user$, translationServiceReady$, configurationToHide$])
|
observableCombineLatest([user$, servicesToHide$, translationServiceReady$])
|
||||||
.subscribe(([user, translation, servicesToHide]: [EPerson, string, Pick<typeof klaroConfiguration, 'name'>[]]) => {
|
.subscribe(([user, servicesToHide, _]: [EPerson, string[], string]) => {
|
||||||
user = cloneDeep(user);
|
user = cloneDeep(user);
|
||||||
|
|
||||||
if (hasValue(user)) {
|
if (hasValue(user)) {
|
||||||
@@ -120,21 +121,6 @@ export class BrowserKlaroService extends KlaroService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private mapInvalidConfiguration(
|
|
||||||
remoteData: RemoteData<ConfigurationProperty>,
|
|
||||||
configurationName: string
|
|
||||||
): Pick<typeof klaroConfiguration, 'name'>[] {
|
|
||||||
if (this.isEmptyOrInvalid(remoteData)) {
|
|
||||||
return [{name: configurationName}];
|
|
||||||
} else {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private isEmptyOrInvalid(remoteData: RemoteData<ConfigurationProperty>): boolean {
|
|
||||||
return !remoteData.hasSucceeded || !remoteData.payload || isEmpty(remoteData.payload.values);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize configuration for the logged in user
|
* Initialize configuration for the logged in user
|
||||||
* @param user The authenticated user
|
* @param user The authenticated user
|
||||||
@@ -302,7 +288,7 @@ export class BrowserKlaroService extends KlaroService {
|
|||||||
/**
|
/**
|
||||||
* remove the google analytics from the services
|
* remove the google analytics from the services
|
||||||
*/
|
*/
|
||||||
private filterConfigServices(servicesToHide: Pick<typeof klaroConfiguration, 'name'>[]): Pick<typeof klaroConfiguration, 'services'>[] {
|
private filterConfigServices(servicesToHide: string[]): Pick<typeof klaroConfiguration, 'services'>[] {
|
||||||
return this.klaroConfig.services.filter(service => !servicesToHide.some(el => el.name === service.name));
|
return this.klaroConfig.services.filter(service => !servicesToHide.some(name => name === service.name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user