Last active
March 15, 2017 18:48
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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