|
| 1 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 2 | +import { LocalStorage } from 'app/shared/storage.service'; |
| 3 | +import { CookiesPopupComponent, storageKey } from './cookies-popup.component'; |
| 4 | + |
| 5 | +describe('CookiesPopupComponent', () => { |
| 6 | + let mockLocalStorage: MockLocalStorage; |
| 7 | + let fixture: ComponentFixture<CookiesPopupComponent>; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + mockLocalStorage = new MockLocalStorage(); |
| 11 | + |
| 12 | + TestBed.configureTestingModule({ |
| 13 | + declarations: [ |
| 14 | + CookiesPopupComponent, |
| 15 | + ], |
| 16 | + providers: [ |
| 17 | + { provide: LocalStorage, useValue: mockLocalStorage }, |
| 18 | + ], |
| 19 | + }); |
| 20 | + |
| 21 | + fixture = TestBed.createComponent(CookiesPopupComponent); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should make the popup visible by default', () => { |
| 25 | + fixture.detectChanges(); |
| 26 | + |
| 27 | + expect(getCookiesPopup()).not.toBeNull(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should include the correct content in the popup', () => { |
| 31 | + fixture.detectChanges(); |
| 32 | + |
| 33 | + const popup = getCookiesPopup() as Element; |
| 34 | + const infoBtn = popup.querySelector<HTMLAnchorElement>('a[mat-button]:nth-child(1)'); |
| 35 | + const okBtn = popup.querySelector<HTMLButtonElement>('button[mat-button]:nth-child(2)'); |
| 36 | + |
| 37 | + expect(popup.textContent).toContain( |
| 38 | + 'This site uses cookies from Google to deliver its services and to analyze traffic.'); |
| 39 | + |
| 40 | + expect(infoBtn).toBeInstanceOf(HTMLElement); |
| 41 | + expect(infoBtn?.href).toBe('https://policies.google.com/technologies/cookies'); |
| 42 | + expect(infoBtn?.textContent).toMatch(/learn more/i); |
| 43 | + |
| 44 | + expect(okBtn).toBeInstanceOf(HTMLElement); |
| 45 | + expect(okBtn?.textContent).toMatch(/ok, got it/i); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should hide the cookies popup if the user has already accepted cookies', () => { |
| 49 | + mockLocalStorage.setItem(storageKey, 'true'); |
| 50 | + fixture = TestBed.createComponent(CookiesPopupComponent); |
| 51 | + |
| 52 | + fixture.detectChanges(); |
| 53 | + |
| 54 | + expect(getCookiesPopup()).toBeNull(); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('acceptCookies()', () => { |
| 58 | + it('should hide the cookies popup', () => { |
| 59 | + fixture.detectChanges(); |
| 60 | + expect(getCookiesPopup()).not.toBeNull(); |
| 61 | + |
| 62 | + fixture.componentInstance.acceptCookies(); |
| 63 | + fixture.detectChanges(); |
| 64 | + expect(getCookiesPopup()).toBeNull(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should store the user\'s confirmation', () => { |
| 68 | + fixture.detectChanges(); |
| 69 | + expect(mockLocalStorage.getItem(storageKey)).toBeNull(); |
| 70 | + |
| 71 | + fixture.componentInstance.acceptCookies(); |
| 72 | + expect(mockLocalStorage.getItem(storageKey)).toBe('true'); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + // Helpers |
| 77 | + function getCookiesPopup() { |
| 78 | + return (fixture.nativeElement as HTMLElement).querySelector('.cookies-popup'); |
| 79 | + } |
| 80 | + |
| 81 | + class MockLocalStorage implements Pick<Storage, 'getItem' | 'setItem'> { |
| 82 | + private items = new Map<string, string>(); |
| 83 | + |
| 84 | + getItem(key: string): string | null { |
| 85 | + return this.items.get(key) ?? null; |
| 86 | + } |
| 87 | + |
| 88 | + setItem(key: string, val: string): void { |
| 89 | + this.items.set(key, val); |
| 90 | + } |
| 91 | + } |
| 92 | +}); |
0 commit comments