Skip to content
This repository was archived by the owner on Mar 4, 2021. It is now read-only.

Added unit tests for upgrade.js #186

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion OpenVBX/tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,22 @@ Data that is populated includes:
## Run

$ cd OpenVBX/tests
$ phpunit AllTests.php
$ phpunit AllTests.php


## Javascript

### What's needed to run

The Javascript tests use the jasmine-node (http://github.com/mhevery/jasmine-node) test framework. They require both the framework and nodejs (http://nodejs.org).

### How to run

From the tests directory, type

$ jasmine-node js

### Suggestions for writing JS tests

- Don't use the DOM directly (see upgrade.js as an example)
- Dependency injection is your friend
214 changes: 214 additions & 0 deletions OpenVBX/tests/js/upgrade.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
/**
* "The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/

* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.

* The Original Code is OpenVBX, released June 15, 2010.

* The Initial Developer of the Original Code is Twilio Inc.
* Portions created by Twilio Inc. are Copyright (C) 2010.
* All Rights Reserved.

* Contributor(s):
**/

require('../../../assets/j/upgrade.js');

describe("upgrade page", function() {
var jQuery_mock;
var setTimeout_mock;
var steps_mock;
var jQuery_return;

// This is run before every 'it' block. It'll setup our mocks.

beforeEach(function(){
jQuery_mock = jasmine.createSpy('jQuery');
setTimeout_mock = jasmine.createSpy('setTimeout');
steps_mock = jasmine.createSpy('steps');
steps_mock.setButtonLoading = jasmine.createSpy('setButtonLoading');
jQuery_return = {
'Steps' : steps_mock,
'text' : function() {}
};
jQuery_mock.andReturn(jQuery_return);
});

it("should be defined", function() {
expect(upgrade_handler).toBeDefined();
});

it("will act on the '#upgrade-steps' DOM object", function() {
// We pass in our mocks
upgrade_handler({'$' : jQuery_mock, 'setTimeout' : setTimeout_mock})();

expect(jQuery_mock.argsForCall[0][0]).toEqual("#upgrade-steps");
});

it("will call the Steps method", function() {
upgrade_handler({'$' : jQuery_mock, 'setTimeout' : setTimeout_mock})();

expect(
steps_mock
).toHaveBeenCalled();
});

it("will call Steps with an object with validateCallback objects",
function() {
upgrade_handler({'$' : jQuery_mock, 'setTimeout' : setTimeout_mock})();
var first_args = steps_mock.mostRecentCall.args[0];

expect(first_args['validateCallbacks']).toBeDefined();
['prev',
'next',
'submit'].forEach(function(member) {
expect(first_args['validateCallbacks'][member]).toBeDefined();
});
});

it("will call Steps with a stepLoadCallback",
function() {
upgrade_handler({'$' : jQuery_mock, 'setTimeout' : setTimeout_mock})();
var first_args = steps_mock.mostRecentCall.args[0];

expect(first_args['stepLoadCallback']).toBeDefined();
});

it("will check for an error", function() {
upgrade_handler({'$' : jQuery_mock, 'setTimeout' : setTimeout_mock})();

expect(jQuery_mock.mostRecentCall.args[0]).toEqual(".error");
});

it("will handle errors correctly", function() {
steps_mock.toggleError = jasmine.createSpy('toggleError');
jQuery_return['text'] = function() { return "Error"; };

upgrade_handler({'$' : jQuery_mock, 'setTimeout' : setTimeout_mock})();

expect(typeof(setTimeout_mock.mostRecentCall.args[0])).toEqual(
"function"
);

expect(setTimeout_mock.mostRecentCall.args[1]).toEqual(1000);
setTimeout_mock.mostRecentCall.args[0]();

expect(jQuery_mock.mostRecentCall.args[0]).toEqual('#upgrade-steps');
expect(steps_mock.toggleError.mostRecentCall.args[0]).toEqual(true);
});

it("will handle ajax errors correctly", function() {
jQuery_mock.ajax = jasmine.createSpy('ajax');
jQuery_mock.ajax.andCallFake(function (args) {
args['error'] ({}, "error text", "HTTP Error");
});
var triggerError_mock = jasmine.createSpy('triggerError');
jQuery_return['triggerError'] = triggerError_mock;

upgrade_handler({
'$' : jQuery_mock,
'setTimeout' : setTimeout_mock,
'OpenVBX' : {
'home' : "somewhere"
}
})();

var first_args = steps_mock.mostRecentCall.args[0];
var validateCallbacks = first_args['validateCallbacks'];
var res = validateCallbacks['submit'](1, 2);

expect(
typeof(triggerError_mock.mostRecentCall.args[0])
).toEqual("string");
});

it("will handle success handler errors correctly", function() {
jQuery_mock.ajax = jasmine.createSpy('ajax');
jQuery_mock.ajax.andCallFake(function (args) {
args['success'] ({
'success' : false,
'message' : "Error"
});
});

var triggerError_mock = jasmine.createSpy('triggerError');
jQuery_return['triggerError'] = triggerError_mock;
upgrade_handler({
'$' : jQuery_mock,
'setTimeout' : setTimeout_mock,
'OpenVBX' : {
'home' : "somewhere"
}
})();

var first_args = steps_mock.mostRecentCall.args[0];
var validateCallbacks = first_args['validateCallbacks'];
var res = validateCallbacks['submit'](1, 2);

expect(triggerError_mock.mostRecentCall.args[0]).toEqual("Error");
});

it("will ask for the right url", function() {
jQuery_mock.ajax = jasmine.createSpy('ajax');
jQuery_return['triggerError'] = function() {};
upgrade_handler({
'$' : jQuery_mock,
'setTimeout' : setTimeout_mock,
'OpenVBX' : {
'home' : "somewhere"
}
})();

var first_args = steps_mock.mostRecentCall.args[0];
var validateCallbacks = first_args['validateCallbacks'];
var res = validateCallbacks['submit'](1, 2);

expect(jQuery_mock.ajax.mostRecentCall.args[0]['url']).toEqual(
"somewhere/upgrade/setup"
);
expect(jQuery_mock.ajax.mostRecentCall.args[0]['type']).toEqual(
"post"
);
});

it("will correctly set the loading state of the submit button", function() {
var button_states = [];

steps_mock.setButtonLoading.andCallFake(function(target, bool) {
button_states.push({
'target' : target,
'bool' : bool
});
});

jQuery_mock.ajax = function () {};
jQuery_return['triggerError'] = function() {};
upgrade_handler({
'$' : jQuery_mock,
'setTimeout' : setTimeout_mock,
'OpenVBX' : {
'home' : "somewhere"
}
})();

var first_args = steps_mock.mostRecentCall.args[0];
var validateCallbacks = first_args['validateCallbacks'];
var res = validateCallbacks['submit'](1, 2);

[{'target':'submit', 'bool': true},
{'target':'submit', 'bool': false}].forEach(function(exp, index) {
expect(button_states[index]['target']).toEqual(
exp['target']
);
expect(button_states[index]['bool']).toEqual(
exp['bool']
);
});
});
});
88 changes: 50 additions & 38 deletions assets/j/upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,58 @@
* Contributor(s):
**/

(function() {
var uh = (function (deps) {
return function() {
deps.$('#upgrade-steps').Steps({
validateCallbacks : {
prev : function(stepId, step) { return true; },
next : function(stepId, step) { return true; },
submit : function(stepId, step) {
var _success = false,
_this = deps.$(this);

$(document).ready(function() {

$('#upgrade-steps').Steps({
validateCallbacks : {
prev : function(stepId, step) { return true; },
next : function(stepId, step) { return true; },
submit : function(stepId, step) {
var _success = false,
_this = $(this);

_this.Steps.setButtonLoading('submit', true);

$.ajax({
url : OpenVBX.home + '/upgrade/setup',
data : {},
type : 'post',
async : false,
dataType : 'json',
success: function(r) {
_success = r.success;
if (!r.success) {
_this.triggerError(r.message);
}
},
error : function(XHR, textStatus, errorThrown) {
_this.triggerError('An application error occurred. Please try again.');
_this.Steps.setButtonLoading('submit', true);

deps.$.ajax({
url : deps.OpenVBX.home + '/upgrade/setup',
data : {},
type : 'post',
async : false,
dataType : 'json',
success: function(r) {
_success = r.success;
if (!r.success) {
_this.triggerError(r.message);
}
},
error : function(XHR, textStatus, errorThrown) {
_this.triggerError('An application error occurred. Please try again.');
}
});

_this.Steps.setButtonLoading('submit', false);
return _success;
}
});

_this.Steps.setButtonLoading('submit', false);
return _success;
},
stepLoadCallback : function(stepId, step) { return true; }
});

if(deps.$('.error').text() != '') {
deps.setTimeout(function() {
deps.$('#upgrade-steps').Steps.toggleError(true)
}, 1000);
}
},
stepLoadCallback : function(stepId, step) { return true; }
}
});

if($('.error').text() != '') {
setTimeout(function() {
$('#upgrade-steps').Steps.toggleError(true)
}, 1000);

if (typeof(document) != "undefined")
$(document).ready(uh({
'$' : $,
'setTimeout' : setTimeout,
'OpenVBX' : OpenVBX
}));
else {
upgrade_handler = uh;
}
});
})();