Files
alighasami 3d5eaf9445
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
first commit
2026-03-17 16:22:57 +03:30

57 lines
2.2 KiB
TypeScript

import { mock } from 'jest-mock-extended';
import type { MqttClient } from 'mqtt';
import type { ICredentialDataDecryptedObject, IExecuteFunctions } from 'n8n-workflow';
import { createClient } from '../GenericFunctions';
import { Mqtt } from '../Mqtt.node';
jest.mock('../GenericFunctions', () => {
const mockMqttClient = mock<MqttClient>();
return {
createClient: jest.fn().mockResolvedValue(mockMqttClient),
};
});
describe('MQTT Node', () => {
const credentials = mock<ICredentialDataDecryptedObject>();
const executeFunctions = mock<IExecuteFunctions>();
beforeEach(() => {
jest.clearAllMocks();
executeFunctions.getCredentials.calledWith('mqtt').mockResolvedValue(credentials);
executeFunctions.getInputData.mockReturnValue([{ json: { testing: true } }]);
executeFunctions.getNodeParameter.calledWith('topic', 0).mockReturnValue('test/topic');
executeFunctions.getNodeParameter.calledWith('options', 0).mockReturnValue({});
});
it('should publish input data', async () => {
executeFunctions.getNodeParameter.calledWith('sendInputData', 0).mockReturnValue(true);
const result = await new Mqtt().execute.call(executeFunctions);
expect(result).toEqual([[{ json: { testing: true } }]]);
expect(executeFunctions.getCredentials).toHaveBeenCalledTimes(1);
expect(executeFunctions.getNodeParameter).toHaveBeenCalledTimes(3);
const mockMqttClient = await createClient(mock());
expect(mockMqttClient.publishAsync).toHaveBeenCalledWith('test/topic', '{"testing":true}', {});
expect(mockMqttClient.endAsync).toHaveBeenCalledTimes(1);
});
it('should publish a custom message', async () => {
executeFunctions.getNodeParameter.calledWith('sendInputData', 0).mockReturnValue(false);
executeFunctions.getNodeParameter.calledWith('message', 0).mockReturnValue('Hello, MQTT!');
const result = await new Mqtt().execute.call(executeFunctions);
expect(result).toEqual([[{ json: { testing: true } }]]);
expect(executeFunctions.getCredentials).toHaveBeenCalledTimes(1);
expect(executeFunctions.getNodeParameter).toHaveBeenCalledTimes(4);
const mockMqttClient = await createClient(mock());
expect(mockMqttClient.publishAsync).toHaveBeenCalledWith('test/topic', 'Hello, MQTT!', {});
expect(mockMqttClient.endAsync).toHaveBeenCalledTimes(1);
});
});