Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions aselo-webchat-react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "LC_ALL=\"en_GB.UTF-8\" react-app-rewired test --watchAll=false --env=jsdom --transformIgnorePatterns \"node_modules/(?!(@twilio-paste|@twilio/conversations))/\"",
"test:nowatch": "yarn test --watchAll=false --verbose --runInBand",
"test:nowatch": "npm test --watchAll=false --verbose --runInBand",
"bootstrap": "node scripts/bootstrap",
"e2eCleanupExistingTasks": "node scripts/e2eCleanupExistingTasks",
"deploy": "yarn build && node scripts/deploy",
"deploy": "npm run build && node scripts/deploy",
"eject": "react-app-rewired eject",
"test:e2e": "cypress open"
},
Expand Down
81 changes: 30 additions & 51 deletions aselo-webchat-react-app/src/__tests__/sessionDataHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ jest.mock("../logger");
Object.defineProperty(navigator, "mediaCapabilities", {
writable: true,
value: {
decodingInfo: jest.fn().mockResolvedValue({} as unknown as MediaCapabilitiesDecodingInfo)
}
decodingInfo: jest.fn().mockResolvedValue({} as unknown as MediaCapabilitiesDecodingInfo),
},
});

const originalEnv = process.env;
Expand All @@ -20,14 +20,14 @@ describe("session data handler", () => {
value: {
getLogger(className: string) {
return new WebChatLogger(className);
}
}
},
},
});
process.env = {
...originalEnv,
APP_VERSION: "1.25.10",
WEBCHAT_VERSION: "3.55"
};
WEBCHAT_VERSION: "3.55",
};
});
afterEach(() => {
jest.clearAllMocks();
Expand Down Expand Up @@ -91,20 +91,20 @@ describe("session data handler", () => {

jest.useFakeTimers().setSystemTime(new Date("2023-01-01"));

const currentTime = Date.now().toString();
const currentTime = Date.now();
const tokenPayload = {
expiration: `${currentTime + 10e5}`,
token: "new token",
// eslint-disable-next-line camelcase
conversation_sid: "sid",
identity: "identity"
identity: "identity",
};
fetchMock.once(() => true, tokenPayload);
await sessionDataHandler.fetchAndStoreNewSession({ formData: {} });

const expected = {
...sessionDataHandler.processNewTokenResponse(tokenPayload),
loginTimestamp: currentTime
...tokenPayload,
loginTimestamp: currentTime,
};
expect(setLocalStorageItemSpy).toHaveBeenCalledTimes(2);
expect(setLocalStorageItemSpy).toHaveBeenNthCalledWith(
Expand All @@ -115,13 +115,13 @@ describe("session data handler", () => {
expiration: "",
identity: "",
conversationSid: "",
loginTimestamp: currentTime
})
loginTimestamp: currentTime,
}),
);
expect(setLocalStorageItemSpy).toHaveBeenNthCalledWith(
2,
"TWILIO_WEBCHAT_WIDGET",
JSON.stringify(expected)
JSON.stringify(expected),
);
});

Expand All @@ -131,7 +131,7 @@ describe("session data handler", () => {
token: "new token",
// eslint-disable-next-line camelcase
conversation_sid: "sid",
identity: "identity"
identity: "identity",
};
fetchMock.once(() => true, tokenPayload);
const { token } = await sessionDataHandler.fetchAndStoreNewSession({ formData: {} });
Expand All @@ -146,8 +146,8 @@ describe("session data handler", () => {
JSON.stringify({
expiration: Date.now() + 10e5,
token: "token",
conversationSid: "sid"
})
conversationSid: "sid",
}),
);

const tokenPayload = sessionDataHandler.tryResumeExistingSession();
Expand All @@ -160,8 +160,8 @@ describe("session data handler", () => {
JSON.stringify({
expiration: Date.now() - 10e5,
token: "token",
conversationSid: "sid"
})
conversationSid: "sid",
}),
);

const tokenPayload = sessionDataHandler.tryResumeExistingSession();
Expand All @@ -181,7 +181,7 @@ describe("session data handler", () => {
jest.spyOn(Object.getPrototypeOf(window.localStorage), "getItem").mockReturnValueOnce({
expiration: Date.now() - 10e5,
token: "token",
conversationSid: "sid"
conversationSid: "sid",
});

const tokenPayload = sessionDataHandler.tryResumeExistingSession();
Expand All @@ -202,8 +202,8 @@ describe("session data handler", () => {
JSON.stringify({
expiration: Date.now() - 10e5,
token: "token",
conversationSid: "sid"
})
conversationSid: "sid",
}),
);
jest.spyOn(window, "fetch").mockImplementationOnce(() => {
throw Error("Backend failed to process the request");
Expand All @@ -219,15 +219,15 @@ describe("session data handler", () => {
JSON.stringify({
expiration: Date.now() + 10e5,
token: "token",
conversationSid: "sid"
})
conversationSid: "sid",
}),
);
const setLocalStorageItemSpy = jest.spyOn(Object.getPrototypeOf(window.localStorage), "setItem");

const tokenPayload = {
expiration: Date.now() + 10e5,
token: "new token",
conversationSid: "sid"
conversationSid: "sid",
};
fetchMock.once(() => true, tokenPayload);
await sessionDataHandler.getUpdatedToken();
Expand All @@ -240,14 +240,14 @@ describe("session data handler", () => {
JSON.stringify({
expiration: Date.now() + 10e5,
token: "token",
conversationSid: "sid"
})
conversationSid: "sid",
}),
);

const tokenPayload = {
expiration: Date.now() + 10e5,
token: "new token",
conversationSid: "sid"
conversationSid: "sid",
};
fetchMock.once(() => true, tokenPayload);
const { token } = await sessionDataHandler.getUpdatedToken();
Expand All @@ -263,8 +263,8 @@ describe("session data handler", () => {
expiration: Date.now() + 10e5,
token: "token",
conversationSid: "sid",
identity: "identity"
})
identity: "identity",
}),
);
sessionDataHandler.clear();
expect(spySetItem).toHaveBeenCalledWith("TWILIO_WEBCHAT_WIDGET", JSON.stringify({ identity: "identity" }));
Expand All @@ -275,29 +275,8 @@ describe("session data handler", () => {
jest.spyOn(window, "fetch").mockRejectedValueOnce("ForcedFailure");

await expect(sessionDataHandler.fetchAndStoreNewSession({ formData: {} })).rejects.toThrowError(
new Error("No results from server")
new Error("No results from server"),
);
});
});

describe("processNewTokenResponse", () => {
it("should process data as expected", () => {
const tokenResponse = {
expiration: "2021-01-01",
token: "token",
// eslint-disable-next-line camelcase
conversation_sid: "sid",
identity: "identity"
};

const processedTokenResponse = sessionDataHandler.processNewTokenResponse(tokenResponse);

expect(processedTokenResponse).toEqual({
expiration: "2021-01-01",
token: "token",
conversationSid: "sid",
identity: "identity"
});
});
});
});
12 changes: 7 additions & 5 deletions aselo-webchat-react-app/src/components/end-chat/EndChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
*/

/* eslint-disable react/require-default-props */
import React, { useState } from "react";
import { useState } from "react";
import { Button } from "@twilio-paste/core/button";
import { useDispatch } from "react-redux";

import { finishChatTask } from "./end-chat-service";
import { sessionDataHandler } from "../../sessionDataHandler";
import { contactBackend, sessionDataHandler } from "../../sessionDataHandler";
import { changeEngagementPhase, updatePreEngagementData } from "../../store/actions/genericActions";
import { EngagementPhase } from "../../store/definitions";

Expand All @@ -41,9 +40,12 @@ export default function EndChat({ channelSid, token, language, action }: Props)
case "finishTask":
try {
setDisabled(true);
await finishChatTask(channelSid, token, language);
await contactBackend("/endChat", { channelSid, token, language });
sessionDataHandler.clear();
dispatch(updatePreEngagementData({ email: "", name: "", query: "" }));
dispatch(changeEngagementPhase({ phase: EngagementPhase.PreEngagementForm }));
} catch (error) {
console.log(error);
console.error(error);
} finally {
setDisabled(false);
}
Expand Down
7 changes: 2 additions & 5 deletions aselo-webchat-react-app/src/components/end-chat/QuickExit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

/* eslint-disable react/require-default-props */
import React from "react";
import { useDispatch } from "react-redux";
import { Button } from "@twilio-paste/core/button";

import { finishChatTask } from "./end-chat-service";
import { sessionDataHandler } from "../../sessionDataHandler";
import { contactBackend, sessionDataHandler } from "../../sessionDataHandler";
import { changeEngagementPhase, updatePreEngagementData } from "../../store/actions/genericActions";
import { EngagementPhase } from "../../store/definitions";

Expand All @@ -41,7 +38,7 @@ export default function QuickExit({ channelSid, token, language, finishTask }: P
if (finishTask) {
// Only if we started a task
try {
await finishChatTask(channelSid, token, language);
await contactBackend("/endChat", { channelSid, token, language });
} catch (error) {
console.error(error);
}
Expand Down

This file was deleted.

7 changes: 0 additions & 7 deletions aselo-webchat-react-app/src/definitions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
export type TokenResponse = {
token: string;
conversation_sid: string;
identity: string;
expiration: string;
};

export type ProcessedTokenResponse = {
token: string;
conversationSid: string;
identity: string;
Expand Down
Loading
Loading