Browse Source

Test created for safe-html pipe

pull/16117/head
masumulu28 3 years ago
parent
commit
7cf6b2c860
  1. 37
      npm/ng-packs/packages/core/src/lib/tests/safe-html.pipe.spec.ts

37
npm/ng-packs/packages/core/src/lib/tests/safe-html.pipe.spec.ts

@ -0,0 +1,37 @@
import { createServiceFactory, SpectatorService } from '@ngneat/spectator/jest';
import { SafeHtmlPipe } from '../pipes';
describe('SafeHtmlPipe', () => {
let pipe: SafeHtmlPipe;
let spectator: SpectatorService<SafeHtmlPipe>;
const createService = createServiceFactory(SafeHtmlPipe);
beforeEach(() => {
spectator = createService();
pipe = spectator.service;
});
it('should create an instance', () => {
expect(pipe).toBeTruthy();
});
it('should return empty string for non-string inputs', () => {
const inputs = [42, false, {}, []];
for (const input of inputs) {
const result = pipe.transform(input as any);
expect(result).toBe('');
}
});
it('should be equals with input after sanitized', () => {
const input = '<div><h1>Hello world!</h1></div>';
const result = pipe.transform(input);
expect(result).toEqual(input);
});
it('should sanitize unsafe HTML content', () => {
const input = `<script>alert("hello world");</script><p><a href='#' onclick="alert('This is an XSS attack!')">Click here!</a></p>`;
const result = pipe.transform(input);
expect(result).toBe(`<p><a href=\"#\">Click here!</a></p>`);
});
});
Loading…
Cancel
Save