Skip to content

Instantly share code, notes, and snippets.

@davidcyp
Last active March 15, 2017 18:48
Show Gist options
  • Save davidcyp/afee65d5efc622733ab143c9acd444d2 to your computer and use it in GitHub Desktop.
Save davidcyp/afee65d5efc622733ab143c9acd444d2 to your computer and use it in GitHub Desktop.
Test based on the outcome of the device capabilities, eg. absence or presence of network
//
// This gist explains how to test alternate paths based on device capabilities which are not under your control,
// eg. presence of network, network quality(3G, Edge, GPRS), ...
//
//
// When this code would be executed in the titanium runtime, the only way to test both code branches is by switching
// the network on/off.
//
function isNetworkPresent(){
if(_.isEqual(Ti.Network.NETWORK_NONE, Ti.Network.getNetworkType()){
Ti.API.warn('Please connect to a network and try again');
return false;
} else {
Ti.App.fireEvent('bar');
return true;
}
}
//
// Both cases can be tested without manually intervening on the device/simulator
//
describe('network.availability test', function() {
Ti = require('tiunit/jsca.api.parser').parse();
var controller;
beforeEach(function () {
controller = require('../app/controller');
spyOn(Ti.API, 'warn');
spyOn(Ti.App, 'fireEvent');
});
it('returns true when there is no network available', function() {
spyOn(Ti.Network, 'getNetworkType').and.returnValue(Ti.Network.NETWORK_NONE);
var result = controller.isNetworkPresent();
expect(result).toBe(false);
expect(Ti.API.warn).toHaveBeenCalledWith("Please connect to a network and try again");
});
it('returns true when there is network available', function(){
spyOn(Ti.Network, 'getNetworkType').and.returnValue(Ti.Network.NETWORK_WIFI);
var result = controller.isNetworkPresent();
expect(result).toBe(true);
expect(Ti.App.fireEvent).toHaveBeenCalledWith("bar");
expect(Ti.API.warn).not.toHaveBeenCalled();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment