Skip to content

Commit

Permalink
resolve lint issues, remove unused dependencies, and improve test org…
Browse files Browse the repository at this point in the history
…anization
  • Loading branch information
tasbi03 committed Dec 6, 2024
1 parent d7d2afb commit cfe4abc
Show file tree
Hide file tree
Showing 25 changed files with 168 additions and 3,267 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

# coverage
/coverage

# editors
.vscode/*
!.vscode/extensions.json
Expand Down
249 changes: 119 additions & 130 deletions __tests__/core/useRcbPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { getValidator } from "../../src/utils/getValidator";
import useRcbPlugin from "../../src/core/useRcbPlugin";

const mockReplaceStyles = jest.fn();
const mockShowToast = jest.fn();

// Mock react-chatbotify dependencies
jest.mock("react-chatbotify", () => ({
useToasts: jest.fn(() => ({ showToast: mockShowToast })),
Expand All @@ -27,163 +29,150 @@ jest.mock("../../src/utils/getValidator", () => ({
const mockedValidateFile = validateFile as jest.Mock;
const mockedGetValidator = getValidator as jest.Mock;


mockedValidateFile.mockReturnValue({
success: false,
promptContent: "Invalid file type",
});

mockedGetValidator.mockReturnValue(mockedValidateFile);

const mockShowToast = jest.fn();
// Define custom event interfaces
interface FileUploadEvent extends Event {
data: { files: File[] | null };
}

interface TextInputEvent extends Event {
data: { inputText: string };
}

// Helper functions
const createFileUploadEvent = (files: File[] | null): FileUploadEvent => {
const event = new Event("rcb-user-upload-file") as FileUploadEvent;
event.data = { files };
return event;
};

const createTextInputEvent = (inputText: string): TextInputEvent => {
const event = new Event("rcb-user-submit-text") as TextInputEvent;
event.data = { inputText };
return event;
};

const renderRcbPluginHook = () => renderHook(() => useRcbPlugin());

describe("useRcbPlugin", () => {
beforeEach(() => {
jest.clearAllMocks(); // Clear mocks before each test
jest.clearAllMocks();
});

test("handles file upload and displays error for invalid file", () => {
const mockFile = new File(["invalid content"], "test.txt", { type: "text/plain" });

// Mock validateFile behavior
mockedValidateFile.mockReturnValue({
success: false,
promptContent: "Invalid file type",
describe("File Upload Handling", () => {
describe("Valid and Invalid Files", () => {
test("displays error for invalid file", () => {
const mockFile = new File(["invalid content"], "test.txt", { type: "text/plain" });
mockedValidateFile.mockReturnValue({
success: false,
promptContent: "Invalid file type",
});

renderRcbPluginHook();
const uploadEvent = createFileUploadEvent([mockFile]);
fireEvent(window, uploadEvent);

expect(mockedValidateFile).toHaveBeenCalledWith(mockFile);
expect(mockShowToast).toHaveBeenCalledWith("Invalid file type", 3000);
});

test("does nothing for valid file", () => {
const mockFile = new File(["valid content"], "test.png", { type: "image/png" });
mockedValidateFile.mockReturnValue({ success: true });

renderRcbPluginHook();
const uploadEvent = createFileUploadEvent([mockFile]);
fireEvent(window, uploadEvent);

expect(mockedValidateFile).toHaveBeenCalledWith(mockFile);
expect(mockShowToast).not.toHaveBeenCalled();
});
});

// Render the hook
renderHook(() => useRcbPlugin());

// Simulate file upload event
const uploadEvent = new Event("rcb-user-upload-file");
(uploadEvent as any).data = { files: [mockFile] };
fireEvent(window, uploadEvent);

// Debugging output
console.log("validateFile calls:", mockedValidateFile.mock.calls);
console.log("showToast calls:", mockShowToast.mock.calls);

// Assertions
expect(mockedValidateFile).toHaveBeenCalledWith(mockFile);
expect(mockShowToast).toHaveBeenCalledWith("Invalid file type", 3000);
});

test("handles file upload and does nothing for valid file", () => {
const mockFile = new File(["valid content"], "test.png", { type: "image/png" });

// Mock validateFile to return success
(validateFile as jest.Mock).mockReturnValue({
success: true,
describe("Edge Cases", () => {
test("handles null file upload", () => {
renderRcbPluginHook();
const uploadEvent = createFileUploadEvent(null);
fireEvent(window, uploadEvent);

expect(mockedValidateFile).not.toHaveBeenCalled();
expect(mockShowToast).not.toHaveBeenCalled();
});

test("handles empty file upload", () => {
renderRcbPluginHook();
const uploadEvent = createFileUploadEvent([]);
fireEvent(window, uploadEvent);

expect(mockedValidateFile).not.toHaveBeenCalled();
expect(mockShowToast).not.toHaveBeenCalled();
});
});
});

// Mock getValidator to return the validateFile function
(getValidator as jest.Mock).mockReturnValue(validateFile);
describe("Text Input Handling", () => {
describe("Valid and Invalid Input", () => {
test("displays error for invalid input", () => {
const mockValidator = jest.fn().mockReturnValue({
success: false,
promptContent: "Invalid input",
});

renderHook(() => useRcbPlugin());
mockedGetValidator.mockReturnValue(mockValidator);

// Simulate file upload event
const uploadEvent = new Event("rcb-user-upload-file");
(uploadEvent as any).data = { files: [mockFile] }; // Attach mock data
fireEvent(window, uploadEvent);
renderRcbPluginHook();
const textEvent = createTextInputEvent("invalid text");
fireEvent(window, textEvent);

// Assertions
expect(validateFile).toHaveBeenCalledWith(mockFile);
expect(mockShowToast).not.toHaveBeenCalled(); // No toast for valid file
});
expect(mockValidator).toHaveBeenCalledWith("invalid text");
expect(mockShowToast).toHaveBeenCalledWith("Invalid input", 3000);
});

test("handles text input and displays error for invalid input", () => {
const mockValidator = jest.fn().mockReturnValue({
success: false,
promptContent: "Invalid input",
});
test("does nothing for valid input", () => {
const mockValidator = jest.fn().mockReturnValue({ success: true });
mockedGetValidator.mockReturnValue(mockValidator);

// Mock getValidator to return the text validator
mockedGetValidator.mockReturnValue(mockValidator);
renderRcbPluginHook();
const textEvent = createTextInputEvent("valid input");
fireEvent(window, textEvent);

renderHook(() => useRcbPlugin());
expect(mockValidator).toHaveBeenCalledWith("valid input");
expect(mockShowToast).not.toHaveBeenCalled();
});
});

// Simulate text input event
const textEvent = new Event("rcb-user-submit-text");
(textEvent as any).data = { inputText: "invalid text" };
fireEvent(window, textEvent);
test("displays error for empty text input", () => {
const mockValidator = jest.fn().mockReturnValue({
success: false,
promptContent: "Input cannot be empty",
});

// Assertions
expect(mockValidator).toHaveBeenCalledWith("invalid text");
expect(mockShowToast).toHaveBeenCalledWith("Invalid input", 3000);
});
mockedGetValidator.mockReturnValue(mockValidator);

test("handles text input and does nothing for valid input", () => {
const mockValidator = jest.fn().mockReturnValue({ success: true });

// Mock getValidator to return the text validator
mockedGetValidator.mockReturnValue(mockValidator);

renderHook(() => useRcbPlugin());

// Simulate text input event
const textEvent = new Event("rcb-user-submit-text");
(textEvent as any).data = { inputText: "valid input" };
fireEvent(window, textEvent);

// Assertions
expect(mockValidator).toHaveBeenCalledWith("valid input");
expect(mockShowToast).not.toHaveBeenCalled(); // No toast for valid input
});
renderRcbPluginHook();
const textEvent = createTextInputEvent("");
fireEvent(window, textEvent);

test("handles empty text input validation", () => {
const mockValidator = jest.fn().mockReturnValue({
success: false,
promptContent: "Input cannot be empty",
expect(mockValidator).toHaveBeenCalledWith("");
expect(mockShowToast).toHaveBeenCalledWith("Input cannot be empty", 3000);
});

mockedGetValidator.mockReturnValue(mockValidator);

renderHook(() => useRcbPlugin());

const textEvent = new Event("rcb-user-submit-text");
(textEvent as any).data = { inputText: "" };
fireEvent(window, textEvent);

// Assertions
expect(mockValidator).toHaveBeenCalledWith("");
expect(mockShowToast).toHaveBeenCalledWith("Input cannot be empty", 3000);
});

test("handles null file upload", () => {
renderHook(() => useRcbPlugin());

const uploadEvent = new Event("rcb-user-upload-file");
(uploadEvent as any).data = { files: null };
fireEvent(window, uploadEvent);

// Assertions
expect(mockedValidateFile).not.toHaveBeenCalled();
expect(mockShowToast).not.toHaveBeenCalled();
});

test("handles empty file upload", () => {
renderHook(() => useRcbPlugin());

// Simulate empty file upload event
const uploadEvent = new Event("rcb-user-upload-file");
(uploadEvent as any).data = { files: [] };
fireEvent(window, uploadEvent);

// Assertions
expect(mockedValidateFile).not.toHaveBeenCalled();
expect(mockShowToast).not.toHaveBeenCalled(); // No toast for empty file list
});
test("restores styles after all toasts are dismissed", () => {
renderHook(() => useRcbPlugin());

// Simulate toast dismissal event
const dismissEvent = new Event("rcb-dismiss-toast");
fireEvent(window, dismissEvent);

// Verify that styles are restored
setTimeout(() => {
expect(mockReplaceStyles).toHaveBeenCalled();
}, 0);
});

describe("Styles Restoration", () => {
test("restores styles after all toasts are dismissed", () => {
renderRcbPluginHook();
const dismissEvent = new Event("rcb-dismiss-toast");
fireEvent(window, dismissEvent);

setTimeout(() => {
expect(mockReplaceStyles).toHaveBeenCalled();
}, 0);
});
});
});
Loading

0 comments on commit cfe4abc

Please sign in to comment.