Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-Reload Page on User Consent Change #2885

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/app/shared/cookies/browser-klaro.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TestBed } from '@angular/core/testing';
import { TestBed, fakeAsync, tick } from '@angular/core/testing';

Check failure on line 1 in src/app/shared/cookies/browser-klaro.service.spec.ts

View workflow job for this annotation

GitHub Actions / tests (18.x)

Imports must be broken into multiple lines if there are more than 1 elements

Check failure on line 1 in src/app/shared/cookies/browser-klaro.service.spec.ts

View workflow job for this annotation

GitHub Actions / tests (18.x)

Run autofix to sort these imports!

Check failure on line 1 in src/app/shared/cookies/browser-klaro.service.spec.ts

View workflow job for this annotation

GitHub Actions / tests (20.x)

Imports must be broken into multiple lines if there are more than 1 elements

Check failure on line 1 in src/app/shared/cookies/browser-klaro.service.spec.ts

View workflow job for this annotation

GitHub Actions / tests (20.x)

Run autofix to sort these imports!
import { TranslateService } from '@ngx-translate/core';
import { getTestScheduler } from 'jasmine-marbles';
import clone from 'lodash/clone';
Expand Down Expand Up @@ -26,6 +26,7 @@
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';
Expand Down Expand Up @@ -128,6 +129,11 @@
service.klaroConfig = mockConfig;
});

afterEach(() => {
// Restore the original location after each test
window.location = originalLocation;
});

it('should be created', () => {
expect(service).toBeTruthy();
});
Expand Down Expand Up @@ -278,13 +284,25 @@

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);

Check failure on line 294 in src/app/shared/cookies/browser-klaro.service.spec.ts

View workflow job for this annotation

GitHub Actions / tests (18.x)

Trailing spaces not allowed

Check failure on line 294 in src/app/shared/cookies/browser-klaro.service.spec.ts

View workflow job for this annotation

GitHub Actions / tests (20.x)

Trailing spaces not allowed
// Simulate time passage for async operations
tick();

Check failure on line 297 in src/app/shared/cookies/browser-klaro.service.spec.ts

View workflow job for this annotation

GitHub Actions / tests (18.x)

Trailing spaces not allowed

Check failure on line 297 in src/app/shared/cookies/browser-klaro.service.spec.ts

View workflow job for this annotation

GitHub Actions / tests (20.x)

Trailing spaces not allowed
// Check if ePersonService.patch was called as expected
expect(ePersonService.patch).toHaveBeenCalledWith(
jasmine.any(EPerson),
jasmine.any(Array) // Adjust based on expected operations

Check failure on line 301 in src/app/shared/cookies/browser-klaro.service.spec.ts

View workflow job for this annotation

GitHub Actions / tests (18.x)

Missing trailing comma

Check failure on line 301 in src/app/shared/cookies/browser-klaro.service.spec.ts

View workflow job for this annotation

GitHub Actions / tests (20.x)

Missing trailing comma
);
// Now check if window.location.reload was called
expect(window.location.reload).toHaveBeenCalled();
}));
});

describe('setSettingsForUser when there are no changes', () => {
Expand Down
20 changes: 14 additions & 6 deletions src/app/shared/cookies/browser-klaro.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,23 +308,22 @@
* @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<any> {
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[]) => {
if (isNotEmpty(operations)) {
return this.ePersonService.patch(user, operations);
}
return observableOf(undefined);
},
),
).subscribe();
})

Check failure on line 325 in src/app/shared/cookies/browser-klaro.service.ts

View workflow job for this annotation

GitHub Actions / tests (18.x)

Missing trailing comma

Check failure on line 325 in src/app/shared/cookies/browser-klaro.service.ts

View workflow job for this annotation

GitHub Actions / tests (20.x)

Missing trailing comma
);
}

/**
Expand All @@ -340,7 +339,16 @@
* @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) => {

Check failure on line 348 in src/app/shared/cookies/browser-klaro.service.ts

View workflow job for this annotation

GitHub Actions / tests (18.x)

Implicit `any` in `catchError`

Check failure on line 348 in src/app/shared/cookies/browser-klaro.service.ts

View workflow job for this annotation

GitHub Actions / tests (20.x)

Implicit `any` in `catchError`
console.error('Error updating user settings', err);
},
});
}

/**
Expand Down
Loading