Skip to content

Commit

Permalink
test: Add initial unit test for utils
Browse files Browse the repository at this point in the history
  • Loading branch information
tjtanjin committed Oct 12, 2024
1 parent 8058491 commit d3cc373
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 3 deletions.
161 changes: 161 additions & 0 deletions __tests__/utils/messageBuilder.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof generateSecureUUID>;

/**
* 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);
});
});
4 changes: 1 addition & 3 deletions src/utils/messageBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { isValidElement } from "react";

import { generateSecureUUID } from "./idGenerator";

/**
Expand All @@ -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()
};
}

0 comments on commit d3cc373

Please sign in to comment.