diff --git a/.changeset/chilly-houses-rule.md b/.changeset/chilly-houses-rule.md new file mode 100644 index 0000000..f2cdbde --- /dev/null +++ b/.changeset/chilly-houses-rule.md @@ -0,0 +1,5 @@ +--- +"@stackla/widget-utils": patch +--- + +Add support for versioning diff --git a/src/embed/embed.spec.ts b/src/embed/embed.spec.ts index e912e39..6c52c8c 100644 --- a/src/embed/embed.spec.ts +++ b/src/embed/embed.spec.ts @@ -1,5 +1,5 @@ /* eslint-disable no-var */ -import { embed } from "." +import { embed, JSONSchema } from "." import fetchMock from "jest-fetch-mock" import { getWidgetV2EmbedCode } from "./v2" import { getWidgetV3EmbedCode } from "./v3" @@ -7,8 +7,18 @@ import { generateDataHTMLStringByParams } from "./embed.params" fetchMock.enableMocks() -const REQUEST_URL = "https://widget-data.stackla.com/widgets/123/version" -const STAGING_REQUEST_URL = "https://widget-data.teaser.stackla.com/widgets/123/version" +const REQUEST_URL = "https://widget-data.stackla.com/widgets/123/version/" +const STAGING_REQUEST_URL = "https://widget-data.teaser.stackla.com/widgets/123/version/" + +const V2Request: JSONSchema = { + widgetVersion: 2, + scriptVersion: 1 +} + +const V3Request: JSONSchema = { + widgetVersion: 3, + scriptVersion: 1 +} describe("load embed code", () => { beforeEach(() => { @@ -17,7 +27,7 @@ describe("load embed code", () => { it("should return the correct embed code for v2", async () => { fetchMock.mockIf(REQUEST_URL, async () => { - return JSON.stringify({ version: 2 }) + return JSON.stringify(V2Request) }) const createdDiv = document.createElement("div") @@ -37,7 +47,7 @@ describe("load embed code", () => { it("should test staging for v2", async () => { fetchMock.mockIf(STAGING_REQUEST_URL, async () => { - return JSON.stringify({ version: 2 }) + return JSON.stringify(V2Request) }) const createdDiv = document.createElement("div") @@ -56,7 +66,7 @@ describe("load embed code", () => { it("should test production for v2", async () => { fetchMock.mockIf(REQUEST_URL, async () => { - return JSON.stringify({ version: 2 }) + return JSON.stringify(V2Request) }) const createdDiv = document.createElement("div") @@ -75,7 +85,7 @@ describe("load embed code", () => { it("should return the correct embed code for v3", async () => { fetchMock.mockIf(REQUEST_URL, async () => { - return JSON.stringify({ version: 3 }) + return JSON.stringify(V3Request) }) const createdDiv = document.createElement("div") @@ -114,24 +124,6 @@ describe("load embed code", () => { } }) - it("should skip the fetch call if the version is provided", async () => { - const createdDiv = document.createElement("div") - await embed({ - widgetId: "123", - root: createdDiv, - version: 3, - dataProperties: { - foo: "bar", - baz: 123 - }, - environment: "production" - }) - - expect(fetchMock).not.toHaveBeenCalled() - expect(createdDiv.innerHTML).toContain(getWidgetV3EmbedCode({ foo: "bar", baz: 123, wid: "123" })) - expect(createdDiv.innerHTML).toContain('
') - }) - it("should test param string method", async () => { const params = generateDataHTMLStringByParams({ foo: "bar", baz: 123, wid: "123" }) @@ -139,11 +131,14 @@ describe("load embed code", () => { }) it("should deal with malicious payloads", async () => { + fetchMock.mockIf(REQUEST_URL, async () => { + return JSON.stringify(V3Request) + }) + const createdDiv = document.createElement("div") await embed({ widgetId: "123", root: createdDiv, - version: 3, dataProperties: { foo: "bar", baz: 123, @@ -152,7 +147,6 @@ describe("load embed code", () => { environment: "production" }) - expect(fetchMock).not.toHaveBeenCalled() expect(createdDiv.innerHTML).toContain( `` ) diff --git a/src/embed/index.ts b/src/embed/index.ts index ddb7c3f..aa41423 100644 --- a/src/embed/index.ts +++ b/src/embed/index.ts @@ -8,18 +8,17 @@ import { getWidgetV2EmbedCode, invokeV2Javascript } from "./v2" import { getWidgetV3EmbedCode, invokeV3Javascript } from "./v3" export type Environment = "staging" | "production" -type Generation = 2 | 3 interface EmbedOptions