@@ -3,7 +3,7 @@ import { renderHook } from '@testing-library/react';
33
44import * as sourceModule from '@/source' ;
55
6- import { useDefaultOrderBy } from '../DBSearchPage' ;
6+ import { getDefaultSourceId , useDefaultOrderBy } from '../DBSearchPage' ;
77import * as metadataModule from '../hooks/useMetadata' ;
88
99// Mock the dependencies
@@ -390,3 +390,57 @@ describe('useDefaultOrderBy', () => {
390390 } ) ;
391391 } ) ;
392392} ) ;
393+
394+ describe ( 'getDefaultSourceId' , ( ) => {
395+ it ( 'returns "" when sources is undefined' , ( ) => {
396+ expect ( getDefaultSourceId ( undefined , undefined ) ) . toBe ( '' ) ;
397+ } ) ;
398+
399+ it ( 'returns "" when sources is empty' , ( ) => {
400+ expect ( getDefaultSourceId ( [ ] , undefined ) ) . toBe ( '' ) ;
401+ } ) ;
402+
403+ it ( 'returns "" when every source is disabled' , ( ) => {
404+ const sources = [
405+ { id : 'a' , disabled : true } ,
406+ { id : 'b' , disabled : true } ,
407+ ] ;
408+ expect ( getDefaultSourceId ( sources , 'a' ) ) . toBe ( '' ) ;
409+ } ) ;
410+
411+ it ( 'returns the last-selected source when it is enabled' , ( ) => {
412+ const sources = [
413+ { id : 'a' , disabled : false } ,
414+ { id : 'b' , disabled : false } ,
415+ { id : 'c' } , // disabled is undefined => treated as enabled
416+ ] ;
417+ expect ( getDefaultSourceId ( sources , 'b' ) ) . toBe ( 'b' ) ;
418+ expect ( getDefaultSourceId ( sources , 'c' ) ) . toBe ( 'c' ) ;
419+ } ) ;
420+
421+ it ( 'falls back to the first enabled source when last-selected is disabled' , ( ) => {
422+ const sources = [
423+ { id : 'a' , disabled : true } ,
424+ { id : 'b' , disabled : false } ,
425+ { id : 'c' , disabled : false } ,
426+ ] ;
427+ expect ( getDefaultSourceId ( sources , 'a' ) ) . toBe ( 'b' ) ;
428+ } ) ;
429+
430+ it ( 'falls back to the first enabled source when last-selected is unknown' , ( ) => {
431+ const sources = [
432+ { id : 'a' , disabled : true } ,
433+ { id : 'b' , disabled : false } ,
434+ ] ;
435+ expect ( getDefaultSourceId ( sources , 'unknown-id' ) ) . toBe ( 'b' ) ;
436+ } ) ;
437+
438+ it ( 'returns the first enabled source when last-selected is undefined and the list is mixed' , ( ) => {
439+ const sources = [
440+ { id : 'a' , disabled : true } ,
441+ { id : 'b' , disabled : false } ,
442+ { id : 'c' , disabled : false } ,
443+ ] ;
444+ expect ( getDefaultSourceId ( sources , undefined ) ) . toBe ( 'b' ) ;
445+ } ) ;
446+ } ) ;
0 commit comments