import type { MockProxy } from 'jest-mock-extended'; import { mock } from 'jest-mock-extended'; import { type INode, SEND_AND_WAIT_OPERATION, type IExecuteFunctions } from 'n8n-workflow'; import * as genericFunctions from '../../GenericFunctions'; import { GoogleChat } from '../../GoogleChat.node'; jest.mock('../../GenericFunctions', () => { const originalModule = jest.requireActual('../../GenericFunctions'); return { ...originalModule, googleApiRequest: jest.fn(), }; }); describe('Test GoogleChat, message => sendAndWait', () => { let googleChat: GoogleChat; let mockExecuteFunctions: MockProxy; beforeEach(() => { googleChat = new GoogleChat(); mockExecuteFunctions = mock(); }); afterEach(() => { jest.clearAllMocks(); }); it('should send message and put execution to wait', async () => { const items = [{ json: { data: 'test' } }]; //node mockExecuteFunctions.getInputData.mockReturnValue(items); mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('message'); mockExecuteFunctions.getNodeParameter.mockReturnValueOnce(SEND_AND_WAIT_OPERATION); mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('spaceID'); mockExecuteFunctions.getNode.mockReturnValue(mock()); mockExecuteFunctions.getInstanceId.mockReturnValue('instanceId'); //getSendAndWaitConfig mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('my message'); mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('my subject'); mockExecuteFunctions.evaluateExpression.mockReturnValueOnce('http://localhost/waiting-webhook'); mockExecuteFunctions.evaluateExpression.mockReturnValueOnce('nodeID'); mockExecuteFunctions.getNodeParameter.mockReturnValueOnce({}); mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('approval'); // configureWaitTillDate mockExecuteFunctions.getNodeParameter.mockReturnValueOnce({}); //options.limitWaitTime.values const result = await googleChat.execute.call(mockExecuteFunctions); expect(result).toEqual([items]); expect(genericFunctions.googleApiRequest).toHaveBeenCalledTimes(1); expect(mockExecuteFunctions.putExecutionToWait).toHaveBeenCalledTimes(1); expect(genericFunctions.googleApiRequest).toHaveBeenCalledWith('POST', '/v1/spaceID/messages', { text: 'my message\n\n\n**\n\n_This_ _message_ _was_ _sent_ _automatically_ _with_ __', }); }); });