From d3cc37396ac0d746fea4a2fd5c9eb23cf087afff Mon Sep 17 00:00:00 2001 From: tjtanjin Date: Sat, 12 Oct 2024 17:41:37 +0800 Subject: [PATCH] test: Add initial unit test for utils --- __tests__/utils/messageBuilder.test.ts | 161 +++++++++++++++++++++++++ src/utils/messageBuilder.ts | 4 +- 2 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 __tests__/utils/messageBuilder.test.ts diff --git a/__tests__/utils/messageBuilder.test.ts b/__tests__/utils/messageBuilder.test.ts new file mode 100644 index 00000000..d92dd0d1 --- /dev/null +++ b/__tests__/utils/messageBuilder.test.ts @@ -0,0 +1,161 @@ +import React from "react"; + +import { createMessage } from "../../src/utils/messageBuilder"; +import { generateSecureUUID } from "../../src/utils/idGenerator"; + +// mocks id generator +jest.mock("../../src/utils/idGenerator"); +const mockedGenerateSecureUUID = generateSecureUUID as jest.MockedFunction; + +/** + * Test for message builder. + */ +describe("createMessage", () => { + beforeEach(() => { + // Reset all mocks before each test + jest.clearAllMocks(); + }); + + it("should create a message with string content correctly", () => { + // mocks message details + const mockId = "mocked-uuid"; + mockedGenerateSecureUUID.mockReturnValue(mockId); + const content = "This is a test message"; + const sender = "bot"; + + // creates message + const message = createMessage(content, sender); + + // checks if message is created correctly + expect(generateSecureUUID).toHaveBeenCalledTimes(1); + expect(message).toEqual({ + id: mockId, + content, + sender, + type: "string", + timestamp: expect.any(String), + }); + + // checks timestamp format + const timestampDate = new Date(message.timestamp); + expect(timestampDate.toUTCString()).toBe(message.timestamp); + }); + + it("should create a message with JSX.Element content correctly", () => { + // mocks message details + const mockId = "mocked-uuid"; + mockedGenerateSecureUUID.mockReturnValue(mockId); + const content = React.createElement("div"); + const sender = "user"; + + // creates message + const message = createMessage(content, sender); + + // checks if message is created correctly + expect(generateSecureUUID).toHaveBeenCalledTimes(1); + expect(message).toEqual({ + id: mockId, + content, + sender, + type: "object", + timestamp: expect.any(String), // We"ll validate the format separately + }); + + // checks timestamp format + const timestampDate = new Date(message.timestamp); + expect(timestampDate.toUTCString()).toBe(message.timestamp); + }); + + it("should handle empty string content correctly", () => { + // mocks message details + const mockId = "mocked-uuid"; + mockedGenerateSecureUUID.mockReturnValue(mockId); + const content = ""; + const sender = "bot"; + + // creates message + const message = createMessage(content, sender); + + // checks if message is created correctly + expect(generateSecureUUID).toHaveBeenCalledTimes(1); + expect(message).toEqual({ + id: mockId, + content, + sender, + type: "string", + timestamp: expect.any(String), + }); + + // checks timestamp format + const timestampDate = new Date(message.timestamp); + expect(timestampDate.toUTCString()).toBe(message.timestamp); + }); + + it("should handle special characters in content correctly", () => { + // mocks message details + const mockId = "mocked-uuid"; + mockedGenerateSecureUUID.mockReturnValue(mockId); + const content = 'Special characters! @#$%^&*()_+-=[]{}|;\':",.<>/?`~'; + const sender = "user"; + + // creates message + const message = createMessage(content, sender); + + // checks if message is created correctly + expect(generateSecureUUID).toHaveBeenCalledTimes(1); + expect(message).toEqual({ + id: mockId, + content, + sender, + type: "string", + timestamp: expect.any(String), + }); + + // checks timestamp format + const timestampDate = new Date(message.timestamp); + expect(timestampDate.toUTCString()).toBe(message.timestamp); + }); + + it("should handle content as a complex JSX.Element correctly", () => { + // mocks message details + const mockId = "mocked-uuid"; + mockedGenerateSecureUUID.mockReturnValue(mockId); + const content = React.createElement( + 'div', + { className: 'container' }, + React.createElement('h1', null, 'Title'), + React.createElement( + 'p', + null, + 'This is a paragraph with ', + React.createElement('strong', null, 'bold'), + ' text.' + ), + React.createElement( + 'ul', + null, + React.createElement('li', null, 'Item 1'), + React.createElement('li', null, 'Item 2'), + React.createElement('li', null, 'Item 3') + ) + ); + const sender = "bot"; + + // creates message + const message = createMessage(content, sender); + + // checks if message is created correctly + expect(generateSecureUUID).toHaveBeenCalledTimes(1); + expect(message).toEqual({ + id: mockId, + content, + sender, + type: "object", + timestamp: expect.any(String), + }); + + // checks timestamp format + const timestampDate = new Date(message.timestamp); + expect(timestampDate.toUTCString()).toBe(message.timestamp); + }); +}); diff --git a/src/utils/messageBuilder.ts b/src/utils/messageBuilder.ts index d3caff3f..d50c4807 100644 --- a/src/utils/messageBuilder.ts +++ b/src/utils/messageBuilder.ts @@ -1,5 +1,3 @@ -import { isValidElement } from "react"; - import { generateSecureUUID } from "./idGenerator"; /** @@ -13,7 +11,7 @@ export const createMessage = (content: string | JSX.Element, sender: string, ) = id: generateSecureUUID(), content, sender, - type: isValidElement(content) ? "object" : "string", + type: typeof content === "string" ? "string" : "object", timestamp: new Date().toUTCString() }; } \ No newline at end of file