Google Analytics wrapper function tests

Testing out simple validation for wrapper.

by Steven Senkus

HTML

<ul id="events"></ul>

CSS

body, li {
    background-color: #000;
    color: #eee;
}
.error {
    color: #d22;
}
.success {
    color: #2a2;
}

JavaScript

// SETUP
var $window = window;
$window.ga = function(a,obj){
    $('#events').append('<li class="success">' + JSON.stringify(obj) + '</li>');
};

function sendEvent(category, action, label, value) {
    var fieldsObj = {
        'hitType': 'event',
    }

    if (category && action) {
        fieldsObj.eventCategory = category;
        fieldsObj.eventAction = action;
    } else {
        $('#events').append('<li class="error">ERROR: ' + JSON.stringify(arguments) + '</li>');
        return;
    }
    if (label) {
        fieldsObj.eventLabel = label;
    }
    if (value) {
        fieldsObj.eventValue = value;
    }
    $window.ga('send', fieldsObj);
}


var testCases = {
    shouldFail: [
        [],
        [null],
        [undefined],
        [false]
        ['cat', null],
        ['cat', undefined],
        ['cat', false],
        [null, null],
        [null, undefined],
        [null, false],
        [null, 'act'],
        [undefined, 'act'],
        [false, 'act']


    ],
    shouldPass: [
        ['cat', 'act'],
        ['cat', 'act', null],
        ['cat', 'act', undefined],
        ['cat', 'act', 'label', 123],
        ['cat', 'act', null, 123123]

    ]
}

// Should be errors
$('<li/>', {
    text: 'SHOULD FAIL'
}).appendTo('#events');

testCases.shouldFail.forEach(function (arr) {
    sendEvent.apply(null, arr)
});

// Should pass validation
$('<li/>', {
    text: 'SHOULD PASS'
}).appendTo('#events');

testCases.shouldPass.forEach(function (arr) {
    sendEvent.apply(null, arr);
});