11import { describe , it , beforeEach , afterEach } from 'bun:test'
22import assert from 'node:assert'
33import sinon from 'sinon'
4- import { highlight } from './index.js'
4+ import { Highlight } from './index.js'
55
66describe ( 'default configuration' , ( ) => {
77 it ( 'should correctly highlight a text' , ( ) => {
@@ -13,24 +13,30 @@ describe('default configuration', () => {
1313 const searchTerm2 = 'yesterday I was in trouble'
1414 const expectedResult2 = '<mark class="orama-highlight">Yesterday</mark> all my <mark class="orama-highlight">trouble</mark>s seemed so far away, now <mark class="orama-highlight">i</mark>t looks as though they\'re here to stay oh, <mark class="orama-highlight">I</mark> bel<mark class="orama-highlight">i</mark>eve <mark class="orama-highlight">i</mark>n <mark class="orama-highlight">yesterday</mark>'
1515
16- assert . strictEqual ( highlight ( text1 , searchTerm1 ) . toString ( ) , expectedResult1 )
17- assert . strictEqual ( highlight ( text2 , searchTerm2 ) . toString ( ) , expectedResult2 )
16+ const highlighter = new Highlight ( )
17+
18+ assert . strictEqual ( highlighter . highlight ( text1 , searchTerm1 ) . HTML , expectedResult1 )
19+ assert . strictEqual ( highlighter . highlight ( text2 , searchTerm2 ) . HTML , expectedResult2 )
1820 } )
1921
2022 it ( 'should return the correct positions' , ( ) => {
2123 const text = 'The quick brown fox jumps over the lazy dog'
2224 const searchTerm = 'fox'
2325 const expectedPositions = [ { start : 16 , end : 18 } ]
2426
25- assert . deepStrictEqual ( highlight ( text , searchTerm ) . positions , expectedPositions )
27+ const highlighter = new Highlight ( )
28+
29+ assert . deepStrictEqual ( highlighter . highlight ( text , searchTerm ) . positions , expectedPositions )
2630 } )
2731
2832 it ( 'should return multiple positions' , ( ) => {
2933 const text = 'The quick brown fox jumps over the lazy dog'
3034 const searchTerm = 'the'
3135 const expectedPositions = [ { start : 0 , end : 2 } , { start : 31 , end : 33 } ]
3236
33- assert . deepStrictEqual ( highlight ( text , searchTerm ) . positions , expectedPositions )
37+ const highlighter = new Highlight ( )
38+
39+ assert . deepStrictEqual ( highlighter . highlight ( text , searchTerm ) . positions , expectedPositions )
3440 } )
3541} )
3642
@@ -44,32 +50,40 @@ describe('custom configuration', () => {
4450 const searchTerm2 = 'yesterday I was in trouble'
4551 const expectedResult2 = 'Yesterday all my <mark class="orama-highlight">trouble</mark>s seemed so far away, now it looks as though they\'re here to stay oh, <mark class="orama-highlight">I</mark> believe <mark class="orama-highlight">in</mark> <mark class="orama-highlight">yesterday</mark>'
4652
47- assert . strictEqual ( highlight ( text1 , searchTerm1 , { caseSensitive : true } ) . toString ( ) , expectedResult1 )
48- assert . strictEqual ( highlight ( text2 , searchTerm2 , { caseSensitive : true } ) . toString ( ) , expectedResult2 )
53+ const highlighter = new Highlight ( { caseSensitive : true } )
54+
55+ assert . strictEqual ( highlighter . highlight ( text1 , searchTerm1 ) . HTML , expectedResult1 )
56+ assert . strictEqual ( highlighter . highlight ( text2 , searchTerm2 ) . HTML , expectedResult2 )
4957 } )
5058
5159 it ( 'should correctly set a custom CSS class' , ( ) => {
5260 const text = 'The quick brown fox jumps over the lazy dog'
5361 const searchTerm = 'fox'
5462 const expectedResult = 'The quick brown <mark class="custom-class">fox</mark> jumps over the lazy dog'
5563
56- assert . strictEqual ( highlight ( text , searchTerm , { CSSClass : 'custom-class' } ) . toString ( ) , expectedResult )
64+ const highlighter = new Highlight ( { CSSClass : 'custom-class' } )
65+
66+ assert . strictEqual ( highlighter . highlight ( text , searchTerm ) . HTML , expectedResult )
5767 } )
5868
5969 it ( 'should correctly use a custom HTML tag' , ( ) => {
6070 const text = 'The quick brown fox jumps over the lazy dog'
6171 const searchTerm = 'fox'
6272 const expectedResult = 'The quick brown <div class="orama-highlight">fox</div> jumps over the lazy dog'
6373
64- assert . strictEqual ( highlight ( text , searchTerm , { HTMLTag : 'div' } ) . toString ( ) , expectedResult )
74+ const highlighter = new Highlight ( { HTMLTag : 'div' } )
75+
76+ assert . strictEqual ( highlighter . highlight ( text , searchTerm ) . HTML , expectedResult )
6577 } )
6678
6779 it ( 'should correctly highlight whole words only' , ( ) => {
6880 const text = 'The quick brown fox jumps over the lazy dog'
6981 const searchTerm = 'fox jump'
7082 const expectedResult = 'The quick brown <mark class="orama-highlight">fox</mark> jumps over the lazy dog'
7183
72- assert . strictEqual ( highlight ( text , searchTerm , { wholeWords : true } ) . toString ( ) , expectedResult )
84+ const highlighter = new Highlight ( { wholeWords : true } )
85+
86+ assert . strictEqual ( highlighter . highlight ( text , searchTerm ) . HTML , expectedResult )
7387 } )
7488} )
7589
@@ -94,10 +108,26 @@ describe('highlight function - infinite loop protection', () => {
94108 return null
95109 } )
96110
97- const result = highlight ( text , searchTerm )
111+ const highlighter = new Highlight ( )
112+ const result = highlighter . highlight ( text , searchTerm )
98113
99- assert . strictEqual ( result . toString ( ) , text )
114+ assert . strictEqual ( result . HTML , text )
100115
101116 assert ( regexExecStub . called )
102117 } )
103118} )
119+
120+ describe ( 'trim method' , ( ) => {
121+ it ( 'should correctly trim the text' , ( ) => {
122+ const text = 'The quick brown fox jumps over the lazy dog'
123+ const searchTerm = 'fox'
124+ const highlighter = new Highlight ( )
125+
126+ assert . strictEqual ( highlighter . highlight ( text , searchTerm ) . trim ( 10 ) , '...rown <mark class="orama-highlight">fox</mark> j...' )
127+ assert . strictEqual ( highlighter . highlight ( text , searchTerm ) . trim ( 5 ) , '...n <mark class="orama-highlight">fox</mark>...' )
128+ assert . strictEqual ( highlighter . highlight ( text , 'the' ) . trim ( 5 ) , '<mark class="orama-highlight">The</mark> q...' )
129+ assert . strictEqual ( highlighter . highlight ( text , 'dog' ) . trim ( 5 ) , '...y <mark class="orama-highlight">dog</mark>' )
130+ assert . strictEqual ( highlighter . highlight ( text , 'dog' ) . trim ( 5 , false ) , 'y <mark class="orama-highlight">dog</mark>' )
131+ assert . strictEqual ( highlighter . highlight ( text , 'the' ) . trim ( 5 , false ) , '<mark class="orama-highlight">The</mark> q' )
132+ } )
133+ } )
0 commit comments