-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstoragespec.js
49 lines (48 loc) · 1.94 KB
/
storagespec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
describe("TurtleStorage", function () {
var storage, localStorage;
beforeEach(function before() {
localStorage = jasmine.createSpyObj("localStorage", [
"setItem",
"getItem",
"removeItem",
"key",
]);
localStorage.length = 5;
var revisions = {
"other-prefix.foo": "gack!",
"other-prefix.phoo": "cough!",
"prefix.bar": "BAR",
"prefix.baz": "BAZ",
"prefix.foo": "FOO",
};
localStorage.key.andCallFake(function(i) {
return Object.keys(revisions).sort()[i];
});
localStorage.getItem.andCallFake(function(key) {
return revisions[key];
});
storage = new Turtle.Storage("prefix", localStorage);
storage.setItem("key1", "value1");
});
it("has a valid spy for localStorage", function () {
expect(localStorage.key(0)).toEqual("other-prefix.foo");
expect(localStorage.key(1)).toEqual("other-prefix.phoo");
expect(localStorage.key(2)).toEqual("prefix.bar");
expect(localStorage.getItem("other-prefix.foo")).toEqual("gack!");
expect(localStorage.getItem("prefix.foo")).toEqual("FOO");
});
it("returns an array of storage keys with the prefix stripped", function () {
expect(storage.keys()).toEqual(["bar", "baz", "foo"]);
});
it("should prefix the storage keys on setItem()", function setItem() {
expect(localStorage.setItem).toHaveBeenCalledWith("prefix.key1", "value1");
});
it("should load the prefixed key on getItem()", function getItem() {
var result = storage.getItem("key1");
expect(localStorage.getItem).toHaveBeenCalledWith("prefix.key1");
});
it("should remove the prefixed key on removeItem()", function removeItem() {
storage.removeItem("key1");
expect(localStorage.removeItem).toHaveBeenCalledWith("prefix.key1");
});
});