Skip to content

Commit

Permalink
fix: Fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
tjtanjin committed Oct 30, 2024
1 parent 0e6ebc6 commit 663774b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 36 deletions.
6 changes: 3 additions & 3 deletions __tests__/hooks/internal/useSettingsInternal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ describe("useSettingsInternal", () => {
result.current.updateSettings({});
});

expect(getCombinedConfig).toHaveBeenCalledWith({}, mockSettings);
expect(deepClone).toHaveBeenCalledWith(mockSettings);
expect(mockSetSettings).toHaveBeenCalledWith(mockSettings);
expect(getCombinedConfig).not.toHaveBeenCalledWith({}, mockSettings);
expect(deepClone).not.toHaveBeenCalledWith(mockSettings);
expect(mockSetSettings).not.toHaveBeenCalledWith(mockSettings);
});
});
2 changes: 1 addition & 1 deletion src/components/ChatBot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const ChatBot = ({
<ChatBotLoader styleRootRef={styleRootRef} id={finalBotId} flow={finalFlow} settings={finalSettings}
styles={finalStyles} themes={finalThemes} plugins={finalPlugins} setConfigLoaded={setConfigLoaded}
/>
{configLoaded && <ChatBotContainer plugins={plugins} setFinalThemes={setFinalThemes} />}
{configLoaded && <ChatBotContainer plugins={plugins} />}
</>
)

Expand Down
7 changes: 2 additions & 5 deletions src/components/ChatBotContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Dispatch, MouseEvent, SetStateAction, useMemo } from "react";
import { MouseEvent, useMemo } from "react";

import ChatBotHeader from "./ChatBotHeader/ChatBotHeader";
import ChatBotBody from "./ChatBotBody/ChatBotBody";
Expand All @@ -16,7 +16,6 @@ import { useBotStatesContext } from "../context/BotStatesContext";
import { useSettingsContext } from "../context/SettingsContext";
import { useStylesContext } from "../context/StylesContext";
import { Plugin } from "../types/Plugin";
import { Theme } from "../types/Theme";

import "./ChatBotContainer.css";

Expand All @@ -27,10 +26,8 @@ import "./ChatBotContainer.css";
*/
const ChatBotContainer = ({
plugins,
setFinalThemes
}: {
plugins?: Array<Plugin>;
setFinalThemes: Dispatch<SetStateAction<Theme | Array<Theme>>>;
}) => {
// handles platform
const isDesktop = useIsDesktopInternal();
Expand Down Expand Up @@ -62,7 +59,7 @@ const ChatBotContainer = ({
useBotEffectsInternal();

// loads plugins
usePluginsInternal(plugins, setFinalThemes);
usePluginsInternal(plugins);

/**
* Retrieves class name for window state.
Expand Down
38 changes: 12 additions & 26 deletions src/hooks/internal/usePluginsInternal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Dispatch, SetStateAction, useEffect } from "react";
import { Dispatch, SetStateAction, useEffect, useState } from "react";

import { getCombinedConfig } from "../../utils/configParser";
import { useSettingsInternal } from "./useSettingsInternal";
import { useStylesInternal } from "./useStylesInternal";
import { Plugin } from "../../types/Plugin";
Expand All @@ -8,8 +9,7 @@ import { Theme } from "../../types/Theme";
/**
* Internal custom hook to handle plugins.
*/
export const usePluginsInternal = (plugins: Array<Plugin> | undefined,
setFinalThemes: Dispatch<SetStateAction<Theme | Array<Theme>>>) => {
export const usePluginsInternal = (plugins: Array<Plugin> | undefined) => {

const { updateSettings } = useSettingsInternal();
const { updateStyles } = useStylesInternal();
Expand All @@ -18,33 +18,19 @@ export const usePluginsInternal = (plugins: Array<Plugin> | undefined,
const setUpInfo = plugins?.map((pluginHook) => pluginHook());

useEffect(() => {
let pluginSettings = {};
let pluginStyles = {};
// applies plugin themes, settings and styles if specified
setUpInfo?.forEach((setUpInfo) => {
if (setUpInfo?.themes) {
if (Array.isArray(setUpInfo.themes)) {
setFinalThemes(prev => {
if (Array.isArray(prev)) {
return [...prev, ...setUpInfo.themes as Array<Theme>];
} else {
return [prev, ...setUpInfo.themes as Array<Theme>];
}
});
} else {
setFinalThemes(prev => {
if (Array.isArray(prev)) {
return [...prev, setUpInfo.themes as Theme];
} else {
return [prev, setUpInfo.themes as Theme];
}
});
}
if (setUpInfo?.settings && Object.keys(setUpInfo?.settings).length !== 0) {
pluginSettings = getCombinedConfig(setUpInfo.settings, pluginSettings);
}
if (setUpInfo?.settings) {
updateSettings(setUpInfo.settings);
}
if (setUpInfo?.styles) {
updateStyles(setUpInfo.styles);
if (setUpInfo?.styles && Object.keys(setUpInfo?.styles).length !== 0) {
pluginStyles = getCombinedConfig(setUpInfo.styles, pluginStyles);
}
});

updateSettings(pluginSettings);
updateStyles(pluginStyles);
}, [plugins])
};
8 changes: 7 additions & 1 deletion src/services/ChatHistoryService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ let historyMessages: Message[] = [];
* @param messages messages containing current conversation with the bot
*/
const saveChatHistory = async (messages: Message[]) => {
if (historyDisabled) {
if (historyDisabled || !storage) {
return;
}

Expand Down Expand Up @@ -83,13 +83,19 @@ const getHistoryMessages = () => {
* @param messages chat history messages to set
*/
const setHistoryMessages = (messages: Message[]) => {
if (!storage) {
return;
}
storage.setItem(historyStorageKey, JSON.stringify(messages));
}

/**
* Clears existing history messages.
*/
const clearHistoryMessages = () => {
if (!storage) {
return;
}
storage.removeItem(historyStorageKey);
}

Expand Down

0 comments on commit 663774b

Please sign in to comment.