|
| 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 | +// Mock react-chatbotify dependencies |
| 8 | +jest.mock("react-chatbotify", () => ({ |
| 9 | + useToasts: jest.fn(() => ({ showToast: mockShowToast })), |
| 10 | + useBotId: jest.fn(() => ({ getBotId: jest.fn().mockReturnValue("bot-id") })), |
| 11 | + useFlow: jest.fn(() => ({ getFlow: jest.fn() })), |
| 12 | + useStyles: jest.fn(() => ({ |
| 13 | + styles: {}, |
| 14 | + updateStyles: jest.fn(), |
| 15 | + replaceStyles: mockReplaceStyles, |
| 16 | + })), |
| 17 | +})); |
| 18 | + |
| 19 | +jest.mock("../../src/utils/validateFile", () => ({ |
| 20 | + validateFile: jest.fn(), |
| 21 | +})); |
| 22 | + |
| 23 | +jest.mock("../../src/utils/getValidator", () => ({ |
| 24 | + getValidator: jest.fn(), |
| 25 | +})); |
| 26 | + |
| 27 | +const mockedValidateFile = validateFile as jest.Mock; |
| 28 | +const mockedGetValidator = getValidator as jest.Mock; |
| 29 | + |
| 30 | + |
| 31 | +mockedValidateFile.mockReturnValue({ |
| 32 | + success: false, |
| 33 | + promptContent: "Invalid file type", |
| 34 | +}); |
| 35 | + |
| 36 | +mockedGetValidator.mockReturnValue(mockedValidateFile); |
| 37 | + |
| 38 | +const mockShowToast = jest.fn(); |
| 39 | + |
| 40 | +describe("useRcbPlugin", () => { |
| 41 | + beforeEach(() => { |
| 42 | + jest.clearAllMocks(); // Clear mocks before each test |
| 43 | + }); |
| 44 | + |
| 45 | + test("handles file upload and displays error for invalid file", () => { |
| 46 | + const mockFile = new File(["invalid content"], "test.txt", { type: "text/plain" }); |
| 47 | + |
| 48 | + // Mock validateFile behavior |
| 49 | + mockedValidateFile.mockReturnValue({ |
| 50 | + success: false, |
| 51 | + promptContent: "Invalid file type", |
| 52 | + }); |
| 53 | + |
| 54 | + // Render the hook |
| 55 | + renderHook(() => useRcbPlugin()); |
| 56 | + |
| 57 | + // Simulate file upload event |
| 58 | + const uploadEvent = new Event("rcb-user-upload-file"); |
| 59 | + (uploadEvent as any).data = { files: [mockFile] }; |
| 60 | + fireEvent(window, uploadEvent); |
| 61 | + |
| 62 | + // Debugging output |
| 63 | + console.log("validateFile calls:", mockedValidateFile.mock.calls); |
| 64 | + console.log("showToast calls:", mockShowToast.mock.calls); |
| 65 | + |
| 66 | + // Assertions |
| 67 | + expect(mockedValidateFile).toHaveBeenCalledWith(mockFile); |
| 68 | + expect(mockShowToast).toHaveBeenCalledWith("Invalid file type", 3000); |
| 69 | + }); |
| 70 | + |
| 71 | + test("handles file upload and does nothing for valid file", () => { |
| 72 | + const mockFile = new File(["valid content"], "test.png", { type: "image/png" }); |
| 73 | + |
| 74 | + // Mock validateFile to return success |
| 75 | + (validateFile as jest.Mock).mockReturnValue({ |
| 76 | + success: true, |
| 77 | + }); |
| 78 | + |
| 79 | + // Mock getValidator to return the validateFile function |
| 80 | + (getValidator as jest.Mock).mockReturnValue(validateFile); |
| 81 | + |
| 82 | + renderHook(() => useRcbPlugin()); |
| 83 | + |
| 84 | + // Simulate file upload event |
| 85 | + const uploadEvent = new Event("rcb-user-upload-file"); |
| 86 | + (uploadEvent as any).data = { files: [mockFile] }; // Attach mock data |
| 87 | + fireEvent(window, uploadEvent); |
| 88 | + |
| 89 | + // Assertions |
| 90 | + expect(validateFile).toHaveBeenCalledWith(mockFile); |
| 91 | + expect(mockShowToast).not.toHaveBeenCalled(); // No toast for valid file |
| 92 | + }); |
| 93 | + |
| 94 | + test("handles text input and displays error for invalid input", () => { |
| 95 | + const mockValidator = jest.fn().mockReturnValue({ |
| 96 | + success: false, |
| 97 | + promptContent: "Invalid input", |
| 98 | + }); |
| 99 | + |
| 100 | + // Mock getValidator to return the text validator |
| 101 | + mockedGetValidator.mockReturnValue(mockValidator); |
| 102 | + |
| 103 | + renderHook(() => useRcbPlugin()); |
| 104 | + |
| 105 | + // Simulate text input event |
| 106 | + const textEvent = new Event("rcb-user-submit-text"); |
| 107 | + (textEvent as any).data = { inputText: "invalid text" }; |
| 108 | + fireEvent(window, textEvent); |
| 109 | + |
| 110 | + // Assertions |
| 111 | + expect(mockValidator).toHaveBeenCalledWith("invalid text"); |
| 112 | + expect(mockShowToast).toHaveBeenCalledWith("Invalid input", 3000); |
| 113 | + }); |
| 114 | + |
| 115 | + test("handles text input and does nothing for valid input", () => { |
| 116 | + const mockValidator = jest.fn().mockReturnValue({ success: true }); |
| 117 | + |
| 118 | + // Mock getValidator to return the text validator |
| 119 | + mockedGetValidator.mockReturnValue(mockValidator); |
| 120 | + |
| 121 | + renderHook(() => useRcbPlugin()); |
| 122 | + |
| 123 | + // Simulate text input event |
| 124 | + const textEvent = new Event("rcb-user-submit-text"); |
| 125 | + (textEvent as any).data = { inputText: "valid input" }; |
| 126 | + fireEvent(window, textEvent); |
| 127 | + |
| 128 | + // Assertions |
| 129 | + expect(mockValidator).toHaveBeenCalledWith("valid input"); |
| 130 | + expect(mockShowToast).not.toHaveBeenCalled(); // No toast for valid input |
| 131 | + }); |
| 132 | + |
| 133 | + test("handles empty text input validation", () => { |
| 134 | + const mockValidator = jest.fn().mockReturnValue({ |
| 135 | + success: false, |
| 136 | + promptContent: "Input cannot be empty", |
| 137 | + }); |
| 138 | + |
| 139 | + mockedGetValidator.mockReturnValue(mockValidator); |
| 140 | + |
| 141 | + renderHook(() => useRcbPlugin()); |
| 142 | + |
| 143 | + const textEvent = new Event("rcb-user-submit-text"); |
| 144 | + (textEvent as any).data = { inputText: "" }; |
| 145 | + fireEvent(window, textEvent); |
| 146 | + |
| 147 | + // Assertions |
| 148 | + expect(mockValidator).toHaveBeenCalledWith(""); |
| 149 | + expect(mockShowToast).toHaveBeenCalledWith("Input cannot be empty", 3000); |
| 150 | + }); |
| 151 | + |
| 152 | + test("handles null file upload", () => { |
| 153 | + renderHook(() => useRcbPlugin()); |
| 154 | + |
| 155 | + const uploadEvent = new Event("rcb-user-upload-file"); |
| 156 | + (uploadEvent as any).data = { files: null }; |
| 157 | + fireEvent(window, uploadEvent); |
| 158 | + |
| 159 | + // Assertions |
| 160 | + expect(mockedValidateFile).not.toHaveBeenCalled(); |
| 161 | + expect(mockShowToast).not.toHaveBeenCalled(); |
| 162 | + }); |
| 163 | + |
| 164 | + test("handles empty file upload", () => { |
| 165 | + renderHook(() => useRcbPlugin()); |
| 166 | + |
| 167 | + // Simulate empty file upload event |
| 168 | + const uploadEvent = new Event("rcb-user-upload-file"); |
| 169 | + (uploadEvent as any).data = { files: [] }; |
| 170 | + fireEvent(window, uploadEvent); |
| 171 | + |
| 172 | + // Assertions |
| 173 | + expect(mockedValidateFile).not.toHaveBeenCalled(); |
| 174 | + expect(mockShowToast).not.toHaveBeenCalled(); // No toast for empty file list |
| 175 | + }); |
| 176 | + test("restores styles after all toasts are dismissed", () => { |
| 177 | + renderHook(() => useRcbPlugin()); |
| 178 | + |
| 179 | + // Simulate toast dismissal event |
| 180 | + const dismissEvent = new Event("rcb-dismiss-toast"); |
| 181 | + fireEvent(window, dismissEvent); |
| 182 | + |
| 183 | + // Verify that styles are restored |
| 184 | + setTimeout(() => { |
| 185 | + expect(mockReplaceStyles).toHaveBeenCalled(); |
| 186 | + }, 0); |
| 187 | + }); |
| 188 | + |
| 189 | +}); |
0 commit comments