Daniel@0
|
1 "use strict";
|
Daniel@0
|
2
|
Daniel@0
|
3 describe("StorageModule", function() {
|
Daniel@0
|
4
|
Daniel@0
|
5 var dummyModule = null;
|
Daniel@0
|
6
|
Daniel@0
|
7 beforeAll(function() {
|
Daniel@0
|
8 jasmine.helpers.dumpStorage();
|
Daniel@0
|
9 jasmine.helpers.clearStorage();
|
Daniel@0
|
10 dummyModule = App.module("DummyModule");
|
Daniel@0
|
11 });
|
Daniel@0
|
12
|
Daniel@0
|
13 beforeEach(function() {
|
Daniel@0
|
14 jasmine.helpers.clearStorage();
|
Daniel@0
|
15 });
|
Daniel@0
|
16
|
Daniel@0
|
17 afterAll(function() {
|
Daniel@0
|
18 jasmine.helpers.restoreStorageFromDump();
|
Daniel@0
|
19 });
|
Daniel@0
|
20
|
Daniel@0
|
21 it("works with strings: writes and reads", function() {
|
Daniel@0
|
22 var testVars = [
|
Daniel@0
|
23 ["MyString", "42"],
|
Daniel@0
|
24 ["MyString2", "test"],
|
Daniel@0
|
25 ];
|
Daniel@0
|
26 var faultyVars = [
|
Daniel@0
|
27 ["MyString", 42],
|
Daniel@0
|
28 ["MyString2", {}],
|
Daniel@0
|
29 ["MyString3", [1,2,3]]
|
Daniel@0
|
30 ["MyString4", null]
|
Daniel@0
|
31 ];
|
Daniel@0
|
32
|
Daniel@0
|
33 for (var i = 0; i < testVars.length; i++) {
|
Daniel@0
|
34 var testVar = testVars[i];
|
Daniel@0
|
35 expect(localStorage.length).toEqual(0);
|
Daniel@0
|
36 expect(App.DataModule.Storage.setStrCache(dummyModule, testVar[0], testVar[1])).toEqual(true);
|
Daniel@0
|
37 expect(localStorage.getItem("dml.DummyModule." + testVar[0])).toEqual(testVar[1]);
|
Daniel@0
|
38 expect(localStorage.length).toEqual(1);
|
Daniel@0
|
39
|
Daniel@0
|
40 expect(App.DataModule.Storage.setStrCache(dummyModule, testVar[0], undefined)).toEqual(true);
|
Daniel@0
|
41 expect(App.DataModule.Storage.getStrCache(dummyModule, testVar[0])).toEqual(undefined);
|
Daniel@0
|
42 expect(localStorage.getItem("dml.DummyModule." + testVar[0])).toEqual(null);
|
Daniel@0
|
43 expect(localStorage.length).toEqual(0);
|
Daniel@0
|
44 }
|
Daniel@0
|
45
|
Daniel@0
|
46 for (var i = 0; i < faultyVars.length; i++) {
|
Daniel@0
|
47 var faultyVar = faultyVars[i];
|
Daniel@0
|
48 expect(function() {
|
Daniel@0
|
49 App.DataModule.Storage.setStrCache(dummyModule, faultyVar[0], faultyVar[1]);
|
Daniel@0
|
50 }).toThrow();
|
Daniel@0
|
51
|
Daniel@0
|
52 expect(localStorage.length).toEqual(0);
|
Daniel@0
|
53 }
|
Daniel@0
|
54 });
|
Daniel@0
|
55
|
Daniel@0
|
56 it("works with strings: returns false when storage is full", function() {
|
Daniel@0
|
57 var storageBecameFull = false;
|
Daniel@0
|
58 var strToWrite = _.str.repeat("x", 100000);
|
Daniel@0
|
59 for (var i = 0; i < 1000; i++) {
|
Daniel@0
|
60 var result = App.DataModule.Storage.setStrCache(dummyModule, "test" + i, strToWrite);
|
Daniel@0
|
61 if (result === true) {
|
Daniel@0
|
62 expect(localStorage.getItem("dml.DummyModule." + "test" + i)).toEqual(strToWrite);
|
Daniel@0
|
63 } else {
|
Daniel@0
|
64 storageBecameFull = true;
|
Daniel@0
|
65 expect(function() {
|
Daniel@0
|
66 localStorage.setItem("dml.DummyModule." + "test" + i, strToWrite);
|
Daniel@0
|
67 }).toThrow();
|
Daniel@0
|
68 break;
|
Daniel@0
|
69 }
|
Daniel@0
|
70 }
|
Daniel@0
|
71 expect(storageBecameFull).toBe(true);
|
Daniel@0
|
72 });
|
Daniel@0
|
73
|
Daniel@0
|
74
|
Daniel@0
|
75 it("works with objects: writes and reads", function() {
|
Daniel@0
|
76 var testVars = [
|
Daniel@0
|
77 ["MyString", {}],
|
Daniel@0
|
78 ["MyString", []],
|
Daniel@0
|
79 ["MyString2", null],
|
Daniel@0
|
80 ["MyString3", {x: 2, y: null, z: undefined, a: "string", b: ["array"], c: {object: true}}],
|
Daniel@0
|
81 ["MyString2", [10, 20, 30]]
|
Daniel@0
|
82 ];
|
Daniel@0
|
83 var faultyVars = [
|
Daniel@0
|
84 ["MyString", 42],
|
Daniel@0
|
85 ["MyString3", "test"],
|
Daniel@0
|
86 ["MyString3", "[1, 2, 3"],
|
Daniel@0
|
87 ["MyString3", "{x: 10"],
|
Daniel@0
|
88 ];
|
Daniel@0
|
89
|
Daniel@0
|
90 for (var i = 0; i < testVars.length; i++) {
|
Daniel@0
|
91 var testVar = testVars[i];
|
Daniel@0
|
92 expect(localStorage.length).toEqual(0);
|
Daniel@0
|
93 expect(App.DataModule.Storage.setObjCache(dummyModule, testVar[0], testVar[1])).toEqual(true);
|
Daniel@0
|
94 expect(localStorage.getItem("dml.DummyModule." + testVar[0])).toEqual(JSON.stringify(testVar[1]));
|
Daniel@0
|
95 expect(localStorage.length).toEqual(1);
|
Daniel@0
|
96
|
Daniel@0
|
97 expect(App.DataModule.Storage.setObjCache(dummyModule, testVar[0], undefined)).toEqual(true);
|
Daniel@0
|
98 expect(App.DataModule.Storage.getObjCache(dummyModule, testVar[0])).toEqual(undefined);
|
Daniel@0
|
99 expect(localStorage.getItem("dml.DummyModule." + testVar[0])).toEqual(null);
|
Daniel@0
|
100 expect(localStorage.length).toEqual(0);
|
Daniel@0
|
101 }
|
Daniel@0
|
102 for (var i = 0; i < faultyVars.length; i++) {
|
Daniel@0
|
103 var faultyVar = faultyVars[i];
|
Daniel@0
|
104 expect(function() {
|
Daniel@0
|
105 App.DataModule.Storage.setObjCache(dummyModule, faultyVar[0], faultyVar[1]);
|
Daniel@0
|
106 }).toThrow();
|
Daniel@0
|
107
|
Daniel@0
|
108 expect(localStorage.length).toEqual(0);
|
Daniel@0
|
109
|
Daniel@0
|
110 if (_.isString(faultyVar[1])) {
|
Daniel@0
|
111 App.DataModule.Storage.setStrCache(dummyModule, faultyVar[0], faultyVar[1]);
|
Daniel@0
|
112 expect(App.DataModule.Storage.getObjCache(dummyModule, faultyVar[0])).toEqual(null);
|
Daniel@0
|
113 App.DataModule.Storage.setStrCache(dummyModule, faultyVar[0], undefined);
|
Daniel@0
|
114 }
|
Daniel@0
|
115
|
Daniel@0
|
116 expect(localStorage.length).toEqual(0);
|
Daniel@0
|
117 }
|
Daniel@0
|
118
|
Daniel@0
|
119 });
|
Daniel@0
|
120
|
Daniel@0
|
121
|
Daniel@0
|
122 });
|