Skip to content

Commit

Permalink
feat(lib): 🎨 Ask for URL instead of publishId
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Mar 14, 2022
1 parent d6b9413 commit 5a2df9f
Show file tree
Hide file tree
Showing 10 changed files with 378 additions and 369 deletions.
2 changes: 1 addition & 1 deletion packages/typebot-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typebot-js",
"version": "2.1.4",
"version": "2.2.0",
"main": "dist/index.js",
"unpkg": "dist/index.umd.min.js",
"license": "MIT",
Expand Down
12 changes: 5 additions & 7 deletions packages/typebot-js/src/iframe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ import './style.css'

export const createIframe = ({
backgroundColor,
viewerHost = 'https://typebot-viewer.vercel.app',
isV1,
url,
...iframeParams
}: IframeParams): HTMLIFrameElement => {
const { publishId, loadWhenVisible, hiddenVariables } = iframeParams
const host = isV1 ? `https://bot.typebot.io` : viewerHost
const iframeUrl = `${host}/${publishId}${parseQueryParams(hiddenVariables)}`
const { loadWhenVisible, hiddenVariables } = iframeParams
const iframeUrl = `${url}${parseQueryParams(hiddenVariables)}`
const iframe = document.createElement('iframe')
iframe.setAttribute(loadWhenVisible ? 'data-src' : 'src', iframeUrl)
iframe.setAttribute('data-id', iframeParams.publishId)
iframe.setAttribute('data-id', url)
const randomThreeLettersId = Math.random().toString(36).substring(7)
const uniqueId = `${publishId}-${randomThreeLettersId}`
const uniqueId = `${url}-${randomThreeLettersId}`
iframe.setAttribute('id', uniqueId)
if (backgroundColor) iframe.style.backgroundColor = backgroundColor
iframe.classList.add('typebot-iframe')
Expand Down
4 changes: 1 addition & 3 deletions packages/typebot-js/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export type IframeParams = {
publishId: string
isV1?: boolean
viewerHost?: string
url: string
backgroundColor?: string
hiddenVariables?: { [key: string]: string | undefined }
customDomain?: string
Expand Down
58 changes: 29 additions & 29 deletions packages/typebot-js/tests/chat/button.spec.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import * as Typebot from "../../src";
import * as Typebot from '../../src'

beforeEach(() => {
document.body.innerHTML = "";
});
document.body.innerHTML = ''
})

it("should have the corresponding custom color", () => {
expect.assertions(1);
it('should have the corresponding custom color', () => {
expect.assertions(1)
Typebot.initBubble({
button: { color: "#222222" },
publishId: "typebot-id",
});
button: { color: '#222222' },
url: 'https://typebot.io/typebot-id',
})
const buttonElement = document.querySelector(
"#typebot-bubble > button"
) as HTMLElement;
expect(buttonElement.style.backgroundColor).toBe("rgb(34, 34, 34)");
});
'#typebot-bubble > button'
) as HTMLElement
expect(buttonElement.style.backgroundColor).toBe('rgb(34, 34, 34)')
})

it("should have the default svg icon", () => {
expect.assertions(1);
it('should have the default svg icon', () => {
expect.assertions(1)
Typebot.initBubble({
publishId: "typebot-id",
});
url: 'https://typebot.io/typebot-id',
})
const buttonIconElement = document.querySelector(
"#typebot-bubble > button > .icon"
) as HTMLElement;
expect(buttonIconElement.tagName).toBe("svg");
});
'#typebot-bubble > button > .icon'
) as HTMLElement
expect(buttonIconElement.tagName).toBe('svg')
})

it("should have the corresponding custom icon", () => {
expect.assertions(1);
it('should have the corresponding custom icon', () => {
expect.assertions(1)
Typebot.initBubble({
button: { iconUrl: "https://web.com/icon.png" },
publishId: "typebot-id",
});
button: { iconUrl: 'https://web.com/icon.png' },
url: 'https://typebot.io/typebot-id',
})
const buttonIconElement = document.querySelector(
"#typebot-bubble > button > .icon"
) as HTMLImageElement;
expect(buttonIconElement.src).toBe("https://web.com/icon.png");
});
'#typebot-bubble > button > .icon'
) as HTMLImageElement
expect(buttonIconElement.src).toBe('https://web.com/icon.png')
})
138 changes: 69 additions & 69 deletions packages/typebot-js/tests/chat/commands.spec.ts
Original file line number Diff line number Diff line change
@@ -1,91 +1,91 @@
import * as Typebot from "../../src";
import * as Typebot from '../../src'

beforeEach(() => {
document.body.innerHTML = "";
});
document.body.innerHTML = ''
})

describe("openBubble", () => {
it("should add the opened bubble", () => {
expect.assertions(3);
describe('openBubble', () => {
it('should add the opened bubble', () => {
expect.assertions(3)
const { open } = Typebot.initBubble({
publishId: "typebot-id",
});
const bubble = document.getElementById("typebot-bubble") as HTMLDivElement;
expect(bubble.classList.contains("iframe-opened")).toBe(false);
open();
expect(bubble.classList.contains("iframe-opened")).toBe(true);
expect(open).not.toThrow();
});
url: 'https://typebot.io/typebot-id',
})
const bubble = document.getElementById('typebot-bubble') as HTMLDivElement
expect(bubble.classList.contains('iframe-opened')).toBe(false)
open()
expect(bubble.classList.contains('iframe-opened')).toBe(true)
expect(open).not.toThrow()
})

it("should hide the proactive message", () => {
expect.assertions(2);
it('should hide the proactive message', () => {
expect.assertions(2)
const { open, openProactiveMessage } = Typebot.initBubble({
publishId: "typebot-id",
url: 'https://typebot.io/typebot-id',
proactiveMessage: {
textContent: "Hi click here!",
avatarUrl: "https://website.com/my-avatar.png",
textContent: 'Hi click here!',
avatarUrl: 'https://website.com/my-avatar.png',
},
});
const bubble = document.getElementById("typebot-bubble") as HTMLDivElement;
if (openProactiveMessage) openProactiveMessage();
expect(bubble.classList.contains("message-opened")).toBe(true);
open();
expect(bubble.classList.contains("message-opened")).toBe(false);
});
});
})
const bubble = document.getElementById('typebot-bubble') as HTMLDivElement
if (openProactiveMessage) openProactiveMessage()
expect(bubble.classList.contains('message-opened')).toBe(true)
open()
expect(bubble.classList.contains('message-opened')).toBe(false)
})
})

describe("closeBubble", () => {
it("should remove the corresponding class", () => {
expect.assertions(2);
describe('closeBubble', () => {
it('should remove the corresponding class', () => {
expect.assertions(2)
const { close, open } = Typebot.initBubble({
publishId: "typebot-id",
});
open();
const bubble = document.getElementById("typebot-bubble") as HTMLDivElement;
expect(bubble.classList.contains("iframe-opened")).toBe(true);
close();
expect(bubble.classList.contains("iframe-opened")).toBe(false);
});
});
url: 'https://typebot.io/typebot-id',
})
open()
const bubble = document.getElementById('typebot-bubble') as HTMLDivElement
expect(bubble.classList.contains('iframe-opened')).toBe(true)
close()
expect(bubble.classList.contains('iframe-opened')).toBe(false)
})
})

describe("openProactiveMessage", () => {
it("should add the opened className", () => {
expect.assertions(1);
describe('openProactiveMessage', () => {
it('should add the opened className', () => {
expect.assertions(1)
const { openProactiveMessage } = Typebot.initBubble({
proactiveMessage: {
textContent: "Hi click here!",
textContent: 'Hi click here!',
},
publishId: "typebot-id",
});
const bubble = document.getElementById("typebot-bubble") as HTMLDivElement;
if (openProactiveMessage) openProactiveMessage();
expect(bubble.classList.contains("message-opened")).toBe(true);
});
url: 'https://typebot.io/typebot-id',
})
const bubble = document.getElementById('typebot-bubble') as HTMLDivElement
if (openProactiveMessage) openProactiveMessage()
expect(bubble.classList.contains('message-opened')).toBe(true)
})

it("shouldn't be returned if no message", () => {
expect.assertions(1);
expect.assertions(1)
const { openProactiveMessage } = Typebot.initBubble({
publishId: "typebot-id",
});
expect(openProactiveMessage).toBeUndefined();
});
});
url: 'typebot-id',
})
expect(openProactiveMessage).toBeUndefined()
})
})

describe("Request commands afterwards", () => {
it("should return defined commands", () => {
describe('Request commands afterwards', () => {
it('should return defined commands', () => {
Typebot.initBubble({
proactiveMessage: {
textContent: "Hi click here!",
textContent: 'Hi click here!',
},
publishId: "typebot-id",
});
url: 'https://typebot.io/typebot-id',
})

const { close, open, openProactiveMessage } = Typebot.getBubbleActions();
expect(close).toBeDefined();
expect(open).toBeDefined();
expect(openProactiveMessage).toBeDefined();
open();
const bubble = document.getElementById("typebot-bubble") as HTMLDivElement;
expect(bubble.classList.contains("iframe-opened")).toBe(true);
});
});
const { close, open, openProactiveMessage } = Typebot.getBubbleActions()
expect(close).toBeDefined()
expect(open).toBeDefined()
expect(openProactiveMessage).toBeDefined()
open()
const bubble = document.getElementById('typebot-bubble') as HTMLDivElement
expect(bubble.classList.contains('iframe-opened')).toBe(true)
})
})
12 changes: 6 additions & 6 deletions packages/typebot-js/tests/chat/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('initBubble', () => {

it('should initialize a bubble embed', () => {
expect.assertions(2)
Typebot.initBubble({ publishId: 'typebot-id' })
Typebot.initBubble({ url: 'https://typebot.io/typebot-id' })
const bubbleElement = document.getElementById('typebot-bubble')
const frame = document.getElementsByTagName('iframe')[0]
expect(frame).toBeDefined()
Expand All @@ -17,22 +17,22 @@ describe('initBubble', () => {
it('should overwrite bubble if exists', () => {
expect.assertions(2)
Typebot.initBubble({
publishId: 'typebot-id',
url: 'https://typebot.io/typebot-id',
hiddenVariables: { var1: 'test' },
})
Typebot.initBubble({ publishId: 'typebot-id2' })
Typebot.initBubble({ url: 'https://typebot.io/typebot-id2' })
const frames = document.getElementsByTagName('iframe')
expect(frames).toHaveLength(1)
expect(frames[0].dataset.src).toBe(
'https://typebot-viewer.vercel.app/typebot-id2?hn=localhost'
'https://typebot.io/typebot-id2?hn=localhost'
)
})

it('show open after the corresponding delay', async () => {
expect.assertions(3)
Typebot.initBubble({
autoOpenDelay: 1000,
publishId: 'typebot-id',
url: 'https://typebot.io/typebot-id',
})
const bubble = document.querySelector('#typebot-bubble') as HTMLDivElement
expect(bubble.classList.contains('iframe-opened')).toBe(false)
Expand All @@ -49,7 +49,7 @@ describe('initBubble', () => {
localStorage.setItem(Typebot.localStorageKeys.rememberClose, 'true')
Typebot.initBubble({
autoOpenDelay: 1000,
publishId: 'typebot-id',
url: 'https://typebot.io/typebot-id',
})
const bubble = document.querySelector('#typebot-bubble') as HTMLDivElement
await new Promise((r) => setTimeout(r, 1500))
Expand Down
10 changes: 5 additions & 5 deletions packages/typebot-js/tests/chat/proactiveMessage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ it('should create the message', () => {
expect.assertions(2)
Typebot.initBubble({
proactiveMessage: { textContent: 'Hi click here!' },
publishId: 'typebot-id',
url: 'https://typebot.io/typebot-id',
})
const paragraphElement = document.querySelector(
'#typebot-bubble > .proactive-message > p'
Expand All @@ -27,7 +27,7 @@ it('should have the corresponding avatar', () => {
textContent: 'Hi click here!',
avatarUrl: 'https://website.com/my-avatar.png',
},
publishId: 'typebot-id',
url: 'https://typebot.io/typebot-id',
})
const avatarElement = document.querySelector(
'#typebot-bubble > .proactive-message > img'
Expand All @@ -41,7 +41,7 @@ it("shouldn't have opened class if delay not defined", () => {
proactiveMessage: {
textContent: 'Hi click here!',
},
publishId: 'typebot-id',
url: 'https://typebot.io/typebot-id',
})
const bubble = document.querySelector('#typebot-bubble') as HTMLDivElement
expect(bubble.classList.contains('message-opened')).toBe(false)
Expand All @@ -54,7 +54,7 @@ it('should show almost immediately if delay is 0', async () => {
textContent: 'Hi click here!',
delay: 0,
},
publishId: 'typebot-id',
url: 'https://typebot.io/typebot-id',
})
const bubble = document.querySelector('#typebot-bubble') as HTMLDivElement
await new Promise((r) => setTimeout(r, 1))
Expand All @@ -68,7 +68,7 @@ it('show after the corresponding delay', async () => {
textContent: 'Hi click here!',
delay: 1000,
},
publishId: 'typebot-id',
url: 'https://typebot.io/typebot-id',
})
const bubble = document.querySelector('#typebot-bubble') as HTMLDivElement
expect(bubble.classList.contains('message-opened')).toBe(false)
Expand Down
Loading