From 862b8d3b9e5b4a5c39e0a78dcd4ef3d850f78608 Mon Sep 17 00:00:00 2001 From: Joel Uong Date: Fri, 10 May 2024 10:39:03 -0400 Subject: [PATCH] update tests --- src/lib/utils.test.ts | 1 + src/static/helperTemplates/index.ts.hbs | 3 +- .../helperTemplates/shopperCustomer.ts.hbs | 40 ------------------- src/test/parameters.test.ts | 27 +++++++------ 4 files changed, 17 insertions(+), 54 deletions(-) delete mode 100644 src/static/helperTemplates/shopperCustomer.ts.hbs diff --git a/src/lib/utils.test.ts b/src/lib/utils.test.ts index 53fd7920..5a83a695 100644 --- a/src/lib/utils.test.ts +++ b/src/lib/utils.test.ts @@ -76,6 +76,7 @@ describe("setupApis", () => { "pricing", "product", "search", + "seller", ]); }); }); diff --git a/src/static/helperTemplates/index.ts.hbs b/src/static/helperTemplates/index.ts.hbs index 67b07730..d9942ab8 100644 --- a/src/static/helperTemplates/index.ts.hbs +++ b/src/static/helperTemplates/index.ts.hbs @@ -6,4 +6,5 @@ */ export * as slasHelpers from "./slas"; -export * as helpers from "./shopperCustomer"; +export const helpers = {} +{{!-- ^ TODO: fix this once custom API PR gets merged in --}} diff --git a/src/static/helperTemplates/shopperCustomer.ts.hbs b/src/static/helperTemplates/shopperCustomer.ts.hbs deleted file mode 100644 index f41c9246..00000000 --- a/src/static/helperTemplates/shopperCustomer.ts.hbs +++ /dev/null @@ -1,40 +0,0 @@ -import { ShopperToken, stripBearer, ResponseError, getObjectFromResponse } from "@commerce-apps/core" -import * as sdk from "../" - -// Type of the first input parameter for the auth function -type AuthFuncParamType = Parameters[0]; -// Type returned by the auth function -type CustomerType = sdk.{{metadata.shopperAuthClient}}.{{metadata.shopperAuthDataType}}; - -/** - * This wraps the parameters for the authorization call to retrieve a token. - * That call can be made directly, this is here just for convenience - * ```ts - * const clientConfig: ClientConfig = { - * parameters: { - * clientId: "XXXXXX", - * organizationId: "XXXX", - * shortCode: "XXX", - * siteId: "XX" - * } - * }; - * helpers.getShopperToken(clientConfig, { type: "guest" }) - * ``` - * @param clientConfig - Client configuration properties - * @param body - Post body required for authorization - * @returns the retrieved Shopper token - */ -export async function getShopperToken ( - clientConfig: sdk.ClientConfig, - body: AuthFuncParamType["body"] -): Promise>{ - let client = new sdk.{{metadata.shopperAuthClient}}(clientConfig); - - const response = await client.{{metadata.shopperAuthApi}}({body}, true); - if (!response.ok) { - throw new ResponseError(response); - } - const dto: CustomerType = await getObjectFromResponse(response); - - return new ShopperToken(dto, stripBearer(response.headers.get("Authorization"))); -} diff --git a/src/test/parameters.test.ts b/src/test/parameters.test.ts index 550b93b0..a13abc3e 100644 --- a/src/test/parameters.test.ts +++ b/src/test/parameters.test.ts @@ -8,7 +8,7 @@ import nock from "nock"; import { expect } from "chai"; import sinon from "sinon"; -import { ShopperCustomers } from "../../renderedTemplates/customer/shopperCustomers/shopperCustomers"; +import { ShopperProducts } from "../../renderedTemplates/product/shopperProducts/shopperProducts"; const SITE_ID = "SITE_ID"; const CLIENT_ID = "CLIENT_ID"; @@ -16,11 +16,12 @@ const SHORT_CODE = "SHORT_CODE"; const ORGANIZATION_ID = "ORGANIZATION_ID"; const MOCK_RESPONSE = { mockResponse: true }; + describe("Parameters", () => { afterEach(() => nock.cleanAll()); it("allow custom query params", async () => { - const customersClient = new ShopperCustomers({ + const productClient = new ShopperProducts({ parameters: { clientId: CLIENT_ID, organizationId: ORGANIZATION_ID, @@ -31,28 +32,28 @@ describe("Parameters", () => { const options = { parameters: { + ids: "ids", c_validCustomParam: "custom_param", }, - body: { type: "guest" }, }; nock(`https://${SHORT_CODE}.api.commercecloud.salesforce.com`) - .post( - `/customer/shopper-customers/v1/organizations/${ORGANIZATION_ID}/customers/actions/login` + .get( + `/product/shopper-products/v1/organizations/${ORGANIZATION_ID}/products` ) .query({ siteId: SITE_ID, - clientId: CLIENT_ID, + ids: "ids", c_validCustomParam: "custom_param", }) .reply(200, MOCK_RESPONSE); - const response = await customersClient.authorizeCustomer(options); + const response = await productClient.getProducts(options); expect(response).to.be.deep.equal(MOCK_RESPONSE); }); it("warns user when invalid param is passed", async () => { - const customersClient = new ShopperCustomers({ + const productClient = new ShopperProducts({ parameters: { clientId: CLIENT_ID, organizationId: ORGANIZATION_ID, @@ -63,23 +64,23 @@ describe("Parameters", () => { const options = { parameters: { + ids: "ids", invalidQueryParam: "invalid_param", }, - body: { type: "guest" }, }; nock(`https://${SHORT_CODE}.api.commercecloud.salesforce.com`) - .post( - `/customer/shopper-customers/v1/organizations/${ORGANIZATION_ID}/customers/actions/login` + .get( + `/product/shopper-products/v1/organizations/${ORGANIZATION_ID}/products` ) .query({ siteId: SITE_ID, - clientId: CLIENT_ID, + ids: "ids", }) .reply(200, MOCK_RESPONSE); const warnSpy = sinon.spy(console, "warn"); - const response = await customersClient.authorizeCustomer(options); + const response = await productClient.getProducts(options); expect(response).to.be.deep.equal(MOCK_RESPONSE); expect(warnSpy.calledWith("Invalid Parameter: invalidQueryParam")).to.be