/** * @jest-environment jsdom */ import highlight from '../'; describe( 'highlight', () => { describe( 'unit test', () => { test( "should return html as is if there's no term", () => { expect( highlight( '', 'lorem ipsum hello world' ) ).toEqual( 'lorem ipsum hello world' ); } ); test( 'should wrap the term with when no custom wrapper node is defined', () => { expect( highlight( 'hello', 'lorem ipsum hello world' ) ).toContain( 'lorem ipsum hello world' ); } ); test( 'should wrap the term with the custom wrapper node when provided', () => { const span = document.createElement( 'span' ); span.setAttribute( 'class', 'marker' ); expect( highlight( 'hello', 'hello world', span ) ).toContain( 'hello world' ); } ); test( 'should wrap case-insensitively, within a single word, with multiple matches, over multiple lines', () => { expect( highlight( 'HEllO', 'merhabaHEllO\nselamHEllOdunya' ) ).toContain( 'merhabaHEllO\nselamHEllOdunya' ); } ); test( 'should be able to handle html elements with children and text nodes', () => { expect( highlight( 'hello', '
hello worldhello world
' ) ).toContain( '
hello worldhello world
' ); } ); } ); describe( 'edge cases', () => { describe( 'unicode', () => { test( 'should wrap when the text and query contains unicode chars', () => { expect( highlight( 'üşiöqç', 'lorem ıpsum üşiöqç' ) ).toContain( 'lorem ıpsum üşiöqç' ); } ); } ); describe( 'html', () => { test( 'should not match parts of html tags', () => { expect( highlight( 'img', 'hello world' ) ).toContain( 'hello world' ); } ); test( 'should prevent html tag injection', () => { expect( highlight( 'script', '' ) ).toContain( '' ); } ); test( 'should prevent html attribute injection', () => { expect( highlight( 'href', 'hello' ) ).toContain( 'hello' ); } ); } ); } ); } );