11import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest' ;
22import { Repository } from '../src/Repository' ;
3- import { SearchResult } from '../src/utils/types' ;
43
54interface TestElement {
65 title : string ;
@@ -95,7 +94,7 @@ describe('Repository', () => {
9594 expect ( repository . search ( 'almostbar' ) ) . toEqual ( [ ] ) ;
9695 } ) ;
9796
98- it ( 'excludes items from search #2 ' , ( ) => {
97+ it ( 'sorts search results alphabetically by title ' , ( ) => {
9998 repository . setOptions ( {
10099 sortMiddleware : ( a : TestElement , b : TestElement ) => {
101100 return a . title . localeCompare ( b . title ) ;
@@ -108,23 +107,90 @@ describe('Repository', () => {
108107 expect ( results [ 2 ] ) . toMatchObject ( loremElement ) ;
109108 } ) ;
110109
110+ it ( 'uses default NoSort when no sortMiddleware provided' , ( ) => {
111+ const results = repository . search ( 'r' ) ;
112+ expect ( results ) . toHaveLength ( 3 ) ;
113+ expect ( results [ 0 ] ) . toMatchObject ( barElement ) ;
114+ expect ( results [ 1 ] ) . toMatchObject ( almostBarElement ) ;
115+ expect ( results [ 2 ] ) . toMatchObject ( loremElement ) ;
116+ } ) ;
117+
118+ it ( 'demonstrates README example: custom sorting by section and caption' , ( ) => {
119+ const testData = [
120+ { section : 'Getting Started' , caption : 'Installation' , title : 'How to install' } ,
121+ { section : 'API Reference' , caption : 'Methods' , title : 'Available methods' } ,
122+ { section : 'Getting Started' , caption : 'Configuration' , title : 'How to configure' } ,
123+ { section : 'API Reference' , caption : 'Properties' , title : 'Object properties' }
124+ ] ;
125+
126+ repository . put ( testData ) ;
127+ repository . setOptions ( {
128+ sortMiddleware : ( a : any , b : any ) => {
129+ const astr = String ( a . section ) + "-" + String ( a . caption ) ;
130+ const bstr = String ( b . section ) + "-" + String ( b . caption ) ;
131+ return astr . localeCompare ( bstr ) ;
132+ } ,
133+ } ) ;
134+
135+ const results = repository . search ( 'How' ) ;
136+ expect ( results ) . toHaveLength ( 2 ) ;
137+ // Should be sorted by section first, then caption
138+ expect ( results [ 0 ] . section ) . toBe ( 'Getting Started' ) ;
139+ expect ( results [ 0 ] . caption ) . toBe ( 'Configuration' ) ;
140+ expect ( results [ 1 ] . section ) . toBe ( 'Getting Started' ) ;
141+ expect ( results [ 1 ] . caption ) . toBe ( 'Installation' ) ;
142+ } ) ;
143+
111144 it ( 'search results should be a clone and not a reference to repository data' , ( ) => {
112145 const query = 'Developer' ;
113- repository . put (
146+ const testData = [
114147 { name : 'Alice' , role : 'Developer' } ,
115- { name : 'Bob' , role : 'Designer' } ,
116- ) ;
148+ { name : 'Bob' , role : 'Designer' }
149+ ] ;
150+ repository . put ( testData ) ;
117151
118152 const results = repository . search ( query ) ;
119153 expect ( results ) . toHaveLength ( 1 ) ;
120154 expect ( results [ 0 ] ) . toMatchObject ( { name : 'Alice' , role : 'Developer' } ) ;
121155
122- ( results as SearchResult [ ] ) . forEach ( result => {
156+ ( results as any [ ] ) . forEach ( result => {
123157 result . role = 'Modified Role' ;
124158 } ) ;
125159
126160 const originalData = repository . search ( query ) ;
127161 expect ( originalData ) . toHaveLength ( 1 ) ;
128162 expect ( originalData [ 0 ] ) . toMatchObject ( { name : 'Alice' , role : 'Developer' } ) ;
129163 } ) ;
164+
165+ it ( 'demonstrates README sortMiddleware example exactly' , ( ) => {
166+ // This test matches the exact example from the README
167+ const testData = [
168+ { section : 'API Reference' , caption : 'Properties' , title : 'Object properties' } ,
169+ { section : 'Getting Started' , caption : 'Installation' , title : 'How to install' } ,
170+ { section : 'API Reference' , caption : 'Methods' , title : 'Available methods' } ,
171+ { section : 'Getting Started' , caption : 'Configuration' , title : 'How to configure' }
172+ ] ;
173+
174+ repository . put ( testData ) ;
175+ repository . setOptions ( {
176+ sortMiddleware : function ( a : any , b : any ) {
177+ var astr = String ( a . section ) + "-" + String ( a . caption ) ;
178+ var bstr = String ( b . section ) + "-" + String ( b . caption ) ;
179+ return astr . localeCompare ( bstr ) ;
180+ } ,
181+ } ) ;
182+
183+ const results = repository . search ( 'a' ) ; // Search for 'a' to get all results
184+ expect ( results ) . toHaveLength ( 4 ) ;
185+
186+ // Should be sorted by section first, then caption alphabetically
187+ expect ( results [ 0 ] . section ) . toBe ( 'API Reference' ) ;
188+ expect ( results [ 0 ] . caption ) . toBe ( 'Methods' ) ;
189+ expect ( results [ 1 ] . section ) . toBe ( 'API Reference' ) ;
190+ expect ( results [ 1 ] . caption ) . toBe ( 'Properties' ) ;
191+ expect ( results [ 2 ] . section ) . toBe ( 'Getting Started' ) ;
192+ expect ( results [ 2 ] . caption ) . toBe ( 'Configuration' ) ;
193+ expect ( results [ 3 ] . section ) . toBe ( 'Getting Started' ) ;
194+ expect ( results [ 3 ] . caption ) . toBe ( 'Installation' ) ;
195+ } ) ;
130196} ) ;
0 commit comments