From 46fc0d93d68356049db3357fa8cdffb850a282b3 Mon Sep 17 00:00:00 2001 From: mkovalua Date: Tue, 16 Sep 2025 19:27:18 +0300 Subject: [PATCH 1/3] fix(tos-banner): tos banner unit test implementation --- .../tos-consent-banner.component.spec.ts | 59 +++++++++++++++++-- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/src/app/features/home/components/tos-consent-banner/tos-consent-banner.component.spec.ts b/src/app/features/home/components/tos-consent-banner/tos-consent-banner.component.spec.ts index e6652e1c0..36af2201f 100644 --- a/src/app/features/home/components/tos-consent-banner/tos-consent-banner.component.spec.ts +++ b/src/app/features/home/components/tos-consent-banner/tos-consent-banner.component.spec.ts @@ -1,22 +1,73 @@ +import { Store } from '@ngxs/store'; + +import { MockComponent } from 'ng-mocks'; + +import { of } from 'rxjs'; + import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { By } from '@angular/platform-browser'; + +import { AcceptTermsOfServiceByUser } from '@core/store/user'; +import { UserSelectors } from '@osf/core/store/user'; +import { MOCK_USER } from '@osf/shared/mocks'; +import { IconComponent } from '@shared/components'; import { TosConsentBannerComponent } from './tos-consent-banner.component'; -describe.skip('TosConsentBannerComponent', () => { +import { TranslationServiceMock } from '@testing/mocks/translation.service.mock'; +import { OSFTestingModule, OSFTestingStoreModule } from '@testing/osf.testing.module'; +import { provideMockStore } from '@testing/providers/store-provider.mock'; +import { ToastServiceMockBuilder } from '@testing/providers/toast-provider.mock'; + +describe('TosConsentBannerComponent', () => { let component: TosConsentBannerComponent; let fixture: ComponentFixture; + let store: jest.Mocked; + let toastServiceMock: ReturnType; beforeEach(async () => { await TestBed.configureTestingModule({ - imports: [TosConsentBannerComponent], + imports: [TosConsentBannerComponent, OSFTestingStoreModule, OSFTestingModule, MockComponent(IconComponent)], + providers: [ + provideMockStore({ + signals: [{ selector: UserSelectors.getCurrentUser, value: MOCK_USER }], + }), + TranslationServiceMock, + ], }).compileComponents(); fixture = TestBed.createComponent(TosConsentBannerComponent); + store = TestBed.inject(Store) as jest.Mocked; component = fixture.componentInstance; + store.dispatch = jest.fn().mockReturnValue(of(undefined)); + toastServiceMock = ToastServiceMockBuilder.create().build(); + fixture.detectChanges(); + }); + + it('should have the "Continue" button disabled by default', () => { + const continueButton = fixture.debugElement.query(By.css('p-button button')).nativeElement; + console.log('continueButton ' + continueButton ) + expect(continueButton.disabled).toBe(true); + }); + + it('should enable the "Continue" button when the checkbox is checked', () => { + const checkboxInput = fixture.debugElement.query(By.css('p-checkbox input')).nativeElement; + checkboxInput.click(); fixture.detectChanges(); + const continueButton = fixture.debugElement.query(By.css('p-button button')).nativeElement; + expect(continueButton.disabled).toBe(false); }); - it('should create', () => { - expect(component).toBeTruthy(); + it('should dispatch AcceptTermsOfServiceByUser action when "Continue" is clicked', () => { + const checkboxInput = fixture.debugElement.query(By.css('p-checkbox input')).nativeElement; + checkboxInput.click(); + fixture.detectChanges(); + + const continueButton = fixture.debugElement.query(By.css('p-button button')).nativeElement; + continueButton.click(); + fixture.detectChanges(); + + expect(store.dispatch).toHaveBeenCalledWith(new AcceptTermsOfServiceByUser()); + expect(toastServiceMock.showError).not.toHaveBeenCalled(); }); }); From b347d5a83411976602ecf7401e2cea03860a06de Mon Sep 17 00:00:00 2001 From: mkovalua Date: Wed, 17 Sep 2025 15:50:30 +0300 Subject: [PATCH 2/3] fix(tos banner): unit test implementation, set mock user value acceptedTermsOfService: false --- .../tos-consent-banner.component.spec.ts | 10 ++++++++++ src/app/shared/mocks/data.mock.ts | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/app/features/home/components/tos-consent-banner/tos-consent-banner.component.spec.ts b/src/app/features/home/components/tos-consent-banner/tos-consent-banner.component.spec.ts index 36af2201f..91e6bd4fc 100644 --- a/src/app/features/home/components/tos-consent-banner/tos-consent-banner.component.spec.ts +++ b/src/app/features/home/components/tos-consent-banner/tos-consent-banner.component.spec.ts @@ -70,4 +70,14 @@ describe('TosConsentBannerComponent', () => { expect(store.dispatch).toHaveBeenCalledWith(new AcceptTermsOfServiceByUser()); expect(toastServiceMock.showError).not.toHaveBeenCalled(); }); + + it('should show toast banner if acceptedTermsOfService is false and "Continue" is clicked', () => { + component.acceptedTermsOfService.set(false); + const continueButton = fixture.debugElement.query(By.css('p-button button')).nativeElement; + continueButton.disabled = false; + continueButton.click(); + fixture.detectChanges(); + expect(component.errorMessage).toEqual('toast.tos-consent.error-message'); + }); + }); diff --git a/src/app/shared/mocks/data.mock.ts b/src/app/shared/mocks/data.mock.ts index 60584caa3..4450e0d60 100644 --- a/src/app/shared/mocks/data.mock.ts +++ b/src/app/shared/mocks/data.mock.ts @@ -11,7 +11,7 @@ export const MOCK_USER: User = { middleNames: '', suffix: '', dateRegistered: new Date('2024-01-01'), - acceptedTermsOfService: true, + acceptedTermsOfService: false, employment: [ { title: 'Software Engineer', From d9bae97094d6ff83ac0414c3f56cef6865d9da68 Mon Sep 17 00:00:00 2001 From: mkovalua Date: Thu, 18 Sep 2025 15:50:20 +0300 Subject: [PATCH 3/3] fix(tos banner): update currentUser into localstorage on 'acceptedTermsOfService' set to true --- src/app/core/store/user/user.state.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/app/core/store/user/user.state.ts b/src/app/core/store/user/user.state.ts index 8404141ec..7b10c9bd6 100644 --- a/src/app/core/store/user/user.state.ts +++ b/src/app/core/store/user/user.state.ts @@ -259,6 +259,7 @@ export class UserState { }, }, }); + localStorage.setItem('currentUser', JSON.stringify(response)) } }) );