You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2012/11/22 21:47:47 UTC

[2/9] js commit: [all] Use argscheck in contacts.js

[all] Use argscheck in contacts.js

https://issues.apache.org/jira/browse/CB-1892


Project: http://git-wip-us.apache.org/repos/asf/cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-js/commit/58990dad
Tree: http://git-wip-us.apache.org/repos/asf/cordova-js/tree/58990dad
Diff: http://git-wip-us.apache.org/repos/asf/cordova-js/diff/58990dad

Branch: refs/heads/master
Commit: 58990dad50f337fd8c71250398d6586ae2b055ac
Parents: f400233
Author: Andrew Grieve <ag...@chromium.org>
Authored: Thu Nov 22 15:38:40 2012 -0500
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Thu Nov 22 15:45:06 2012 -0500

----------------------------------------------------------------------
 lib/common/plugin/contacts.js |   17 +++++++----------
 test/test.contacts.js         |   31 ++++++++++++++-----------------
 2 files changed, 21 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-js/blob/58990dad/lib/common/plugin/contacts.js
----------------------------------------------------------------------
diff --git a/lib/common/plugin/contacts.js b/lib/common/plugin/contacts.js
index 2f5a51a..96964ca 100644
--- a/lib/common/plugin/contacts.js
+++ b/lib/common/plugin/contacts.js
@@ -19,7 +19,8 @@
  *
 */
 
-var exec = require('cordova/exec'),
+var argscheck = require('cordova/argscheck'),
+    exec = require('cordova/exec'),
     ContactError = require('cordova/plugin/ContactError'),
     utils = require('cordova/utils'),
     Contact = require('cordova/plugin/Contact');
@@ -38,13 +39,9 @@ var contacts = {
      * @return array of Contacts matching search criteria
      */
     find:function(fields, successCB, errorCB, options) {
-        if (!successCB) {
-            throw new TypeError("You must specify a success callback for the find command.");
-        }
-        if (!fields || (utils.isArray(fields) && fields.length === 0)) {
-            if (typeof errorCB === "function") {
-                errorCB(new ContactError(ContactError.INVALID_ARGUMENT_ERROR));
-            }
+        argscheck.checkArgs('afFO', 'contacts.find', arguments);
+        if (!fields.length) {
+            errorCB && errorCB(new ContactError(ContactError.INVALID_ARGUMENT_ERROR));
         } else {
             var win = function(result) {
                 var cs = [];
@@ -65,9 +62,9 @@ var contacts = {
      * @returns new Contact object
      */
     create:function(properties) {
-        var i;
+        argscheck.checkArgs('O', 'contacts.create', arguments);
         var contact = new Contact();
-        for (i in properties) {
+        for (var i in properties) {
             if (typeof contact[i] !== 'undefined' && properties.hasOwnProperty(i)) {
                 contact[i] = properties[i];
             }

http://git-wip-us.apache.org/repos/asf/cordova-js/blob/58990dad/test/test.contacts.js
----------------------------------------------------------------------
diff --git a/test/test.contacts.js b/test/test.contacts.js
index af8df23..4fa0d0d 100644
--- a/test/test.contacts.js
+++ b/test/test.contacts.js
@@ -25,6 +25,10 @@ describe("contacts", function () {
         Contact = require('cordova/plugin/Contact'),
         ContactError = require('cordova/plugin/ContactError');
 
+    afterEach(function() {
+        exec.reset();
+    });
+
     describe("create", function () {
         it("returns a new contact", function () {
             expect(contacts.create() instanceof Contact).toBe(true);
@@ -51,34 +55,27 @@ describe("contacts", function () {
 
     describe("find", function () {
         it("throws an error with no success callback", function () {
-            expect(contacts.find).toThrow("You must specify a success callback for the find command.");
-        });
-        
-        it("doesn't call exec with null fields", function () {
-            exec.reset();
-            contacts.find(null, jasmine.createSpy());
-            expect(exec).not.toHaveBeenCalled();
+            expect(function() {contacts.find()}).toThrow();
         });
 
-        it("doesn't call exec with empty fields", function () {
-            exec.reset();
-            contacts.find([], jasmine.createSpy());
-            expect(exec).not.toHaveBeenCalled();
+        it("doesn't call exec with null fields", function () {
+            expect(function() {contacts.find(null, jasmine.createSpy())}).toThrow();
         });
 
         it("calls the error callback when no fields provided", function () {
             var success = jasmine.createSpy(),
                 error = jasmine.createSpy();
 
-            contacts.find(undefined, success, error);
+            contacts.find([], success, error);
+            expect(exec).not.toHaveBeenCalled();
             expect(error).toHaveBeenCalledWith(new ContactError(ContactError.INVALID_ARGUMENT_ERROR));
         });
 
         it("calls exec", function () {
-            //http://www.imdb.com/title/tt0181536/ 
+            //http://www.imdb.com/title/tt0181536/
             var success = jasmine.createSpy(),
                 error = jasmine.createSpy(),
-                fields = {name: "Forrester"},
+                fields = ['*'],
                 options = {};
 
             contacts.find(fields, success, error, options);
@@ -91,14 +88,14 @@ describe("contacts", function () {
             //http://www.imdb.com/title/tt0266543/
             var success = jasmine.createSpy(),
                 error = jasmine.createSpy(),
-                fields = {name: "Nemo"},
+                fields = ['*'],
                 options = {};
 
             spyOn(contacts, "create");
             contacts.find(fields, success, error, options);
             //exec the success callback
             exec.mostRecentCall.args[0]([{
-                name: "Nemo", 
+                name: "Nemo",
                 note: "He has a lucky fin"
             },
             {
@@ -121,7 +118,7 @@ describe("contacts", function () {
             //http://www.imdb.com/title/tt0889134/
             var success = jasmine.createSpy(),
                 error = jasmine.createSpy(),
-                fields = {name: "Amanda"},
+                fields = ['*'],
                 options = {};
 
             spyOn(contacts, "create").andReturn("Rehab");