|
| 1 | +import { Component, provideZonelessChangeDetection } from '@angular/core'; |
| 2 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 3 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 4 | +import { By } from '@angular/platform-browser'; |
| 5 | +import { CustomTooltipDirective } from './custom-tooltip.directive'; |
| 6 | + |
| 7 | +@Component({ |
| 8 | + imports: [CustomTooltipDirective], |
| 9 | + template: `<button matTooltip="placeholder">host</button>` |
| 10 | +}) |
| 11 | +class TestHostComponent { } |
| 12 | + |
| 13 | +describe('CustomTooltipDirective', () => { |
| 14 | + let fixture: ComponentFixture<TestHostComponent>; |
| 15 | + let directive: CustomTooltipDirective; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + vi.useFakeTimers(); |
| 19 | + TestBed.configureTestingModule({ |
| 20 | + providers: [provideZonelessChangeDetection()], |
| 21 | + imports: [TestHostComponent], |
| 22 | + }).compileComponents(); |
| 23 | + fixture = TestBed.createComponent(TestHostComponent); |
| 24 | + fixture.detectChanges(); |
| 25 | + const el = fixture.debugElement.query(By.directive(CustomTooltipDirective)); |
| 26 | + directive = el.injector.get(CustomTooltipDirective); |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(() => { |
| 30 | + vi.clearAllTimers(); |
| 31 | + vi.useRealTimers(); |
| 32 | + document.querySelectorAll('.custom-tooltip').forEach(n => n.remove()); |
| 33 | + }); |
| 34 | + |
| 35 | + // Tooltip text can carry untrusted values (e.g. CF usernames). The directive |
| 36 | + // must render it as text, never parse it as HTML. |
| 37 | + it('renders tooltip text as plain text, not parsed HTML (XSS-safe)', () => { |
| 38 | + const payload = '<img src=x onerror="alert(1)">alice'; |
| 39 | + directive.tooltipText = payload; |
| 40 | + directive.onMouseEnter(new MouseEvent('mouseenter')); |
| 41 | + vi.advanceTimersByTime(300); |
| 42 | + |
| 43 | + const tip = document.body.querySelector('.custom-tooltip'); |
| 44 | + expect(tip).toBeTruthy(); |
| 45 | + expect(tip?.textContent).toBe(payload); |
| 46 | + expect(tip?.querySelector('img')).toBeNull(); |
| 47 | + }); |
| 48 | +}); |
0 commit comments