11import { httpClient , AxiosInstance } from '@contentstack/core' ;
2+ import { jest } from '@jest/globals' ;
23import MockAdapter from 'axios-mock-adapter' ;
34import { Stack } from '../../src/lib/stack' ;
45import { Asset } from '../../src/lib/asset' ;
@@ -8,6 +9,7 @@ import { syncResult } from '../utils/mocks';
89import { synchronization } from '../../src/lib/synchronization' ;
910import { ContentTypeQuery } from '../../src/lib/contenttype-query' ;
1011import { AssetQuery } from '../../src/lib/asset-query' ;
12+ import { StackConfig } from '../../src/lib/types' ;
1113
1214jest . mock ( '../../src/lib/synchronization' ) ;
1315const syncMock = < jest . Mock < typeof synchronization > > ( < unknown > synchronization ) ;
@@ -29,7 +31,7 @@ describe('Stack class tests', () => {
2931 environment : '' ,
3032 } ) ;
3133
32- stack = new Stack ( client , config ( ) ) ;
34+ stack = new Stack ( client , config ( ) as StackConfig ) ;
3335 } ) ;
3436 it ( 'should test import of class Stack' , ( done ) => {
3537 expect ( stack ) . toBeInstanceOf ( Stack ) ;
@@ -60,4 +62,85 @@ describe('Stack class tests', () => {
6062 expect ( result ) . toEqual ( syncResult ) ;
6163 syncMock . mockReset ( ) ;
6264 } ) ;
65+
66+ it ( 'should set live preview parameters correctly when live_preview is true' , ( done ) => {
67+ const query = {
68+ live_preview : 'live_preview_hash' ,
69+ contentTypeUid : 'contentTypeUid' ,
70+ entryUid : 'entryUid' ,
71+ preview_timestamp : 'timestamp' ,
72+ include_applied_variants : true ,
73+ } ;
74+
75+ stack . config . live_preview = { enable : true , live_preview : 'true' } ;
76+ stack . livePreviewQuery ( query ) ;
77+
78+ expect ( stack . getClient ( ) . stackConfig . live_preview ) . toEqual ( {
79+ live_preview : 'live_preview_hash' ,
80+ contentTypeUid : 'contentTypeUid' ,
81+ enable : true ,
82+ entryUid : 'entryUid' ,
83+ preview_timestamp : 'timestamp' ,
84+ include_applied_variants : true ,
85+ } ) ;
86+ done ( ) ;
87+ } ) ;
88+
89+ it ( 'should set live preview parameters to null when live_preview is false' , ( ) => {
90+ const query = {
91+ live_preview : '' ,
92+ } ;
93+
94+ stack . config . live_preview = { enable : false , live_preview : '' } ;
95+ stack . livePreviewQuery ( query ) ;
96+
97+ expect ( stack . getClient ( ) . stackConfig . live_preview ) . toEqual ( {
98+ live_preview : null ,
99+ contentTypeUid : null ,
100+ entryUid : null ,
101+ preview_timestamp : null ,
102+ include_applied_variants : false ,
103+ } ) ;
104+ } ) ;
105+
106+ it ( 'should set release_id header when release_id is present in query' , ( ) => {
107+ const query = {
108+ live_preview : 'live_preview_hash' ,
109+ release_id : 'releaseId' ,
110+ } ;
111+
112+ stack . livePreviewQuery ( query ) ;
113+
114+ expect ( stack . getClient ( ) . defaults . headers [ 'release_id' ] ) . toEqual ( 'releaseId' ) ;
115+ } ) ;
116+
117+ it ( 'should delete release_id header when release_id is not present in query' , ( ) => {
118+ stack . getClient ( ) . defaults . headers [ 'release_id' ] = 'releaseId' ;
119+ const query = { live_preview : 'live_preview_hash' } ;
120+
121+ stack . livePreviewQuery ( query ) ;
122+
123+ expect ( stack . getClient ( ) . defaults . headers [ 'release_id' ] ) . toBeUndefined ( ) ;
124+ } ) ;
125+
126+ it ( 'should set preview_timestamp header when preview_timestamp is present in query' , ( ) => {
127+ const query = {
128+ live_preview : 'live_preview_hash' ,
129+ preview_timestamp : 'timestamp' ,
130+ } ;
131+
132+ stack . livePreviewQuery ( query ) ;
133+
134+ expect ( stack . getClient ( ) . defaults . headers [ 'preview_timestamp' ] ) . toEqual ( 'timestamp' ) ;
135+ } ) ;
136+
137+ it ( 'should delete preview_timestamp header when preview_timestamp is not present in query' , ( ) => {
138+ stack . getClient ( ) . defaults . headers [ 'preview_timestamp' ] = 'timestamp' ;
139+ const query = { live_preview : 'live_preview_hash' } ;
140+
141+ stack . livePreviewQuery ( query ) ;
142+
143+ expect ( stack . getClient ( ) . defaults . headers [ 'preview_timestamp' ] ) . toBeUndefined ( ) ;
144+ } ) ;
63145} ) ;
146+
0 commit comments