|
| 1 | +/* eslint-disable no-var */ |
| 2 | +import { embed } from "." |
| 3 | +import fetchMock from "jest-fetch-mock" |
| 4 | +import { getWidgetV2EmbedCode } from "./v2" |
| 5 | +import { getWidgetV3EmbedCode } from "./v3" |
| 6 | +import { generateDataHTMLStringByParams } from "./embed.params" |
| 7 | + |
| 8 | +fetchMock.enableMocks() |
| 9 | + |
| 10 | +const REQUEST_URL = "https://widget-data.stackla.com/123/version" |
| 11 | + |
| 12 | +describe("load embed code", () => { |
| 13 | + beforeEach(() => { |
| 14 | + fetchMock.resetMocks() |
| 15 | + }) |
| 16 | + |
| 17 | + it("should return the correct embed code for v2", async () => { |
| 18 | + fetchMock.mockIf(REQUEST_URL, async () => { |
| 19 | + return JSON.stringify({ version: "2" }) |
| 20 | + }) |
| 21 | + |
| 22 | + const createdDiv = document.createElement("div") |
| 23 | + await embed({ |
| 24 | + widgetId: "123", |
| 25 | + root: createdDiv, |
| 26 | + dataProperties: { |
| 27 | + foo: "bar", |
| 28 | + baz: 123 |
| 29 | + }, |
| 30 | + environment: "production" |
| 31 | + }) |
| 32 | + |
| 33 | + expect(createdDiv.innerHTML).toBe(getWidgetV2EmbedCode({ foo: "bar", baz: 123 }, "production")) |
| 34 | + }) |
| 35 | + |
| 36 | + it("should return the correct embed code for v3", async () => { |
| 37 | + fetchMock.mockIf(REQUEST_URL, async () => { |
| 38 | + return JSON.stringify({ version: "3" }) |
| 39 | + }) |
| 40 | + |
| 41 | + const createdDiv = document.createElement("div") |
| 42 | + await embed({ |
| 43 | + widgetId: "123", |
| 44 | + root: createdDiv, |
| 45 | + dataProperties: { |
| 46 | + foo: "bar", |
| 47 | + baz: 123 |
| 48 | + }, |
| 49 | + environment: "production" |
| 50 | + }) |
| 51 | + |
| 52 | + expect(createdDiv.innerHTML).toBe(getWidgetV3EmbedCode({ foo: "bar", baz: 123 }, "production")) |
| 53 | + }) |
| 54 | + |
| 55 | + it("should throw an error if the version is not supported", async () => { |
| 56 | + fetchMock.mockIf(REQUEST_URL, async () => { |
| 57 | + return JSON.stringify({ version: "4" }) |
| 58 | + }) |
| 59 | + |
| 60 | + const createdDiv = document.createElement("div") |
| 61 | + try { |
| 62 | + await embed({ |
| 63 | + widgetId: "123", |
| 64 | + root: createdDiv, |
| 65 | + dataProperties: { |
| 66 | + foo: "bar", |
| 67 | + baz: 123 |
| 68 | + }, |
| 69 | + environment: "production" |
| 70 | + }) |
| 71 | + } catch (error) { |
| 72 | + expect(error).toBe("Failed to embed widget. No widget code accessible with version 4") |
| 73 | + } |
| 74 | + }) |
| 75 | + |
| 76 | + it("should skip the fetch call if the version is provided", async () => { |
| 77 | + const createdDiv = document.createElement("div") |
| 78 | + await embed({ |
| 79 | + widgetId: "123", |
| 80 | + root: createdDiv, |
| 81 | + version: "3", |
| 82 | + dataProperties: { |
| 83 | + foo: "bar", |
| 84 | + baz: 123 |
| 85 | + }, |
| 86 | + environment: "production" |
| 87 | + }) |
| 88 | + |
| 89 | + expect(fetchMock).not.toHaveBeenCalled() |
| 90 | + expect(createdDiv.innerHTML).toBe(getWidgetV3EmbedCode({ foo: "bar", baz: 123 }, "production")) |
| 91 | + }) |
| 92 | + |
| 93 | + it("should test param string method", async () => { |
| 94 | + const params = generateDataHTMLStringByParams({ foo: "bar", baz: 123 }) |
| 95 | + |
| 96 | + expect(params).toBe(' data-foo="bar" data-baz="123"') |
| 97 | + }) |
| 98 | + |
| 99 | + it("should deal with malicious payloads", async () => { |
| 100 | + const createdDiv = document.createElement("div") |
| 101 | + await embed({ |
| 102 | + widgetId: "123", |
| 103 | + root: createdDiv, |
| 104 | + version: "3", |
| 105 | + dataProperties: { |
| 106 | + foo: "bar", |
| 107 | + baz: 123, |
| 108 | + '><img src="x" onerror="alert(1)">': '"><img src="x" onerror="alert(1)">' |
| 109 | + }, |
| 110 | + environment: "production" |
| 111 | + }) |
| 112 | + |
| 113 | + expect(fetchMock).not.toHaveBeenCalled() |
| 114 | + expect(createdDiv.innerHTML).toContain( |
| 115 | + `<div id="ugc-widget" data-foo="bar" data-baz="123" data-%3e%3cimg%20src%3d%22x%22%20onerror%3d%22alert(1)%22%3e="%22%3E%3Cimg%20src%3D%22x%22%20onerror%3D%22alert(1)%22%3E"></div>` |
| 116 | + ) |
| 117 | + }) |
| 118 | +}) |
0 commit comments