forked from jeromegn/Backbone.localStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
3,814 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<!DOCTYPE HTML> | ||
<html> | ||
<head> | ||
<title>Backbone Test Suite</title> | ||
<link rel="stylesheet" href="vendor/qunit.css" type="text/css" media="screen" /> | ||
<!-- let's wait and see if we need these | ||
<script type="text/javascript" src="vendor/json2.js"></script> | ||
<script type="text/javascript" src="vendor/jquery-1.5.js"></script> | ||
<script type="text/javascript" src="vendor/jslitmus.js"></script> | ||
--> | ||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script> | ||
<script type="text/javascript" src="vendor/qunit.js"></script> | ||
<script type="text/javascript" src="vendor/underscore-1.1.6.js"></script> | ||
<script type="text/javascript" src="vendor/backbone.js"></script> | ||
<script type="text/javascript" src="../backbone.localStorage.js"></script> | ||
|
||
|
||
<!-- tests --> | ||
<script type="text/javascript" src="test.js"></script> | ||
|
||
</head> | ||
<body> | ||
<h1 id="qunit-header">Backbone Test Suite</h1> | ||
<h2 id="qunit-banner"></h2> | ||
<h2 id="qunit-userAgent"></h2> | ||
<ol id="qunit-tests"></ol> | ||
<br /><br /> | ||
<h1 id="qunit-header"><a href="#">Backbone Speed Suite</a></h1> | ||
|
||
<!-- | ||
<div id="jslitmus_container" style="margin: 20px 10px;"></div> | ||
--> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
$(document).ready(function() { | ||
module("localStorage"); | ||
|
||
var Library = Backbone.Collection.extend({ | ||
localStorage: new window.Store("libraryStore") | ||
|
||
// is the problem with my library that is has no model reference? | ||
}); | ||
|
||
var library = new Library(); | ||
|
||
var attrs = { | ||
title : 'The Tempest', | ||
author : 'Bill Shakespeare', | ||
length : 123 | ||
}; | ||
|
||
// Make sure there is no library collection when we start | ||
|
||
var killStorage = function (name) { | ||
for (var i = 0; i < localStorage.length; i++) { | ||
var key = localStorage.key(i); | ||
if (key.indexOf(name) !== -1) { | ||
localStorage.removeItem(key); | ||
} | ||
} | ||
|
||
}; | ||
|
||
// read from the library object that shouldn't exist when we start | ||
test("collection", function() { | ||
killStorage('libraryStore'); | ||
library.fetch(); | ||
equals(library.length, 0, 'empty read'); | ||
|
||
library.create(attrs); | ||
equals(library.length, 1, 'one item added'); | ||
equals(library.first().get('title'), 'The Tempest', 'title was read'); | ||
equals(library.first().get('author'), 'Bill Shakespeare', 'author was read'); | ||
equals(library.first().get('length'), 123, 'length was read'); | ||
|
||
library.first().save({id: '1-the-tempest', author: 'William Shakespeare'}); | ||
equals(library.first().get('id'), '1-the-tempest', 'verify ID update'); | ||
equals(library.first().get('title'), 'The Tempest', 'verify title is still there'); | ||
equals(library.first().get('author'), 'William Shakespeare', 'verify author update'); | ||
equals(library.first().get('length'), 123, 'verify length is still there'); | ||
library.each(function(book) { | ||
book.destroy(); | ||
}); | ||
equals(library.length, 0, 'item was destroyed and library is empty'); | ||
|
||
}); | ||
|
||
/* | ||
test("sync: update", function() { | ||
library.first().save({id: '1-the-tempest', author: 'William Shakespeare'}); | ||
equals(lastRequest.url, '/library/1-the-tempest'); | ||
equals(lastRequest.type, 'PUT'); | ||
equals(lastRequest.dataType, 'json'); | ||
var data = JSON.parse(lastRequest.data); | ||
equals(data.id, '1-the-tempest'); | ||
equals(data.title, 'The Tempest'); | ||
equals(data.author, 'William Shakespeare'); | ||
equals(data.length, 123); | ||
}); | ||
test("sync: update with emulateHTTP and emulateJSON", function() { | ||
Backbone.emulateHTTP = Backbone.emulateJSON = true; | ||
library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}); | ||
equals(lastRequest.url, '/library/2-the-tempest'); | ||
equals(lastRequest.type, 'POST'); | ||
equals(lastRequest.dataType, 'json'); | ||
equals(lastRequest.data._method, 'PUT'); | ||
var data = JSON.parse(lastRequest.data.model); | ||
equals(data.id, '2-the-tempest'); | ||
equals(data.author, 'Tim Shakespeare'); | ||
equals(data.length, 123); | ||
Backbone.emulateHTTP = Backbone.emulateJSON = false; | ||
}); | ||
test("sync: update with just emulateHTTP", function() { | ||
Backbone.emulateHTTP = true; | ||
library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}); | ||
equals(lastRequest.url, '/library/2-the-tempest'); | ||
equals(lastRequest.type, 'POST'); | ||
equals(lastRequest.contentType, 'application/json'); | ||
var data = JSON.parse(lastRequest.data); | ||
equals(data.id, '2-the-tempest'); | ||
equals(data.author, 'Tim Shakespeare'); | ||
equals(data.length, 123); | ||
Backbone.emulateHTTP = false; | ||
}); | ||
test("sync: update with just emulateJSON", function() { | ||
Backbone.emulateJSON = true; | ||
library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}); | ||
equals(lastRequest.url, '/library/2-the-tempest'); | ||
equals(lastRequest.type, 'PUT'); | ||
equals(lastRequest.contentType, 'application/x-www-form-urlencoded'); | ||
var data = JSON.parse(lastRequest.data.model); | ||
equals(data.id, '2-the-tempest'); | ||
equals(data.author, 'Tim Shakespeare'); | ||
equals(data.length, 123); | ||
Backbone.emulateJSON = false; | ||
}); | ||
test("sync: read model", function() { | ||
library.first().fetch(); | ||
equals(lastRequest.url, '/library/2-the-tempest'); | ||
equals(lastRequest.type, 'GET'); | ||
ok(_.isEmpty(lastRequest.data)); | ||
}); | ||
test("sync: destroy with emulateHTTP", function() { | ||
Backbone.emulateHTTP = Backbone.emulateJSON = true; | ||
library.first().destroy(); | ||
equals(lastRequest.url, '/library/2-the-tempest'); | ||
equals(lastRequest.type, 'POST'); | ||
equals(JSON.stringify(lastRequest.data), '{"_method":"DELETE"}'); | ||
Backbone.emulateHTTP = Backbone.emulateJSON = false; | ||
}); | ||
test("sync: urlError", function() { | ||
model = new Backbone.Model(); | ||
raises(function() { | ||
model.fetch(); | ||
}); | ||
model.fetch({url: '/one/two'}); | ||
equals(lastRequest.url, '/one/two'); | ||
}); | ||
*/ | ||
|
||
}); |
Oops, something went wrong.