Permalink
Browse files

Add explicit test detection.

  • Loading branch information...
1 parent 4c588f4 commit 90ffbafb62b10353e6438719b396e62e97fe07bd @ryan-roemer ryan-roemer committed Feb 22, 2013
Showing with 15 additions and 23 deletions.
  1. +2 −1 .gitignore
  2. +1 −1 backbone.localStorage-min.js
  3. +8 −7 backbone.localStorage.js
  4. +4 −14 spec/localStorage_spec.js
View
@@ -1 +1,2 @@
-node_modules
+node_modules
+\.DS_Store

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -100,7 +100,7 @@ _.extend(Backbone.LocalStorage.prototype, {
localStorage: function() {
return localStorage;
},
-
+
// fix for "illegal access" error on Android when JSON.parse is passed null
jsonData: function (data) {
return data && JSON.parse(data);
@@ -110,11 +110,11 @@ _.extend(Backbone.LocalStorage.prototype, {
// localSync delegate to the model or collection's
// *localStorage* property, which should be an instance of `Store`.
-// window.Store.sync and Backbone.localSync is deprectated, use Backbone.LocalStorage.sync instead
+// window.Store.sync and Backbone.localSync is deprecated, use Backbone.LocalStorage.sync instead
Backbone.LocalStorage.sync = window.Store.sync = Backbone.localSync = function(method, model, options) {
var store = model.localStorage || model.collection.localStorage;
- var resp, errorMessage, syncDfd = $.Deferred && $.Deferred(); //If $ is having Deferred - use it.
+ var resp, errorMessage, syncDfd = $.Deferred && $.Deferred(); //If $ is having Deferred - use it.
try {
@@ -134,7 +134,8 @@ Backbone.LocalStorage.sync = window.Store.sync = Backbone.localSync = function(m
}
} catch(error) {
- if (error.code === DOMException.QUOTA_EXCEEDED_ERR && window.localStorage.length === 0)
+ // Special test case: Can't easily mock `window.localStorage.length`, so add test detection.
+ if (error.code === DOMException.QUOTA_EXCEEDED_ERR && (window.localStorage.length === 0 || window.localStorage.setItem._isTest))
errorMessage = "Private browsing is unsupported";
else
errorMessage = error.message;
@@ -154,19 +155,19 @@ Backbone.LocalStorage.sync = window.Store.sync = Backbone.localSync = function(m
} else {
errorMessage = errorMessage ? errorMessage
: "Record Not Found";
-
+
model.trigger("error", model, errorMessage, options);
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);
View
@@ -291,21 +291,17 @@ describe("Backbone.localStorage", function(){
describe("private browsing", function(){
var model = new Model()
- , oldSetItem = window.localStorage.setItem
- , oldLength = window.localStorage.length;
+ , oldSetItem = window.localStorage.setItem;
before(function(){
- // Patch localStorage length to allow error trap.
- Object.defineProperty(window.localStorage, "length", {
- configurable: true,
- get: function() { return 0; }
- });
-
window.localStorage.setItem = function(){
var error = new Error();
error.code = DOMException.QUOTA_EXCEEDED_ERR;
throw error;
};
+
+ // Indicate test only function.
+ window.localStorage.setItem._isTest = true;
});
var error;
@@ -330,12 +326,6 @@ describe("Backbone.localStorage", function(){
});
after(function(){
- // Restore localStorage length.
- Object.defineProperty(window.localStorage, "length", {
- configurable: true,
- value: oldLength
- });
-
window.localStorage.setItem = oldSetItem;
})

0 comments on commit 90ffbaf

Please sign in to comment.