Permalink
Browse files

Add pathable function for manual stubbing.

  • Loading branch information...
1 parent 1f838f8 commit 557d0277fecacc7d0649166a038a55b8c6839c68 @ryan-roemer ryan-roemer committed Feb 22, 2013
Showing with 17 additions and 16 deletions.
  1. +1 −1 backbone.localStorage-min.js
  2. +6 −2 backbone.localStorage.js
  3. +10 −11 spec/localStorage_spec.js
  4. +0 −2 spec/runner.html

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

Oops, something went wrong.
View
@@ -118,6 +118,11 @@ _.extend(Backbone.LocalStorage.prototype, {
_.chain(local).keys()
.filter(function (k) { return itemRe.test(k); })
.each(function (k) { local.removeItem(k); });
+ },
+
+ // Size of localStorage.
+ _storageSize: function() {
+ return this.localStorage().length;
}
});
@@ -148,8 +153,7 @@ Backbone.LocalStorage.sync = window.Store.sync = Backbone.localSync = function(m
}
} catch(error) {
- // 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))
+ if (error.code === DOMException.QUOTA_EXCEEDED_ERR && store._storageSize() === 0)
errorMessage = "Private browsing is unsupported";
else
errorMessage = error.message;
View
@@ -274,22 +274,20 @@ describe("Backbone.localStorage", function(){
describe("private browsing", function(){
var model = new Model()
- , stub
+ , oldSetItem = window.localStorage.setItem
+ , oldStorageSize = model.localStorage._storageSize
, error;
before(function(done){
model.localStorage._clear();
- // Mock browser conditions for private error.
- stub = sinon.stub(window.localStorage, "setItem").throws(function(){
+ // Patch browser conditions for private error.
+ model.localStorage._storageSize = function(){ return 0; };
+ window.localStorage.setItem = function(){
var error = new Error();
error.code = DOMException.QUOTA_EXCEEDED_ERR;
- return error;
- }());
-
- // TODO MOCK & REMOVE LIB SHIM
- // Indicate test only function (wiped on unpatch).
- window.localStorage.setItem._isTest = true;
+ throw error;
+ };
model.save(attributes, {
error: function(model, err){
@@ -311,8 +309,9 @@ describe("Backbone.localStorage", function(){
});
after(function(){
- // Unwrap stubs.
- stub.restore();
+ // Unwrap patches.
+ model.localStorage._storageSize = oldStorageSize;
+ window.localStorage.setItem = oldSetItem;
})
});
View
@@ -17,8 +17,6 @@
window.assert = chai.assert;
</script>
- <script src="support/sinon.js"></script>
-
<script src="support/underscore.js"></script>
<script src="support/backbone.js"></script>

0 comments on commit 557d027

Please sign in to comment.