Test SafeHtml pipe for string to be sanitized

Viewed 694

I would like test if unsafe string was wrapped in SafeHtml object

This is my SafeHtmlPipe

@Pipe({ name: "safehtml" })
export class SafeHtmlPipe implements PipeTransform {
    constructor(private sanitized: DomSanitizer) { }

    transform(value: string): SafeHtml {
        return this.sanitized.bypassSecurityTrustHtml(value);
    }
}

SafeHtml spec

describe('SafeHtmlPipe', () => {
    let sanitizer;
    let pipe: SafeHtmlPipe;
    const unsafeHtml = `<a href="javascript:alert('XSS')">Unsafe</a>`;

    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [BrowserModule]
        });

        sanitizer = TestBed.get(DomSanitizer);
        pipe = new SafeHtmlPipe(sanitizer);

    });

    it('should create an instance', () => {
        expect(pipe).toBeTruthy();
    });

    it('should transform html to SafeHtml', () => {
        let safeHtml = pipe.transform(unsafeHtml);
        // check if unsafe html wrapped in SafeHtml object
    });
});
0 Answers
Related