mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
refactor(MQTT Node): Refactor, fix duplicate triggers, and add Unit tests (#9847)
Co-authored-by: Elias Meire <elias@meire.dev>
This commit is contained in:
committed by
GitHub
parent
e51de9d391
commit
164ec72c0d
56
packages/nodes-base/nodes/MQTT/test/Mqtt.node.test.ts
Normal file
56
packages/nodes-base/nodes/MQTT/test/Mqtt.node.test.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import type { MqttClient } from 'mqtt';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import type { ICredentialDataDecryptedObject, IExecuteFunctions } from 'n8n-workflow';
|
||||
|
||||
import { Mqtt } from '../Mqtt.node';
|
||||
import { createClient } from '../GenericFunctions';
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user