From 0049e05ce965296de04e8a831eab8a39a94c8832 Mon Sep 17 00:00:00 2001 From: mila2999 Date: Sat, 12 Oct 2024 13:32:01 +0300 Subject: [PATCH] [Task] Add unit test cases for OptionProcessor #185 --- .../BlockService/OptionProcessor.test.tsx | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 __tests__/services/BlockService/OptionProcessor.test.tsx diff --git a/__tests__/services/BlockService/OptionProcessor.test.tsx b/__tests__/services/BlockService/OptionProcessor.test.tsx new file mode 100644 index 00000000..c95f550c --- /dev/null +++ b/__tests__/services/BlockService/OptionProcessor.test.tsx @@ -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( + + ); + }); + + 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( + + ); + }); + + 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( + + ); + }); + + 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( + + ); + }); + + 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(); + }); +});