1
+ import * as contentstack from '../../src/lib/contentstack' ;
2
+ import { TEntry } from './types' ;
3
+ import dotenv from 'dotenv' ;
4
+
5
+ dotenv . config ( ) ;
6
+
7
+ const apiKey = process . env . API_KEY as string
8
+ const deliveryToken = process . env . DELIVERY_TOKEN as string
9
+ const environment = process . env . ENVIRONMENT as string
10
+
11
+ describe ( 'Live preview tests' , ( ) => {
12
+ test ( 'should check for values initialized' , ( ) => {
13
+ const stack = contentstack . Stack ( {
14
+ apiKey : apiKey ,
15
+ deliveryToken : deliveryToken ,
16
+ environment : environment ,
17
+ } ) ;
18
+ const livePreviewObject = stack . config . live_preview ;
19
+ expect ( livePreviewObject ) . toBeUndefined ( ) ;
20
+ expect ( stack . config . host ) . toBe ( 'cdn.contentstack.io' ) ;
21
+ } ) ;
22
+
23
+ test ( 'should check host when live preview is enabled and management token is provided' , ( ) => {
24
+ const stack = contentstack . Stack ( {
25
+ apiKey : apiKey ,
26
+ deliveryToken : deliveryToken ,
27
+ environment : environment ,
28
+ live_preview : {
29
+ enable : true ,
30
+ management_token : 'management_token'
31
+ }
32
+ } )
33
+ const livePreviewObject = stack . config . live_preview
34
+ expect ( livePreviewObject ) . not . toBeUndefined ( ) ;
35
+ expect ( livePreviewObject ) . toHaveProperty ( 'enable' ) ;
36
+ expect ( livePreviewObject ) . toHaveProperty ( 'host' ) ;
37
+ expect ( livePreviewObject ) . not . toHaveProperty ( 'preview' ) ;
38
+ expect ( stack . config . host ) . toBe ( 'api.contentstack.io' ) ;
39
+ } ) ;
40
+
41
+ test ( 'should check host when live preview is disabled and management token is provided' , ( ) => {
42
+ const stack = contentstack . Stack ( {
43
+ apiKey : apiKey ,
44
+ deliveryToken : deliveryToken ,
45
+ environment : environment ,
46
+ live_preview : {
47
+ enable : false ,
48
+ management_token : 'management_token'
49
+ }
50
+ } )
51
+ const livePreviewObject = stack . config . live_preview
52
+ expect ( livePreviewObject ) . not . toBeUndefined ( ) ;
53
+ expect ( livePreviewObject ) . toHaveProperty ( 'enable' ) ;
54
+ expect ( livePreviewObject ) . not . toHaveProperty ( 'host' ) ;
55
+ expect ( livePreviewObject ) . not . toHaveProperty ( 'preview' ) ;
56
+ expect ( stack . config . host ) . toBe ( 'cdn.contentstack.io' ) ;
57
+ } ) ;
58
+
59
+ test ( 'should check host when live preview is enabled and preview token is provided' , ( ) => {
60
+ const stack = contentstack . Stack ( {
61
+ apiKey : apiKey ,
62
+ deliveryToken : deliveryToken ,
63
+ environment : environment ,
64
+ live_preview : {
65
+ enable : true ,
66
+ preview_token : 'preview_token'
67
+ }
68
+ } )
69
+ const livePreviewObject = stack . config . live_preview
70
+ expect ( livePreviewObject ) . not . toBeUndefined ( ) ;
71
+ expect ( livePreviewObject ) . toHaveProperty ( 'enable' ) ;
72
+ expect ( livePreviewObject ) . toHaveProperty ( 'host' ) ;
73
+ expect ( livePreviewObject ) . not . toHaveProperty ( 'preview' ) ;
74
+ expect ( stack . config . host ) . toBe ( 'rest-preview.contentstack.com' ) ;
75
+ } ) ;
76
+
77
+ test ( 'should check host when live preview is disabled and preview token is provided' , ( ) => {
78
+ const stack = contentstack . Stack ( {
79
+ apiKey : apiKey ,
80
+ deliveryToken : deliveryToken ,
81
+ environment : environment ,
82
+ live_preview : {
83
+ enable : false ,
84
+ preview_token : 'preview_token'
85
+ }
86
+ } )
87
+ const livePreviewObject = stack . config . live_preview
88
+ expect ( livePreviewObject ) . not . toBeUndefined ( ) ;
89
+ expect ( livePreviewObject ) . toHaveProperty ( 'enable' ) ;
90
+ expect ( livePreviewObject ) . not . toHaveProperty ( 'host' ) ;
91
+ expect ( livePreviewObject ) . not . toHaveProperty ( 'preview' ) ;
92
+ expect ( stack . config . host ) . toBe ( 'cdn.contentstack.io' ) ;
93
+ } ) ;
94
+ } ) ;
95
+
96
+ describe ( 'Live preview query Entry API tests' , ( ) => {
97
+ it ( 'should check for entry is when live preview is enabled with managemenet token' , async ( ) => {
98
+ const stack = contentstack . Stack ( {
99
+ apiKey : process . env . API_KEY as string ,
100
+ deliveryToken : process . env . DELIVERY_TOKEN as string ,
101
+ environment : process . env . ENVIRONMENT as string ,
102
+ live_preview : {
103
+ enable : true ,
104
+ management_token : 'management_token'
105
+ }
106
+ } )
107
+ stack . livePreviewQuery ( {
108
+ contentTypeUid : 'contentTypeUid' ,
109
+ live_preview : 'ser' ,
110
+ } )
111
+ const result = await stack . ContentType ( 'contentTypeUid' ) . Entry ( 'entryUid' ) . fetch < TEntry > ( ) ;
112
+ expect ( result ) . toBeDefined ( ) ;
113
+ expect ( result . _version ) . toBeDefined ( ) ;
114
+ expect ( result . locale ) . toEqual ( 'en-us' ) ;
115
+ expect ( result . uid ) . toBeDefined ( ) ;
116
+ expect ( result . created_by ) . toBeDefined ( ) ;
117
+ expect ( result . updated_by ) . toBeDefined ( ) ;
118
+ } ) ;
119
+
120
+ it ( 'should check for entry is when live preview is disabled with managemenet token' , async ( ) => {
121
+ const stack = contentstack . Stack ( {
122
+ apiKey : process . env . API_KEY as string ,
123
+ deliveryToken : process . env . DELIVERY_TOKEN as string ,
124
+ environment : process . env . ENVIRONMENT as string ,
125
+ live_preview : {
126
+ enable : false ,
127
+ management_token : 'management_token'
128
+ }
129
+ } )
130
+ stack . livePreviewQuery ( {
131
+ contentTypeUid : 'contentTypeUid' ,
132
+ live_preview : 'ser' ,
133
+ } )
134
+ const result = await stack . ContentType ( 'contentTypeUid' ) . Entry ( 'entryUid' ) . fetch < TEntry > ( ) ;
135
+ expect ( result ) . toBeDefined ( ) ;
136
+ expect ( result . _version ) . toBeDefined ( ) ;
137
+ expect ( result . locale ) . toEqual ( 'en-us' ) ;
138
+ expect ( result . uid ) . toBeDefined ( ) ;
139
+ expect ( result . created_by ) . toBeDefined ( ) ;
140
+ expect ( result . updated_by ) . toBeDefined ( ) ;
141
+ } ) ;
142
+
143
+ it ( 'should check for entry is when live preview is disabled with preview token' , async ( ) => {
144
+ const stack = contentstack . Stack ( {
145
+ apiKey : process . env . API_KEY as string ,
146
+ deliveryToken : process . env . DELIVERY_TOKEN as string ,
147
+ environment : process . env . ENVIRONMENT as string ,
148
+ live_preview : {
149
+ enable : false ,
150
+ preview_token : 'preview_token'
151
+ }
152
+ } )
153
+ stack . livePreviewQuery ( {
154
+ contentTypeUid : 'contentTypeUid' ,
155
+ live_preview : 'ser' ,
156
+ } )
157
+ const result = await stack . ContentType ( 'contentTypeUid' ) . Entry ( 'entryUid' ) . fetch < TEntry > ( ) ;
158
+ expect ( result ) . toBeDefined ( ) ;
159
+ expect ( result . _version ) . toBeDefined ( ) ;
160
+ expect ( result . locale ) . toEqual ( 'en-us' ) ;
161
+ expect ( result . uid ) . toBeDefined ( ) ;
162
+ expect ( result . created_by ) . toBeDefined ( ) ;
163
+ expect ( result . updated_by ) . toBeDefined ( ) ;
164
+ } ) ;
165
+ } )
0 commit comments