Skip to content

Commit

Permalink
test: Update test folder structure to mirror src
Browse files Browse the repository at this point in the history
  • Loading branch information
tjtanjin committed Oct 6, 2024
1 parent 9280a19 commit ab7a206
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { expect } from "@jest/globals";
import { render, screen, fireEvent } from "@testing-library/react";
import "@testing-library/jest-dom/jest-globals";

import AudioButton from "../../src/components/Buttons/AudioButton/AudioButton";
import { DefaultSettings } from "../../src/constants/internal/DefaultSettings";
import AudioButton from "../../../src/components/Buttons/AudioButton/AudioButton";
import { DefaultSettings } from "../../../src/constants/internal/DefaultSettings";

import { TestChatBotProvider } from "../__mocks__/TestChatBotContext";
import { TestChatBotProvider } from "../../__mocks__/TestChatBotContext";

/**
* Helper function to render AudioButton with different settings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { expect } from "@jest/globals";
import { render, screen, fireEvent } from "@testing-library/react";
import "@testing-library/jest-dom/jest-globals";

import NotificationButton from "../../src/components/Buttons/NotificationButton/NotificationButton";
import { DefaultSettings } from "../../src/constants/internal/DefaultSettings";
import NotificationButton from "../../../src/components/Buttons/NotificationButton/NotificationButton";
import { DefaultSettings } from "../../../src/constants/internal/DefaultSettings";

import { TestChatBotProvider } from "../__mocks__/TestChatBotContext";
import { TestChatBotProvider } from "../../__mocks__/TestChatBotContext";

/**
* Helper function to render NotificationButton with different settings.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { renderHook, act } from "@testing-library/react";
import { expect } from "@jest/globals";

import { useAudioInternal } from "../../src/hooks/internal/useAudioInternal";
import { useRcbEventInternal } from "../../src/hooks/internal/useRcbEventInternal";
import { RcbEvent } from "../../src/constants/RcbEvent";
import { useAudioInternal } from "../../../src/hooks/internal/useAudioInternal";
import { useRcbEventInternal } from "../../../src/hooks/internal/useRcbEventInternal";
import { RcbEvent } from "../../../src/constants/RcbEvent";

import { TestChatBotProvider } from "../__mocks__/TestChatBotContext";
import { MockDefaultSettings } from "../__mocks__/constants";
import { TestChatBotProvider } from "../../__mocks__/TestChatBotContext";
import { MockDefaultSettings } from "../../__mocks__/constants";

// mocks internal hooks
jest.mock("../../src/hooks/internal/useRcbEventInternal");
jest.mock("../../../src/hooks/internal/useRcbEventInternal");
const mockUseRcbEventInternal = useRcbEventInternal as jest.MockedFunction<typeof useRcbEventInternal>;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { renderHook, act } from "@testing-library/react";
import { expect } from "@jest/globals";

import { getHistoryMessages, loadChatHistory } from "../../src/services/ChatHistoryService";
import { generateSecureUUID } from "../../src/utils/idGenerator";
import { useChatHistoryInternal } from "../../src/hooks/internal/useChatHistoryInternal";
import { useRcbEventInternal } from "../../src/hooks/internal/useRcbEventInternal";
import { RcbEvent } from "../../src/constants/RcbEvent";
import { getHistoryMessages, loadChatHistory } from "../../../src/services/ChatHistoryService";
import { generateSecureUUID } from "../../../src/utils/idGenerator";
import { useChatHistoryInternal } from "../../../src/hooks/internal/useChatHistoryInternal";
import { useRcbEventInternal } from "../../../src/hooks/internal/useRcbEventInternal";
import { RcbEvent } from "../../../src/constants/RcbEvent";

import { TestChatBotProvider } from "../__mocks__/TestChatBotContext";
import { MockDefaultSettings } from "../__mocks__/constants";
import { TestChatBotProvider } from "../../__mocks__/TestChatBotContext";
import { MockDefaultSettings } from "../../__mocks__/constants";

// mocks internal hooks and services
jest.mock("../../src/hooks/internal/useRcbEventInternal");
jest.mock("../../../src/hooks/internal/useRcbEventInternal");
const mockUseRcbEventInternal = useRcbEventInternal as jest.MockedFunction<typeof useRcbEventInternal>;
jest.mock("../../src/services/ChatHistoryService");
jest.mock("../../../src/services/ChatHistoryService");
const mockGetHistoryMessages = getHistoryMessages as jest.MockedFunction<typeof getHistoryMessages>;
const mockLoadChatHistory = loadChatHistory as jest.MockedFunction<typeof loadChatHistory>;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { renderHook, act } from "@testing-library/react";
import { expect } from "@jest/globals";

import { useChatWindowInternal } from "../../src/hooks/internal/useChatWindowInternal";
import { useRcbEventInternal } from "../../src/hooks/internal/useRcbEventInternal";
import { RcbEvent } from "../../src/constants/RcbEvent";
import { useChatWindowInternal } from "../../../src/hooks/internal/useChatWindowInternal";
import { useRcbEventInternal } from "../../../src/hooks/internal/useRcbEventInternal";
import { RcbEvent } from "../../../src/constants/RcbEvent";

import { TestChatBotProvider } from "../__mocks__/TestChatBotContext";
import { MockDefaultSettings } from "../__mocks__/constants";
import { TestChatBotProvider } from "../../__mocks__/TestChatBotContext";
import { MockDefaultSettings } from "../../__mocks__/constants";

// mocks internal hooks and services
jest.mock("../../src/hooks/internal/useRcbEventInternal");
jest.mock("../../../src/hooks/internal/useRcbEventInternal");
const mockUseRcbEventInternal = useRcbEventInternal as jest.MockedFunction<typeof useRcbEventInternal>;

/**
Expand Down
74 changes: 74 additions & 0 deletions __tests__/hooks/internal/useFirstInteractionInternal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { renderHook, act } from "@testing-library/react";
import { expect } from "@jest/globals";

import { useFirstInteractionInternal } from "../../../src/hooks/internal/useFirstInteractionInternal";

jest.mock("../../../src/context/BotStatesContext", () => ({
useBotStatesContext: jest.fn(() => ({
hasInteractedPage: false,
setHasInteractedPage: jest.fn(),
hasFlowStarted: false,
setHasFlowStarted: jest.fn(),
})),
}));

jest.mock("../../../src/context/SettingsContext", () => ({
useSettingsContext: jest.fn(() => ({
settings: {
general: {
flowStartTrigger: "ON_PAGE_INTERACT",
},
},
})),
}));
class MockSpeechSynthesisUtterance {
text = "";
onend: () => void = () => {};

constructor() {
setTimeout(() => {
this.onend();
}, 0);
}
}

const MockSpeechSynthesis = {
speak: jest.fn().mockImplementation((utterance: MockSpeechSynthesisUtterance) => {
utterance.onend();
}),
};

global.SpeechSynthesisUtterance = MockSpeechSynthesisUtterance as any;
global.speechSynthesis = MockSpeechSynthesis as any;

describe("useFirstInteractionInternal", () => {
let setHasInteractedPage: jest.Mock;
let setHasFlowStarted: jest.Mock;

beforeEach(() => {
setHasInteractedPage = jest.fn();
setHasFlowStarted = jest.fn();

(require("../../../src/context/BotStatesContext").useBotStatesContext as jest.Mock).mockReturnValue({
hasInteractedPage: false,
setHasInteractedPage,
hasFlowStarted: false,
setHasFlowStarted,
});
});

afterEach(() => {
jest.clearAllMocks();
});

it("should call setHasInteractedPage and setHasFlowStarted on interaction", () => {
const { result } = renderHook(() => useFirstInteractionInternal());

act(() => {
result.current.handleFirstInteraction();
});

expect(setHasInteractedPage).toHaveBeenCalledWith(true);
expect(setHasFlowStarted).toHaveBeenCalledWith(true);
});
});
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { renderHook, act } from "@testing-library/react";
import { expect } from "@jest/globals";

import { useNotificationInternal } from "../../src/hooks/internal/useNotificationsInternal";
import { useRcbEventInternal } from "../../src/hooks/internal/useRcbEventInternal";
import { RcbEvent } from "../../src/constants/RcbEvent";
import { useNotificationInternal } from "../../../src/hooks/internal/useNotificationsInternal";
import { useRcbEventInternal } from "../../../src/hooks/internal/useRcbEventInternal";
import { RcbEvent } from "../../../src/constants/RcbEvent";

import { TestChatBotProvider } from "../__mocks__/TestChatBotContext";
import { MockDefaultSettings } from "../__mocks__/constants";
import { TestChatBotProvider } from "../../__mocks__/TestChatBotContext";
import { MockDefaultSettings } from "../../__mocks__/constants";

// mocks internal hooks and services
jest.mock("../../src/hooks/internal/useRcbEventInternal");
jest.mock("../../../src/hooks/internal/useRcbEventInternal");
const mockUseRcbEventInternal = useRcbEventInternal as jest.MockedFunction<typeof useRcbEventInternal>;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { renderHook, act } from "@testing-library/react";
import { expect } from "@jest/globals";

import { syncVoiceWithChatInput } from "../../src/services/VoiceService";
import { useVoiceInternal } from "../../src/hooks/internal/useVoiceInternal";
import { useRcbEventInternal } from "../../src/hooks/internal/useRcbEventInternal";
import { RcbEvent } from "../../src/constants/RcbEvent";
import { syncVoiceWithChatInput } from "../../../src/services/VoiceService";
import { useVoiceInternal } from "../../../src/hooks/internal/useVoiceInternal";
import { useRcbEventInternal } from "../../../src/hooks/internal/useRcbEventInternal";
import { RcbEvent } from "../../../src/constants/RcbEvent";

import { TestChatBotProvider } from "../__mocks__/TestChatBotContext";
import { MockDefaultSettings } from "../__mocks__/constants";
import { TestChatBotProvider } from "../../__mocks__/TestChatBotContext";
import { MockDefaultSettings } from "../../__mocks__/constants";

// mocks internal hooks and services
jest.mock("../../src/hooks/internal/useRcbEventInternal");
jest.mock("../../src/services/VoiceService");
jest.mock("../../../src/hooks/internal/useRcbEventInternal");
jest.mock("../../../src/services/VoiceService");
const mockUseRcbEventInternal = useRcbEventInternal as jest.MockedFunction<typeof useRcbEventInternal>;
const mockSyncVoiceWithChatInput = syncVoiceWithChatInput as jest.MockedFunction<typeof syncVoiceWithChatInput>;

Expand Down
27 changes: 27 additions & 0 deletions __tests__/hooks/internal/userBotIdInternal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { renderHook } from "@testing-library/react";
import { expect } from "@jest/globals";

import { useBotIdInternal } from "../../../src/hooks/internal/useBotIdInternal";
import { useBotRefsContext } from "../../../src/context/BotRefsContext";

// Mock the useBotRefsContext
jest.mock("../../../src/context/BotRefsContext");

// Cast the mock to allow TypeScript to recognize jest mock methods
const mockedUseBotRefsContext = useBotRefsContext as jest.Mock;

describe("useBotIdInternal", () => {
it("should return the correct botId", () => {
// Mock botIdRef
const botIdRef = { current: "test-bot-id" };

// Mock implementation of useBotRefsContext
mockedUseBotRefsContext.mockReturnValue({ botIdRef });

// Render the hook
const { result } = renderHook(() => useBotIdInternal());

// Call the getBotId method and assert the returned value
expect(result.current.getBotId()).toBe("test-bot-id");
});
});
74 changes: 0 additions & 74 deletions __tests__/hooks/useFirstInteractionInternal.test.ts

This file was deleted.

27 changes: 0 additions & 27 deletions __tests__/hooks/userBotIdInternal.test.ts

This file was deleted.

0 comments on commit ab7a206

Please sign in to comment.