@@ -3,6 +3,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
3
3
import type { ResponseFn } from './sbFetch' ;
4
4
import SbFetch from './sbFetch' ;
5
5
import { SbHelpers } from './sbHelpers' ;
6
+ import type { ISbLink } from './interfaces' ;
6
7
7
8
// Mocking external dependencies
8
9
vi . mock ( '../src/sbFetch' , ( ) => {
@@ -376,6 +377,132 @@ describe('storyblokClient', () => {
376
377
// Verify the API was called only once (no relation resolution)
377
378
expect ( mockGet ) . toHaveBeenCalledTimes ( 1 ) ;
378
379
} ) ;
380
+
381
+ describe ( 'cdn/links endpoint' , ( ) => {
382
+ it ( 'should fetch links with dates when include_dates is set to 1' , async ( ) => {
383
+ const mockLinksResponse = {
384
+ data : {
385
+ links : {
386
+ 'story-1' : {
387
+ id : 1 ,
388
+ uuid : 'story-1-uuid' ,
389
+ slug : 'story-1' ,
390
+ name : 'Story 1' ,
391
+ is_folder : false ,
392
+ parent_id : 0 ,
393
+ published : true ,
394
+ position : 0 ,
395
+ // Date fields included because of include_dates: 1
396
+ created_at : '2024-01-01T10:00:00.000Z' ,
397
+ published_at : '2024-01-01T11:00:00.000Z' ,
398
+ updated_at : '2024-01-02T10:00:00.000Z' ,
399
+ } ,
400
+ 'story-2' : {
401
+ id : 2 ,
402
+ uuid : 'story-2-uuid' ,
403
+ slug : 'story-2' ,
404
+ name : 'Story 2' ,
405
+ is_folder : false ,
406
+ parent_id : 0 ,
407
+ published : true ,
408
+ position : 1 ,
409
+ created_at : '2024-01-03T10:00:00.000Z' ,
410
+ published_at : '2024-01-03T11:00:00.000Z' ,
411
+ updated_at : '2024-01-04T10:00:00.000Z' ,
412
+ } ,
413
+ } ,
414
+ } ,
415
+ headers : { } ,
416
+ status : 200 ,
417
+ } ;
418
+
419
+ const mockGet = vi . fn ( ) . mockResolvedValue ( mockLinksResponse ) ;
420
+
421
+ client . client = {
422
+ get : mockGet ,
423
+ post : vi . fn ( ) ,
424
+ setFetchOptions : vi . fn ( ) ,
425
+ baseURL : 'https://api.storyblok.com/v2' ,
426
+ } ;
427
+
428
+ const response = await client . get ( 'cdn/links' , {
429
+ version : 'draft' ,
430
+ include_dates : 1 ,
431
+ } ) ;
432
+
433
+ // Verify the structure of the response
434
+ expect ( response ) . toHaveProperty ( 'data.links' ) ;
435
+
436
+ // Check if links are present and have the correct structure
437
+ expect ( response . data . links [ 'story-1' ] ) . toBeDefined ( ) ;
438
+ expect ( response . data . links [ 'story-2' ] ) . toBeDefined ( ) ;
439
+
440
+ // Verify date fields are present in the response
441
+ const link : ISbLink = response . data . links [ 'story-1' ] ;
442
+ expect ( link ) . toHaveProperty ( 'created_at' ) ;
443
+ expect ( link ) . toHaveProperty ( 'published_at' ) ;
444
+ expect ( link ) . toHaveProperty ( 'updated_at' ) ;
445
+
446
+ // Verify the date formats
447
+ const DATETIME_FORMAT = / ^ \d { 4 } - \d { 2 } - \d { 2 } T \d { 2 } : \d { 2 } : \d { 2 } \. \d { 3 } Z $ / ;
448
+ expect ( link . created_at ) . toMatch ( DATETIME_FORMAT ) ;
449
+ expect ( link . published_at ) . toMatch ( DATETIME_FORMAT ) ;
450
+ expect ( link . updated_at ) . toMatch ( DATETIME_FORMAT ) ;
451
+
452
+ // Verify the API was called with correct parameters
453
+ expect ( mockGet ) . toHaveBeenCalledWith ( '/cdn/links' , {
454
+ cv : 0 ,
455
+ token : 'test-token' ,
456
+ version : 'draft' ,
457
+ include_dates : 1 ,
458
+ } ) ;
459
+ expect ( mockGet ) . toHaveBeenCalledTimes ( 1 ) ;
460
+ } ) ;
461
+
462
+ it ( 'should handle links response without dates when include_dates is not set' , async ( ) => {
463
+ const mockResponse = {
464
+ data : {
465
+ links : {
466
+ 'story-1' : {
467
+ id : 1 ,
468
+ uuid : 'story-1-uuid' ,
469
+ slug : 'story-1' ,
470
+ name : 'Story 1' ,
471
+ is_folder : false ,
472
+ parent_id : 0 ,
473
+ published : true ,
474
+ position : 0 ,
475
+ // No date fields
476
+ } ,
477
+ } ,
478
+ } ,
479
+ headers : { } ,
480
+ status : 200 ,
481
+ } ;
482
+
483
+ const mockGet = vi . fn ( ) . mockResolvedValue ( mockResponse ) ;
484
+ client . client . get = mockGet ;
485
+
486
+ const response = await client . get ( 'cdn/links' , { version : 'draft' } ) ;
487
+
488
+ expect ( response . data . links [ 'story-1' ] ) . not . toHaveProperty ( 'created_at' ) ;
489
+ expect ( response . data . links [ 'story-1' ] ) . not . toHaveProperty ( 'published_at' ) ;
490
+ expect ( response . data . links [ 'story-1' ] ) . not . toHaveProperty ( 'updated_at' ) ;
491
+ } ) ;
492
+
493
+ it ( 'should handle errors gracefully' , async ( ) => {
494
+ const mockGet = vi . fn ( ) . mockRejectedValue ( {
495
+ status : 404 ,
496
+ } ) ;
497
+ client . client . get = mockGet ;
498
+
499
+ await expect ( client . get ( 'cdn/links' , {
500
+ version : 'draft' ,
501
+ } ) ) . rejects . toMatchObject ( {
502
+ status : 404 ,
503
+ } ) ;
504
+ } ) ;
505
+ } ) ;
379
506
} ) ;
380
507
381
508
describe ( 'getAll' , ( ) => {
0 commit comments