Permalink
Browse files

[all] Use argscheck in contacts.js

  • Loading branch information...
1 parent f400233 commit 58990dad50f337fd8c71250398d6586ae2b055ac @agrieve agrieve committed Nov 22, 2012
Showing with 21 additions and 27 deletions.
  1. +7 −10 lib/common/plugin/contacts.js
  2. +14 −17 test/test.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];
}
View
@@ -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");

0 comments on commit 58990da

Please sign in to comment.