Skip to content

Commit

Permalink
Extend testsuite
Browse files Browse the repository at this point in the history
  • Loading branch information
dwt committed Jan 26, 2012
1 parent 089b33a commit 82ecca1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 38 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -16,7 +16,7 @@ Create your collections like so:
```javascript
window.SomeCollection = Backbone.Collection.extend({

localStorage: new Store("SomeCollection"), // Unique name within your app.
localStorage: new Backbone.LocalStorage("SomeCollection"), // Unique name within your app.

// ... everything else is normal.

Expand All @@ -28,6 +28,7 @@ Feel free to use Backbone as you usually would, this is a drop-in replacement.
## Credits

Thanks to [Mark Woodall](https://github.com/llad) for the QUnit tests.
Thanks to [Martin Häcker](https://github.com/dwt) for the many fixes and the test isolation.

## Licensed

Expand Down
6 changes: 4 additions & 2 deletions backbone.localStorage.js
@@ -1,5 +1,5 @@
/**
* Backbone localStorage Adapter v1.0
* Backbone localStorage Adapter
* https://github.com/jeromegn/Backbone.localStorage
*/

Expand Down Expand Up @@ -80,7 +80,7 @@ _.extend(Backbone.LocalStorage.prototype, {

// localSync delegate to the model or collection's
// *localStorage* property, which should be an instance of `Store`.
// window.Store.sync is deprectated, use Backbone.LocalStorage.sync instead
// window.Store.sync and Backbone.localSync is deprectated, use Backbone.LocalStorage.sync instead
Backbone.LocalStorage.sync = window.Store.sync = Backbone.localSync = function(method, model, options, error) {

// Backwards compatibility with Backbone <= 0.3.3
Expand Down Expand Up @@ -112,3 +112,5 @@ Backbone.LocalStorage.sync = window.Store.sync = Backbone.localSync = function(m
// the original 'Backbone.sync' is still available in 'Backbone.ajaxSync'
Backbone.ajaxSync = Backbone.sync;
Backbone.sync = Backbone.LocalStorage.sync;

})();
44 changes: 9 additions & 35 deletions tests/test.js
Expand Up @@ -59,14 +59,14 @@ $(document).ready(function() {
equals(library.first().get('length'), 123, 'verify length is still there');

library.fetch();
equals(library.length, 1, 'should not create second object when changing ID');
equals(library.length, 2, 'should not auto remove first object when changing ID');
});

test("should remove from collection", function() {
_(23).times(function() {
library.create(attrs);
});
library.each(function(book) {
_(23).times(function(index) {
library.create({id: index});
});
_(library.toArray()).chain().clone().each(function(book) {
book.destroy();
});
equals(library.length, 0, 'item was destroyed and library is empty');
Expand Down Expand Up @@ -117,7 +117,7 @@ $(document).ready(function() {
author : 'Bill Shakespeare',
length : 123
},
localStorage = new Backbone.LocalStorage('TheTempest')
localStorage : new Backbone.LocalStorage('TheTempest')
});

var book = null;
Expand All @@ -137,36 +137,10 @@ $(document).ready(function() {
});

test("should remove book when destroying", function() {
book.save()
book.save({author: 'fnord'})
equals(Book.prototype.localStorage.findAll().length, 1, 'book removed');
book.destroy()
book.fetch()
equals(book.get('author'), undefined, 'attributes not initialized from defaults');
equals(Book.prototype.localStorage.findAll().length, 0, 'book removed');
});

test("Model: localSync", function() {
// Write to localStorage/store
book.save();

equals(book.get('title'), 'The Tempest', 'model created');

// Set, but don't save
book.set({ 'title': "Wombat's Fun Adventure" });
equals(book.get('title'), "Wombat's Fun Adventure", 'title changed, but not saved');

book.fetch();

// Read, the unsaved title value above should be discarded
equals(book.get('title'), 'The Tempest', 'read from store successful')

// Update
book.save({ author: 'William Shakespeare'});
book.fetch();
equals(book.get('author'), 'William Shakespeare', 'author successfully updated');
equals(book.get('length'), 123, 'verify length is still there');

// Delete
book.destroy();

});

});

0 comments on commit 82ecca1

Please sign in to comment.