mirror of
https://github.com/Koenkk/zigbee2mqtt.git
synced 2026-10-15 18:49:56 +00:00
Fix MQTT tests
This commit is contained in:
+23
-2
@@ -1,6 +1,7 @@
|
||||
process.env.NOTIFY_SOCKET = 'mocked';
|
||||
const data = require('./stub/data');
|
||||
const logger = require('./stub/logger');
|
||||
const Transport = require('winston-transport');
|
||||
const zigbeeHerdsman = require('./stub/zigbeeHerdsman');
|
||||
const MQTT = require('./stub/mqtt');
|
||||
const path = require('path');
|
||||
@@ -325,21 +326,41 @@ describe('Controller', () => {
|
||||
});
|
||||
|
||||
it('Handle mqtt message', async () => {
|
||||
class DummyTransport extends Transport {
|
||||
log(info, callback) {}
|
||||
}
|
||||
const transport = new DummyTransport();
|
||||
logger.setTransportsEnabled(true);
|
||||
let spyTransport = jest.spyOn(transport, 'log').mockImplementation();
|
||||
logger.addTransport(transport);
|
||||
|
||||
await controller.start();
|
||||
logger.debug.mockClear();
|
||||
await MQTT.events.message('dummytopic', 'dummymessage');
|
||||
expect(logger.debug).toHaveBeenCalledWith("Received MQTT message on 'dummytopic' with data 'dummymessage'", LOG_MQTT_NS);
|
||||
expect(spyTransport).toHaveBeenCalledWith({"level": "debug", "message": "Received MQTT message on 'dummytopic' with data 'dummymessage'", "namespace": LOG_MQTT_NS}, expect.any(Function));
|
||||
logger.removeTransport(transport)
|
||||
logger.setTransportsEnabled(false);
|
||||
});
|
||||
|
||||
it('Skip MQTT messages on topic we published to', async () => {
|
||||
class DummyTransport extends Transport {
|
||||
log(info, callback) {}
|
||||
}
|
||||
const transport = new DummyTransport();
|
||||
logger.setTransportsEnabled(true);
|
||||
let spyTransport = jest.spyOn(transport, 'log').mockImplementation();
|
||||
logger.addTransport(transport);
|
||||
|
||||
await controller.start();
|
||||
logger.debug.mockClear();
|
||||
await MQTT.events.message('zigbee2mqtt/skip-this-topic', 'skipped');
|
||||
expect(logger.debug).toHaveBeenCalledWith("Received MQTT message on 'zigbee2mqtt/skip-this-topic' with data 'skipped'", LOG_MQTT_NS);
|
||||
expect(spyTransport).toHaveBeenCalledWith({"level": "debug", "message": "Received MQTT message on 'zigbee2mqtt/skip-this-topic' with data 'skipped'", "namespace": LOG_MQTT_NS}, expect.any(Function));
|
||||
logger.debug.mockClear();
|
||||
await controller.mqtt.publish('skip-this-topic', '', {});
|
||||
await MQTT.events.message('zigbee2mqtt/skip-this-topic', 'skipped');
|
||||
expect(logger.debug).toHaveBeenCalledTimes(0);
|
||||
logger.removeTransport(transport)
|
||||
logger.setTransportsEnabled(false);
|
||||
});
|
||||
|
||||
it('On zigbee event message', async () => {
|
||||
|
||||
+6
-5
@@ -5,8 +5,9 @@ let namespacedLevels = {};
|
||||
let transports = [];
|
||||
|
||||
let transportsEnabled = false;
|
||||
const callTransports = (level, message, namespace) => {
|
||||
const callTransports = (level, messageOrLambda, namespace) => {
|
||||
if (transportsEnabled) {
|
||||
const message = messageOrLambda instanceof Function ? messageOrLambda() : messageOrLambda;
|
||||
for (const transport of transports) {
|
||||
transport.log({level, message, namespace}, () => {});
|
||||
}
|
||||
@@ -15,10 +16,10 @@ const callTransports = (level, message, namespace) => {
|
||||
|
||||
const mock = {
|
||||
init: jest.fn(),
|
||||
info: jest.fn().mockImplementation((msg, namespace = 'z2m') => callTransports('info', msg, namespace)),
|
||||
warning: jest.fn().mockImplementation((msg, namespace = 'z2m') => callTransports('warning', msg, namespace)),
|
||||
error: jest.fn().mockImplementation((msg, namespace = 'z2m') => callTransports('error', msg, namespace)),
|
||||
debug: jest.fn().mockImplementation((msg, namespace = 'z2m') => callTransports('debug', msg, namespace)),
|
||||
info: jest.fn().mockImplementation((messageOrLambda, namespace = 'z2m') => callTransports('info', messageOrLambda, namespace)),
|
||||
warning: jest.fn().mockImplementation((messageOrLambda, namespace = 'z2m') => callTransports('warning', messageOrLambda, namespace)),
|
||||
error: jest.fn().mockImplementation((messageOrLambda, namespace = 'z2m') => callTransports('error', messageOrLambda, namespace)),
|
||||
debug: jest.fn().mockImplementation((messageOrLambda, namespace = 'z2m') => callTransports('debug', messageOrLambda, namespace)),
|
||||
cleanup: jest.fn(),
|
||||
logOutput: jest.fn(),
|
||||
add: (transport) => transports.push(transport),
|
||||
|
||||
Reference in New Issue
Block a user