11import { expect , test } from 'vitest' ;
22
3- import { canViewWorkBook } from '$lib/utils/workbooks' ;
3+ import { canViewWorkBook , getUrlSlugFrom } from '$lib/utils/workbooks' ;
44import { Roles } from '$lib/types/user' ;
5+ import { type WorkbookList , WorkBookType } from '$lib/types/workbook' ;
6+
7+ function createWorkBookListBase ( overrides : Partial < WorkbookList > = { } ) : WorkbookList {
8+ return {
9+ id : 1 ,
10+ authorId : '3' ,
11+ authorName : 'Alice' ,
12+ title : '実装力を鍛える問題集' ,
13+ description : '' ,
14+ editorialUrl : '' ,
15+ isPublished : false ,
16+ isOfficial : false ,
17+ isReplenished : false ,
18+ workBookType : WorkBookType . CREATED_BY_USER ,
19+ workBookTasks : [ ] ,
20+ ...overrides ,
21+ } ;
22+ }
523
624describe ( 'Workbooks' , ( ) => {
725 describe ( 'can view workbooks' , ( ) => {
@@ -25,4 +43,26 @@ describe('Workbooks', () => {
2543 } ) ;
2644 } ) ;
2745 } ) ;
46+
47+ describe ( 'get url slug from workbook' , ( ) => {
48+ test ( 'returns urlSlug when it exists' , ( ) => {
49+ const workbook = createWorkBookListBase ( { id : 1 , urlSlug : '2-sat' } ) ;
50+ expect ( getUrlSlugFrom ( workbook ) ) . toBe ( '2-sat' ) ;
51+ } ) ;
52+
53+ test ( 'returns id as string when urlSlug is null' , ( ) => {
54+ const workbook = createWorkBookListBase ( { id : 10 , urlSlug : null } ) ;
55+ expect ( getUrlSlugFrom ( workbook ) ) . toBe ( '10' ) ;
56+ } ) ;
57+
58+ test ( 'returns id as string when urlSlug is undefined' , ( ) => {
59+ const workbook = createWorkBookListBase ( { id : 123 , urlSlug : undefined } ) ;
60+ expect ( getUrlSlugFrom ( workbook ) ) . toBe ( '123' ) ;
61+ } ) ;
62+
63+ test ( 'returns id as string when urlSlug is empty string' , ( ) => {
64+ const workbook = createWorkBookListBase ( { id : 999 , urlSlug : '' } ) ;
65+ expect ( getUrlSlugFrom ( workbook ) ) . toBe ( '999' ) ;
66+ } ) ;
67+ } ) ;
2868} ) ;
0 commit comments