Permalink
Browse files

[all] Use argscheck in compass.js.

  • Loading branch information...
1 parent 9ba65d4 commit f400233bef00da6d3678d95e4abf745a1f62e07f @agrieve agrieve committed Nov 22, 2012
Showing with 19 additions and 57 deletions.
  1. +7 −27 lib/common/plugin/compass.js
  2. +12 −30 test/test.compass.js
@@ -19,7 +19,8 @@
*
*/
-var exec = require('cordova/exec'),
+var argscheck = require('cordova/argscheck'),
+ exec = require('cordova/exec'),
utils = require('cordova/utils'),
CompassHeading = require('cordova/plugin/CompassHeading'),
CompassError = require('cordova/plugin/CompassError'),
@@ -34,23 +35,13 @@ var exec = require('cordova/exec'),
* @param {CompassOptions} options The options for getting the heading data (not used).
*/
getCurrentHeading:function(successCallback, errorCallback, options) {
- // successCallback required
- if (typeof successCallback !== "function") {
- console.log("Compass Error: successCallback is not a function");
- return;
- }
-
- // errorCallback optional
- if (errorCallback && (typeof errorCallback !== "function")) {
- console.log("Compass Error: errorCallback is not a function");
- return;
- }
+ argscheck.checkArgs('fFO', 'compass.getCurrentHeading', arguments);
var win = function(result) {
var ch = new CompassHeading(result.magneticHeading, result.trueHeading, result.headingAccuracy, result.timestamp);
successCallback(ch);
};
- var fail = function(code) {
+ var fail = errorCallback && function(code) {
var ce = new CompassError(code);
errorCallback(ce);
};
@@ -70,22 +61,11 @@ var exec = require('cordova/exec'),
* specifies to watch via a distance filter rather than time.
*/
watchHeading:function(successCallback, errorCallback, options) {
+ argscheck.checkArgs('fFO', 'compass.watchHeading', arguments);
// Default interval (100 msec)
var frequency = (options !== undefined && options.frequency !== undefined) ? options.frequency : 100;
var filter = (options !== undefined && options.filter !== undefined) ? options.filter : 0;
- // successCallback required
- if (typeof successCallback !== "function") {
- console.log("Compass Error: successCallback is not a function");
- return;
- }
-
- // errorCallback optional
- if (errorCallback && (typeof errorCallback !== "function")) {
- console.log("Compass Error: errorCallback is not a function");
- return;
- }
-
var id = utils.createUUID();
if (filter > 0) {
// is an iOS request for watch by filter, no timer needed
@@ -109,8 +89,8 @@ var exec = require('cordova/exec'),
// Stop javascript timer & remove from timer list
if (id && timers[id]) {
if (timers[id] != "iOS") {
- clearInterval(timers[id]);
- } else {
+ clearInterval(timers[id]);
+ } else {
// is iOS watch by filter so call into device to stop
exec(null, null, "Compass", "stopHeading", []);
}
View
@@ -29,26 +29,17 @@ describe("compass", function () {
});
describe("when getting the current heading", function () {
- it("logs an error and doesn't call exec when no success callback given", function () {
- spyOn(console, "log");
- compass.getCurrentHeading();
- expect(console.log).toHaveBeenCalledWith("Compass Error: successCallback is not a function");
- expect(exec).not.toHaveBeenCalledWith(jasmine.any(Function), undefined, "Compass", "getHeading", []);
+ it("throws an error and doesn't call exec when no success callback given", function () {
+ expect(function() { compass.getCurrentHeading() }).toThrow();
});
- it("logs an error and doesn't call exec when success isn't a function", function () {
- spyOn(console, "log");
- compass.getCurrentHeading(12);
- expect(console.log).toHaveBeenCalledWith("Compass Error: successCallback is not a function");
- expect(exec).not.toHaveBeenCalledWith(jasmine.any(Function), undefined, "Compass", "getHeading", []);
+ it("throws an error and doesn't call exec when success isn't a function", function () {
+ expect(function() { compass.getCurrentHeading(12) }).toThrow();
});
- it("logs an error and doesn't call exec when error isn't a function", function () {
+ it("throws an error and doesn't call exec when error isn't a function", function () {
var func = function () {};
- spyOn(console, "log");
- compass.getCurrentHeading(func, 12);
- expect(console.log).toHaveBeenCalledWith("Compass Error: errorCallback is not a function");
- expect(exec).not.toHaveBeenCalledWith(func, 12, "Compass", "getHeading", []);
+ expect(function() { compass.getCurrentHeading(func, 12) }).toThrow();
});
it("calls exec", function () {
@@ -65,26 +56,17 @@ describe("compass", function () {
spyOn(window, "setInterval").andReturn("def");
});
- it("logs an error and doesn't call exec when no success callback given", function () {
- spyOn(console, "log");
- compass.watchHeading();
- expect(console.log).toHaveBeenCalledWith("Compass Error: successCallback is not a function");
- expect(exec).not.toHaveBeenCalledWith(jasmine.any(Function), undefined, "Compass", "getHeading", []);
+ it("throws an error and doesn't call exec when no success callback given", function () {
+ expect(function() { compass.watchHeading() }).toThrow();
});
- it("logs an error and doesn't call exec when success isn't a function", function () {
- spyOn(console, "log");
- compass.watchHeading(12);
- expect(console.log).toHaveBeenCalledWith("Compass Error: successCallback is not a function");
- expect(exec).not.toHaveBeenCalledWith(jasmine.any(Function), undefined, "Compass", "getHeading", []);
+ it("throws an error and doesn't call exec when success isn't a function", function () {
+ expect(function() { compass.watchHeading(12) }).toThrow();
});
- it("logs an error and doesn't call exec when error isn't a function", function () {
+ it("throws an error and doesn't call exec when error isn't a function", function () {
var func = function () {};
- spyOn(console, "log");
- compass.watchHeading(func, 12);
- expect(console.log).toHaveBeenCalledWith("Compass Error: errorCallback is not a function");
- expect(exec).not.toHaveBeenCalledWith(func, 12, "Compass", "getHeading", []);
+ expect(function() { compass.watchHeading(func, 12) }).toThrow();
});
it("generates and returns a uuid for the watch", function () {

0 comments on commit f400233

Please sign in to comment.