vendredi 9 novembre 2018

How do I write a test for this javascript code?

I'm trying to write a plugin for HomeBridge and have run in to a problem. I started out by just writing some code, start up Homebridge and test it. That worked out find in the beginning but after a while as I added more functionality every time I start HomeBridge there's a lot of testing just to ensure that I haven't broken anything. I mainly work with Java and have just started out with JavaScript. I have copied the patten on how the plugin should be designed. Theres very little documentation about how to write a plugin so I'm kind of in the dark here when it comes to best practice and so on. I have simplified the code so it won't take that much space but the structure is intact. So my question is: How do I test this code?

index.js

let Service, Characteristic;

let request = require('request');

module.exports = function(homebridge) {
    Service = homebridge.hap.Service;
    Characteristic = homebridge.hap.Characteristic;
    homebridge.registerAccessory("homebridge-myplugin", "MyPlugin", MyPluginDevice);
};

function MyPluginDevice(log, config) {
    this.log = log;
    this.url = config['url'];
    this.id = config['id'];
}

MyPluginDevice.prototype = {
    getSwitchOnCharacteristic: function(callback) {
        this.httpRequest(this.url, null, 'GET', function(error, response, body) {
                if (error) {
                    this.log("Fail: %s", error.message);
                    callback(error);
                } else {
                    const newState = body['state'];
                    return callback(null, newState);
                }
            }.bind(this)
        );
    },

    setSwitchOnCharacteristic: function(state, callback) {
        this.httpRequest(this.url, requestBody, 'POST', function(error, response, body) {
            if (error) {
                this.log("Fail: %s", error.message);
                callback(error);
            } else {

                callback();
            }
        }.bind(this));
    },

    httpRequest: function(url, body, theMethod, callback) {
        request(
            {
                url: url,
                body: body,
                method: theMethod,
                auth: {
                    user: 'nexa',
                    pass: 'nexa',
                    sendImmediately: false
                },
                json: true
            },
            function(error, response, body) {
                callback(error, response, body)
            })
    },

    getServices: function() {
        let informationService = new Service.AccessoryInformation();
        informationService.setCharacteristic(Characteristic.Manufacturer, "My Nexa plugin").setCharacteristic(Characteristic.Model, "My Nexa Plugin Model").setCharacteristic(Characteristic.SerialNumber, "A very special number");
        this.switchService = new Service.Switch(this.name);
        this.switchService
            .getCharacteristic(Characteristic.On)
            .on("get", this.getSwitchOnCharacteristic.bind(this))
            .on("set", this.setSwitchOnCharacteristic.bind(this));
        return [this.switchService];
    }
};

After having been at it for a while I can't write a test for this code. I'm having problem with variables being null and however I try to get around it I always end up with some part of the code not initiated. I have tried:

let MyPluginDevice = require('./index');
let myDevice = new MyPluginDevice(homebridgeMock); 

but that leaves me with a problem with getSwitchOnCharacteristic and setSwitchOnCharacteristic. My other approach is to access the MyPluginDevice through my homebridgeMock. But that leaves me with getSwitchOnCharacteristic and setSwitchOnCharacteristicas null or not a function.

I'm kind of out of ideas and my skills is not that good so I can spot the problem or of I have implemented the code in a way that it can't be tested. I got no idea how other developers have done when writing a plugin but I would feel much safer if I could have a few tests running.

Help me Stackoverflow, you are my only hope!

Aucun commentaire:

Enregistrer un commentaire