From df1825d65e3c1d1ddbc70be71f3c584c3e26124b Mon Sep 17 00:00:00 2001 From: qm3ster Date: Sun, 10 Mar 2019 05:11:50 +0200 Subject: [PATCH] Use `mockReturnValue` instead of `mockImplementation` where possible --- test/controller.test.js | 50 ++++++++++------------- test/devicePublish.test.js | 40 ++++++++----------- test/deviceReceive.test.js | 82 +++++++++++++++----------------------- test/homeassistant.test.js | 38 +++++++----------- test/utils.js | 8 ++-- 5 files changed, 90 insertions(+), 128 deletions(-) diff --git a/test/controller.test.js b/test/controller.test.js index 1108ac553..563146d93 100644 --- a/test/controller.test.js +++ b/test/controller.test.js @@ -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}; diff --git a/test/devicePublish.test.js b/test/devicePublish.test.js index 1d4d001f5..1789bcdfa 100644 --- a/test/devicePublish.test.js +++ b/test/devicePublish.test.js @@ -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'; diff --git a/test/deviceReceive.test.js b/test/deviceReceive.test.js index e3be20c72..7bc2486b6 100644 --- a/test/deviceReceive.test.js +++ b/test/deviceReceive.test.js @@ -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); diff --git a/test/homeassistant.test.js b/test/homeassistant.test.js index 83559f653..dcd0dc882 100644 --- a/test/homeassistant.test.js +++ b/test/homeassistant.test.js @@ -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); diff --git a/test/utils.js b/test/utils.js index 27a46882e..756838f94 100644 --- a/test/utils.js +++ b/test/utils.js @@ -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}]};