1- import { beforeEach , describe , expect , it , vi } from 'vitest' ;
1+ import { expect } from 'chai' ;
2+ import sinon from 'sinon' ;
3+ import axios from 'axios' ;
4+ import { configHandler , authHandler } from '@contentstack/cli-utilities' ;
25import { scheduleEntryAction } from '../../src/lib/create-stack' ;
36
4- const { mockPost } = vi . hoisted ( ( ) => ( { mockPost : vi . fn ( ) } ) ) ;
5-
6- vi . mock ( 'axios' , ( ) => ( { default : { post : mockPost } } ) ) ;
7-
8- vi . mock ( '@contentstack/cli-utilities' , ( ) => ( {
9- configHandler : {
10- get : vi . fn ( ) . mockImplementation ( ( key : string ) => {
11- if ( key === 'authorisationType' ) return 'BASIC' ;
12- if ( key === 'authtoken' ) return 'test-authtoken' ;
13- if ( key === 'region' ) return 'NA' ;
14- return undefined ;
15- } ) ,
16- } ,
17- authHandler : {
18- checkExpiryAndRefresh : vi . fn ( ) . mockResolvedValue ( undefined ) ,
19- } ,
20- } ) ) ;
21-
227describe ( 'scheduleEntryAction' , ( ) => {
238 const API_KEY = 'blt-test-api-key' ;
249 const ENTRY_OPTS = {
@@ -30,26 +15,36 @@ describe('scheduleEntryAction', () => {
3015 scheduledAt : '2026-08-01T10:00:00.000Z' ,
3116 } ;
3217
18+ let postStub : sinon . SinonStub ;
19+
3320 beforeEach ( ( ) => {
34- mockPost . mockReset ( ) ;
35- mockPost . mockResolvedValue ( { data : { } } ) ;
21+ postStub = sinon . stub ( axios , 'post' ) . resolves ( { data : { } } as any ) ;
22+ sinon . stub ( configHandler , 'get' ) . callsFake ( ( key : string ) => {
23+ if ( key === 'authorisationType' ) return 'BASIC' ;
24+ if ( key === 'authtoken' ) return 'test-authtoken' ;
25+ if ( key === 'region' ) return 'NA' ;
26+ return undefined ;
27+ } ) ;
28+ sinon . stub ( authHandler , 'checkExpiryAndRefresh' ) . resolves ( undefined ) ;
3629 } ) ;
3730
31+ afterEach ( ( ) => sinon . restore ( ) ) ;
32+
3833 it ( 'sends api_version: 3.2 header on entry publish' , async ( ) => {
3934 await scheduleEntryAction ( API_KEY , ENTRY_OPTS ) ;
4035
41- expect ( mockPost ) . toHaveBeenCalledOnce ( ) ;
42- const [ url , , { headers } ] = mockPost . mock . calls [ 0 ] ;
43- expect ( url ) . toContain ( '/v3/content_types/blog/entries/entry123/publish' ) ;
44- expect ( headers ) . toMatchObject ( { api_version : '3.2' } ) ;
36+ expect ( postStub . calledOnce ) . to . be . true ;
37+ const [ url , , { headers } ] = postStub . firstCall . args ;
38+ expect ( url ) . to . contain ( '/v3/content_types/blog/entries/entry123/publish' ) ;
39+ expect ( headers ) . to . include ( { api_version : '3.2' } ) ;
4540 } ) ;
4641
4742 it ( 'sends api_version: 3.2 header on entry unpublish' , async ( ) => {
4843 await scheduleEntryAction ( API_KEY , { ...ENTRY_OPTS , action : 'unpublish' } ) ;
4944
50- const [ url , , { headers } ] = mockPost . mock . calls [ 0 ] ;
51- expect ( url ) . toContain ( '/v3/content_types/blog/entries/entry123/unpublish' ) ;
52- expect ( headers ) . toMatchObject ( { api_version : '3.2' } ) ;
45+ const [ url , , { headers } ] = postStub . firstCall . args ;
46+ expect ( url ) . to . contain ( '/v3/content_types/blog/entries/entry123/unpublish' ) ;
47+ expect ( headers ) . to . include ( { api_version : '3.2' } ) ;
5348 } ) ;
5449
5550 it ( 'sends api_version: 3.2 header on asset publish (sys_assets)' , async ( ) => {
@@ -59,9 +54,9 @@ describe('scheduleEntryAction', () => {
5954 entryUid : 'asset456' ,
6055 } ) ;
6156
62- const [ url , , { headers } ] = mockPost . mock . calls [ 0 ] ;
63- expect ( url ) . toContain ( '/v3/assets/asset456/publish' ) ;
64- expect ( headers ) . toMatchObject ( { api_version : '3.2' } ) ;
57+ const [ url , , { headers } ] = postStub . firstCall . args ;
58+ expect ( url ) . to . contain ( '/v3/assets/asset456/publish' ) ;
59+ expect ( headers ) . to . include ( { api_version : '3.2' } ) ;
6560 } ) ;
6661
6762 it ( 'sends api_version: 3.2 header on asset unpublish (sys_assets)' , async ( ) => {
@@ -72,22 +67,22 @@ describe('scheduleEntryAction', () => {
7267 action : 'unpublish' ,
7368 } ) ;
7469
75- const [ url , , { headers } ] = mockPost . mock . calls [ 0 ] ;
76- expect ( url ) . toContain ( '/v3/assets/asset456/unpublish' ) ;
77- expect ( headers ) . toMatchObject ( { api_version : '3.2' } ) ;
70+ const [ url , , { headers } ] = postStub . firstCall . args ;
71+ expect ( url ) . to . contain ( '/v3/assets/asset456/unpublish' ) ;
72+ expect ( headers ) . to . include ( { api_version : '3.2' } ) ;
7873 } ) ;
7974
8075 it ( 'includes branch in headers alongside api_version when branch option is provided' , async ( ) => {
8176 await scheduleEntryAction ( API_KEY , { ...ENTRY_OPTS , branch : 'feature-branch' } ) ;
8277
83- const [ , , { headers } ] = mockPost . mock . calls [ 0 ] ;
84- expect ( headers ) . toMatchObject ( { api_version : '3.2' , branch : 'feature-branch' } ) ;
78+ const [ , , { headers } ] = postStub . firstCall . args ;
79+ expect ( headers ) . to . include ( { api_version : '3.2' , branch : 'feature-branch' } ) ;
8580 } ) ;
8681
8782 it ( 'omits branch from headers when no branch option is given' , async ( ) => {
8883 await scheduleEntryAction ( API_KEY , ENTRY_OPTS ) ;
8984
90- const [ , , { headers } ] = mockPost . mock . calls [ 0 ] ;
91- expect ( headers ) . not . toHaveProperty ( 'branch' ) ;
85+ const [ , , { headers } ] = postStub . firstCall . args ;
86+ expect ( headers ) . to . not . have . property ( 'branch' ) ;
9287 } ) ;
9388} ) ;
0 commit comments