Use mockReturnValue instead of mockImplementation where possible

This commit is contained in:
qm3ster
2019-03-10 13:32:36 +01:00
committed by Koen Kanters
parent 532b3b2484
commit df1825d65e
5 changed files with 90 additions and 128 deletions
+22 -28
View File
@@ -9,10 +9,8 @@ describe('Controller', () => {
beforeEach(() => {
utils.stubLogger(jest);
jest.spyOn(settings, 'getDevice').mockImplementation((ieeeAddr) => {
return {friendly_name: 'test'};
});
mqttPublish = jest.spyOn(mqtt.prototype, 'publish').mockImplementation(() => {});
jest.spyOn(settings, 'getDevice').mockReturnValue({friendly_name: 'test'});
mqttPublish = jest.spyOn(mqtt.prototype, 'publish').mockReturnValue(undefined);
controller = new Controller();
controller.zigbee = {
getDevice: () => {
@@ -38,18 +36,16 @@ describe('Controller', () => {
});
it('Should handle a zigbee message when include_device_information is set', () => {
jest.spyOn(settings, 'get').mockImplementation(() => {
return {
mqtt: {
include_device_information: true,
},
advanced: {
cache_state: false,
},
experimental: {
output: 'json',
},
};
jest.spyOn(settings, 'get').mockReturnValue({
mqtt: {
include_device_information: true,
},
advanced: {
cache_state: false,
},
experimental: {
output: 'json',
},
});
const device = {ieeeAddr: '0x12345678', modelId: 'TRADFRI bulb E27 CWS opal 600lm'};
@@ -71,18 +67,16 @@ describe('Controller', () => {
});
it('Should output to attribute', () => {
jest.spyOn(settings, 'get').mockImplementation(() => {
return {
mqtt: {
include_device_information: false,
},
advanced: {
cache_state: false,
},
experimental: {
output: 'attribute',
},
};
jest.spyOn(settings, 'get').mockReturnValue({
mqtt: {
include_device_information: false,
},
advanced: {
cache_state: false,
},
experimental: {
output: 'attribute',
},
});
const payload = {temperature: 1, humidity: 2};
+17 -23
View File
@@ -66,7 +66,7 @@ describe('DevicePublish', () => {
it('Should publish messages to zigbee devices', async () => {
zigbee.publish.mockClear();
publishEntityState.mockClear();
jest.spyOn(settings, 'getIeeeAddrByFriendlyName').mockImplementation(() => '0x00000002');
jest.spyOn(settings, 'getIeeeAddrByFriendlyName').mockReturnValue('0x00000002');
zigbee.getDevice = () => ({modelId: 'LCT003'});
devicePublish.onMQTTMessage('zigbee2mqtt/wohnzimmer.light.wall.right/set', JSON.stringify({state: 'ON'}));
expect(zigbee.publish).toHaveBeenCalledTimes(1);
@@ -512,7 +512,7 @@ describe('DevicePublish', () => {
);
it('Should publish messages to groups', async () => {
jest.spyOn(settings, 'getGroupIDByFriendlyName').mockImplementation(() => '1');
jest.spyOn(settings, 'getGroupIDByFriendlyName').mockReturnValue('1');
zigbee.publish.mockClear();
publishEntityState.mockClear();
devicePublish.onMQTTMessage('zigbee2mqtt/group/group_1/set', JSON.stringify({state: 'ON'}));
@@ -537,7 +537,7 @@ describe('DevicePublish', () => {
});
it('Should publish messages to groups with brightness_percent', async () => {
jest.spyOn(settings, 'getGroupIDByFriendlyName').mockImplementation(() => '1');
jest.spyOn(settings, 'getGroupIDByFriendlyName').mockReturnValue('1');
zigbee.publish.mockClear();
publishEntityState.mockClear();
devicePublish.onMQTTMessage('zigbee2mqtt/group/group_1/set', JSON.stringify({brightness_percent: 50}));
@@ -562,7 +562,7 @@ describe('DevicePublish', () => {
});
it('Should publish messages to groups with on and brightness', async () => {
jest.spyOn(settings, 'getGroupIDByFriendlyName').mockImplementation(() => '1');
jest.spyOn(settings, 'getGroupIDByFriendlyName').mockReturnValue('1');
zigbee.publish.mockClear();
publishEntityState.mockClear();
devicePublish.onMQTTMessage('zigbee2mqtt/group/group_1/set', JSON.stringify({state: 'ON', brightness: 50}));
@@ -587,7 +587,7 @@ describe('DevicePublish', () => {
});
it('Should publish messages to groups with off and brightness', async () => {
jest.spyOn(settings, 'getGroupIDByFriendlyName').mockImplementation(() => '1');
jest.spyOn(settings, 'getGroupIDByFriendlyName').mockReturnValue('1');
zigbee.publish.mockClear();
publishEntityState.mockClear();
devicePublish.onMQTTMessage('zigbee2mqtt/group/group_1/set', JSON.stringify({state: 'OFF', brightness: 5}));
@@ -660,12 +660,10 @@ describe('DevicePublish', () => {
});
it('Should parse topic with when base topic has multiple slashes', () => {
jest.spyOn(settings, 'get').mockImplementation(() => {
return {
mqtt: {
base_topic: 'zigbee2mqtt/at/my/home',
},
};
jest.spyOn(settings, 'get').mockReturnValue({
mqtt: {
base_topic: 'zigbee2mqtt/at/my/home',
},
});
const topic = 'zigbee2mqtt/at/my/home/my_device_id2/get';
@@ -684,12 +682,10 @@ describe('DevicePublish', () => {
});
it('Should parse topic with when base and deviceID have multiple slashes', () => {
jest.spyOn(settings, 'get').mockImplementation(() => {
return {
mqtt: {
base_topic: 'zigbee2mqtt/at/my/basement',
},
};
jest.spyOn(settings, 'get').mockReturnValue({
mqtt: {
base_topic: 'zigbee2mqtt/at/my/basement',
},
});
const topic = 'zigbee2mqtt/at/my/basement/floor0/basement/my_device_id2/set';
@@ -749,12 +745,10 @@ describe('DevicePublish', () => {
});
it('Should parse set with and slashes in base and deviceID postfix topic', () => {
jest.spyOn(settings, 'get').mockImplementation(() => {
return {
mqtt: {
base_topic: 'zigbee2mqtt/at/my/home',
},
};
jest.spyOn(settings, 'get').mockReturnValue({
mqtt: {
base_topic: 'zigbee2mqtt/at/my/home',
},
});
const topic = 'zigbee2mqtt/at/my/home/my/device/in/basement/sensor/bottom_left/get';
+32 -50
View File
@@ -19,7 +19,7 @@ describe('DeviceReceive', () => {
beforeEach(() => {
utils.stubLogger(jest);
jest.spyOn(settings, 'addDevice').mockImplementation(() => {});
jest.spyOn(settings, 'addDevice').mockReturnValue(undefined);
publishEntityState = jest.fn();
deviceReceive = new DeviceReceive(null, mqtt, null, publishEntityState);
});
@@ -65,9 +65,7 @@ describe('DeviceReceive', () => {
it('Should handle a zigbee message with 1 precision', () => {
const device = {ieeeAddr: '0x12345678'};
jest.spyOn(settings, 'getDevice').mockImplementation(() => {
return {temperature_precision: 1};
});
jest.spyOn(settings, 'getDevice').mockReturnValue({temperature_precision: 1});
const message = utils.zigbeeMessage(
device, 'msTemperatureMeasurement', 'attReport', {measuredValue: -85}, 1
);
@@ -78,9 +76,7 @@ describe('DeviceReceive', () => {
it('Should handle a zigbee message with 0 precision', () => {
const device = {ieeeAddr: '0x12345678'};
jest.spyOn(settings, 'getDevice').mockImplementation(() => {
return {temperature_precision: 0};
});
jest.spyOn(settings, 'getDevice').mockReturnValue({temperature_precision: 0});
const message = utils.zigbeeMessage(
device, 'msTemperatureMeasurement', 'attReport', {measuredValue: -85}, 1
);
@@ -91,19 +87,15 @@ describe('DeviceReceive', () => {
it('Should handle a zigbee message with 1 precision when set via device_options', () => {
const device = {ieeeAddr: '0x12345678'};
jest.spyOn(settings, 'get').mockImplementation(() => {
return {
device_options: {
temperature_precision: 1,
},
advanced: {
last_seen: 'disable',
},
};
});
jest.spyOn(settings, 'getDevice').mockImplementation(() => {
return {};
jest.spyOn(settings, 'get').mockReturnValue({
device_options: {
temperature_precision: 1,
},
advanced: {
last_seen: 'disable',
},
});
jest.spyOn(settings, 'getDevice').mockReturnValue({});
const message = utils.zigbeeMessage(
device, 'msTemperatureMeasurement', 'attReport', {measuredValue: -85}, 1
);
@@ -115,20 +107,16 @@ describe('DeviceReceive', () => {
it('Should handle a zigbee message with 2 precision when overrides device_options', () => {
const device = {ieeeAddr: '0x12345678'};
jest.spyOn(settings, 'get').mockImplementation(() => {
return {
device_options: {
temperature_precision: 1,
},
advanced: {
last_seen: 'disable',
},
};
jest.spyOn(settings, 'get').mockReturnValue({
device_options: {
temperature_precision: 1,
},
advanced: {
last_seen: 'disable',
},
});
jest.spyOn(settings, 'getDevice').mockImplementation(() => {
return {
temperature_precision: 2,
};
jest.spyOn(settings, 'getDevice').mockReturnValue({
temperature_precision: 2,
});
const message = utils.zigbeeMessage(
device, 'msTemperatureMeasurement', 'attReport', {measuredValue: -85}, 1
@@ -198,12 +186,10 @@ describe('DeviceReceive', () => {
it('Should publish last_seen epoch', () => {
const device = {ieeeAddr: '0x12345678'};
const message = utils.zigbeeMessage(device, 'genOnOff', 'attReport', {onOff: 1}, 1);
jest.spyOn(settings, 'get').mockImplementation(() => {
return {
advanced: {
last_seen: 'epoch',
},
};
jest.spyOn(settings, 'get').mockReturnValue({
advanced: {
last_seen: 'epoch',
},
});
deviceReceive.onZigbeeMessage(message, device, WXKG02LM);
expect(publishEntityState).toHaveBeenCalledTimes(1);
@@ -213,12 +199,10 @@ describe('DeviceReceive', () => {
it('Should publish last_seen ISO_8601', () => {
const device = {ieeeAddr: '0x12345678'};
const message = utils.zigbeeMessage(device, 'genOnOff', 'attReport', {onOff: 1}, 1);
jest.spyOn(settings, 'get').mockImplementation(() => {
return {
advanced: {
last_seen: 'ISO_8601',
},
};
jest.spyOn(settings, 'get').mockReturnValue({
advanced: {
last_seen: 'ISO_8601',
},
});
deviceReceive.onZigbeeMessage(message, device, WXKG02LM);
expect(publishEntityState).toHaveBeenCalledTimes(1);
@@ -228,12 +212,10 @@ describe('DeviceReceive', () => {
it('Should publish last_seen ISO_8601_local', () => {
const device = {ieeeAddr: '0x12345678'};
const message = utils.zigbeeMessage(device, 'genOnOff', 'attReport', {onOff: 1}, 1);
jest.spyOn(settings, 'get').mockImplementation(() => {
return {
advanced: {
last_seen: 'ISO_8601_local',
},
};
jest.spyOn(settings, 'get').mockReturnValue({
advanced: {
last_seen: 'ISO_8601_local',
},
});
deviceReceive.onZigbeeMessage(message, device, WXKG02LM);
expect(publishEntityState).toHaveBeenCalledTimes(1);
+15 -23
View File
@@ -37,9 +37,7 @@ describe('HomeAssistant extension', () => {
it('Should discover devices', () => {
let payload = null;
jest.spyOn(settings, 'getDevice').mockImplementation(() => {
return {friendly_name: 'my_device'};
});
jest.spyOn(settings, 'getDevice').mockReturnValue({friendly_name: 'my_device'});
homeassistant.discover('0x12345678', WSDCGQ11LM, false);
expect(mqtt.publish).toHaveBeenCalledTimes(5);
@@ -166,13 +164,11 @@ describe('HomeAssistant extension', () => {
it('Should discover devices with precision', () => {
let payload = null;
jest.spyOn(settings, 'getDevice').mockImplementation(() => {
return {
friendly_name: 'my_device',
humidity_precision: 0,
temperature_precision: 1,
pressure_precision: 2,
};
jest.spyOn(settings, 'getDevice').mockReturnValue({
friendly_name: 'my_device',
humidity_precision: 0,
temperature_precision: 1,
pressure_precision: 2,
});
homeassistant.discover('0x12345678', WSDCGQ11LM, false);
@@ -300,17 +296,15 @@ describe('HomeAssistant extension', () => {
it('Should discover devices with overriden user configuration', () => {
let payload = null;
jest.spyOn(settings, 'getDevice').mockImplementation(() => {
return {
friendly_name: 'my_device',
homeassistant: {
expire_after: 30,
icon: 'mdi:test',
temperature: {
expire_after: 90,
},
jest.spyOn(settings, 'getDevice').mockReturnValue({
friendly_name: 'my_device',
homeassistant: {
expire_after: 30,
icon: 'mdi:test',
temperature: {
expire_after: 90,
},
};
},
});
homeassistant.discover('0x12345678', WSDCGQ11LM, false);
@@ -448,9 +442,7 @@ describe('HomeAssistant extension', () => {
it('Should discover devices with cover_position', () => {
let payload = null;
jest.spyOn(settings, 'getDevice').mockImplementation(() => {
return {friendly_name: 'my_device'};
});
jest.spyOn(settings, 'getDevice').mockReturnValue({friendly_name: 'my_device'});
homeassistant.discover('0x12345678', SV01, false);
expect(mqtt.publish).toHaveBeenCalledTimes(5);
+4 -4
View File
@@ -2,10 +2,10 @@ const logger = require('../lib/util/logger');
module.exports = {
stubLogger: (jest) => {
jest.spyOn(logger, 'info').mockImplementation(() => {});
jest.spyOn(logger, 'warn').mockImplementation(() => {});
jest.spyOn(logger, 'debug').mockImplementation(() => {});
jest.spyOn(logger, 'error').mockImplementation(() => {});
jest.spyOn(logger, 'info').mockReturnValue(undefined);
jest.spyOn(logger, 'warn').mockReturnValue(undefined);
jest.spyOn(logger, 'debug').mockReturnValue(undefined);
jest.spyOn(logger, 'error').mockReturnValue(undefined);
},
zigbeeMessage: (device, cid, type, data, epId) => {
return {data: {cid, data}, type, endpoints: [{device, epId}]};