Skip to content

Commit

Permalink
[Task] Add unit test cases for CheckboxProcessor #175 (#200)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrey Kishtov <andreyk@jfrog.com>
  • Loading branch information
andreykishtov and Andrey Kishtov authored Oct 12, 2024
1 parent c35b8b2 commit ce8b458
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions __tests__/services/BlockService/CheckboxProcessor.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { processCheckboxes } from "../../../src/services/BlockService/CheckboxProcessor";
import { Block } from "../../../src/types/Block";
import { Flow } from "../../../src/types/Flow";
import { Params } from "../../../src/types/Params";

describe("processCheckboxes", () => {
let mockFlow: Flow;
let mockBlock: Block;
let mockParams: Params;

beforeEach(() => {
mockFlow = {} as Flow;
mockBlock = {} as Block;
mockParams = {
injectMessage: jest.fn(),
} as unknown as Params;
});

it("should return early if block has no checkboxes", async () => {
await processCheckboxes(mockFlow, mockBlock, "somePath", mockParams);

expect(mockParams.injectMessage).not.toHaveBeenCalled();
});

it("should process checkboxes when checkboxes are provided as a function", async () => {
const checkboxItems = ["Option 1", "Option 2"];
mockBlock.checkboxes = jest.fn().mockReturnValue(checkboxItems);

await processCheckboxes(mockFlow, mockBlock, "somePath", mockParams);

expect(mockBlock.checkboxes).toHaveBeenCalledWith(mockParams);
expect(mockParams.injectMessage).toHaveBeenCalled();
});

it("should handle async checkbox functions", async () => {
const asyncCheckboxItems = ["Option A", "Option B"];
mockBlock.checkboxes = jest.fn().mockResolvedValue(asyncCheckboxItems);

await processCheckboxes(mockFlow, mockBlock, "somePath", mockParams);

expect(mockBlock.checkboxes).toHaveBeenCalledWith(mockParams);
expect(mockParams.injectMessage).toHaveBeenCalled();
});

it("should convert checkbox array to object with items", async () => {
mockBlock.checkboxes = ["Checkbox 1", "Checkbox 2"];

await processCheckboxes(mockFlow, mockBlock, "somePath", mockParams);

const expectedCheckboxes = { items: ["Checkbox 1", "Checkbox 2"] };
expect(mockParams.injectMessage).toHaveBeenCalled();
const [content] = (mockParams.injectMessage as jest.Mock).mock.calls[0];
expect(content.props.checkboxes).toMatchObject(expectedCheckboxes);
});

it("should set min and max values when not provided", async () => {
mockBlock.checkboxes = { items: ["Item 1", "Item 2"] };

await processCheckboxes(mockFlow, mockBlock, "somePath", mockParams);

const expectedCheckboxes = { items: ["Item 1", "Item 2"], min: 1, max: 2 };
expect(mockParams.injectMessage).toHaveBeenCalled();
const [content] = (mockParams.injectMessage as jest.Mock).mock.calls[0];
expect(content.props.checkboxes).toMatchObject(expectedCheckboxes);
});

it("should handle invalid min/max values", async () => {
mockBlock.checkboxes = { items: ["Item 1", "Item 2"], min: 3, max: 2 };

await processCheckboxes(mockFlow, mockBlock, "somePath", mockParams);

const expectedCheckboxes = { items: ["Item 1", "Item 2"], min: 2, max: 2 };
expect(mockParams.injectMessage).toHaveBeenCalled();
const [content] = (mockParams.injectMessage as jest.Mock).mock.calls[0];
console.log("content.props.checkboxes:", content.props.checkboxes);
console.log("expectedCheckboxes:", expectedCheckboxes);

expect(content.props.checkboxes).toMatchObject(expectedCheckboxes);
});

it("should not inject message if no items are present in checkboxes", async () => {
mockBlock.checkboxes = { items: [] };

await processCheckboxes(mockFlow, mockBlock, "somePath", mockParams);

expect(mockParams.injectMessage).not.toHaveBeenCalled();
});
});

0 comments on commit ce8b458

Please sign in to comment.