Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/flex-webchat-ui' into FLEXIN-74
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/__tests__/index.test.tsx
#	src/index.tsx
  • Loading branch information
ashishkumarTWLO committed Oct 16, 2023
2 parents 3b4a1ea + 90e01cf commit 8db9c5f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 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
30 changes: 26 additions & 4 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 @@ -28,7 +29,7 @@ describe("Index", () => {
const root = document.createElement("div");
root.id = "twilio-webchat-widget-root";
document.body.appendChild(root);
initWebchat({ deploymentKey: "xyz" });
initWebchat({ deploymentKey: "CV000000" });

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

const region = "Foo";
initWebchat({ deploymentKey: "xyz", region });
initWebchat({ deploymentKey: "CV000000", region });

expect(setRegionSpy).toBeCalledWith(region);
});
Expand All @@ -59,15 +60,15 @@ describe("Index", () => {
it("initializes config", () => {
const initConfigSpy = jest.spyOn(initActions, "initConfig");

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

expect(initConfigSpy).toBeCalled();
});

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

const deploymentKey = "DKxxxxxxxxxxxx";
const deploymentKey = "CV000000";
initWebchat({ deploymentKey });

expect(initConfigSpy).toBeCalledWith(expect.objectContaining({ deploymentKey, theme: { isLight: true } }));
Expand All @@ -88,5 +89,26 @@ describe("Index", () => {
expect(warningSpy).toBeCalledTimes(1);
expect(warningSpy).toHaveBeenCalledWith("someKey is not supported.");
});

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 @@ -24,7 +25,7 @@ const defaultConfig: ConfigState = {
};

const initWebchat = async (userConfig: UserConfig) => {
const validKeys = ["deploymentKey", "region", "theme"];
const validKeys = ["deploymentKey", "region", "theme", "appStatus"];
const logger = window.Twilio.getLogger(`InitWebChat`);

if (!userConfig || !userConfig.deploymentKey) {
Expand All @@ -37,6 +38,9 @@ const initWebchat = async (userConfig: UserConfig) => {
}
}

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

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

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

0 comments on commit 8db9c5f

Please sign in to comment.