Skip to content

Commit 743ca1d

Browse files
authored
fix(tos banner): not show tos banner if there is no authentificated user (#464)
* fix(tos banner): if user is authenticated we check whether is accepted terms of service to hide banner or show if not otherwise user is not authenticated we hide banner always * fix(tos banner): resolve CR comments * fix(tos banner): resolve CR comments
1 parent ed63429 commit 743ca1d

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

src/app/core/components/osf-banners/tos-consent-banner/tos-consent-banner.component.spec.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { Store } from '@ngxs/store';
22

33
import { MockComponent } from 'ng-mocks';
44

5+
import { of } from 'rxjs';
6+
57
import { ComponentFixture, TestBed } from '@angular/core/testing';
68
import { By } from '@angular/platform-browser';
79

@@ -16,13 +18,15 @@ import { TranslationServiceMock } from '@testing/mocks/translation.service.mock'
1618
import { OSFTestingStoreModule } from '@testing/osf.testing.module';
1719
import { provideMockStore } from '@testing/providers/store-provider.mock';
1820

19-
describe('Component: Tos Consent Banner', () => {
21+
22+
describe('TosConsentBannerComponent', () => {
23+
let component: TosConsentBannerComponent;
2024
let fixture: ComponentFixture<TosConsentBannerComponent>;
21-
let store: Store;
25+
let store: jest.Mocked<Store>;
2226

2327
beforeEach(async () => {
2428
await TestBed.configureTestingModule({
25-
imports: [OSFTestingStoreModule, TosConsentBannerComponent, MockComponent(IconComponent)],
29+
imports: [TosConsentBannerComponent, OSFTestingStoreModule, MockComponent(IconComponent)],
2630
providers: [
2731
provideMockStore({
2832
signals: [{ selector: UserSelectors.getCurrentUser, value: MOCK_USER }],
@@ -32,7 +36,9 @@ describe('Component: Tos Consent Banner', () => {
3236
}).compileComponents();
3337

3438
fixture = TestBed.createComponent(TosConsentBannerComponent);
35-
store = TestBed.inject(Store);
39+
store = TestBed.inject(Store) as jest.Mocked<Store>;
40+
component = fixture.componentInstance;
41+
store.dispatch = jest.fn().mockReturnValue(of(undefined));
3642
fixture.detectChanges();
3743
});
3844

@@ -50,7 +56,6 @@ describe('Component: Tos Consent Banner', () => {
5056
});
5157

5258
it('should dispatch AcceptTermsOfServiceByUser action when "Continue" is clicked', () => {
53-
jest.spyOn(store, 'dispatch');
5459
const checkboxInput = fixture.debugElement.query(By.css('p-checkbox input')).nativeElement;
5560
checkboxInput.click();
5661
fixture.detectChanges();
@@ -61,4 +66,25 @@ describe('Component: Tos Consent Banner', () => {
6166

6267
expect(store.dispatch).toHaveBeenCalledWith(new AcceptTermsOfServiceByUser());
6368
});
69+
70+
it('should return true for "acceptedTermsOfServiceChange" when user is null to not show banner', async () => {
71+
await TestBed.resetTestingModule()
72+
.configureTestingModule({
73+
imports: [TosConsentBannerComponent, OSFTestingStoreModule, MockComponent(IconComponent)],
74+
providers: [
75+
provideMockStore({
76+
signals: [{ selector: UserSelectors.getCurrentUser, value: null }],
77+
}),
78+
TranslationServiceMock,
79+
],
80+
})
81+
.compileComponents();
82+
83+
const fixture = TestBed.createComponent(TosConsentBannerComponent);
84+
const component = fixture.componentInstance;
85+
86+
fixture.detectChanges();
87+
expect(component.acceptedTermsOfServiceChange()).toBe(true);
88+
});
89+
6490
});

src/app/core/components/osf-banners/tos-consent-banner/tos-consent-banner.component.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Button } from 'primeng/button';
66
import { Checkbox } from 'primeng/checkbox';
77
import { Message } from 'primeng/message';
88

9-
import { Component, computed, signal } from '@angular/core';
9+
import { Component, computed, inject, signal } from '@angular/core';
1010
import { FormsModule } from '@angular/forms';
1111
import { RouterLink } from '@angular/router';
1212

@@ -52,10 +52,16 @@ export class TosConsentBannerComponent {
5252
/**
5353
* Computed signal indicating whether the user has already accepted the Terms of Service.
5454
*/
55-
readonly acceptedTermsOfServiceChange = computed(() => {
56-
const user = this.currentUser();
57-
return user?.acceptedTermsOfService ?? false;
58-
});
55+
acceptedTermsOfServiceChange = computed(() =>
56+
{
57+
const user = this.currentUser()
58+
/**
59+
* if user is authenticated we check whether is accepted terms of service to hide banner or show if not
60+
* otherwise user is not authenticated we hide banner always
61+
*/
62+
return user ? user.acceptedTermsOfService : true;
63+
}
64+
);
5965

6066
/**
6167
* Triggered when the user clicks the Continue button.

src/assets/i18n/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@
222222
"and": " and ",
223223
"privacyPolicy": " privacy policy.",
224224
"haveReadAndAgree": "I've read and agree to the terms and conditions",
225-
"continue": "Continue"
225+
"continue": "Continue",
226+
"errorMessage": "We were unable to save your consent."
226227
},
227228
"cookie-consent": {
228229
"message": "Notice: This website relies on cookies to help provide a better user experience. By clicking accept or continuing to use the site, you consent to our use of cookies. See our Privacy Policy and Cookie Use for more information.",

0 commit comments

Comments
 (0)