Skip to content

Commit

Permalink
[all] Use argscheck in contacts.js
Browse files Browse the repository at this point in the history
  • Loading branch information
agrieve committed Nov 22, 2012
1 parent f400233 commit 58990da
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
17 changes: 7 additions & 10 deletions lib/common/plugin/contacts.js
Expand Up @@ -19,7 +19,8 @@
* *
*/ */


var exec = require('cordova/exec'), var argscheck = require('cordova/argscheck'),
exec = require('cordova/exec'),
ContactError = require('cordova/plugin/ContactError'), ContactError = require('cordova/plugin/ContactError'),
utils = require('cordova/utils'), utils = require('cordova/utils'),
Contact = require('cordova/plugin/Contact'); Contact = require('cordova/plugin/Contact');
Expand All @@ -38,13 +39,9 @@ var contacts = {
* @return array of Contacts matching search criteria * @return array of Contacts matching search criteria
*/ */
find:function(fields, successCB, errorCB, options) { find:function(fields, successCB, errorCB, options) {
if (!successCB) { argscheck.checkArgs('afFO', 'contacts.find', arguments);
throw new TypeError("You must specify a success callback for the find command."); if (!fields.length) {
} errorCB && errorCB(new ContactError(ContactError.INVALID_ARGUMENT_ERROR));
if (!fields || (utils.isArray(fields) && fields.length === 0)) {
if (typeof errorCB === "function") {
errorCB(new ContactError(ContactError.INVALID_ARGUMENT_ERROR));
}
} else { } else {
var win = function(result) { var win = function(result) {
var cs = []; var cs = [];
Expand All @@ -65,9 +62,9 @@ var contacts = {
* @returns new Contact object * @returns new Contact object
*/ */
create:function(properties) { create:function(properties) {
var i; argscheck.checkArgs('O', 'contacts.create', arguments);
var contact = new Contact(); var contact = new Contact();
for (i in properties) { for (var i in properties) {
if (typeof contact[i] !== 'undefined' && properties.hasOwnProperty(i)) { if (typeof contact[i] !== 'undefined' && properties.hasOwnProperty(i)) {
contact[i] = properties[i]; contact[i] = properties[i];
} }
Expand Down
31 changes: 14 additions & 17 deletions test/test.contacts.js
Expand Up @@ -25,6 +25,10 @@ describe("contacts", function () {
Contact = require('cordova/plugin/Contact'), Contact = require('cordova/plugin/Contact'),
ContactError = require('cordova/plugin/ContactError'); ContactError = require('cordova/plugin/ContactError');


afterEach(function() {
exec.reset();
});

describe("create", function () { describe("create", function () {
it("returns a new contact", function () { it("returns a new contact", function () {
expect(contacts.create() instanceof Contact).toBe(true); expect(contacts.create() instanceof Contact).toBe(true);
Expand All @@ -51,34 +55,27 @@ describe("contacts", function () {


describe("find", function () { describe("find", function () {
it("throws an error with no success callback", function () { it("throws an error with no success callback", function () {
expect(contacts.find).toThrow("You must specify a success callback for the find command."); expect(function() {contacts.find()}).toThrow();
});

it("doesn't call exec with null fields", function () {
exec.reset();
contacts.find(null, jasmine.createSpy());
expect(exec).not.toHaveBeenCalled();
}); });


it("doesn't call exec with empty fields", function () { it("doesn't call exec with null fields", function () {
exec.reset(); expect(function() {contacts.find(null, jasmine.createSpy())}).toThrow();
contacts.find([], jasmine.createSpy());
expect(exec).not.toHaveBeenCalled();
}); });


it("calls the error callback when no fields provided", function () { it("calls the error callback when no fields provided", function () {
var success = jasmine.createSpy(), var success = jasmine.createSpy(),
error = 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)); expect(error).toHaveBeenCalledWith(new ContactError(ContactError.INVALID_ARGUMENT_ERROR));
}); });


it("calls exec", function () { it("calls exec", function () {
//http://www.imdb.com/title/tt0181536/ //http://www.imdb.com/title/tt0181536/
var success = jasmine.createSpy(), var success = jasmine.createSpy(),
error = jasmine.createSpy(), error = jasmine.createSpy(),
fields = {name: "Forrester"}, fields = ['*'],
options = {}; options = {};


contacts.find(fields, success, error, options); contacts.find(fields, success, error, options);
Expand All @@ -91,14 +88,14 @@ describe("contacts", function () {
//http://www.imdb.com/title/tt0266543/ //http://www.imdb.com/title/tt0266543/
var success = jasmine.createSpy(), var success = jasmine.createSpy(),
error = jasmine.createSpy(), error = jasmine.createSpy(),
fields = {name: "Nemo"}, fields = ['*'],
options = {}; options = {};


spyOn(contacts, "create"); spyOn(contacts, "create");
contacts.find(fields, success, error, options); contacts.find(fields, success, error, options);
//exec the success callback //exec the success callback
exec.mostRecentCall.args[0]([{ exec.mostRecentCall.args[0]([{
name: "Nemo", name: "Nemo",
note: "He has a lucky fin" note: "He has a lucky fin"
}, },
{ {
Expand All @@ -121,7 +118,7 @@ describe("contacts", function () {
//http://www.imdb.com/title/tt0889134/ //http://www.imdb.com/title/tt0889134/
var success = jasmine.createSpy(), var success = jasmine.createSpy(),
error = jasmine.createSpy(), error = jasmine.createSpy(),
fields = {name: "Amanda"}, fields = ['*'],
options = {}; options = {};


spyOn(contacts, "create").andReturn("Rehab"); spyOn(contacts, "create").andReturn("Rehab");
Expand Down

0 comments on commit 58990da

Please sign in to comment.