Skip to content

Commit

Permalink
Wrote test cases for FunctionProcessor (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pratik00019 authored Oct 12, 2024
1 parent 8957b06 commit c35b8b2
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions __tests__/services/FunctionProcessor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { processFunction } from "../../src/services/BlockService/FunctionProcessor";
import { Block } from "../../src/types/Block";
import { Params } from "../../src/types/Params";

describe('processFunction', () => {
let params: Params;

beforeEach(() => {
params = {
userInput: "",
currPath: null,
prevPath: null,
goToPath: jest.fn(),
setTextAreaValue: jest.fn(),
injectMessage: jest.fn(),
streamMessage: jest.fn(),
removeMessage: jest.fn(),
endStreamMessage: jest.fn(),
showToast: jest.fn(),
dismissToast: jest.fn(),
openChat: jest.fn()
};
});

it('should return undefined if block has no function', async () => {
const block: Block = { function: undefined };

const result = await processFunction(block, params);

expect(result).toBeUndefined();
});

it('should return the result of the function if it is not a promise', async () => {
const block: Block = { function: jest.fn(() => 'result') };

const result = await processFunction(block, params);

expect(result).toBe('result');
});

it('should return the resolved value of the function if it is a promise', async () => {
const block: Block = { function: jest.fn(() => Promise.resolve('resolved value')) };

const result = await processFunction(block, params);

expect(result).toBe('resolved value');
});

it('should handle function throwing an error', async () => {
const block: Block = { function: jest.fn(() => { throw new Error('error'); }) };

await expect(processFunction(block, params)).rejects.toThrow('error');
});

it('should handle function returning a rejected promise', async () => {
const block: Block = { function: jest.fn(() => Promise.reject('rejected value')) };

await expect(processFunction(block, params)).rejects.toBe('rejected value');
});
});

0 comments on commit c35b8b2

Please sign in to comment.