backbone.dropboxdatastore
Advanced tools
Comparing version 0.0.2 to 0.2.1
@@ -31,63 +31,172 @@ (function (root, factory) { | ||
// Instance methods of DropboxDatastore | ||
_.extend(Backbone.DropboxDatastore.prototype, { | ||
// Save the current state of the **Store** to *Dropbox Datastore*. | ||
save: function(model) { | ||
// Insert new record to *Dropbox.Datastore.Table*. | ||
create: function(model, callback) { | ||
this.getTable(function(table) { | ||
var record = table.insert(model.toJSON()); | ||
callback(Backbone.DropboxDatastore.recordToJson(record)); | ||
}); | ||
}, | ||
// Add a model to *Dropbox Datastore*. | ||
create: function(model) { | ||
// Update existing record in *Dropbox.Datastore.Table*. | ||
update: function(model, callback) { | ||
this.getTable(_.bind(function(table) { | ||
var record = this._findRecordSync(table, model); | ||
if (record) { | ||
record.update(model.toJSON()); | ||
} else { | ||
record = table.insert(model.toJSON()); | ||
} | ||
callback(Backbone.DropboxDatastore.recordToJson(record)); | ||
}, this)); | ||
}, | ||
// Update a model in *Dropbox Datastore*. | ||
update: function(model) { | ||
// Find record from *Dropbox.Datastore.Table* by id. | ||
find: function(model, callback) { | ||
this.getTable(_.bind(function(table) { | ||
var record = this._findRecordSync(table, model); | ||
if (record) { | ||
callback(Backbone.DropboxDatastore.recordToJson(record)); | ||
} else { | ||
throw new Error('Record not found'); | ||
} | ||
}, this)); | ||
}, | ||
// Retrieve a model from *Dropbox Datastore* by id. | ||
find: function(model) { | ||
// Find all records currently in *Dropbox.Datastore.Table*. | ||
findAll: function(callback) { | ||
this.getTable(function(table) { | ||
var result = _.map(table.query(), Backbone.DropboxDatastore.recordToJson); | ||
callback(result); | ||
}); | ||
}, | ||
// Return the array of all models currently in table. | ||
findAll: function() { | ||
// Remove record from *Dropbox.Datastore.Table*. | ||
destroy: function(model, callback) { | ||
this.getTable(_.bind(function(table) { | ||
var record = this._findRecordSync(table, model); | ||
if (record) { | ||
record.deleteRecord(); | ||
} | ||
callback({}); | ||
}, this)); | ||
}, | ||
// Delete a model from *Dropbox Datastore*. | ||
destroy: function(model) { | ||
// lazy table getter | ||
getTable: function(callback) { | ||
var onGetDatastore; | ||
if (this._table) { | ||
// To be consistent to async nature of this method defers invoking | ||
// the function using Underscore defer | ||
_.defer(callback, this._table); | ||
} else { | ||
Backbone.DropboxDatastore.getDatastore(_.bind(function(datastore) { | ||
this._table = datastore.getTable(this.name); | ||
callback(this._table); | ||
}, this)); | ||
} | ||
}, | ||
dropboxDatastore: function() { | ||
// TODO: throw exception if doesn't exist | ||
return Backbone.DropboxDatastore.datastore; | ||
_findRecordSync: function(table, model) { | ||
var params = {}, | ||
record; | ||
if (model.isNew()) { | ||
throw new Error('Cannot fetch data for model without id'); | ||
} else { | ||
if (model.idAttribute === 'id') { | ||
record = table.get(model.id); | ||
} else { | ||
params[model.idAttribute] = model.id; | ||
record = _.first(table.query(params)); | ||
} | ||
return record; | ||
} | ||
} | ||
}); | ||
// dropboxDatastoreSync delegate to the model or collection's | ||
// *dropboxDatastore* property, which should be an instance of `Backbone.DropboxDatastore`. | ||
Backbone.DropboxDatastore.sync = Backbone.dropboxDatastoreSync = function(method, model, options) { | ||
var store = model.dropboxDatastore || model.collection.dropboxDatastore; | ||
// Static methods of DropboxDatastore | ||
_.extend(Backbone.DropboxDatastore, { | ||
getDatastore: function(callback) { | ||
var onOpenDefaultDatastore; | ||
var resp, errorMessage, syncDfd = Backbone.$.Deferred && Backbone.$.Deferred(); //If $ is having Deferred - use it. | ||
if (this._datastore) { | ||
// To be consistent to async nature of this method defers invoking | ||
// the function using Underscore defer | ||
_.defer(callback, this._datastore); | ||
} else { | ||
// Bind and partial applying _onOpenDefaultDatastore by callback | ||
onOpenDefaultDatastore = _.bind(this._onOpenDefaultDatastore, this, callback); | ||
try { | ||
// we can open only one instance of Datastore simultaneously | ||
this.getDatastoreManager().openDefaultDatastore(onOpenDefaultDatastore); | ||
} | ||
}, | ||
_onOpenDefaultDatastore: function(callback, error, datastore) { | ||
if (error) { | ||
throw new Error('Error on openDefaultDatastore: ' + error.responseText); | ||
} | ||
// cache opened datastore | ||
this._datastore = datastore; | ||
callback(datastore); | ||
}, | ||
getDatastoreManager: function() { | ||
return this.getDropboxClient().getDatastoreManager(); | ||
}, | ||
getDropboxClient: function() { | ||
var client = Backbone.DropboxDatastore.client; | ||
if (!client) { | ||
throw new Error('Client should be defined for Backbone.DropboxDatastore'); | ||
} | ||
if (!client.isAuthenticated()) { | ||
throw new Error('Client should be authenticated for Backbone.DropboxDatastore'); | ||
} | ||
return client; | ||
}, | ||
// Using to convert returned Dropbox Datastore records to JSON | ||
recordToJson: function(record) { | ||
return _.extend(record.getFields(), { | ||
id: record.getId() | ||
}); | ||
}, | ||
// dropboxDatastoreSync delegate to the model or collection's | ||
// *dropboxDatastore* property, which should be an instance of `Backbone.DropboxDatastore`. | ||
sync: function(method, model, options) { | ||
var store = model.dropboxDatastore || model.collection.dropboxDatastore, | ||
syncDfd = Backbone.$.Deferred && Backbone.$.Deferred(), //If $ is having Deferred - use it. | ||
syncCallback = _.partial(Backbone.DropboxDatastore._syncCallback, model, options, syncDfd); // partial apply callback with some attributes | ||
switch (method) { | ||
case 'read': | ||
resp = model.id === void 0 ? store.findAll() : store.find(model); | ||
break; | ||
case 'create': | ||
resp = store.create(model); | ||
break; | ||
case 'update': | ||
resp = store.update(model); | ||
break; | ||
case 'delete': | ||
resp = store.destroy(model); | ||
break; | ||
case 'read': | ||
// check if it is a collection or model | ||
if (model instanceof Backbone.Collection) { | ||
store.findAll(syncCallback); | ||
} else { | ||
store.find(model, syncCallback); | ||
} | ||
break; | ||
case 'create': | ||
store.create(model, syncCallback); | ||
break; | ||
case 'update': | ||
store.update(model, syncCallback); | ||
break; | ||
case 'delete': | ||
store.destroy(model, syncCallback); | ||
break; | ||
} | ||
} catch(error) { | ||
errorMessage = error.message; | ||
} | ||
return syncDfd && syncDfd.promise(); | ||
}, | ||
if (resp) { | ||
_syncCallback: function(model, options, syncDfd, resp) { | ||
if (options && options.success) { | ||
@@ -103,35 +212,12 @@ if (Backbone.VERSION === '0.9.10') { | ||
} | ||
} else { | ||
errorMessage = errorMessage ? errorMessage : 'Record Not Found'; | ||
if (options && options.error) { | ||
if (Backbone.VERSION === '0.9.10') { | ||
options.error(model, errorMessage, options); | ||
} else { | ||
options.error(errorMessage); | ||
} | ||
} | ||
if (syncDfd) { | ||
syncDfd.reject(errorMessage); | ||
} | ||
} | ||
}); | ||
// add compatibility with $.ajax | ||
// always execute callback for success and error | ||
if (options && options.complete) { | ||
options.complete(resp); | ||
} | ||
Backbone.originalSync = Backbone.sync; | ||
return syncDfd && syncDfd.promise(); | ||
}; | ||
Backbone.ajaxSync = Backbone.sync; | ||
Backbone.getSyncMethod = function(model) { | ||
if(model.dropboxDatastore || (model.collection && model.collection.dropboxDatastore)) { | ||
return Backbone.dropboxDatastoreSync; | ||
return Backbone.DropboxDatastore.sync; | ||
} else { | ||
return Backbone.ajaxSync; | ||
return Backbone.originalSync; | ||
} | ||
@@ -141,3 +227,3 @@ }; | ||
// Override 'Backbone.sync' to default to dropboxDatastoreSync, | ||
// the original 'Backbone.sync' is still available in 'Backbone.ajaxSync' | ||
// the original 'Backbone.sync' is still available in 'Backbone.originalSync' | ||
Backbone.sync = function(method, model, options) { | ||
@@ -144,0 +230,0 @@ return Backbone.getSyncMethod(model).call(this, method, model, options); |
@@ -1,3 +0,3 @@ | ||
/*! backbone.dropboxDatastore - v0.0.2 - 2013-10-06 | ||
/*! backbone.dropboxdatastore - v0.2.1 - 2013-10-10 | ||
* Copyright (c) 2013 Dmytro Yarmak <dmytroyarmak@gmail.com>;*/ | ||
!function(a,b){"use strict";"object"==typeof exports&&"function"==typeof require?module.exports=b(require("underscore"),require("backbone")):"function"==typeof define&&define.amd?define(["underscore","backbone"],function(c,d){return b(c||a._,d||a.Backbone)}):b(_,Backbone)}(this,function(a,b){"use strict";return b.DropboxDatastore=function(a){this.name=a},a.extend(b.DropboxDatastore.prototype,{save:function(){},create:function(){},update:function(){},find:function(){},findAll:function(){},destroy:function(){},dropboxDatastore:function(){return b.DropboxDatastore.datastore}}),b.DropboxDatastore.sync=b.dropboxDatastoreSync=function(a,c,d){var e,f,g=c.dropboxDatastore||c.collection.dropboxDatastore,h=b.$.Deferred&&b.$.Deferred();try{switch(a){case"read":e=void 0===c.id?g.findAll():g.find(c);break;case"create":e=g.create(c);break;case"update":e=g.update(c);break;case"delete":e=g.destroy(c)}}catch(i){f=i.message}return e?(d&&d.success&&("0.9.10"===b.VERSION?d.success(c,e,d):d.success(e)),h&&h.resolve(e)):(f=f?f:"Record Not Found",d&&d.error&&("0.9.10"===b.VERSION?d.error(c,f,d):d.error(f)),h&&h.reject(f)),d&&d.complete&&d.complete(e),h&&h.promise()},b.ajaxSync=b.sync,b.getSyncMethod=function(a){return a.dropboxDatastore||a.collection&&a.collection.dropboxDatastore?b.dropboxDatastoreSync:b.ajaxSync},b.sync=function(a,c,d){return b.getSyncMethod(c).call(this,a,c,d)},b.DropboxDatastore}); | ||
!function(a,b){"use strict";"object"==typeof exports&&"function"==typeof require?module.exports=b(require("underscore"),require("backbone")):"function"==typeof define&&define.amd?define(["underscore","backbone"],function(c,d){return b(c||a._,d||a.Backbone)}):b(_,Backbone)}(this,function(a,b){"use strict";return b.DropboxDatastore=function(a){this.name=a},a.extend(b.DropboxDatastore.prototype,{create:function(a,c){this.getTable(function(d){var e=d.insert(a.toJSON());c(b.DropboxDatastore.recordToJson(e))})},update:function(c,d){this.getTable(a.bind(function(a){var e=this._findRecordSync(a,c);e?e.update(c.toJSON()):e=a.insert(c.toJSON()),d(b.DropboxDatastore.recordToJson(e))},this))},find:function(c,d){this.getTable(a.bind(function(a){var e=this._findRecordSync(a,c);if(!e)throw new Error("Record not found");d(b.DropboxDatastore.recordToJson(e))},this))},findAll:function(c){this.getTable(function(d){var e=a.map(d.query(),b.DropboxDatastore.recordToJson);c(e)})},destroy:function(b,c){this.getTable(a.bind(function(a){var d=this._findRecordSync(a,b);d&&d.deleteRecord(),c({})},this))},getTable:function(c){this._table?a.defer(c,this._table):b.DropboxDatastore.getDatastore(a.bind(function(a){this._table=a.getTable(this.name),c(this._table)},this))},_findRecordSync:function(b,c){var d,e={};if(c.isNew())throw new Error("Cannot fetch data for model without id");return"id"===c.idAttribute?d=b.get(c.id):(e[c.idAttribute]=c.id,d=a.first(b.query(e))),d}}),a.extend(b.DropboxDatastore,{getDatastore:function(b){var c;this._datastore?a.defer(b,this._datastore):(c=a.bind(this._onOpenDefaultDatastore,this,b),this.getDatastoreManager().openDefaultDatastore(c))},_onOpenDefaultDatastore:function(a,b,c){if(b)throw new Error("Error on openDefaultDatastore: "+b.responseText);this._datastore=c,a(c)},getDatastoreManager:function(){return this.getDropboxClient().getDatastoreManager()},getDropboxClient:function(){var a=b.DropboxDatastore.client;if(!a)throw new Error("Client should be defined for Backbone.DropboxDatastore");if(!a.isAuthenticated())throw new Error("Client should be authenticated for Backbone.DropboxDatastore");return a},recordToJson:function(b){return a.extend(b.getFields(),{id:b.getId()})},sync:function(c,d,e){var f=d.dropboxDatastore||d.collection.dropboxDatastore,g=b.$.Deferred&&b.$.Deferred(),h=a.partial(b.DropboxDatastore._syncCallback,d,e,g);switch(c){case"read":d instanceof b.Collection?f.findAll(h):f.find(d,h);break;case"create":f.create(d,h);break;case"update":f.update(d,h);break;case"delete":f.destroy(d,h)}return g&&g.promise()},_syncCallback:function(a,c,d,e){c&&c.success&&("0.9.10"===b.VERSION?c.success(a,e,c):c.success(e)),d&&d.resolve(e)}}),b.originalSync=b.sync,b.getSyncMethod=function(a){return a.dropboxDatastore||a.collection&&a.collection.dropboxDatastore?b.DropboxDatastore.sync:b.originalSync},b.sync=function(a,c,d){return b.getSyncMethod(c).call(this,a,c,d)},b.DropboxDatastore}); |
{ | ||
"name": "backbone.dropboxDatastore", | ||
"version": "0.0.2", | ||
"version": "0.2.1", | ||
"main": "./backbone.dropboxDatastore.js", | ||
@@ -5,0 +5,0 @@ "dependencies": { |
# Changelog | ||
## 0.2.1 - Oct 10, 2013 | ||
- don't throw exception when try to update or delete not existing model | ||
- create new record when try to update not existing one | ||
- return success when try to delete not existing record | ||
## 0.2.0 - Oct 9, 2013 | ||
- finish implementation of all CRUD methods | ||
- write some docs | ||
## 0.1.5 - Oct 8, 2013 | ||
- implement save for existing model(update) | ||
## 0.1.4 - Oct 8, 2013 | ||
- implement save for new model(create) | ||
## 0.1.3 - Oct 7, 2013 | ||
- implement fetch for model | ||
## 0.1.2 - Oct 7, 2013 | ||
- implement fetch for collection | ||
## 0.1.1 - Oct 7, 2013 | ||
- add instance method to get table | ||
## 0.1.0 - Oct 6, 2013 | ||
- add static method to open and get dataset | ||
## 0.0.2 - Oct 6, 2013 | ||
@@ -8,2 +35,2 @@ - prepare grunt tasks for development process | ||
## 0.0.1 - Oct 5, 2013 | ||
- starting develope Dropbox Datastore adapter for Backbone.js | ||
- starting develop Dropbox Datastore adapter for Backbone.js |
@@ -21,3 +21,3 @@ /*jshint camelcase: false*/ | ||
src: '<%= pkg.main %>', | ||
dest: '<%= pkg.name %>.min.js' | ||
dest: 'backbone.dropboxDatastore.min.js' | ||
} | ||
@@ -29,3 +29,3 @@ }, | ||
}, | ||
all: ['Gruntfile.js', '<%= pkg.main %>', 'spec/*_spec.js'] | ||
all: ['Gruntfile.js', '<%= pkg.main %>', 'spec/*Spec.js'] | ||
}, | ||
@@ -41,2 +41,8 @@ jasmine: { | ||
} | ||
}, | ||
watch: { | ||
test: { | ||
files: ['<%= pkg.main %>', 'spec/*Spec.js'], | ||
tasks: ['jasmine'] | ||
} | ||
} | ||
@@ -49,2 +55,3 @@ }); | ||
grunt.loadNpmTasks('grunt-contrib-jasmine'); | ||
grunt.loadNpmTasks('grunt-contrib-watch'); | ||
// Default task. | ||
@@ -51,0 +58,0 @@ grunt.registerTask('default', ['test', 'uglify']); |
{ | ||
"name": "backbone.dropboxdatastore", | ||
"description": "Backbone Dropbox Datastore API adapter", | ||
"version": "0.0.2", | ||
"keywords": ["backbone", "dropbox", "Datastore", "Datastore API"], | ||
"version": "0.2.1", | ||
"keywords": [ | ||
"backbone", | ||
"dropbox", | ||
"Datastore", | ||
"Datastore API" | ||
], | ||
"author": "Dmytro Yarmak <dmytroyarmak@gmail.com>", | ||
@@ -24,3 +29,4 @@ "license": "MIT", | ||
"grunt-contrib-jshint": "~0.6.4", | ||
"grunt-contrib-jasmine": "~0.5.2" | ||
"grunt-contrib-jasmine": "~0.5.2", | ||
"grunt-contrib-watch": "~0.5.3" | ||
}, | ||
@@ -27,0 +33,0 @@ "main": "backbone.dropboxDatastore.js", |
@@ -1,2 +0,2 @@ | ||
# Backbone Dropbox Datastore Adapter v0.0.2 | ||
# Backbone Dropbox Datastore Adapter v0.2.1 | ||
@@ -11,9 +11,36 @@ [![Build Status](https://secure.travis-ci.org/dmytroyarmak/Backbone.dropboxDatastore.png?branch=master)](http://travis-ci.org/dmytroyarmak/Backbone.dropboxDatastore) | ||
Include Backbone.dropboxDatastore after having included Backbone.js: | ||
You need to have registered application with an OAuth redirect URI registered on the [App Console](https://www.dropbox.com/developers/apps) before using the Dropbox Datastore API. | ||
Include [Datastore API SDKs](https://www.dropbox.com/developers/datastore/sdks/js) and Backbone.dropboxDatastore after having included Backbone.js: | ||
```html | ||
<script type="text/javascript" src="backbone.js"></script> | ||
<script type="text/javascript" src="backbone.dropboxDatastore.js"></script> | ||
<script type="text/javascript" src="https://www.dropbox.com/static/api/dropbox-datastores-1.0-latest.js"></script> | ||
``` | ||
To start using the Datastore API, you'll need to create a Client object. This object lets you link to a Dropbox user's account, which is the first step to working with data on their behalf. | ||
You'll want to create the client object as soon as your app has loaded. Depending on your JavaScript framework, this can be done in a variety of different ways. For example, in jQuery, you would use the $() function. | ||
After authentication you should set client object to Backbone.DropboxDatastore.client property. | ||
*Be sure to replace APP_KEY with the real value for your app.* | ||
```javascript | ||
var client = new Dropbox.Client({key: APP_KEY}); | ||
// Try to finish OAuth authorization. | ||
client.authenticate({interactive: false}); | ||
// Redirect to Dropbox to authenticate if client isn't authenticated | ||
if (!client.isAuthenticated()) client.authenticate(); | ||
// Set client for Backbone.DropboxDatastore to work with Dropbox | ||
Backbone.DropboxDatastore.client = client; | ||
// Client is authenticated and set for Backbone.DropboxDatastore. | ||
// You can use CRUD methods of collection with DropboxDatastore | ||
``` | ||
Create your collections like so: | ||
@@ -24,3 +51,3 @@ | ||
dropboxDatastore: new Backbone.DropboxDatastore("SomeCollection"), // Unique name within your app. | ||
dropboxDatastore: new Backbone.DropboxDatastore('SomeCollection'), // Unique name within your app. | ||
@@ -31,2 +58,4 @@ // ... everything else is normal. | ||
``` | ||
For more details how Dropbox Datastore API works read tutorial: [Using the Datastore API in JavaScript](https://www.dropbox.com/developers/datastore/tutorial/js) | ||
### RequireJS | ||
@@ -44,6 +73,7 @@ | ||
paths: { | ||
jquery: "lib/jquery", | ||
underscore: "lib/underscore", | ||
backbone: "lib/backbone", | ||
dropboxdatastore: "lib/backbone.dropboxDatastore" | ||
jquery: 'lib/jquery', | ||
underscore: 'lib/underscore', | ||
backbone: 'lib/backbone', | ||
dropbox: 'https://www.dropbox.com/static/api/dropbox-datastores-1.0-latest', | ||
dropboxdatastore: 'lib/backbone.dropboxDatastore' | ||
} | ||
@@ -53,7 +83,9 @@ }); | ||
*Create client, authorize it and set to Backbone.DropboxDatastore.client as in previous section.* | ||
Define your collection as a module: | ||
```javascript | ||
define("someCollection", ["dropboxdatastore"], function() { | ||
define('someCollection', ['dropboxdatastore'], function() { | ||
var SomeCollection = Backbone.Collection.extend({ | ||
dropboxDatastore: new Backbone.DropboxDatastore("SomeCollection") // Unique name within your app. | ||
dropboxDatastore: new Backbone.DropboxDatastore('SomeCollection') // Unique name within your app. | ||
}); | ||
@@ -67,3 +99,3 @@ | ||
```javascript | ||
require(["someCollection"], function(someCollection) { | ||
require(['someCollection'], function(someCollection) { | ||
// ready to use someCollection | ||
@@ -78,3 +110,3 @@ }); | ||
```javascript | ||
Backbone.DropboxDatastore = require("backbone.dropboxdatastore"); | ||
Backbone.DropboxDatastore = require('backbone.dropboxdatastore'); | ||
``` | ||
@@ -99,3 +131,3 @@ | ||
Copyright (c) 2010 Jerome Gravel-Niquet | ||
Copyright (c) 2013 Dmytro Yarmak | ||
@@ -102,0 +134,0 @@ Permission is hereby granted, free of charge, to any person obtaining |
describe('Backbone.sync', function() { | ||
var syncSpy; | ||
@@ -15,5 +16,7 @@ | ||
}); | ||
}); | ||
describe('Backbone.getSyncMethod', function(){ | ||
var DropboxDatastoreModel = Backbone.Model.extend({ | ||
@@ -27,31 +30,912 @@ dropboxDatastore: 'dropboxDatastoreMock' | ||
beforeEach(function() { | ||
spyOn(Backbone, 'ajaxSync'); | ||
spyOn(Backbone, 'dropboxDatastoreSync'); | ||
spyOn(Backbone, 'originalSync'); | ||
spyOn(Backbone.DropboxDatastore, 'sync'); | ||
}); | ||
it('for model with dropboxDatastore returns dropboxDatastoreSync', function() { | ||
it('for model with dropboxDatastore returns DropboxDatastore.sync', function() { | ||
var model = new DropboxDatastoreModel(); | ||
expect(Backbone.getSyncMethod(model)).toBe(Backbone.dropboxDatastoreSync); | ||
expect(Backbone.getSyncMethod(model)).toBe(Backbone.DropboxDatastore.sync); | ||
}); | ||
it('for model without dropboxDatastore returns ajaxSync', function() { | ||
it('for model without dropboxDatastore returns originalSync', function() { | ||
var model = new Backbone.Model(); | ||
expect(Backbone.getSyncMethod(model)).toBe(Backbone.ajaxSync); | ||
expect(Backbone.getSyncMethod(model)).toBe(Backbone.originalSync); | ||
}); | ||
it('for model of collection with dropboxDatastore returns dropboxDatastoreSync', function() { | ||
it('for model of collection with dropboxDatastore returns DropboxDatastore.sync', function() { | ||
var collection = new DropboxDatastoreCollection(), | ||
model = new Backbone.Model({}, {collection: collection}); | ||
expect(Backbone.getSyncMethod(model)).toBe(Backbone.dropboxDatastoreSync); | ||
expect(Backbone.getSyncMethod(model)).toBe(Backbone.DropboxDatastore.sync); | ||
}); | ||
it('for collection with dropboxDatastore returns dropboxDatastoreSync', function() { | ||
it('for collection with dropboxDatastore returns DropboxDatastore.sync', function() { | ||
var collection = new DropboxDatastoreCollection(); | ||
expect(Backbone.getSyncMethod(collection)).toBe(Backbone.dropboxDatastoreSync); | ||
expect(Backbone.getSyncMethod(collection)).toBe(Backbone.DropboxDatastore.sync); | ||
}); | ||
it('for collection without dropboxDatastore returns ajaxSync', function() { | ||
it('for collection without dropboxDatastore returns originalSync', function() { | ||
var collection = new Backbone.Collection(); | ||
expect(Backbone.getSyncMethod(collection)).toBe(Backbone.ajaxSync); | ||
expect(Backbone.getSyncMethod(collection)).toBe(Backbone.originalSync); | ||
}); | ||
}); | ||
describe('Backbone.DropboxDatastore instance methods', function() { | ||
var dropboxDatastore; | ||
beforeEach(function() { | ||
dropboxDatastore = new Backbone.DropboxDatastore('tableName'); | ||
}); | ||
it('store passed argument into name property', function() { | ||
expect(dropboxDatastore.name).toBe('tableName'); | ||
}); | ||
describe('#getTable', function() { | ||
var callbackSpy, datastoreSpy; | ||
beforeEach(function() { | ||
callbackSpy = jasmine.createSpy('callback'); | ||
datastoreSpy = jasmine.createSpyObj('datastore', ['getTable']); | ||
datastoreSpy.getTable.andReturn('tableMock'); | ||
spyOn(Backbone.DropboxDatastore, 'getDatastore'); | ||
}); | ||
describe('if table is stored', function() { | ||
beforeEach(function() { | ||
dropboxDatastore.getTable(callbackSpy); | ||
}); | ||
it('call getDatastore on Backbone.DropboxDatastore', function() { | ||
expect(Backbone.DropboxDatastore.getDatastore).toHaveBeenCalled(); | ||
}); | ||
describe('when callback called', function() { | ||
beforeEach(function() { | ||
// Explicit call callback because we stub getDatastore | ||
Backbone.DropboxDatastore.getDatastore.mostRecentCall.args[0](datastoreSpy); | ||
}); | ||
it('store returned Datastore', function() { | ||
expect(dropboxDatastore._table).toBe('tableMock'); | ||
}); | ||
it('call callback with stored Datastore', function() { | ||
expect(callbackSpy).toHaveBeenCalledWith('tableMock'); | ||
}); | ||
}); | ||
}); | ||
describe('if table is not stored', function() { | ||
beforeEach(function() { | ||
dropboxDatastore._table = 'storedTableMock'; | ||
runs(function() { | ||
dropboxDatastore.getTable(callbackSpy); | ||
}); | ||
waitsFor(function() { | ||
return callbackSpy.calls.length; | ||
}, 'The callback should be called.', 10); | ||
}); | ||
it('do not call getDatastore on Backbone.DropboxDatastore', function() { | ||
expect(Backbone.DropboxDatastore.getDatastore).not.toHaveBeenCalled(); | ||
}); | ||
it('call callback with stored Datastore', function() { | ||
expect(callbackSpy).toHaveBeenCalledWith('storedTableMock'); | ||
}); | ||
}); | ||
}); | ||
describe('#findAll', function() { | ||
var tableSpy, callbackSpy; | ||
beforeEach(function() { | ||
callbackSpy = jasmine.createSpy('callback'); | ||
spyOn(Backbone.DropboxDatastore, 'recordToJson').andCallFake(function(record) { | ||
switch(record) { | ||
case 'record1Spy': return 'fields1Mock'; | ||
case 'record2Spy': return 'fields2Mock'; | ||
} | ||
}); | ||
tableSpy = jasmine.createSpyObj('table', ['query']); | ||
tableSpy.query.andReturn(['record1Spy', 'record2Spy']); | ||
spyOn(dropboxDatastore, 'getTable'); | ||
dropboxDatastore.findAll(callbackSpy); | ||
// Explicit call callback on success getTable | ||
dropboxDatastore.getTable.mostRecentCall.args[0](tableSpy); | ||
}); | ||
it('call getTable', function() { | ||
expect(dropboxDatastore.getTable).toHaveBeenCalled(); | ||
}); | ||
it('call query on table', function() { | ||
expect(tableSpy.query).toHaveBeenCalled(); | ||
}); | ||
it('call Backbone.DropboxDatastore.recordToJson with each returned records', function() { | ||
expect(Backbone.DropboxDatastore.recordToJson.calls.length).toBe(2); | ||
}); | ||
it('call callback with array of fields', function() { | ||
expect(callbackSpy).toHaveBeenCalledWith(['fields1Mock', 'fields2Mock']); | ||
}); | ||
}); | ||
describe('#find', function() { | ||
var callbackSpy; | ||
beforeEach(function() { | ||
callbackSpy = jasmine.createSpy('callback'); | ||
spyOn(dropboxDatastore, 'getTable'); | ||
}); | ||
describe('when model exits', function() { | ||
beforeEach(function() { | ||
spyOn(dropboxDatastore, '_findRecordSync').andReturn('recordMock'); | ||
spyOn(Backbone.DropboxDatastore, 'recordToJson').andReturn('fieldsMock'); | ||
dropboxDatastore.find('modelMock', callbackSpy); | ||
// Explicit call callback on success getTable | ||
dropboxDatastore.getTable.mostRecentCall.args[0]('tableMock'); | ||
}); | ||
it('call getTable', function() { | ||
expect(dropboxDatastore.getTable).toHaveBeenCalled(); | ||
}); | ||
it('call #_findRecordSync with table and model', function() { | ||
expect(dropboxDatastore._findRecordSync).toHaveBeenCalledWith('tableMock', 'modelMock'); | ||
}); | ||
it('call Backbone.DropboxDatastore.recordToJson with found record', function() { | ||
expect(Backbone.DropboxDatastore.recordToJson).toHaveBeenCalledWith('recordMock'); | ||
}); | ||
it('call callback with fields', function() { | ||
expect(callbackSpy).toHaveBeenCalledWith('fieldsMock'); | ||
}); | ||
}); | ||
describe('when model does not exist', function() { | ||
beforeEach(function() { | ||
spyOn(dropboxDatastore, '_findRecordSync').andReturn(null); | ||
dropboxDatastore.find('modelMock', callbackSpy); | ||
}); | ||
it('throw error', function() { | ||
expect(function() { | ||
// Explicit call callback on success getTable | ||
dropboxDatastore.getTable.mostRecentCall.args[0]('tableMock'); | ||
}).toThrow(); | ||
}); | ||
}); | ||
}); | ||
describe('#_findRecordSync', function() { | ||
var modelSpy, tableSpy, result; | ||
describe('when model have id', function() { | ||
beforeEach(function() { | ||
modelSpy = jasmine.createSpyObj('model', ['isNew']); | ||
modelSpy.isNew.andReturn(false); | ||
modelSpy.id = 'idMock'; | ||
}); | ||
describe('with idAttribute equal "id"', function() { | ||
beforeEach(function() { | ||
modelSpy.idAttribute = 'id'; | ||
tableSpy = jasmine.createSpyObj('table', ['get']); | ||
tableSpy.get.andReturn('recordSpy'); | ||
result = dropboxDatastore._findRecordSync(tableSpy, modelSpy); | ||
}); | ||
it('call get on table', function() { | ||
expect(tableSpy.get).toHaveBeenCalledWith('idMock'); | ||
}); | ||
it('return found record', function() { | ||
expect(result).toBe('recordSpy'); | ||
}); | ||
}); | ||
describe('with idAttribute not equal "id"', function() { | ||
beforeEach(function() { | ||
modelSpy.idAttribute = 'idAttributeMock'; | ||
tableSpy = jasmine.createSpyObj('table', ['query']); | ||
tableSpy.query.andReturn(['recordSpy']); | ||
result = dropboxDatastore._findRecordSync(tableSpy, modelSpy); | ||
}); | ||
it('call query on table', function() { | ||
expect(tableSpy.query).toHaveBeenCalledWith({idAttributeMock: 'idMock'}); | ||
}); | ||
it('return found record', function() { | ||
expect(result).toBe('recordSpy'); | ||
}); | ||
}); | ||
}); | ||
describe('when model does not have id', function() { | ||
beforeEach(function() { | ||
modelSpy = jasmine.createSpyObj('model', ['isNew']); | ||
modelSpy.isNew.andReturn(true); | ||
}); | ||
it('throw error', function() { | ||
expect(function() { | ||
dropboxDatastore._findRecordSync(tableSpy, modelSpy); | ||
}).toThrow(); | ||
}); | ||
}); | ||
}); | ||
describe('#create', function() { | ||
var tableSpy, callbackSpy, modelSpy; | ||
beforeEach(function() { | ||
callbackSpy = jasmine.createSpy('callback'); | ||
modelSpy = jasmine.createSpyObj('model', ['toJSON']); | ||
modelSpy.toJSON.andReturn('modelAttributesMock'); | ||
spyOn(Backbone.DropboxDatastore, 'recordToJson').andCallFake(function(record) { | ||
if (record === 'recordSpy') { | ||
return 'fieldsMock'; | ||
} | ||
}); | ||
spyOn(dropboxDatastore, 'getTable'); | ||
dropboxDatastore.create(modelSpy, callbackSpy); | ||
tableSpy = jasmine.createSpyObj('table', ['insert']); | ||
tableSpy.insert.andReturn('recordSpy'); | ||
// Explicit call callback on success getTable | ||
dropboxDatastore.getTable.mostRecentCall.args[0](tableSpy); | ||
}); | ||
it('call getTable', function() { | ||
expect(dropboxDatastore.getTable).toHaveBeenCalled(); | ||
}); | ||
it('call insert on table', function() { | ||
expect(modelSpy.toJSON).toHaveBeenCalled(); | ||
expect(tableSpy.insert).toHaveBeenCalledWith('modelAttributesMock'); | ||
}); | ||
it('call Backbone.DropboxDatastore.recordToJson with created record', function() { | ||
expect(Backbone.DropboxDatastore.recordToJson).toHaveBeenCalledWith('recordSpy'); | ||
}); | ||
it('call callback with fields of new record', function() { | ||
expect(callbackSpy).toHaveBeenCalledWith('fieldsMock'); | ||
}); | ||
}); | ||
describe('#update', function() { | ||
var modelSpy, callbackSpy; | ||
beforeEach(function() { | ||
modelSpy = jasmine.createSpyObj('model', ['toJSON']); | ||
modelSpy.toJSON.andReturn('attributesMock'); | ||
callbackSpy = jasmine.createSpy(); | ||
spyOn(dropboxDatastore, 'getTable'); | ||
}); | ||
describe('when model exists', function() { | ||
var recordSpy; | ||
beforeEach(function() { | ||
recordSpy = jasmine.createSpyObj('foundRecord', ['update']); | ||
spyOn(dropboxDatastore, '_findRecordSync').andReturn(recordSpy); | ||
spyOn(Backbone.DropboxDatastore, 'recordToJson').andReturn('fieldsMock'); | ||
dropboxDatastore.update(modelSpy, callbackSpy); | ||
// Explicit call callback on success getTable | ||
dropboxDatastore.getTable.mostRecentCall.args[0]('tableMock'); | ||
}); | ||
it('call getTable', function() { | ||
expect(dropboxDatastore.getTable).toHaveBeenCalled(); | ||
}); | ||
it('call #_findRecordSync', function() { | ||
expect(dropboxDatastore._findRecordSync).toHaveBeenCalledWith('tableMock', modelSpy); | ||
}); | ||
it('call update on found record ', function() { | ||
expect(recordSpy.update).toHaveBeenCalledWith('attributesMock'); | ||
}); | ||
it('call Backbone.DropboxDatastore.recordToJson with updated record', function() { | ||
expect(Backbone.DropboxDatastore.recordToJson).toHaveBeenCalledWith(recordSpy); | ||
}); | ||
it('call callback with fields of updated record', function() { | ||
expect(callbackSpy).toHaveBeenCalledWith('fieldsMock'); | ||
}); | ||
}); | ||
describe('when model does not exist', function() { | ||
var tableSpy; | ||
beforeEach(function() { | ||
spyOn(dropboxDatastore, '_findRecordSync').andReturn(null); | ||
tableSpy = jasmine.createSpyObj('table', ['insert']); | ||
tableSpy.insert.andReturn('recordMock'); | ||
spyOn(Backbone.DropboxDatastore, 'recordToJson').andReturn('fieldsMock'); | ||
dropboxDatastore.update(modelSpy, callbackSpy); | ||
// Explicit call callback on success getTable | ||
dropboxDatastore.getTable.mostRecentCall.args[0](tableSpy); | ||
}); | ||
it('call getTable', function() { | ||
expect(dropboxDatastore.getTable).toHaveBeenCalled(); | ||
}); | ||
it('call #_findRecordSync', function() { | ||
expect(dropboxDatastore._findRecordSync).toHaveBeenCalledWith(tableSpy, modelSpy); | ||
}); | ||
it('call insert on table ', function() { | ||
expect(tableSpy.insert).toHaveBeenCalledWith('attributesMock'); | ||
}); | ||
it('call Backbone.DropboxDatastore.recordToJson with inserted record', function() { | ||
expect(Backbone.DropboxDatastore.recordToJson).toHaveBeenCalledWith('recordMock'); | ||
}); | ||
it('call callback with fields of updated record', function() { | ||
expect(callbackSpy).toHaveBeenCalledWith('fieldsMock'); | ||
}); | ||
}); | ||
}); | ||
describe('#destroy', function() { | ||
var modelSpy, callbackSpy; | ||
beforeEach(function() { | ||
modelSpy = jasmine.createSpyObj('model', ['toJSON']); | ||
modelSpy.toJSON.andReturn('attributesMock'); | ||
callbackSpy = jasmine.createSpy(); | ||
spyOn(dropboxDatastore, 'getTable'); | ||
}); | ||
describe('when model exists', function() { | ||
var recordSpy; | ||
beforeEach(function() { | ||
recordSpy = jasmine.createSpyObj('foundRecord', ['deleteRecord']); | ||
spyOn(dropboxDatastore, '_findRecordSync').andReturn(recordSpy); | ||
dropboxDatastore.destroy(modelSpy, callbackSpy); | ||
// Explicit call callback on success getTable | ||
dropboxDatastore.getTable.mostRecentCall.args[0]('tableMock'); | ||
}); | ||
it('call getTable', function() { | ||
expect(dropboxDatastore.getTable).toHaveBeenCalled(); | ||
}); | ||
it('call #_findRecordSync', function() { | ||
expect(dropboxDatastore._findRecordSync).toHaveBeenCalledWith('tableMock', modelSpy); | ||
}); | ||
it('call deleteRecord on found record ', function() { | ||
expect(recordSpy.deleteRecord).toHaveBeenCalledWith(); | ||
}); | ||
it('call callback with empty object', function() { | ||
expect(callbackSpy).toHaveBeenCalledWith({}); | ||
}); | ||
}); | ||
describe('when model does not exist', function() { | ||
beforeEach(function() { | ||
spyOn(dropboxDatastore, '_findRecordSync').andReturn(null); | ||
dropboxDatastore.destroy(modelSpy, callbackSpy); | ||
// Explicit call callback on success getTable | ||
dropboxDatastore.getTable.mostRecentCall.args[0]('tableMock'); | ||
}); | ||
it('call getTable', function() { | ||
expect(dropboxDatastore.getTable).toHaveBeenCalled(); | ||
}); | ||
it('call #_findRecordSync', function() { | ||
expect(dropboxDatastore._findRecordSync).toHaveBeenCalledWith('tableMock', modelSpy); | ||
}); | ||
it('call callback with empty object', function() { | ||
expect(callbackSpy).toHaveBeenCalledWith({}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('Backbone.DropboxDatastore static methods', function() { | ||
describe('getDropboxClient', function() { | ||
it('throw error if Backbone.DropboxDatastore.client is not defined', function() { | ||
Backbone.DropboxDatastore.client = undefined; | ||
expect(function() { | ||
Backbone.DropboxDatastore.getDropboxClient(); | ||
}).toThrow(); | ||
}); | ||
it('throw error if Backbone.DropboxDatastore.client is not authorized', function() { | ||
Backbone.DropboxDatastore.client = jasmine.createSpyObj('client', ['isAuthenticated']); | ||
Backbone.DropboxDatastore.client.isAuthenticated.andReturn(false); | ||
expect(function() { | ||
Backbone.DropboxDatastore.getDropboxClient(); | ||
}).toThrow(); | ||
}); | ||
it('return Backbone.DropboxDatastore.client if defined and authorized', function() { | ||
Backbone.DropboxDatastore.client = jasmine.createSpyObj('client', ['isAuthenticated']); | ||
Backbone.DropboxDatastore.client.isAuthenticated.andReturn(true); | ||
expect(Backbone.DropboxDatastore.getDropboxClient()).toBe(Backbone.DropboxDatastore.client); | ||
}); | ||
}); | ||
describe('getDatastoreManager', function() { | ||
it('return result of calling getDatastoreManager on result of getDropboxClient', function() { | ||
var clientSpy = jasmine.createSpyObj('client', ['getDatastoreManager']), | ||
result; | ||
clientSpy.getDatastoreManager.andReturn('datastoreManagerMock'); | ||
spyOn(Backbone.DropboxDatastore, 'getDropboxClient').andReturn(clientSpy); | ||
result = Backbone.DropboxDatastore.getDatastoreManager(); | ||
expect(Backbone.DropboxDatastore.getDropboxClient).toHaveBeenCalled(); | ||
expect(clientSpy.getDatastoreManager).toHaveBeenCalled(); | ||
expect(result).toBe('datastoreManagerMock'); | ||
}); | ||
}); | ||
describe('getDatastore', function() { | ||
var datastoreManagerSpy, callbackSpy; | ||
beforeEach(function() { | ||
datastoreManagerSpy = jasmine.createSpyObj('datastoreManager', ['openDefaultDatastore']); | ||
spyOn(Backbone.DropboxDatastore, 'getDatastoreManager').andReturn(datastoreManagerSpy); | ||
callbackSpy = jasmine.createSpy('callback'); | ||
}); | ||
describe('if Datastore is not opened', function() { | ||
beforeEach(function() { | ||
Backbone.DropboxDatastore.getDatastore(callbackSpy); | ||
}); | ||
it('call openDefaultDatastore on datastoreManager', function() { | ||
expect(datastoreManagerSpy.openDefaultDatastore).toHaveBeenCalled(); | ||
}); | ||
describe('if openDefaultDatastore processed with error', function() { | ||
it('throw an error', function() { | ||
expect(function() { | ||
datastoreManagerSpy.openDefaultDatastore.mostRecentCall.args[0]('errorMock', null); | ||
}).toThrow(); | ||
}); | ||
}); | ||
describe('if openDefaultDatastore processed without errors', function() { | ||
beforeEach(function() { | ||
datastoreManagerSpy.openDefaultDatastore.mostRecentCall.args[0](null, 'datastoreMock'); | ||
}); | ||
it('store returned Datastore and call callback with stored Datastore', function() { | ||
expect(callbackSpy).toHaveBeenCalledWith('datastoreMock'); | ||
expect(Backbone.DropboxDatastore._datastore).toBe('datastoreMock'); | ||
}); | ||
}); | ||
}); | ||
describe('if Datastore is opened', function() { | ||
beforeEach(function() { | ||
Backbone.DropboxDatastore._datastore = 'storedDatastoreMock'; | ||
runs(function() { | ||
Backbone.DropboxDatastore.getDatastore(callbackSpy); | ||
}); | ||
waitsFor(function() { | ||
return callbackSpy.calls.length; | ||
}, 'The callback should be called.', 10); | ||
}); | ||
it('do not call openDefaultDatastore on datastoreManager', function() { | ||
expect(datastoreManagerSpy.openDefaultDatastore).not.toHaveBeenCalled(); | ||
}); | ||
it('call callback with stored Datastore', function() { | ||
expect(callbackSpy).toHaveBeenCalledWith('storedDatastoreMock'); | ||
}); | ||
}); | ||
}); | ||
describe('recordToJson', function() { | ||
var recordSpy, result; | ||
beforeEach(function() { | ||
recordSpy = jasmine.createSpyObj('record', ['getFields', 'getId']); | ||
recordSpy.getFields.andReturn({foo: 1, bar: 'lalala'}); | ||
recordSpy.getId.andReturn('idMock'); | ||
result = Backbone.DropboxDatastore.recordToJson(recordSpy); | ||
}); | ||
it('call getFields of record', function() { | ||
expect(recordSpy.getFields).toHaveBeenCalled(); | ||
}); | ||
it('return result of getFields', function() { | ||
expect(result).toEqual({id: 'idMock', foo: 1, bar: 'lalala'}); | ||
}); | ||
}); | ||
describe('sync', function() { | ||
var result, datastoreSpy; | ||
describe('for collection', function() { | ||
var collection; | ||
beforeEach(function() { | ||
datastoreSpy = jasmine.createSpyObj('datastore', ['findAll']); | ||
collection = new Backbone.Collection(); | ||
collection.dropboxDatastore = datastoreSpy; | ||
}); | ||
describe('method equal read', function() { | ||
beforeEach(function() { | ||
spyOn(_, 'partial').andReturn('partialAppliedSyncCallbackMock'); | ||
}); | ||
describe('there is Deferred method on Backbone.$', function() { | ||
var deferredSpy; | ||
beforeEach(function() { | ||
deferredSpy = jasmine.createSpyObj('deferred', ['promise']); | ||
deferredSpy.promise.andReturn('promiseMock'); | ||
Backbone.$ = { | ||
Deferred: jasmine.createSpy('Deferred').andReturn(deferredSpy) | ||
}; | ||
result = Backbone.DropboxDatastore.sync('read', collection, 'optionsMock'); | ||
}); | ||
it('create Deferred object', function() { | ||
expect(Backbone.$.Deferred).toHaveBeenCalled(); | ||
}); | ||
it('partial apply _syncCallback', function() { | ||
expect(_.partial).toHaveBeenCalledWith(Backbone.DropboxDatastore._syncCallback, collection, 'optionsMock', deferredSpy); | ||
}); | ||
it('call findAll on dropboxDatastore with partial applied syncCallback', function() { | ||
expect(datastoreSpy.findAll).toHaveBeenCalledWith('partialAppliedSyncCallbackMock'); | ||
}); | ||
it('return promise of Deferred object', function() { | ||
expect(result).toBe('promiseMock'); | ||
}); | ||
}); | ||
describe('there is no Deferred method on Backbone.$', function() { | ||
beforeEach(function() { | ||
Backbone.$ = {}; | ||
result = Backbone.DropboxDatastore.sync('read', collection, 'optionsMock'); | ||
}); | ||
it('partial apply _syncCallback', function() { | ||
expect(_.partial).toHaveBeenCalledWith(Backbone.DropboxDatastore._syncCallback, collection, 'optionsMock', undefined); | ||
}); | ||
it('call findAll on dropboxDatastore with partial applied syncCallback', function() { | ||
expect(datastoreSpy.findAll).toHaveBeenCalledWith('partialAppliedSyncCallbackMock'); | ||
}); | ||
it('return undefined', function() { | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('for model', function() { | ||
var model; | ||
beforeEach(function() { | ||
datastoreSpy = jasmine.createSpyObj('datastore', ['find', 'create', 'update', 'destroy']); | ||
model = new Backbone.Model(); | ||
model.collection = { | ||
dropboxDatastore: datastoreSpy | ||
}; | ||
}); | ||
describe('method equal read', function() { | ||
beforeEach(function() { | ||
spyOn(_, 'partial').andReturn('partialAppliedSyncCallbackMock'); | ||
}); | ||
describe('there is Deferred method on Backbone.$', function() { | ||
var deferredSpy; | ||
beforeEach(function() { | ||
deferredSpy = jasmine.createSpyObj('deferred', ['promise']); | ||
deferredSpy.promise.andReturn('promiseMock'); | ||
Backbone.$ = { | ||
Deferred: jasmine.createSpy('Deferred').andReturn(deferredSpy) | ||
}; | ||
result = Backbone.DropboxDatastore.sync('read', model, 'optionsMock'); | ||
}); | ||
it('create Deferred object', function() { | ||
expect(Backbone.$.Deferred).toHaveBeenCalled(); | ||
}); | ||
it('partial apply _syncCallback', function() { | ||
expect(_.partial).toHaveBeenCalledWith(Backbone.DropboxDatastore._syncCallback, model, 'optionsMock', deferredSpy); | ||
}); | ||
it('call find on dropboxDatastore with model and partial applied syncCallback', function() { | ||
expect(datastoreSpy.find).toHaveBeenCalledWith(model, 'partialAppliedSyncCallbackMock'); | ||
}); | ||
it('return promise of Deferred object', function() { | ||
expect(result).toBe('promiseMock'); | ||
}); | ||
}); | ||
describe('there is no Deferred method on Backbone.$', function() { | ||
beforeEach(function() { | ||
Backbone.$ = {}; | ||
result = Backbone.DropboxDatastore.sync('read', model, 'optionsMock'); | ||
}); | ||
it('partial apply _syncCallback', function() { | ||
expect(_.partial).toHaveBeenCalledWith(Backbone.DropboxDatastore._syncCallback, model, 'optionsMock', undefined); | ||
}); | ||
it('call find on dropboxDatastore with model and partial applied syncCallback', function() { | ||
expect(datastoreSpy.find).toHaveBeenCalledWith(model, 'partialAppliedSyncCallbackMock'); | ||
}); | ||
it('return undefined', function() { | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
}); | ||
describe('method equal create', function() { | ||
beforeEach(function() { | ||
spyOn(_, 'partial').andReturn('partialAppliedSyncCallbackMock'); | ||
}); | ||
describe('there is Deferred method on Backbone.$', function() { | ||
var deferredSpy; | ||
beforeEach(function() { | ||
deferredSpy = jasmine.createSpyObj('deferred', ['promise']); | ||
deferredSpy.promise.andReturn('promiseMock'); | ||
Backbone.$ = { | ||
Deferred: jasmine.createSpy('Deferred').andReturn(deferredSpy) | ||
}; | ||
result = Backbone.DropboxDatastore.sync('create', model, 'optionsMock'); | ||
}); | ||
it('create Deferred object', function() { | ||
expect(Backbone.$.Deferred).toHaveBeenCalled(); | ||
}); | ||
it('partial apply _syncCallback', function() { | ||
expect(_.partial).toHaveBeenCalledWith(Backbone.DropboxDatastore._syncCallback, model, 'optionsMock', deferredSpy); | ||
}); | ||
it('call create on dropboxDatastore with model and partial applied syncCallback', function() { | ||
expect(datastoreSpy.create).toHaveBeenCalledWith(model, 'partialAppliedSyncCallbackMock'); | ||
}); | ||
it('return promise of Deferred object', function() { | ||
expect(result).toBe('promiseMock'); | ||
}); | ||
}); | ||
describe('there is no Deferred method on Backbone.$', function() { | ||
beforeEach(function() { | ||
Backbone.$ = {}; | ||
result = Backbone.DropboxDatastore.sync('create', model, 'optionsMock'); | ||
}); | ||
it('partial apply _syncCallback', function() { | ||
expect(_.partial).toHaveBeenCalledWith(Backbone.DropboxDatastore._syncCallback, model, 'optionsMock', undefined); | ||
}); | ||
it('call create on dropboxDatastore with model and partial applied syncCallback', function() { | ||
expect(datastoreSpy.create).toHaveBeenCalledWith(model, 'partialAppliedSyncCallbackMock'); | ||
}); | ||
it('return undefined', function() { | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
}); | ||
describe('method equal update', function() { | ||
beforeEach(function() { | ||
spyOn(_, 'partial').andReturn('partialAppliedSyncCallbackMock'); | ||
}); | ||
describe('there is Deferred method on Backbone.$', function() { | ||
var deferredSpy; | ||
beforeEach(function() { | ||
deferredSpy = jasmine.createSpyObj('deferred', ['promise']); | ||
deferredSpy.promise.andReturn('promiseMock'); | ||
Backbone.$ = { | ||
Deferred: jasmine.createSpy('Deferred').andReturn(deferredSpy) | ||
}; | ||
result = Backbone.DropboxDatastore.sync('update', model, 'optionsMock'); | ||
}); | ||
it('create Deferred object', function() { | ||
expect(Backbone.$.Deferred).toHaveBeenCalled(); | ||
}); | ||
it('partial apply _syncCallback', function() { | ||
expect(_.partial).toHaveBeenCalledWith(Backbone.DropboxDatastore._syncCallback, model, 'optionsMock', deferredSpy); | ||
}); | ||
it('call update on dropboxDatastore with model and partial applied syncCallback', function() { | ||
expect(datastoreSpy.update).toHaveBeenCalledWith(model, 'partialAppliedSyncCallbackMock'); | ||
}); | ||
it('return promise of Deferred object', function() { | ||
expect(result).toBe('promiseMock'); | ||
}); | ||
}); | ||
describe('there is no Deferred method on Backbone.$', function() { | ||
beforeEach(function() { | ||
Backbone.$ = {}; | ||
result = Backbone.DropboxDatastore.sync('update', model, 'optionsMock'); | ||
}); | ||
it('partial apply _syncCallback', function() { | ||
expect(_.partial).toHaveBeenCalledWith(Backbone.DropboxDatastore._syncCallback, model, 'optionsMock', undefined); | ||
}); | ||
it('call update on dropboxDatastore with model and partial applied syncCallback', function() { | ||
expect(datastoreSpy.update).toHaveBeenCalledWith(model, 'partialAppliedSyncCallbackMock'); | ||
}); | ||
it('return undefined', function() { | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
}); | ||
describe('method equal delete', function() { | ||
beforeEach(function() { | ||
spyOn(_, 'partial').andReturn('partialAppliedSyncCallbackMock'); | ||
}); | ||
describe('there is Deferred method on Backbone.$', function() { | ||
var deferredSpy; | ||
beforeEach(function() { | ||
deferredSpy = jasmine.createSpyObj('deferred', ['promise']); | ||
deferredSpy.promise.andReturn('promiseMock'); | ||
Backbone.$ = { | ||
Deferred: jasmine.createSpy('Deferred').andReturn(deferredSpy) | ||
}; | ||
result = Backbone.DropboxDatastore.sync('delete', model, 'optionsMock'); | ||
}); | ||
it('create Deferred object', function() { | ||
expect(Backbone.$.Deferred).toHaveBeenCalled(); | ||
}); | ||
it('partial apply _syncCallback', function() { | ||
expect(_.partial).toHaveBeenCalledWith(Backbone.DropboxDatastore._syncCallback, model, 'optionsMock', deferredSpy); | ||
}); | ||
it('call destroy on dropboxDatastore with model and partial applied syncCallback', function() { | ||
expect(datastoreSpy.destroy).toHaveBeenCalledWith(model, 'partialAppliedSyncCallbackMock'); | ||
}); | ||
it('return promise of Deferred object', function() { | ||
expect(result).toBe('promiseMock'); | ||
}); | ||
}); | ||
describe('there is no Deferred method on Backbone.$', function() { | ||
beforeEach(function() { | ||
Backbone.$ = {}; | ||
result = Backbone.DropboxDatastore.sync('delete', model, 'optionsMock'); | ||
}); | ||
it('partial apply _syncCallback', function() { | ||
expect(_.partial).toHaveBeenCalledWith(Backbone.DropboxDatastore._syncCallback, model, 'optionsMock', undefined); | ||
}); | ||
it('call destroy on dropboxDatastore with model and partial applied syncCallback', function() { | ||
expect(datastoreSpy.destroy).toHaveBeenCalledWith(model, 'partialAppliedSyncCallbackMock'); | ||
}); | ||
it('return undefined', function() { | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('_syncCallback', function() { | ||
var options; | ||
describe('options.success is passed', function() { | ||
beforeEach(function() { | ||
options = {success: jasmine.createSpy('successCallback')}; | ||
}); | ||
describe('Backbone.VERSION is 0.9.10', function() { | ||
beforeEach(function() { | ||
Backbone.VERSION = '0.9.10'; | ||
}); | ||
describe('syncDfd is passed', function() { | ||
var deferredSpy; | ||
beforeEach(function() { | ||
deferredSpy = jasmine.createSpyObj('deferred', ['resolve']); | ||
Backbone.DropboxDatastore._syncCallback('modelMock', options, deferredSpy, 'respMock'); | ||
}); | ||
it('call callback with 3 params', function() { | ||
expect(options.success).toHaveBeenCalledWith('modelMock', 'respMock', options); | ||
}); | ||
it('call resolve on syncDfd', function() { | ||
expect(deferredSpy.resolve).toHaveBeenCalledWith('respMock'); | ||
}); | ||
}); | ||
describe('syncDfd is not passed', function() { | ||
beforeEach(function() { | ||
Backbone.DropboxDatastore._syncCallback('modelMock', options, null, 'respMock'); | ||
}); | ||
it('call callback with 3 params', function() { | ||
expect(options.success).toHaveBeenCalledWith('modelMock', 'respMock', options); | ||
}); | ||
}); | ||
}); | ||
describe('Backbone.VERSION is not 0.9.10', function() { | ||
beforeEach(function() { | ||
Backbone.VERSION = '1.0.0'; | ||
}); | ||
describe('syncDfd is passed', function() { | ||
var deferredSpy; | ||
beforeEach(function() { | ||
deferredSpy = jasmine.createSpyObj('deferred', ['resolve']); | ||
Backbone.DropboxDatastore._syncCallback('modelMock', options, deferredSpy, 'respMock'); | ||
}); | ||
it('call callback with 1 param', function() { | ||
expect(options.success).toHaveBeenCalledWith('respMock'); | ||
}); | ||
it('call resolve on syncDfd', function() { | ||
expect(deferredSpy.resolve).toHaveBeenCalledWith('respMock'); | ||
}); | ||
}); | ||
describe('syncDfd is not passed', function() { | ||
beforeEach(function() { | ||
Backbone.DropboxDatastore._syncCallback('modelMock', options, null, 'respMock'); | ||
}); | ||
it('call callback with 1 param', function() { | ||
expect(options.success).toHaveBeenCalledWith('respMock'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('options.success is not passed', function() { | ||
describe('syncDfd is passed', function() { | ||
var deferredSpy; | ||
beforeEach(function() { | ||
deferredSpy = jasmine.createSpyObj('deferred', ['resolve']); | ||
Backbone.DropboxDatastore._syncCallback('modelMock', {}, deferredSpy, 'respMock'); | ||
}); | ||
it('call resolve on syncDfd', function() { | ||
expect(deferredSpy.resolve).toHaveBeenCalledWith('respMock'); | ||
}); | ||
}); | ||
describe('syncDfd is not passed', function() { | ||
beforeEach(function() { | ||
Backbone.DropboxDatastore._syncCallback('modelMock', {}, null, 'respMock'); | ||
it('do nothing'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
54642
1043
144
5
1