mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-15 00:54:06 -08:00
ee7147c6b3
Some checks are pending
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions
Benchmark Docker Image CI / build (push) Waiting to run
67 lines
1.8 KiB
TypeScript
67 lines
1.8 KiB
TypeScript
import { mock } from 'jest-mock-extended';
|
|
import { MqttClient } from 'mqtt';
|
|
import { ApplicationError } from 'n8n-workflow';
|
|
|
|
import { createClient, type MqttCredential } from '../GenericFunctions';
|
|
|
|
describe('createClient', () => {
|
|
beforeEach(() => jest.clearAllMocks());
|
|
|
|
it('should create a client with minimal credentials', async () => {
|
|
const mockConnect = jest.spyOn(MqttClient.prototype, 'connect').mockImplementation(function (
|
|
this: MqttClient,
|
|
) {
|
|
setImmediate(() => this.emit('connect', mock()));
|
|
return this;
|
|
});
|
|
|
|
const credentials = mock<MqttCredential>({
|
|
protocol: 'mqtt',
|
|
host: 'localhost',
|
|
port: 1883,
|
|
clean: true,
|
|
clientId: 'testClient',
|
|
ssl: false,
|
|
});
|
|
const client = await createClient(credentials);
|
|
|
|
expect(mockConnect).toBeCalledTimes(1);
|
|
expect(client).toBeDefined();
|
|
expect(client).toBeInstanceOf(MqttClient);
|
|
expect(client.options).toMatchObject({
|
|
protocol: 'mqtt',
|
|
host: 'localhost',
|
|
port: 1883,
|
|
clean: true,
|
|
clientId: 'testClient',
|
|
});
|
|
});
|
|
|
|
it('should reject with ApplicationError on connection error and close connection', async () => {
|
|
const mockConnect = jest.spyOn(MqttClient.prototype, 'connect').mockImplementation(function (
|
|
this: MqttClient,
|
|
) {
|
|
setImmediate(() => this.emit('error', new Error('Connection failed')));
|
|
return this;
|
|
});
|
|
const mockEnd = jest.spyOn(MqttClient.prototype, 'end').mockImplementation();
|
|
|
|
const credentials: MqttCredential = {
|
|
protocol: 'mqtt',
|
|
host: 'localhost',
|
|
port: 1883,
|
|
clean: true,
|
|
clientId: 'testClientId',
|
|
username: 'testUser',
|
|
password: 'testPass',
|
|
ssl: false,
|
|
};
|
|
|
|
const clientPromise = createClient(credentials);
|
|
|
|
await expect(clientPromise).rejects.toThrow(ApplicationError);
|
|
expect(mockConnect).toBeCalledTimes(1);
|
|
expect(mockEnd).toBeCalledTimes(1);
|
|
});
|
|
});
|