Add unit test for sanitize html

This commit is contained in:
Allen Snook 2018-10-16 15:54:33 -07:00
parent 95080bd68b
commit 9638b063a0
1 changed files with 33 additions and 0 deletions

View File

@ -0,0 +1,33 @@
/**
* @format
* @jest-environment jsdom
*/
/**
* External dependencies
*/
import dompurify from 'dompurify';
/**
* Internal dependencies
*/
import sanitizeHtml, { ALLOWED_TAGS, ALLOWED_ATTR } from '../../sanitize-html';
describe( 'sanitizeHtml', () => {
/* eslint-disable quotes */
const html = `<html><body><a href="#" malformed="attribute">Link</a></body></html>`;
/* eslint-enable quotes */
test( 'should remove any html tags and attributes which are not allowed', () => {
expect( sanitizeHtml( html ) ).toEqual( {
__html: '<a href="#">Link</a>',
} );
} );
test( 'should call dompurify.sanitize with list of allowed tags and attributes', () => {
const sanitizeMock = jest.spyOn( dompurify, 'sanitize' );
sanitizeHtml( html );
expect( sanitizeMock ).toHaveBeenCalledWith( html, { ALLOWED_ATTR, ALLOWED_TAGS } );
} );
} );