Skip to content

Commit

Permalink
[Task] Add unit test cases for OptionProcessor #185 (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mila2999 authored Oct 12, 2024
1 parent ce8b458 commit dd1b08d
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions __tests__/services/BlockService/OptionProcessor.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import React from "react";
import { expect } from "@jest/globals";

import { processOptions } from "../../../src/services/BlockService/OptionProcessor";
import { Params } from "../../../src/types/Params";
import { Block } from "../../../src/types/Block";
import { Flow } from "../../../src/types/Flow";
import UserOptions from "../../../src/components/ChatBotBody/UserOptions/UserOptions";

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

// Mock Params with injectMessage function
beforeEach(() => {
mockParams = {
injectMessage: jest.fn() as Params["injectMessage"],
} as Params;

mockBlock = {
options: undefined,
} as Block;

mockFlow = {} as Flow;
});

it("should not call injectMessage if block has no options", async () => {
await processOptions(mockFlow, mockBlock, "somePath", mockParams);
expect(mockParams.injectMessage).not.toHaveBeenCalled();
});

it("should process static options and call injectMessage", async () => {
const staticOptions = ["Option1", "Option2"];
mockBlock.options = staticOptions;

await processOptions(mockFlow, mockBlock, "somePath", mockParams);
expect(mockParams.injectMessage).toHaveBeenCalledWith(
<UserOptions options={{ items: staticOptions, reusable: false }} path={"somePath"} />
);
});

it("should process dynamic options (function) and call injectMessage", async () => {
const dynamicOptions = ["DynamicOption1", "DynamicOption2"];
mockBlock.options = jest.fn().mockReturnValue(dynamicOptions);

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

expect(mockBlock.options).toHaveBeenCalledWith(mockParams);
expect(mockParams.injectMessage).toHaveBeenCalledWith(
<UserOptions options={{ items: dynamicOptions, reusable: false }} path={"somePath"} />
);
});

it("should await async function options and call injectMessage with resolved value", async () => {
const asyncOptions = ["AsyncOption1", "AsyncOption2"];
mockBlock.options = jest.fn().mockResolvedValue(asyncOptions);

await processOptions(mockFlow, mockBlock, "somePath", mockParams);
expect(mockBlock.options).toHaveBeenCalledWith(mockParams);
expect(mockParams.injectMessage).toHaveBeenCalledWith(
<UserOptions options={{ items: asyncOptions, reusable: false }} path={"somePath"} />
);
});

it("should set reusable to false by default if not provided", async () => {
const staticOptions = ["Option1", "Option2"];
mockBlock.options = staticOptions;

await processOptions(mockFlow, mockBlock, "somePath", mockParams);
expect(mockParams.injectMessage).toHaveBeenCalledWith(
<UserOptions options={{ items: staticOptions, reusable: false }} path={"somePath"} />
);
});

it("should not inject message if options is empty array", async () => {
mockBlock.options = [];

await processOptions(mockFlow, mockBlock, "somePath", mockParams);
expect(mockParams.injectMessage).not.toHaveBeenCalled();
});

it("should not inject message if options has no 'items'", async () => {
mockBlock.options = [];

await processOptions(mockFlow, mockBlock, "somePath", mockParams);
expect(mockParams.injectMessage).not.toHaveBeenCalled();
});
});

0 comments on commit dd1b08d

Please sign in to comment.