lundi 2 janvier 2017

Is it a bad practice to expose private functions in module pattern?

I am working on an application that will get the user's coordinates using the HTML5 Geolocation api. What I am stuck on is how should I test the private functions? I am new to the test driven development so pardon me because I am still learning the ropes.

Here's the app:

var countryBot = (function($){
    /**
     * country div
     *
     * @type {Element}
     */
    var $country = $('#countryDiv');

    var api = {};

    /**
     * Makes a call to /country endpoint with lat, lon
     *
     * @param {int} lat latitude from geolocation api
     *
     * @param {int} lon longitude from geolocation api
     */
    var getCountry = function(lat, lon) {
        var url = '/country?lat=' + lat + '&lon=' + lon;
        $.ajax({
            url: url,
            type: 'GET',
            success: showCountry
        });
    };

    /**
     * Calls the render function to show data
     *
     * @param {object} data Data from the call
     */
    var showCountry = function(data) {
      render(data, $country);
    };

    /**
     * Shows the data
     *
     * @param {object} data Data from the call
     *
     * @param {element} element to which we should append the data
     */
    var render  = function(data, el) {
        el.append(data.name);
    };

    var init = function() {
        if('geolocation' in navigator) {
            navigator.geolocation.getCurrentPosition(function(position) {
                getCountry(position.coords.latitude, position.coords.longitude);
            });
        }
    };

    api.init = init;

    return api;
})($);

countryBot.init();

Or should I just test the callback in the init function?

Even though this code works without any issues, but is it possible to make this code more testable?

Aucun commentaire:

Enregistrer un commentaire