@@ -8,7 +8,7 @@ const stack = stackInstance();
88const countryUsa = process . env . TAX_COUNTRY_USA || 'usa'
99const locale = process . env . TAX_LOCALE || 'en-us'
1010
11- describe ( "Terms API test cases" , ( ) => {
11+ describe ( "Terms Query API test cases" , ( ) => {
1212 it ( "should check for terms is defined" , async ( ) => {
1313 const result = await makeTerms ( countryUsa ) . find < TTerm > ( ) ;
1414 if ( result . terms ) {
@@ -38,9 +38,94 @@ describe("Terms API test cases", () => {
3838 expect ( result . terms ) . toBeDefined ( ) ;
3939 }
4040 } ) ;
41+
42+ it ( "should return terms for given locale when locale() is chained" , async ( ) => {
43+ const result = await makeTerms ( "taxonomy_testing" ) . locale ( "hi-in" ) . find < TTerm > ( ) ;
44+ expect ( result ) . toBeDefined ( ) ;
45+ } ) ;
46+
47+ it ( "should return terms with fallback when includeFallback() is chained" , async ( ) => {
48+ const result = await makeTerms ( "taxonomy_testing" ) . includeFallback ( ) . find < TTerm > ( ) ;
49+ expect ( result ) . toBeDefined ( ) ;
50+ } ) ;
51+
52+ it ( "should return localized terms with fallback when locale() and includeFallback() are chained" , async ( ) => {
53+ const result = await makeTerms ( "taxonomy_testing" ) . locale ( "hi-in" ) . includeFallback ( ) . find < TTerm > ( ) ;
54+ expect ( result ) . toBeDefined ( ) ;
55+ } ) ;
4156} ) ;
57+
4258function makeTerms ( taxonomyUid = "" ) : TermQuery {
4359 const terms = stack . taxonomy ( taxonomyUid ) . term ( ) ;
44-
4560 return terms ;
4661}
62+
63+ describe ( "Term Query API test cases - gadgets taxonomy" , ( ) => {
64+ // Case 1: locale=en-us, include_fallback=false
65+ // Returns 5 terms: tablet, laptop, smartwatch, smartphone, headphone — all en-us
66+ it ( "should return all 5 en-us terms when locale is en-us and includeFallback is not set" , async ( ) => {
67+ const result = await stack . taxonomy ( "gadgets" ) . term ( ) . locale ( "en-us" ) . find < TTerm > ( ) ;
68+ expect ( result ) . toBeDefined ( ) ;
69+ expect ( result . terms ) . toBeDefined ( ) ;
70+ expect ( result . terms . length ) . toBe ( 5 ) ;
71+ const byUid = Object . fromEntries ( result . terms . map ( ( t : any ) => [ t . uid , t ] ) ) ;
72+ expect ( byUid [ "tablet" ] . name ) . toBe ( "Tablet" ) ;
73+ expect ( byUid [ "laptop" ] . name ) . toBe ( "Laptop" ) ;
74+ expect ( byUid [ "smartwatch" ] . name ) . toBe ( "Smartwatch" ) ;
75+ expect ( byUid [ "smartphone" ] . name ) . toBe ( "Smartphone" ) ;
76+ expect ( byUid [ "headphone" ] . name ) . toBe ( "Headphone" ) ;
77+ result . terms . forEach ( ( t : any ) => expect ( t . locale ) . toBe ( "en-us" ) ) ;
78+ } ) ;
79+
80+ // Case 2: locale=en-us, include_fallback=true
81+ // Returns same 5 terms — all en-us (no change since en-us is master)
82+ it ( "should return all 5 en-us terms when locale is en-us and includeFallback is true" , async ( ) => {
83+ const result = await stack . taxonomy ( "gadgets" ) . term ( ) . locale ( "en-us" ) . includeFallback ( ) . find < TTerm > ( ) ;
84+ expect ( result ) . toBeDefined ( ) ;
85+ expect ( result . terms ) . toBeDefined ( ) ;
86+ expect ( result . terms . length ) . toBe ( 5 ) ;
87+ const byUid = Object . fromEntries ( result . terms . map ( ( t : any ) => [ t . uid , t ] ) ) ;
88+ expect ( byUid [ "tablet" ] . name ) . toBe ( "Tablet" ) ;
89+ expect ( byUid [ "laptop" ] . name ) . toBe ( "Laptop" ) ;
90+ expect ( byUid [ "smartwatch" ] . name ) . toBe ( "Smartwatch" ) ;
91+ expect ( byUid [ "smartphone" ] . name ) . toBe ( "Smartphone" ) ;
92+ expect ( byUid [ "headphone" ] . name ) . toBe ( "Headphone" ) ;
93+ result . terms . forEach ( ( t : any ) => expect ( t . locale ) . toBe ( "en-us" ) ) ;
94+ } ) ;
95+
96+ // Case 3: locale=fr-fr, include_fallback=false
97+ // Returns 3 fr-fr terms only — tablet and laptop have no fr-fr translation so they are excluded
98+ it ( "should return only 3 fr-fr localized terms when locale is fr-fr and includeFallback is false" , async ( ) => {
99+ const result = await stack . taxonomy ( "gadgets" ) . term ( ) . locale ( "fr-fr" ) . find < TTerm > ( ) ;
100+ expect ( result ) . toBeDefined ( ) ;
101+ expect ( result . terms ) . toBeDefined ( ) ;
102+ expect ( result . terms . length ) . toBe ( 3 ) ;
103+ const byUid = Object . fromEntries ( result . terms . map ( ( t : any ) => [ t . uid , t ] ) ) ;
104+ expect ( byUid [ "headphone" ] . name ) . toBe ( "Headphone-fr" ) ;
105+ expect ( byUid [ "smartphone" ] . name ) . toBe ( "Smartphone-fr" ) ;
106+ expect ( byUid [ "smartwatch" ] . name ) . toBe ( "Smartwatch-fr" ) ;
107+ expect ( byUid [ "tablet" ] ) . toBeUndefined ( ) ;
108+ expect ( byUid [ "laptop" ] ) . toBeUndefined ( ) ;
109+ result . terms . forEach ( ( t : any ) => expect ( t . locale ) . toBe ( "fr-fr" ) ) ;
110+ } ) ;
111+
112+ // Case 4: locale=fr-fr, include_fallback=true
113+ // Returns 5 terms: 3 in fr-fr + 2 fallback to en-us (tablet, laptop)
114+ it ( "should return 5 terms with fr-fr terms and en-us fallback when locale is fr-fr and includeFallback is true" , async ( ) => {
115+ const result = await stack . taxonomy ( "gadgets" ) . term ( ) . locale ( "fr-fr" ) . includeFallback ( ) . find < TTerm > ( ) ;
116+ expect ( result ) . toBeDefined ( ) ;
117+ expect ( result . terms ) . toBeDefined ( ) ;
118+ expect ( result . terms . length ) . toBe ( 5 ) ;
119+ const byUid = Object . fromEntries ( result . terms . map ( ( t : any ) => [ t . uid , t ] ) ) ;
120+ expect ( byUid [ "headphone" ] . name ) . toBe ( "Headphone-fr" ) ;
121+ expect ( byUid [ "headphone" ] . locale ) . toBe ( "fr-fr" ) ;
122+ expect ( byUid [ "smartphone" ] . name ) . toBe ( "Smartphone-fr" ) ;
123+ expect ( byUid [ "smartphone" ] . locale ) . toBe ( "fr-fr" ) ;
124+ expect ( byUid [ "smartwatch" ] . name ) . toBe ( "Smartwatch-fr" ) ;
125+ expect ( byUid [ "smartwatch" ] . locale ) . toBe ( "fr-fr" ) ;
126+ expect ( byUid [ "tablet" ] . name ) . toBe ( "Tablet" ) ;
127+ expect ( byUid [ "tablet" ] . locale ) . toBe ( "en-us" ) ;
128+ expect ( byUid [ "laptop" ] . name ) . toBe ( "Laptop" ) ;
129+ expect ( byUid [ "laptop" ] . locale ) . toBe ( "en-us" ) ;
130+ } ) ;
131+ } ) ;
0 commit comments