Skip to content
Browse files

Using localStorage in a browser that doesn't support it will fail.

  • Loading branch information...
1 parent 51fe038 commit c672751ced884d1e4e4a1052eea5369f690b0094 @benpickles committed
Showing with 59 additions and 104 deletions.
  1. +59 −104 test/tests/model_local_storage_test.js
View
163 test/tests/model_local_storage_test.js
@@ -1,127 +1,82 @@
-module("Model.localStorage")
-
-if (window.localStorage) {
- // localStorage tests
- var localStorageTest = function(name, func) {
- test(name, function() {
- localStorage.clear()
- func()
- })
+module("Model.localStorage", {
+ teardown: function() {
+ if (window.localStorage) window.localStorage.clear()
}
+})
- localStorageTest("read", function() {
- var Post = Model("post", function() {
- this.use(Model.localStorage)
- })
+test("read", function() {
+ if (!window.localStorage)
+ return ok(false, "localStorage not supported in this browser")
- Post.persistence.read(function(models) {
- equal(models.length, 0)
- })
-
- localStorage.setItem("post-a", JSON.stringify({ a: "a" }))
- localStorage.setItem("post-b", JSON.stringify({ b: "b" }))
- localStorage.setItem("post-collection", '["post-a","post-b"]')
-
- Post.persistence.read(function(models) {
- equal(models.length, 2)
-
- var post1 = models[0]
- var post2 = models[1]
-
- equal("post-a", post1.uid)
- deepEqual({ a: "a" }, post1.attr())
- equal("post-b", post2.uid)
- deepEqual({ b: "b" }, post2.attr())
-
- Post.collection = [post1, post2]
- })
-
- Post.persistence.read(function(models) {
- equal(models.length, 0, "filters models already in the collection")
- })
+ var Post = Model("post", function() {
+ this.use(Model.localStorage)
})
- localStorageTest("create, update, destroy", function() {
- var Post = Model("post", function() {
- this.use(Model.localStorage)
- })
-
- equal(Post.collection.length, 0)
- equal(localStorage.length, 0)
-
- var post = new Post({ title: "foo", foo: "bar" })
- post.save()
-
- equal(Post.collection.length, 1)
- equal(localStorage.length, 2)
- deepEqual({ title: "foo", foo: "bar" }, JSON.parse(localStorage[post.uid]))
- equal(localStorage["post-collection"], '["' + post.uid + '"]',
- "should be stored in localStorage")
+ Post.persistence.read(function(models) {
+ equal(models.length, 0)
+ })
- post.attr({ title: ".", foo: null, bar: "baz" })
- post.save()
+ localStorage.setItem("post-a", JSON.stringify({ a: "a" }))
+ localStorage.setItem("post-b", JSON.stringify({ b: "b" }))
+ localStorage.setItem("post-collection", '["post-a","post-b"]')
- equal(Post.collection.length, 1)
- equal(localStorage.length, 2)
- deepEqual({ title: ".", foo: null, bar: "baz" }, JSON.parse(localStorage[post.uid]))
- equal(localStorage["post-collection"], '["' + post.uid + '"]',
- "should not alter localStorage list")
+ Post.persistence.read(function(models) {
+ equal(models.length, 2)
- post.destroy()
+ var post1 = models[0]
+ var post2 = models[1]
- equal(Post.collection.length, 0)
- equal(localStorage.length, 1)
- ok(!localStorage[post.uid])
- equal(localStorage["post-collection"], "[]",
- "should be removed from localStorage list")
+ equal("post-a", post1.uid)
+ deepEqual({ a: "a" }, post1.attr())
+ equal("post-b", post2.uid)
+ deepEqual({ b: "b" }, post2.attr())
- Post.persistence.read(function(models) {
- equal(models.length, 0)
- })
- })
-} else {
- // localStorage not supported tests
- test("read", function() {
- var Post = Model("post", function() {
- this.use(Model.localStorage)
- })
-
- Post.persistence.read(function(models) {
- equal(models.length, 0)
- })
+ Post.collection = [post1, post2]
})
- test("create, update, destroy", function() {
- var Post = Model("post", function() {
- this.use(Model.localStorage)
- })
+ Post.persistence.read(function(models) {
+ equal(models.length, 0, "filters models already in the collection")
+ })
+})
- equal(Post.collection.length, 0)
+test("create, update, destroy", function() {
+ if (!window.localStorage)
+ return ok(false, "localStorage not supported in this browser")
- var post = new Post({ title: "foo", foo: "bar" })
- post.save()
+ var Post = Model("post", function() {
+ this.use(Model.localStorage)
+ })
- equal(Post.collection.length, 1)
+ equal(Post.collection.length, 0)
+ equal(localStorage.length, 0)
- Post.persistence.read(function(models) {
- equal(models.length, 0)
- })
+ var post = new Post({ title: "foo", foo: "bar" })
+ post.save()
- post.attr({ title: ".", foo: null, bar: "baz" })
- post.save()
+ equal(Post.collection.length, 1)
+ equal(localStorage.length, 2)
+ deepEqual({ title: "foo", foo: "bar" }, JSON.parse(localStorage[post.uid]))
+ equal(localStorage["post-collection"], '["' + post.uid + '"]',
+ "should be stored in localStorage")
- equal(Post.collection.length, 1)
+ post.attr({ title: ".", foo: null, bar: "baz" })
+ post.save()
- Post.persistence.read(function(models) {
- equal(models.length, 0)
- })
+ equal(Post.collection.length, 1)
+ equal(localStorage.length, 2)
+ deepEqual({ title: ".", foo: null, bar: "baz" }, JSON.parse(localStorage[post.uid]))
+ equal(localStorage["post-collection"], '["' + post.uid + '"]',
+ "should not alter localStorage list")
- post.destroy()
+ post.destroy()
- equal(Post.collection.length, 0)
+ equal(Post.collection.length, 0)
+ equal(localStorage.length, 1)
+ ok(!localStorage[post.uid])
+ equal(localStorage["post-collection"], "[]",
+ "should be removed from localStorage list")
- Post.persistence.read(function(models) {
- equal(models.length, 0)
- })
+ Post.persistence.read(function(models) {
+ equal(models.length, 0)
})
-}
+})

0 comments on commit c672751

Please sign in to comment.
Something went wrong with that request. Please try again.