@@ -8,7 +8,7 @@ import nock from 'nock';
88import { Response } from 'node-fetch' ;
99import * as environment from './environment' ;
1010import ClientConfig from '../clientConfig' ;
11- import { doFetch } from './fetchHelper' ;
11+ import { doFetch , transferParams , ParameterKey } from './fetchHelper' ;
1212
1313describe ( 'doFetch' , ( ) => {
1414 const basePath = 'https://short_code.api.commercecloud.salesforce.com' ;
@@ -139,3 +139,121 @@ describe('doFetch', () => {
139139 ) ;
140140 } ) ;
141141} ) ;
142+
143+ describe ( 'transferParams' , ( ) => {
144+ const optionParams = {
145+ // organizationId and siteId are not defined here
146+ id : 'id' ,
147+ inventoryIds : 'inventoryIds' ,
148+ expand : [ 'expand1' , 'expand2' ] ,
149+ allImages : true ,
150+ perPricebook : true ,
151+ select : 'select' ,
152+ currency : 'currency' ,
153+ locale : 'locale' ,
154+ } ;
155+
156+ const configParameters = {
157+ shortCode : 'short_code' ,
158+ organizationId : 'organization_id' ,
159+ clientId : 'client_id' ,
160+ siteId : 'site_id' ,
161+ } ;
162+
163+ const getProductRequired = [ 'organizationId' , 'id' ] ;
164+
165+ const currentParamKeys : Array < ParameterKey > = [
166+ {
167+ parameterName : 'organizationId' ,
168+ isQueryParameter : false ,
169+ } ,
170+ {
171+ parameterName : 'id' ,
172+ isQueryParameter : false ,
173+ } ,
174+ {
175+ parameterName : 'inventoryIds' ,
176+ isQueryParameter : true ,
177+ } ,
178+ {
179+ parameterName : 'expand' ,
180+ isQueryParameter : true ,
181+ } ,
182+ {
183+ parameterName : 'allImages' ,
184+ isQueryParameter : true ,
185+ } ,
186+ {
187+ parameterName : 'perPricebook' ,
188+ isQueryParameter : true ,
189+ } ,
190+ {
191+ parameterName : 'select' ,
192+ isQueryParameter : true ,
193+ } ,
194+ {
195+ parameterName : 'currency' ,
196+ isQueryParameter : true ,
197+ } ,
198+ {
199+ parameterName : 'locale' ,
200+ isQueryParameter : true ,
201+ } ,
202+ {
203+ parameterName : 'siteId' ,
204+ isQueryParameter : true ,
205+ } ,
206+ ] ;
207+
208+ test ( 'transferParams successfully populates the query and path parameters object' , ( ) => {
209+ const queryParameters = { } ;
210+ const pathParameters = { } ;
211+
212+ transferParams ( {
213+ optionParams,
214+ configParams : configParameters ,
215+ queryParams : queryParameters ,
216+ pathParams : pathParameters ,
217+ paramKeys : currentParamKeys ,
218+ requiredParamKeys : getProductRequired ,
219+ } ) ;
220+
221+ const expectedQueryParameters = {
222+ allImages : true ,
223+ currency : 'currency' ,
224+ expand : [ 'expand1' , 'expand2' ] ,
225+ inventoryIds : 'inventoryIds' ,
226+ locale : 'locale' ,
227+ perPricebook : true ,
228+ siteId : 'site_id' ,
229+ select : 'select' ,
230+ } ;
231+
232+ const expectedPathParameters = {
233+ id : 'id' ,
234+ organizationId : 'organization_id' ,
235+ } ;
236+
237+ expect ( queryParameters ) . toEqual ( expectedQueryParameters ) ;
238+ expect ( pathParameters ) . toEqual ( expectedPathParameters ) ;
239+ } ) ;
240+
241+ test ( 'transferParams throws error when required parameter is not passed' , ( ) => {
242+ const queryParameters = { } ;
243+ const pathParameters = { } ;
244+
245+ // remove id from optionParams
246+ const { id, ...newOptionParams } = optionParams ;
247+
248+ expect ( ( ) =>
249+ transferParams ( {
250+ optionParams : newOptionParams ,
251+ configParams : configParameters ,
252+ queryParams : queryParameters ,
253+ pathParams : pathParameters ,
254+ paramKeys : currentParamKeys ,
255+ requiredParamKeys : getProductRequired ,
256+ } )
257+ ) . toThrowError ( 'Missing required parameter: id' ) ;
258+ } ) ;
259+ } ) ;
0 commit comments