1+ import { stackInstance } from '../utils/stack-instance' ;
2+ import { TEntry } from './types' ;
3+
4+ const stack = stackInstance ( ) ;
5+ const contentTypeUid = process . env . CONTENT_TYPE_UID || 'sample_content_type' ;
6+ const entryUid = process . env . ENTRY_UID || 'sample_entry' ;
7+ const variantUid = process . env . VARIANT_UID || 'sample_variant' ;
8+
9+ describe ( 'Entry Variants API Tests' , ( ) => {
10+ describe ( 'Single Entry Variant Operations' , ( ) => {
11+ it ( 'should fetch entry with specific variant' , async ( ) => {
12+ const result = await stack . contentType ( contentTypeUid ) . entry ( entryUid )
13+ . variants ( variantUid )
14+ . fetch < TEntry > ( ) ;
15+
16+ expect ( result ) . toBeDefined ( ) ;
17+ expect ( result . uid ) . toBe ( entryUid ) ;
18+ // Note: The SDK uses variants() method and sets x-cs-variant-uid header
19+ // The actual variant data structure depends on the CMS response
20+ } ) ;
21+
22+ it ( 'should fetch entry with multiple variants' , async ( ) => {
23+ const variantUids = [ variantUid , 'variant_2' , 'variant_3' ] ;
24+
25+ const result = await stack . contentType ( contentTypeUid ) . entry ( entryUid )
26+ . variants ( variantUids )
27+ . fetch < TEntry > ( ) ;
28+
29+ expect ( result ) . toBeDefined ( ) ;
30+ expect ( result . uid ) . toBe ( entryUid ) ;
31+ // Multiple variants are passed as comma-separated string in header
32+ } ) ;
33+
34+ it ( 'should include metadata with variant requests' , async ( ) => {
35+ const result = await stack . contentType ( contentTypeUid ) . entry ( entryUid )
36+ . variants ( variantUid )
37+ . includeMetadata ( )
38+ . fetch < TEntry > ( ) ;
39+
40+ expect ( result ) . toBeDefined ( ) ;
41+ expect ( result . uid ) . toBe ( entryUid ) ;
42+ // Metadata should be included when requested
43+ } ) ;
44+
45+ it ( 'should apply variant with reference inclusion' , async ( ) => {
46+ const result = await stack . contentType ( contentTypeUid ) . entry ( entryUid )
47+ . variants ( variantUid )
48+ . includeReference ( )
49+ . fetch < TEntry > ( ) ;
50+
51+ expect ( result ) . toBeDefined ( ) ;
52+ expect ( result . uid ) . toBe ( entryUid ) ;
53+ // Variants should work with reference inclusion
54+ } ) ;
55+ } ) ;
56+
57+ describe ( 'Entry Variants Query Operations' , ( ) => {
58+ it ( 'should query entries with specific variant' , async ( ) => {
59+ const result = await stack . contentType ( contentTypeUid ) . entry ( )
60+ . variants ( variantUid )
61+ . find < { entries : TEntry [ ] } > ( ) ;
62+
63+ expect ( result ) . toBeDefined ( ) ;
64+ expect ( result . entries ) . toBeDefined ( ) ;
65+ expect ( result . entries ! . length ) . toBeGreaterThan ( 0 ) ;
66+
67+ // The variant header is sent, affecting the response
68+ const entry = result . entries ! [ 0 ] as any ;
69+ expect ( entry . uid ) . toBeDefined ( ) ;
70+ } ) ;
71+
72+ it ( 'should query entries with multiple variants' , async ( ) => {
73+ const variantUids = [ variantUid , 'variant_2' ] ;
74+
75+ const result = await stack . contentType ( contentTypeUid ) . entry ( )
76+ . variants ( variantUids )
77+ . find < { entries : TEntry [ ] } > ( ) ;
78+
79+ expect ( result ) . toBeDefined ( ) ;
80+ expect ( result . entries ) . toBeDefined ( ) ;
81+ expect ( result . entries ! . length ) . toBeGreaterThan ( 0 ) ;
82+
83+ // Multiple variants are passed as comma-separated string
84+ const entry = result . entries ! [ 0 ] as any ;
85+ expect ( entry . uid ) . toBeDefined ( ) ;
86+ } ) ;
87+
88+ it ( 'should filter entries with variant using query' , async ( ) => {
89+ const result = await stack . contentType ( contentTypeUid ) . entry ( )
90+ . variants ( variantUid )
91+ . query ( )
92+ . equalTo ( 'uid' , entryUid )
93+ . find < { entries : TEntry [ ] } > ( ) ;
94+
95+ expect ( result ) . toBeDefined ( ) ;
96+ expect ( result . entries ) . toBeDefined ( ) ;
97+
98+ if ( result . entries && result . entries . length > 0 ) {
99+ result . entries . forEach ( ( entry : any ) => {
100+ expect ( entry . uid ) . toBe ( entryUid ) ;
101+ } ) ;
102+ }
103+ } ) ;
104+
105+ it ( 'should support pagination with variant queries' , async ( ) => {
106+ const result = await stack . contentType ( contentTypeUid ) . entry ( )
107+ . variants ( variantUid )
108+ . limit ( 5 )
109+ . skip ( 0 )
110+ . find < { entries : TEntry [ ] } > ( ) ;
111+
112+ expect ( result ) . toBeDefined ( ) ;
113+ expect ( result . entries ) . toBeDefined ( ) ;
114+ expect ( result . entries ! . length ) . toBeLessThanOrEqual ( 5 ) ;
115+
116+ if ( result . entries && result . entries . length > 0 ) {
117+ const entry = result . entries [ 0 ] as any ;
118+ expect ( entry . uid ) . toBeDefined ( ) ;
119+ }
120+ } ) ;
121+
122+ it ( 'should include count with variant queries' , async ( ) => {
123+ const result = await stack . contentType ( contentTypeUid ) . entry ( )
124+ . variants ( variantUid )
125+ . includeCount ( )
126+ . find < { entries : TEntry [ ] , count : number } > ( ) ;
127+
128+ expect ( result ) . toBeDefined ( ) ;
129+ expect ( result . entries ) . toBeDefined ( ) ;
130+ expect ( result . count ) . toBeDefined ( ) ;
131+ expect ( typeof result . count ) . toBe ( 'number' ) ;
132+
133+ if ( result . entries && result . entries . length > 0 ) {
134+ const entry = result . entries [ 0 ] as any ;
135+ expect ( entry . uid ) . toBeDefined ( ) ;
136+ }
137+ } ) ;
138+ } ) ;
139+
140+ describe ( 'Variant Field and Content Operations' , ( ) => {
141+ it ( 'should fetch entry with variant and content type' , async ( ) => {
142+ const result = await stack . contentType ( contentTypeUid ) . entry ( entryUid )
143+ . variants ( variantUid )
144+ . includeContentType ( )
145+ . fetch < TEntry > ( ) ;
146+
147+ expect ( result ) . toBeDefined ( ) ;
148+ expect ( result . uid ) . toBe ( entryUid ) ;
149+ expect ( result . title ) . toBeDefined ( ) ;
150+ // Content type should be included with variant
151+ } ) ;
152+
153+ it ( 'should fetch entry with variant and specific fields only' , async ( ) => {
154+ const result = await stack . contentType ( contentTypeUid ) . entry ( entryUid )
155+ . variants ( variantUid )
156+ . only ( [ 'title' , 'uid' ] )
157+ . fetch < TEntry > ( ) ;
158+
159+ expect ( result ) . toBeDefined ( ) ;
160+ expect ( result . uid ) . toBe ( entryUid ) ;
161+ expect ( result . title ) . toBeDefined ( ) ;
162+ // Only specified fields should be returned
163+ } ) ;
164+
165+ it ( 'should handle variant with embedded items' , async ( ) => {
166+ const result = await stack . contentType ( contentTypeUid ) . entry ( entryUid )
167+ . variants ( variantUid )
168+ . includeEmbeddedItems ( )
169+ . fetch < TEntry > ( ) ;
170+
171+ expect ( result ) . toBeDefined ( ) ;
172+ expect ( result . uid ) . toBe ( entryUid ) ;
173+ // Embedded items should be included with variant
174+ } ) ;
175+ } ) ;
176+
177+ describe ( 'Variant Performance and Basic Tests' , ( ) => {
178+ it ( 'should apply variant with additional parameters' , async ( ) => {
179+ const result = await stack . contentType ( contentTypeUid ) . entry ( entryUid )
180+ . variants ( variantUid )
181+ . addParams ( { 'variant_context' : 'mobile' } )
182+ . fetch < TEntry > ( ) ;
183+
184+ expect ( result ) . toBeDefined ( ) ;
185+ expect ( result . uid ) . toBe ( entryUid ) ;
186+ // Variant context is passed as additional parameter
187+ } ) ;
188+
189+ it ( 'should handle variant with multiple parameters' , async ( ) => {
190+ const result = await stack . contentType ( contentTypeUid ) . entry ( entryUid )
191+ . variants ( variantUid )
192+ . addParams ( {
193+ 'variant_context' : 'mobile' ,
194+ 'user_segment' : 'premium'
195+ } )
196+ . fetch < TEntry > ( ) ;
197+
198+ expect ( result ) . toBeDefined ( ) ;
199+ expect ( result . uid ) . toBe ( entryUid ) ;
200+ // Multiple parameters should be handled
201+ } ) ;
202+
203+ it ( 'should handle variant with locale specification' , async ( ) => {
204+ const result = await stack . contentType ( contentTypeUid ) . entry ( entryUid )
205+ . variants ( variantUid )
206+ . locale ( 'en-us' )
207+ . fetch < TEntry > ( ) ;
208+
209+ expect ( result ) . toBeDefined ( ) ;
210+ expect ( result . uid ) . toBe ( entryUid ) ;
211+ expect ( result . locale ) . toBe ( 'en-us' ) ;
212+ // Variant should work with locale
213+ } ) ;
214+ } ) ;
215+
216+ describe ( 'Variant Error Handling' , ( ) => {
217+ it ( 'should handle variant queries with reasonable performance' , async ( ) => {
218+ const startTime = Date . now ( ) ;
219+
220+ const result = await stack . contentType ( contentTypeUid ) . entry ( )
221+ . variants ( variantUid )
222+ . limit ( 10 )
223+ . find < { entries : TEntry [ ] } > ( ) ;
224+
225+ const endTime = Date . now ( ) ;
226+ const duration = endTime - startTime ;
227+
228+ expect ( result ) . toBeDefined ( ) ;
229+ expect ( result . entries ) . toBeDefined ( ) ;
230+ expect ( duration ) . toBeLessThan ( 10000 ) ; // Should complete within 10 seconds
231+
232+ if ( result . entries && result . entries . length > 0 ) {
233+ const entry = result . entries [ 0 ] as any ;
234+ expect ( entry . uid ) . toBeDefined ( ) ;
235+ }
236+ } ) ;
237+
238+ it ( 'should handle repeated variant requests consistently' , async ( ) => {
239+ // First request
240+ const result1 = await stack . contentType ( contentTypeUid ) . entry ( entryUid )
241+ . variants ( variantUid )
242+ . fetch < TEntry > ( ) ;
243+
244+ // Second request
245+ const result2 = await stack . contentType ( contentTypeUid ) . entry ( entryUid )
246+ . variants ( variantUid )
247+ . fetch < TEntry > ( ) ;
248+
249+ expect ( result1 ) . toBeDefined ( ) ;
250+ expect ( result2 ) . toBeDefined ( ) ;
251+ expect ( result1 . uid ) . toBe ( result2 . uid ) ;
252+
253+ // Both requests should return consistent data
254+ expect ( result1 . uid ) . toBe ( entryUid ) ;
255+ expect ( result2 . uid ) . toBe ( entryUid ) ;
256+ } ) ;
257+ } ) ;
258+
259+ describe ( 'Advanced Variant Error Handling' , ( ) => {
260+ it ( 'should handle invalid variant UIDs gracefully' , async ( ) => {
261+ try {
262+ await stack . contentType ( contentTypeUid ) . entry ( entryUid )
263+ . variants ( 'invalid_variant_uid' )
264+ . fetch < TEntry > ( ) ;
265+ } catch ( error ) {
266+ expect ( error ) . toBeDefined ( ) ;
267+ // Should return meaningful error message
268+ }
269+ } ) ;
270+
271+ it ( 'should handle basic variant requests' , async ( ) => {
272+ const result = await stack . contentType ( contentTypeUid ) . entry ( entryUid )
273+ . variants ( variantUid )
274+ . fetch < TEntry > ( ) ;
275+
276+ expect ( result ) . toBeDefined ( ) ;
277+ expect ( result . uid ) . toBe ( entryUid ) ;
278+ // Variant header should be applied
279+ } ) ;
280+
281+ it ( 'should handle variant query errors gracefully' , async ( ) => {
282+ try {
283+ await stack . contentType ( 'invalid_content_type' ) . entry ( )
284+ . variants ( variantUid )
285+ . find < { entries : TEntry [ ] } > ( ) ;
286+ } catch ( error ) {
287+ expect ( error ) . toBeDefined ( ) ;
288+ // Should handle error gracefully
289+ }
290+ } ) ;
291+ } ) ;
292+
293+ describe ( 'Variant Integration Tests' , ( ) => {
294+ it ( 'should support variant with reference inclusion' , async ( ) => {
295+ const result = await stack . contentType ( contentTypeUid ) . entry ( entryUid )
296+ . variants ( variantUid )
297+ . includeReference ( )
298+ . fetch < TEntry > ( ) ;
299+
300+ expect ( result ) . toBeDefined ( ) ;
301+ expect ( result . uid ) . toBe ( entryUid ) ;
302+ // Reference inclusion should work with variants
303+ } ) ;
304+
305+ it ( 'should handle variant with locale specification' , async ( ) => {
306+ const result = await stack . contentType ( contentTypeUid ) . entry ( entryUid )
307+ . variants ( variantUid )
308+ . locale ( 'en-us' )
309+ . fetch < TEntry > ( ) ;
310+
311+ expect ( result ) . toBeDefined ( ) ;
312+ expect ( result . uid ) . toBe ( entryUid ) ;
313+ expect ( result . locale ) . toBe ( 'en-us' ) ;
314+ // Variant should work with locale
315+ } ) ;
316+
317+ it ( 'should support variant with field selection' , async ( ) => {
318+ const result = await stack . contentType ( contentTypeUid ) . entry ( entryUid )
319+ . variants ( variantUid )
320+ . only ( [ 'title' , 'uid' ] )
321+ . fetch < TEntry > ( ) ;
322+
323+ expect ( result ) . toBeDefined ( ) ;
324+ expect ( result . uid ) . toBe ( entryUid ) ;
325+ expect ( result . title ) . toBeDefined ( ) ;
326+ // Only specified fields should be returned with variant
327+ } ) ;
328+ } ) ;
329+ } ) ;
0 commit comments