Skip to content

Commit

Permalink
test: add unit test for MessageProcessor service
Browse files Browse the repository at this point in the history
  • Loading branch information
heinthetaung-dev committed Oct 12, 2024
1 parent dd1b08d commit 8b9c34b
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions __tests__/services/MessageProcessor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { expect } from "@jest/globals";
import { processMessage } from "../../src/services/BlockService/MessageProcessor";
import { Params } from "../../src/types/Params";
import { Block } from "../../src/types/Block";

describe("MessageProcessor", () => {
let mockParams: Params;
let mockBlock: Block;

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

mockBlock = {} as Block;
});

// No message in block
it("should not inject message if block has no message", async () => {
await processMessage(mockBlock, mockParams);

// Make sure injectMessage was not called
expect(mockParams.injectMessage).not.toHaveBeenCalled();
});

// Empty string message
it("should not inject message if block message is an empty string", async () => {
mockBlock.message = " ";
await processMessage(mockBlock, mockParams);

// Make sure injectMessage was not called if the message just contains whitespace
expect(mockParams.injectMessage).not.toHaveBeenCalled();
});

// Valid string message
it("should inject message if block has a non-empty string message", async () => {
const message = "Test Message";
mockBlock.message = message;
await processMessage(mockBlock, mockParams);

// Make sure injectMessage was called with the correct message
expect(mockParams.injectMessage).toHaveBeenCalledWith(message);
});

// Function returning invalid content
it("should not inject message if block message is a function returning invalid content", async () => {
const functionResult = null;
mockBlock.message = jest.fn().mockReturnValue(functionResult);

await processMessage(mockBlock, mockParams);

// Make sure injectMessage was not called if function returns null
expect(mockParams.injectMessage).not.toHaveBeenCalledWith(functionResult);
});

// Function returning valid content
it("should inject message if block message is a function returning valid content", async () => {
const functionResult = "Function Result";
mockBlock.message = jest.fn().mockReturnValue(functionResult);

await processMessage(mockBlock, mockParams);

// Check if the message function was called with correct params
expect(mockBlock.message).toHaveBeenCalledWith(mockParams);

// Make sure injectMessage was called with the function's return value
expect(mockParams.injectMessage).toHaveBeenCalledWith(functionResult);
});

// Function returning a promise with invalid content
it("should not inject message if block message is a function returning a promise with invalid content", async () => {
mockBlock.message = jest.fn().mockResolvedValue(null);

await processMessage(mockBlock, mockParams);

// Make sure injectMessage was not called if content is invalid (null)
expect(mockParams.injectMessage).not.toHaveBeenCalled();
});

// Function returning a promise with valid content
it("should inject message if block message is a function returning a promise with valid content", async () => {
const promiseResult = "Async Function Result";
mockBlock.message = jest.fn().mockResolvedValue(promiseResult);

await processMessage(mockBlock, mockParams);

// Check if the message function was called with correct params
expect(mockBlock.message).toHaveBeenCalledWith(mockParams);

// Make sure injectMessage was called with the resolved promise value
expect(mockParams.injectMessage).toHaveBeenCalledWith(promiseResult);
});
});

0 comments on commit 8b9c34b

Please sign in to comment.