-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add unit test for MessageProcessor service
- Loading branch information
1 parent
dd1b08d
commit 8b9c34b
Showing
1 changed file
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |