@@ -12,6 +12,16 @@ import {
1212} from "../utils/constant" ;
1313import { httpClient , AxiosInstance } from "@contentstack/core" ;
1414import MockAdapter from "axios-mock-adapter" ;
15+ import * as Utils from "@contentstack/utils" ;
16+
17+ // Mock getContentstackEndpoint from @contentstack /utils
18+ jest . mock ( "@contentstack/utils" , ( ) => ( {
19+ getContentstackEndpoint : jest . fn ( ) ,
20+ } ) ) ;
21+
22+ const mockGetContentstackEndpoint = Utils . getContentstackEndpoint as jest . MockedFunction <
23+ typeof Utils . getContentstackEndpoint
24+ > ;
1525
1626let client : AxiosInstance ;
1727let mockClient : MockAdapter ;
@@ -23,35 +33,185 @@ beforeAll(() => {
2333
2434describe ( "Utils functions" , ( ) => {
2535 describe ( "getHostforRegion function" , ( ) => {
26- it ( "should return custom host when provided" , ( ) => {
36+ beforeEach ( ( ) => {
37+ // Reset mock before each test
38+ mockGetContentstackEndpoint . mockReset ( ) ;
39+ } ) ;
40+
41+ it ( "should return custom host when provided and not call getContentstackEndpoint" , ( ) => {
2742 const customHost = "custom.example.com" ;
2843 const result = getHostforRegion ( Region . EU , customHost ) ;
44+
2945 expect ( result ) . toBe ( customHost ) ;
46+ expect ( mockGetContentstackEndpoint ) . not . toHaveBeenCalled ( ) ;
3047 } ) ;
3148
32- it ( "should return default host when no region is provided" , ( ) => {
33- const result = getHostforRegion ( ) ;
49+ it ( "should call getContentstackEndpoint with correct parameters when no host is provided" , ( ) => {
50+ mockGetContentstackEndpoint . mockReturnValue ( HOST_URL ) ;
51+
52+ const result = getHostforRegion ( Region . US ) ;
53+
54+ expect ( mockGetContentstackEndpoint ) . toHaveBeenCalledWith (
55+ Region . US ,
56+ "contentDelivery" ,
57+ true
58+ ) ;
3459 expect ( result ) . toBe ( HOST_URL ) ;
3560 } ) ;
3661
37- it ( "should return correct host for each region using Region enum" , ( ) => {
38- // Test using Region enum values
39- expect ( getHostforRegion ( Region . EU ) ) . toBe ( HOST_EU_REGION ) ;
40- expect ( getHostforRegion ( Region . AU ) ) . toBe ( HOST_AU_REGION ) ;
41- expect ( getHostforRegion ( Region . AZURE_NA ) ) . toBe ( HOST_AZURE_NA_REGION ) ;
42- expect ( getHostforRegion ( Region . AZURE_EU ) ) . toBe ( "azure-eu-cdn.contentstack.com" ) ;
43- expect ( getHostforRegion ( Region . GCP_NA ) ) . toBe ( HOST_GCP_NA_REGION ) ;
44- expect ( getHostforRegion ( Region . GCP_EU ) ) . toBe ( HOST_GCP_EU_REGION ) ;
62+
63+ it ( "should return correct host for EU region using getContentstackEndpoint" , ( ) => {
64+ mockGetContentstackEndpoint . mockReturnValue ( HOST_EU_REGION ) ;
65+
66+ const result = getHostforRegion ( Region . EU ) ;
67+
68+ expect ( mockGetContentstackEndpoint ) . toHaveBeenCalledWith (
69+ Region . EU ,
70+ "contentDelivery" ,
71+ true
72+ ) ;
73+ expect ( result ) . toBe ( HOST_EU_REGION ) ;
74+ } ) ;
75+
76+ it ( "should return correct host for AU region using getContentstackEndpoint" , ( ) => {
77+ mockGetContentstackEndpoint . mockReturnValue ( HOST_AU_REGION ) ;
78+
79+ const result = getHostforRegion ( Region . AU ) ;
80+
81+ expect ( mockGetContentstackEndpoint ) . toHaveBeenCalledWith (
82+ Region . AU ,
83+ "contentDelivery" ,
84+ true
85+ ) ;
86+ expect ( result ) . toBe ( HOST_AU_REGION ) ;
4587 } ) ;
4688
47- it ( "should return default host for US region" , ( ) => {
48- expect ( getHostforRegion ( Region . US ) ) . toBe ( HOST_URL ) ;
89+ it ( "should return correct host for AZURE_NA region using getContentstackEndpoint" , ( ) => {
90+ mockGetContentstackEndpoint . mockReturnValue ( HOST_AZURE_NA_REGION ) ;
91+
92+ const result = getHostforRegion ( Region . AZURE_NA ) ;
93+
94+ expect ( mockGetContentstackEndpoint ) . toHaveBeenCalledWith (
95+ Region . AZURE_NA ,
96+ "contentDelivery" ,
97+ true
98+ ) ;
99+ expect ( result ) . toBe ( HOST_AZURE_NA_REGION ) ;
49100 } ) ;
50101
51- it ( "should prioritize custom host over region" , ( ) => {
102+ it ( "should return correct host for AZURE_EU region using getContentstackEndpoint" , ( ) => {
103+ const azureEuHost = "azure-eu-cdn.contentstack.com" ;
104+ mockGetContentstackEndpoint . mockReturnValue ( azureEuHost ) ;
105+
106+ const result = getHostforRegion ( Region . AZURE_EU ) ;
107+
108+ expect ( mockGetContentstackEndpoint ) . toHaveBeenCalledWith (
109+ Region . AZURE_EU ,
110+ "contentDelivery" ,
111+ true
112+ ) ;
113+ expect ( result ) . toBe ( azureEuHost ) ;
114+ } ) ;
115+
116+ it ( "should return correct host for GCP_NA region using getContentstackEndpoint" , ( ) => {
117+ mockGetContentstackEndpoint . mockReturnValue ( HOST_GCP_NA_REGION ) ;
118+
119+ const result = getHostforRegion ( Region . GCP_NA ) ;
120+
121+ expect ( mockGetContentstackEndpoint ) . toHaveBeenCalledWith (
122+ Region . GCP_NA ,
123+ "contentDelivery" ,
124+ true
125+ ) ;
126+ expect ( result ) . toBe ( HOST_GCP_NA_REGION ) ;
127+ } ) ;
128+
129+ it ( "should return correct host for GCP_EU region using getContentstackEndpoint" , ( ) => {
130+ mockGetContentstackEndpoint . mockReturnValue ( HOST_GCP_EU_REGION ) ;
131+
132+ const result = getHostforRegion ( Region . GCP_EU ) ;
133+
134+ expect ( mockGetContentstackEndpoint ) . toHaveBeenCalledWith (
135+ Region . GCP_EU ,
136+ "contentDelivery" ,
137+ true
138+ ) ;
139+ expect ( result ) . toBe ( HOST_GCP_EU_REGION ) ;
140+ } ) ;
141+
142+ it ( "should return correct host for US region using getContentstackEndpoint" , ( ) => {
143+ mockGetContentstackEndpoint . mockReturnValue ( HOST_URL ) ;
144+
145+ const result = getHostforRegion ( Region . US ) ;
146+
147+ expect ( mockGetContentstackEndpoint ) . toHaveBeenCalledWith (
148+ Region . US ,
149+ "contentDelivery" ,
150+ true
151+ ) ;
152+ expect ( result ) . toBe ( HOST_URL ) ;
153+ } ) ;
154+
155+ it ( "should prioritize custom host over region and not call getContentstackEndpoint" , ( ) => {
52156 const customHost = "priority.example.com" ;
157+
53158 const result = getHostforRegion ( Region . EU , customHost ) ;
159+
54160 expect ( result ) . toBe ( customHost ) ;
161+ expect ( mockGetContentstackEndpoint ) . not . toHaveBeenCalled ( ) ;
162+ } ) ;
163+
164+ it ( "should handle string region values and call getContentstackEndpoint" , ( ) => {
165+ const stringRegion = "eu" ;
166+ mockGetContentstackEndpoint . mockReturnValue ( HOST_EU_REGION ) ;
167+
168+ const result = getHostforRegion ( stringRegion ) ;
169+
170+ expect ( mockGetContentstackEndpoint ) . toHaveBeenCalledWith (
171+ stringRegion ,
172+ "contentDelivery" ,
173+ true
174+ ) ;
175+ expect ( result ) . toBe ( HOST_EU_REGION ) ;
176+ } ) ;
177+
178+ it ( "should handle empty string host and call getContentstackEndpoint" , ( ) => {
179+ mockGetContentstackEndpoint . mockReturnValue ( HOST_URL ) ;
180+
181+ const result = getHostforRegion ( Region . US , "" ) ;
182+
183+ // Empty string is falsy, so it should call getContentstackEndpoint
184+ expect ( mockGetContentstackEndpoint ) . toHaveBeenCalledWith (
185+ Region . US ,
186+ "contentDelivery" ,
187+ true
188+ ) ;
189+ expect ( result ) . toBe ( HOST_URL ) ;
190+ } ) ;
191+
192+ it ( "should always pass 'contentDelivery' as service parameter" , ( ) => {
193+ mockGetContentstackEndpoint . mockReturnValue ( HOST_URL ) ;
194+
195+ getHostforRegion ( Region . US ) ;
196+ getHostforRegion ( Region . EU ) ;
197+ getHostforRegion ( Region . AU ) ;
198+
199+ expect ( mockGetContentstackEndpoint ) . toHaveBeenCalledTimes ( 3 ) ;
200+ mockGetContentstackEndpoint . mock . calls . forEach ( ( call ) => {
201+ expect ( call [ 1 ] ) . toBe ( "contentDelivery" ) ;
202+ } ) ;
203+ } ) ;
204+
205+ it ( "should always pass true as omitHttps parameter" , ( ) => {
206+ mockGetContentstackEndpoint . mockReturnValue ( HOST_URL ) ;
207+
208+ getHostforRegion ( Region . US ) ;
209+ getHostforRegion ( Region . EU ) ;
210+
211+ expect ( mockGetContentstackEndpoint ) . toHaveBeenCalledTimes ( 2 ) ;
212+ mockGetContentstackEndpoint . mock . calls . forEach ( ( call ) => {
213+ expect ( call [ 2 ] ) . toBe ( true ) ;
214+ } ) ;
55215 } ) ;
56216 } ) ;
57217
0 commit comments