diff --git a/src/app/shared/cookies/browser-klaro.service.spec.ts b/src/app/shared/cookies/browser-klaro.service.spec.ts index 953734d38c5..6df703d4f69 100644 --- a/src/app/shared/cookies/browser-klaro.service.spec.ts +++ b/src/app/shared/cookies/browser-klaro.service.spec.ts @@ -1,4 +1,4 @@ -import { TestBed } from '@angular/core/testing'; +import { TestBed, fakeAsync, tick } from '@angular/core/testing'; import { TranslateService } from '@ngx-translate/core'; import { getTestScheduler } from 'jasmine-marbles'; import clone from 'lodash/clone'; @@ -26,6 +26,7 @@ import { import { ANONYMOUS_STORAGE_NAME_KLARO } from './klaro-configuration'; describe('BrowserKlaroService', () => { + let originalLocation; const trackingIdProp = 'google.analytics.key'; const trackingIdTestValue = 'mock-tracking-id'; const googleAnalytics = 'google-analytics'; @@ -128,6 +129,11 @@ describe('BrowserKlaroService', () => { service.klaroConfig = mockConfig; }); + afterEach(() => { + // Restore the original location after each test + window.location = originalLocation; + }); + it('should be created', () => { expect(service).toBeTruthy(); }); @@ -278,13 +284,25 @@ describe('BrowserKlaroService', () => { spyOn(updatedUser, 'setMetadata'); spyOn(JSON, 'stringify').and.returnValue(cookieConsentString); + spyOn(window.location, 'reload').and.stub(); // Prevents actual reload ePersonService.createPatchFromCache.and.returnValue(observableOf([operation])); - }); - it('should call patch on the data service', () => { - service.setSettingsForUser(updatedUser, cookieConsent); - expect(updatedUser.setMetadata).toHaveBeenCalledWith(COOKIE_MDFIELD, undefined, cookieConsentString); - expect(ePersonService.patch).toHaveBeenCalledWith(updatedUser, [operation]); - }); + ePersonService.patch.and.returnValue(observableOf(undefined)); // Ensure this is mocked if it returns an Observable. + }); + it('should call patch on the data service and reload the page', fakeAsync(() => { + // Call your method + service.updateSettingsForUsers(user); + + // Simulate time passage for async operations + tick(); + + // Check if ePersonService.patch was called as expected + expect(ePersonService.patch).toHaveBeenCalledWith( + jasmine.any(EPerson), + jasmine.any(Array) // Adjust based on expected operations + ); + // Now check if window.location.reload was called + expect(window.location.reload).toHaveBeenCalled(); + })); }); describe('setSettingsForUser when there are no changes', () => { diff --git a/src/app/shared/cookies/browser-klaro.service.ts b/src/app/shared/cookies/browser-klaro.service.ts index f673a417363..4bb5e95b1b4 100644 --- a/src/app/shared/cookies/browser-klaro.service.ts +++ b/src/app/shared/cookies/browser-klaro.service.ts @@ -308,13 +308,13 @@ export class BrowserKlaroService extends KlaroService { * @param user The user to save the settings for * @param config The consent settings for the user to save */ - setSettingsForUser(user: EPerson, config: object) { + setSettingsForUser(user: EPerson, config: object): Observable { if (isNotEmpty(config)) { user.setMetadata(COOKIE_MDFIELD, undefined, JSON.stringify(config)); } else { user.removeMetadata(COOKIE_MDFIELD); } - this.ePersonService.createPatchFromCache(user) + return this.ePersonService.createPatchFromCache(user) .pipe( take(1), switchMap((operations: Operation[]) => { @@ -322,9 +322,8 @@ export class BrowserKlaroService extends KlaroService { return this.ePersonService.patch(user, operations); } return observableOf(undefined); - }, - ), - ).subscribe(); + }) + ); } /** @@ -340,7 +339,16 @@ export class BrowserKlaroService extends KlaroService { * @param user */ updateSettingsForUsers(user: EPerson) { - this.setSettingsForUser(user, this.cookieService.get(this.getStorageName(user.uuid))); + this.setSettingsForUser(user, this.cookieService.get(this.getStorageName(user.uuid))) + .subscribe({ + next: (response) => { + // Settings saved successfully, now reload the page + window.location.reload(); + }, + error: (err) => { + console.error('Error updating user settings', err); + }, + }); } /**