Repository navigation
[ENG-12397] 2. Cookie consent is not saved after accepting Cookies #1079
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
Merged
brianjgeiger
merged 2 commits into
CenterForOpenScience:feature/pbs-26-20
from
nsemets:fix/ENG-12397
Oct 15, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 28 additions & 18 deletions
46
...core/components/osf-banners/cookie-consent-banner/cookie-consent-banner.component.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,53 +1,63 @@ | ||
| import { CookieService } from 'ngx-cookie-service'; | ||
| import { MockComponent, MockProvider } from 'ng-mocks'; | ||
|
|
||
| import { PLATFORM_ID } from '@angular/core'; | ||
| import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
|
||
| import { CookieConsentService } from '@core/services/cookie-consent.service'; | ||
| import { IconComponent } from '@osf/shared/components/icon/icon.component'; | ||
|
|
||
| import { provideOSFCore } from '@testing/osf.testing.provider'; | ||
| import { CookieServiceMock } from '@testing/providers/cookie-service.mock'; | ||
| import { CookieConsentServiceMock, CookieConsentServiceMockType } from '@testing/providers/cookie-consent.service.mock'; | ||
|
|
||
| import { CookieConsentBannerComponent } from './cookie-consent-banner.component'; | ||
|
|
||
| describe('Component: Cookie Consent Banner', () => { | ||
| let fixture: ComponentFixture<CookieConsentBannerComponent>; | ||
| let component: CookieConsentBannerComponent; | ||
| let cookieConsentMock: CookieConsentServiceMockType; | ||
|
|
||
| const cookieServiceMock = CookieServiceMock.simple(); | ||
| function setup(overrides: { hasConsent?: boolean; isBrowser?: boolean } = {}) { | ||
| cookieConsentMock = CookieConsentServiceMock.simple(); | ||
| cookieConsentMock.hasConsent.mockReturnValue(overrides.hasConsent ?? false); | ||
|
|
||
| beforeEach(() => { | ||
| TestBed.configureTestingModule({ | ||
| imports: [CookieConsentBannerComponent, MockComponent(IconComponent)], | ||
| providers: [provideOSFCore(), MockProvider(CookieService, cookieServiceMock)], | ||
| providers: [ | ||
| provideOSFCore(), | ||
| MockProvider(CookieConsentService, cookieConsentMock), | ||
| MockProvider(PLATFORM_ID, overrides.isBrowser === false ? 'server' : 'browser'), | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| it('should show the banner if cookie is not set', () => { | ||
| cookieServiceMock.check.mockReturnValue(false); | ||
| fixture = TestBed.createComponent(CookieConsentBannerComponent); | ||
| component = fixture.componentInstance; | ||
| } | ||
|
|
||
| it('should show the banner when consent is missing', () => { | ||
| setup(); | ||
|
|
||
| expect(component.displayBanner()).toBe(true); | ||
| }); | ||
|
|
||
| it('should hide the banner if cookie is set', () => { | ||
| cookieServiceMock.check.mockReturnValue(true); | ||
| fixture = TestBed.createComponent(CookieConsentBannerComponent); | ||
| component = fixture.componentInstance; | ||
| it('should hide the banner when consent is already recorded', () => { | ||
| setup({ hasConsent: true }); | ||
|
|
||
| expect(component.displayBanner()).toBe(false); | ||
| }); | ||
|
|
||
| it('should set cookie and hide banner on acceptCookies()', () => { | ||
| cookieServiceMock.check.mockReturnValue(false); | ||
| fixture = TestBed.createComponent(CookieConsentBannerComponent); | ||
| component = fixture.componentInstance; | ||
| it('should keep the banner hidden outside the browser', () => { | ||
| setup({ isBrowser: false }); | ||
|
|
||
| component.acceptCookies(); | ||
| expect(component.displayBanner()).toBe(false); | ||
| expect(cookieConsentMock.hasConsent).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| expect(cookieServiceMock.set).toHaveBeenCalledWith('cookie-consent', 'true', new Date('9999-12-31T23:59:59Z'), '/'); | ||
| it('should accept cookies and hide the banner', () => { | ||
| setup(); | ||
|
|
||
| component.acceptCookies(); | ||
|
|
||
| expect(cookieConsentMock.accept).toHaveBeenCalled(); | ||
| expect(component.displayBanner()).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { CookieService } from 'ngx-cookie-service'; | ||
| import { MockProvider } from 'ng-mocks'; | ||
|
|
||
| import { TestBed } from '@angular/core/testing'; | ||
|
|
||
| import { CookieServiceMock, CookieServiceMockType } from '@testing/providers/cookie-service.mock'; | ||
|
|
||
| import { CookieConsentService } from './cookie-consent.service'; | ||
|
|
||
| describe('CookieConsentService', () => { | ||
| let service: CookieConsentService; | ||
| let cookieServiceMock: CookieServiceMockType; | ||
|
|
||
| beforeEach(() => { | ||
| cookieServiceMock = CookieServiceMock.simple(); | ||
|
|
||
| TestBed.configureTestingModule({ | ||
| providers: [CookieConsentService, MockProvider(CookieService, cookieServiceMock)], | ||
| }); | ||
|
|
||
| service = TestBed.inject(CookieConsentService); | ||
| }); | ||
|
|
||
| it('should report consent when the cookie exists', () => { | ||
| cookieServiceMock.check.mockReturnValue(true); | ||
|
|
||
| expect(service.hasConsent()).toBe(true); | ||
| expect(cookieServiceMock.check).toHaveBeenCalledWith('cookie-consent'); | ||
| }); | ||
|
|
||
| it('should report no consent when the cookie is missing', () => { | ||
| expect(service.hasConsent()).toBe(false); | ||
| expect(cookieServiceMock.check).toHaveBeenCalledWith('cookie-consent'); | ||
| }); | ||
|
|
||
| it('should store consent until year 9999', () => { | ||
| service.accept(); | ||
|
|
||
| expect(cookieServiceMock.set).toHaveBeenCalledWith('cookie-consent', 'true', new Date('9999-12-31T23:59:59Z'), '/'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { CookieService } from 'ngx-cookie-service'; | ||
|
|
||
| import { inject, Injectable } from '@angular/core'; | ||
|
|
||
| @Injectable({ providedIn: 'root' }) | ||
| export class CookieConsentService { | ||
| private readonly cookies = inject(CookieService); | ||
| private readonly cookieName = 'cookie-consent'; | ||
|
|
||
| hasConsent(): boolean { | ||
| return this.cookies.check(this.cookieName); | ||
| } | ||
|
|
||
| accept(): void { | ||
| this.cookies.set(this.cookieName, 'true', new Date('9999-12-31T23:59:59Z'), '/'); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or does this keep it from showing on SSR. If so, why the exception for non-browsers in the service?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The non-browser return in the service was an extra guard. It can be removed from service.