|
| 1 | +import { renderHook, fireEvent } from "@testing-library/react"; |
| 2 | +import { validateFile } from "../../src/utils/validateFile"; |
| 3 | +import { getValidator } from "../../src/utils/getValidator"; |
| 4 | +import useRcbPlugin from "../../src/core/useRcbPlugin"; |
| 5 | + |
| 6 | +const mockReplaceStyles = jest.fn(); |
| 7 | +const mockShowToast = jest.fn(); |
| 8 | + |
| 9 | +// Mock react-chatbotify dependencies |
| 10 | +jest.mock("react-chatbotify", () => ({ |
| 11 | + useToasts: jest.fn(() => ({ showToast: mockShowToast })), |
| 12 | + useBotId: jest.fn(() => ({ getBotId: jest.fn().mockReturnValue("bot-id") })), |
| 13 | + useFlow: jest.fn(() => ({ getFlow: jest.fn() })), |
| 14 | + useStyles: jest.fn(() => ({ |
| 15 | + styles: {}, |
| 16 | + updateStyles: jest.fn(), |
| 17 | + replaceStyles: mockReplaceStyles, |
| 18 | + })), |
| 19 | +})); |
| 20 | + |
| 21 | +jest.mock("../../src/utils/validateFile", () => ({ |
| 22 | + validateFile: jest.fn(), |
| 23 | +})); |
| 24 | + |
| 25 | +jest.mock("../../src/utils/getValidator", () => ({ |
| 26 | + getValidator: jest.fn(), |
| 27 | +})); |
| 28 | + |
| 29 | +const mockedValidateFile = validateFile as jest.Mock; |
| 30 | +const mockedGetValidator = getValidator as jest.Mock; |
| 31 | + |
| 32 | +mockedValidateFile.mockReturnValue({ |
| 33 | + success: false, |
| 34 | + promptContent: "Invalid file type", |
| 35 | +}); |
| 36 | + |
| 37 | +mockedGetValidator.mockReturnValue(mockedValidateFile); |
| 38 | + |
| 39 | +// Define custom event interfaces |
| 40 | +interface FileUploadEvent extends Event { |
| 41 | + data: { files: File[] | null }; |
| 42 | +} |
| 43 | + |
| 44 | +interface TextInputEvent extends Event { |
| 45 | + data: { inputText: string }; |
| 46 | +} |
| 47 | + |
| 48 | +// Helper functions |
| 49 | +const createFileUploadEvent = (files: File[] | null): FileUploadEvent => { |
| 50 | + const event = new Event("rcb-user-upload-file") as FileUploadEvent; |
| 51 | + event.data = { files }; |
| 52 | + return event; |
| 53 | +}; |
| 54 | + |
| 55 | +const createTextInputEvent = (inputText: string): TextInputEvent => { |
| 56 | + const event = new Event("rcb-user-submit-text") as TextInputEvent; |
| 57 | + event.data = { inputText }; |
| 58 | + return event; |
| 59 | +}; |
| 60 | + |
| 61 | +const renderRcbPluginHook = () => renderHook(() => useRcbPlugin()); |
| 62 | + |
| 63 | +describe("useRcbPlugin", () => { |
| 64 | + beforeEach(() => { |
| 65 | + jest.clearAllMocks(); |
| 66 | + }); |
| 67 | + |
| 68 | + describe("File Upload Handling", () => { |
| 69 | + describe("Valid and Invalid Files", () => { |
| 70 | + test("displays error for invalid file", () => { |
| 71 | + const mockFile = new File(["invalid content"], "test.txt", { type: "text/plain" }); |
| 72 | + mockedValidateFile.mockReturnValue({ |
| 73 | + success: false, |
| 74 | + promptContent: "Invalid file type", |
| 75 | + }); |
| 76 | + |
| 77 | + renderRcbPluginHook(); |
| 78 | + const uploadEvent = createFileUploadEvent([mockFile]); |
| 79 | + fireEvent(window, uploadEvent); |
| 80 | + |
| 81 | + expect(mockedValidateFile).toHaveBeenCalledWith(mockFile); |
| 82 | + expect(mockShowToast).toHaveBeenCalledWith("Invalid file type", 3000); |
| 83 | + }); |
| 84 | + |
| 85 | + test("does nothing for valid file", () => { |
| 86 | + const mockFile = new File(["valid content"], "test.png", { type: "image/png" }); |
| 87 | + mockedValidateFile.mockReturnValue({ success: true }); |
| 88 | + |
| 89 | + renderRcbPluginHook(); |
| 90 | + const uploadEvent = createFileUploadEvent([mockFile]); |
| 91 | + fireEvent(window, uploadEvent); |
| 92 | + |
| 93 | + expect(mockedValidateFile).toHaveBeenCalledWith(mockFile); |
| 94 | + expect(mockShowToast).not.toHaveBeenCalled(); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe("Edge Cases", () => { |
| 99 | + test("handles null file upload", () => { |
| 100 | + renderRcbPluginHook(); |
| 101 | + const uploadEvent = createFileUploadEvent(null); |
| 102 | + fireEvent(window, uploadEvent); |
| 103 | + |
| 104 | + expect(mockedValidateFile).not.toHaveBeenCalled(); |
| 105 | + expect(mockShowToast).not.toHaveBeenCalled(); |
| 106 | + }); |
| 107 | + |
| 108 | + test("handles empty file upload", () => { |
| 109 | + renderRcbPluginHook(); |
| 110 | + const uploadEvent = createFileUploadEvent([]); |
| 111 | + fireEvent(window, uploadEvent); |
| 112 | + |
| 113 | + expect(mockedValidateFile).not.toHaveBeenCalled(); |
| 114 | + expect(mockShowToast).not.toHaveBeenCalled(); |
| 115 | + }); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + describe("Text Input Handling", () => { |
| 120 | + describe("Valid and Invalid Input", () => { |
| 121 | + test("displays error for invalid input", () => { |
| 122 | + const mockValidator = jest.fn().mockReturnValue({ |
| 123 | + success: false, |
| 124 | + promptContent: "Invalid input", |
| 125 | + }); |
| 126 | + |
| 127 | + mockedGetValidator.mockReturnValue(mockValidator); |
| 128 | + |
| 129 | + renderRcbPluginHook(); |
| 130 | + const textEvent = createTextInputEvent("invalid text"); |
| 131 | + fireEvent(window, textEvent); |
| 132 | + |
| 133 | + expect(mockValidator).toHaveBeenCalledWith("invalid text"); |
| 134 | + expect(mockShowToast).toHaveBeenCalledWith("Invalid input", 3000); |
| 135 | + }); |
| 136 | + |
| 137 | + test("does nothing for valid input", () => { |
| 138 | + const mockValidator = jest.fn().mockReturnValue({ success: true }); |
| 139 | + mockedGetValidator.mockReturnValue(mockValidator); |
| 140 | + |
| 141 | + renderRcbPluginHook(); |
| 142 | + const textEvent = createTextInputEvent("valid input"); |
| 143 | + fireEvent(window, textEvent); |
| 144 | + |
| 145 | + expect(mockValidator).toHaveBeenCalledWith("valid input"); |
| 146 | + expect(mockShowToast).not.toHaveBeenCalled(); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + test("displays error for empty text input", () => { |
| 151 | + const mockValidator = jest.fn().mockReturnValue({ |
| 152 | + success: false, |
| 153 | + promptContent: "Input cannot be empty", |
| 154 | + }); |
| 155 | + |
| 156 | + mockedGetValidator.mockReturnValue(mockValidator); |
| 157 | + |
| 158 | + renderRcbPluginHook(); |
| 159 | + const textEvent = createTextInputEvent(""); |
| 160 | + fireEvent(window, textEvent); |
| 161 | + |
| 162 | + expect(mockValidator).toHaveBeenCalledWith(""); |
| 163 | + expect(mockShowToast).toHaveBeenCalledWith("Input cannot be empty", 3000); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + describe("Styles Restoration", () => { |
| 168 | + test("restores styles after all toasts are dismissed", () => { |
| 169 | + renderRcbPluginHook(); |
| 170 | + const dismissEvent = new Event("rcb-dismiss-toast"); |
| 171 | + fireEvent(window, dismissEvent); |
| 172 | + |
| 173 | + setTimeout(() => { |
| 174 | + expect(mockReplaceStyles).toHaveBeenCalled(); |
| 175 | + }, 0); |
| 176 | + }); |
| 177 | + }); |
| 178 | +}); |
0 commit comments