jeromegn/Backbone.localStorage — pull request #75 by ryan-roemer (2013-02-22 16:20:19+00:00)
Title: Keep tests from wiping all localStorage in Chrome, Safari.

Great project! This adapter is fantastic, especially for quick projects needing less than a full REST infrastructure.

I occasionally do dev. on the adapter itself (like when Backbone.js changed some breaking API stuff in v0.9.10). In running the unit tests in Chrome, I noticed that my *other* Backbone.js applications all lost their `localStorage` data. I traced this back to the `window.localStorage.clear()` calls.

This patch adds a `clearStorage` helper to wipe out just the storage that that a specific instantiation of the adapter uses (rather than all of `localStorage`).

In doing this, one other issue that arose is that the `"Private browsing is unsupported"` error check relies on a `window.localStorage.length === 0` test. As reliably mocking this condition in Safari and Chrome seems impossible (on my short set of attempts), I added an explicit test check `window.localStorage.setItem._isTest` on the already patched `window.localStorage.setItem` method.

I can totally understand if adding explicit test detection in the actual library is not a favored approach. I can rejigger that in any other way you think might be better, but I don't think I'm going to be able to fake the `window.localStorage.length === 0` test.

At any rate, the current patch passes:

* Chrome
* Safari
* `make test` (mocha phantom)

Thanks!,
Ryan