Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: chatdisabledProcessor #188

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions __tests__/services/BlockService/ChatDisabledProcessor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { processChatDisabled } from "../../../src/services/BlockService/ChatDisabledProcessor";
import { Block } from "../../../src/types/Block";
import { Params } from "../../../src/types/Params";


describe('processChatDisabled', () => {
let mockSetTextAreaDisabled: jest.Mock;
let block: Block;
let params: Params;

beforeEach(() => {
mockSetTextAreaDisabled = jest.fn();
params = {
userInput: "test input",
currPath: null,
prevPath: null,
goToPath: jest.fn(),
setTextAreaValue: jest.fn(),
injectMessage: jest.fn(() => Promise.resolve(null)),
streamMessage: jest.fn(() => Promise.resolve(null)),
removeMessage: jest.fn(() => Promise.resolve(null)),
endStreamMessage: jest.fn(() => Promise.resolve(true)),
showToast: jest.fn(),
dismissToast: jest.fn(() => null),
openChat: jest.fn(),
files: undefined,
} as Params;
});

it('should do nothing if block.chatDisabled is null', async () => {
block = { chatDisabled: null } as Block;

await processChatDisabled(block, mockSetTextAreaDisabled, params);

expect(mockSetTextAreaDisabled).not.toHaveBeenCalled();
});

it('should call setTextAreaDisabled with the boolean value if block.chatDisabled is a boolean', async () => {
block = { chatDisabled: true } as Block;

await processChatDisabled(block, mockSetTextAreaDisabled, params);

expect(mockSetTextAreaDisabled).toHaveBeenCalledWith(true);
});

it('should call setTextAreaDisabled with the return value of a func if block.chatDisabled is a func', async () => {
block = {
chatDisabled: jest.fn(() => false)
} as unknown as Block;

await processChatDisabled(block, mockSetTextAreaDisabled, params);

expect(block.chatDisabled).toHaveBeenCalledWith(params);
expect(mockSetTextAreaDisabled).toHaveBeenCalledWith(false);
});

it('should await the promise if block.chatDisabled is a function that returns a promise', async () => {
block = {
chatDisabled: jest.fn(() => Promise.resolve(true))
} as unknown as Block;

await processChatDisabled(block, mockSetTextAreaDisabled, params);

expect(block.chatDisabled).toHaveBeenCalledWith(params);
expect(mockSetTextAreaDisabled).toHaveBeenCalledWith(true);
});

it('should call setTextAreaDisabled with null if block.chatDisabled is a function returning null', async () => {
block = {
chatDisabled: jest.fn(() => null)
} as unknown as Block;

await processChatDisabled(block, mockSetTextAreaDisabled, params);

expect(block.chatDisabled).toHaveBeenCalledWith(params);
expect(mockSetTextAreaDisabled).toHaveBeenCalledWith(null);
});

it('should handle errors when block.chatDisabled function throws an error', async () => {
block = {
chatDisabled: jest.fn(() => {
throw new Error('Test error');
})
} as unknown as Block;

await expect(processChatDisabled(block, mockSetTextAreaDisabled, params))
.rejects
.toThrow('Test error');

expect(mockSetTextAreaDisabled).not.toHaveBeenCalled();
});

it('should handle promise rejection if block.chatDisabled function returns a rejected promise', async () => {
block = {
chatDisabled: jest.fn(() => Promise.reject('Promise rejection'))
} as unknown as Block;

await expect(processChatDisabled(block, mockSetTextAreaDisabled, params))
.rejects
.toEqual('Promise rejection');

expect(mockSetTextAreaDisabled).not.toHaveBeenCalled();
});

it('should handle unexpected data type for block.chatDisabled (e.g., an object)', async () => {
block = { chatDisabled: {} } as unknown as Block;

await processChatDisabled(block, mockSetTextAreaDisabled, params);

// Expect that setTextAreaDisabled is called with an invalid type, in this case '{}'
expect(mockSetTextAreaDisabled).toHaveBeenCalledWith({});
});
});
2 changes: 1 addition & 1 deletion src/types/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type Block = {
((params: Params) => Promise<{items: Array<string>, max?: number, min?: number, sendOutput?: boolean, reusable?: boolean}>);
component?: JSX.Element | void | ((params: Params) => JSX.Element | void) |
((params: Params) => Promise<JSX.Element | void>);
chatDisabled?: boolean | ((params: Params) => boolean) | ((params: Params) => Promise<boolean>);
chatDisabled?: boolean | ((params: Params) => boolean) | ((params: Params) => Promise<boolean>) | null;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a specific reason this was added? For chatDisabled, it should only take in a boolean value or a function that returns a boolean value.

isSensitive?: boolean | ((params: Params) => boolean) | ((params: Params) => Promise<boolean>);
transition?: number | {duration: number, interruptable?: boolean} | void |
((params: Params) => number | {duration: number, interruptable?: boolean} | void) |
Expand Down