Skip to content

Commit

Permalink
FLEXIN-486 - Adding functionality to toggle the widget opening (#90)
Browse files Browse the repository at this point in the history
* FLEXIN-486 - Adding functionality to toggle the widget opening
  • Loading branch information
AnudeepChPaul authored Oct 13, 2023
1 parent 52c4ff2 commit 90e01cf
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ window.addEventListener("DOMContentLoaded", () => {
Twilio.initWebchat({
deploymentKey: urlParams.get("deploymentKey"),
region: urlParams.get("region"),
appStatus: urlParams.get("appStatus"),
theme: {
isLight: isLightTheme
}
Expand Down
32 changes: 27 additions & 5 deletions src/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { sessionDataHandler } from "../sessionDataHandler";
import { WebchatWidget } from "../components/WebchatWidget";
import { store } from "../store/store";
import * as initActions from "../store/actions/initActions";
import * as genericActions from "../store/actions/genericActions";

jest.mock("react-dom");

Expand All @@ -16,7 +17,7 @@ describe("Index", () => {
beforeAll(() => {
getLogger = jest.fn();
});

afterEach(() => {
jest.clearAllMocks();
});
Expand All @@ -28,7 +29,7 @@ describe("Index", () => {
const root = document.createElement("div");
root.id = "twilio-webchat-widget-root";
document.body.appendChild(root);
initWebchat({});
initWebchat({ deploymentKey: "CV000000" });

expect(renderSpy).toBeCalledWith(
<Provider store={store}>
Expand All @@ -42,15 +43,15 @@ describe("Index", () => {
const setEndpointSpy = jest.spyOn(sessionDataHandler, "setEndpoint");

const serverUrl = "serverUrl";
initWebchat({ serverUrl });
initWebchat({ serverUrl, deploymentKey: "CV000000" });

expect(setEndpointSpy).toBeCalledWith(serverUrl);
});

it("initializes config", () => {
const initConfigSpy = jest.spyOn(initActions, "initConfig");

initWebchat({});
initWebchat({ deploymentKey: "CV000000" });

expect(initConfigSpy).toBeCalled();
});
Expand All @@ -59,9 +60,30 @@ describe("Index", () => {
const initConfigSpy = jest.spyOn(initActions, "initConfig");

const serverUrl = "serverUrl";
initWebchat({ serverUrl });
initWebchat({ serverUrl, deploymentKey: "CV000000" });

expect(initConfigSpy).toBeCalledWith(expect.objectContaining({ serverUrl, theme: { isLight: true } }));
});

it("triggers expaneded true if appStatus is open", () => {
const changeExpandedStatusSpy = jest.spyOn(genericActions, "changeExpandedStatus");

initWebchat({ deploymentKey: "CV000000", appStatus: "open" });
expect(changeExpandedStatusSpy).toHaveBeenCalledWith({ expanded: true });
});

it("triggers expaneded false if appStatus is closed", () => {
const changeExpandedStatusSpy = jest.spyOn(genericActions, "changeExpandedStatus");

initWebchat({ deploymentKey: "CV000000", appStatus: "closed" });
expect(changeExpandedStatusSpy).toHaveBeenCalledWith({ expanded: false });
});

it("triggers expaneded false with default appStatus", () => {
const changeExpandedStatusSpy = jest.spyOn(genericActions, "changeExpandedStatus");

initWebchat({ deploymentKey: "CV000000" });
expect(changeExpandedStatusSpy).toHaveBeenCalledWith({ expanded: false });
});
});
});
6 changes: 5 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { sessionDataHandler } from "./sessionDataHandler";
import { initConfig } from "./store/actions/initActions";
import { ConfigState, UserConfig } from "./store/definitions";
import { initLogger, getLogger } from "./logger";
import { changeExpandedStatus } from "./store/actions/genericActions";

const defaultConfig: ConfigState = {
deploymentKey: "",
Expand All @@ -26,7 +27,7 @@ const defaultConfig: ConfigState = {
const initWebchat = async (userConfig: UserConfig) => {
// eslint-disable-next-line no-warning-comments
// TODO: serverUrl needs to be removed with PR #74
const validKeys = ["deploymentKey", "region", "theme", "serverUrl"];
const validKeys = ["deploymentKey", "region", "theme", "serverUrl", "appStatus"];
const logger = window.Twilio.getLogger(`InitWebChat`);

// eslint-disable-next-line no-warning-comments
Expand All @@ -41,6 +42,9 @@ const initWebchat = async (userConfig: UserConfig) => {
}
});

store.dispatch(changeExpandedStatus({ expanded: userConfig.appStatus === "open" }));
delete userConfig.appStatus;

const webchatConfig = merge({}, defaultConfig, userConfig);

sessionDataHandler.setEndpoint(webchatConfig.serverUrl);
Expand Down
1 change: 1 addition & 0 deletions src/store/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type UserConfig = {
serverUrl?: string;
deploymentKey: string;
region?: string;
appStatus?: "open";
theme?: {
isLight?: boolean;
overrides?: Partial<GenericThemeShape>;
Expand Down

0 comments on commit 90e01cf

Please sign in to comment.