diff --git a/src/apps/urls.test.ts b/src/apps/urls.test.ts index c8e0a2e17..c63fb24d7 100644 --- a/src/apps/urls.test.ts +++ b/src/apps/urls.test.ts @@ -1,31 +1,47 @@ import { resolveAppIframeUrl } from "@saleor/apps/urls"; - -jest.mock("@saleor/config", () => ({ - ...jest.requireActual("@saleor/config"), - getApiUrl: () => "https://shop.saleor.cloud/graphql/", -})); +import * as config from "@saleor/config"; describe("resolveAppIframeUrl", () => { afterAll(() => { jest.clearAllMocks(); }); - it.each<[string, string, Record, string]>([ - [ - "XyZ123", - "https://my-app.vercel.app", - { param1: "param1" }, - "https://my-app.vercel.app?domain=shop.saleor.cloud&saleorApiUrl=https%3A%2F%2Fshop.saleor.cloud%2Fgraphql%2F&id=XyZ123¶m1=param1", - ], - [ - "AbC987", - "https://my-app.vercel.app/configuration", - { param1: "param1", param2: "param2" }, - "https://my-app.vercel.app/configuration?domain=shop.saleor.cloud&saleorApiUrl=https%3A%2F%2Fshop.saleor.cloud%2Fgraphql%2F&id=AbC987¶m1=param1¶m2=param2", - ], - ])("Generates valid URL from segments", (id, appUrl, params, expectedUrl) => { - const result = resolveAppIframeUrl(id, appUrl, params); + describe("For full URL provided in env", () => { + beforeEach(() => { + jest + .spyOn(config, "getApiUrl") + .mockImplementation(() => "https://shop.saleor.cloud/graphql/"); + }); - expect(result).toEqual(expectedUrl); + it.each<[string, string, Record, string]>([ + [ + "XyZ123", + "https://my-app.vercel.app", + { param1: "param1" }, + "https://my-app.vercel.app?domain=shop.saleor.cloud&saleorApiUrl=https%3A%2F%2Fshop.saleor.cloud%2Fgraphql%2F&id=XyZ123¶m1=param1", + ], + [ + "AbC987", + "https://my-app.vercel.app/configuration", + { param1: "param1", param2: "param2" }, + "https://my-app.vercel.app/configuration?domain=shop.saleor.cloud&saleorApiUrl=https%3A%2F%2Fshop.saleor.cloud%2Fgraphql%2F&id=AbC987¶m1=param1¶m2=param2", + ], + ])( + "Generates valid URL from segments", + (id, appUrl, params, expectedUrl) => { + const result = resolveAppIframeUrl(id, appUrl, params); + + expect(result).toEqual(expectedUrl); + }, + ); }); + + /** + * Test this scenario for cloud deployments where origin is computed. + * + * Current jest is not set up for testing location/URL + */ + test.todo( + "Test if URL is valid when API_URL in env is absolute path like /graphql/", + ); }); diff --git a/src/apps/urls.ts b/src/apps/urls.ts index 19727e579..a6de69f84 100644 --- a/src/apps/urls.ts +++ b/src/apps/urls.ts @@ -123,8 +123,11 @@ export const resolveAppIframeUrl = ( appUrl: string, params: AppDetailsUrlQueryParams, ) => { - const apiUrl = getApiUrl(); - const apiUrlHost = new URL(getApiUrl()).hostname; + const apiUrl = new URL(getApiUrl(), window.location.origin).href; + /** + * Use host to preserve port, in case of multiple Saleors running on localhost + */ + const apiUrlHost = new URL(apiUrl).host; const iframeContextQueryString = `?${stringifyQs( {