import { RpcClient } from '@sky-distribution/message-queue-rpc-library'; import { Container } from 'typedi'; import { Messenger } from '../../../services/Messenger/Messenger.service'; import { setupContainer } from '../../testUtils'; const TEST_RPC_RESPONSE = { correct: { msg: { value: 'test message response' } }, withError: { msg: { error: 'Some ERRrrrrrRORrrrR!' }, }, }; const rpcClientMocked = { sendRpcCmd: jest .fn() .mockResolvedValueOnce({ msg: TEST_RPC_RESPONSE.correct, }) .mockResolvedValueOnce({ msg: TEST_RPC_RESPONSE.withError, }), } as unknown as RpcClient; const TEST_RPC_ARGS = { cmd: 'test.command', arg: { uuaAuthorization: 'token', }, }; describe('MessengerService', () => { beforeEach(() => { jest.clearAllMocks(); setupContainer(); }); it.each([ ['without error', TEST_RPC_RESPONSE.correct], ['with error message', TEST_RPC_RESPONSE.withError], ])('should send Rpc message with send()', async (_desc, expectedResponse) => { // given Container.set('rpcClient', rpcClientMocked); const messenger = new Messenger(); const sendRpcCmd = jest.spyOn(rpcClientMocked, 'sendRpcCmd'); // when const reply = await messenger.send(TEST_RPC_ARGS.cmd, TEST_RPC_ARGS.arg); // then expect(sendRpcCmd) .toHaveBeenCalled(); const sendRpcCmdInvokedWith = sendRpcCmd.mock.calls[0][0]; expect(sendRpcCmdInvokedWith.msg) .toEqual(TEST_RPC_ARGS); expect(reply) .toEqual(expectedResponse); }); });