UNPKG

4.34 kBJavaScriptView Raw
1tests = {
2 outputError:null,
3 assert:assert,
4 runFirstPass:runFirstPass,
5 runSecondPass:runSecondPass,
6 failed:false
7}
8
9function assert(truthy, msg) {
10 if (!truthy) {
11 tests.outputError('bad assert: ' + msg);
12 if (store.disabled) { tests.outputError('<br>Note that store.disabled == true') }
13 tests.failed = true
14 }
15}
16
17function runFirstPass() {
18 store.clear()
19
20 store.get('unsetValue') // see https://github.com/marcuswestin/store.js/issues/63
21
22 store.set('foo', 'bar')
23 assert(store.get('foo') == 'bar', "stored key 'foo' not equal to stored value 'bar'")
24
25 store.remove('foo')
26 assert(store.get('foo') == null, "removed key 'foo' not null")
27
28 assert(store.set('foo','value') == 'value', "store#set returns the stored value")
29
30 store.set('foo', 'bar1')
31 store.set('foo', 'bar2')
32 assert(store.get('foo') == 'bar2', "key 'foo' is not equal to second value set 'bar2'")
33
34 store.set('foo', 'bar')
35 store.set('bar', 'foo')
36 store.remove('foo')
37 assert(store.get('bar') == 'foo', "removing key 'foo' also removed key 'bar'")
38
39 store.set('foo', 'bar')
40 store.set('bar', 'foo')
41 store.clear()
42 assert(store.get('foo') == null && store.get('bar') == null, "keys foo and bar not cleared after store cleared")
43
44 store.transact('foosact', function(val) {
45 assert(typeof val == 'object', "new key is not an object at beginning of transaction")
46 val.foo = 'foo'
47 })
48 store.transact('foosact', function(val) {
49 assert(val.foo == 'foo', "first transaction did not register")
50 val.bar = 'bar'
51 })
52 assert(store.get('foosact').bar == 'bar', "second transaction did not register")
53
54 store.set('foo', { name: 'marcus', arr: [1,2,3] })
55 assert(typeof store.get('foo') == 'object', "type of stored object 'foo' is not 'object'")
56 assert(store.get('foo') instanceof Object, "stored object 'foo' is not an instance of Object")
57 assert(store.get('foo').name == 'marcus', "property 'name' of stored object 'foo' is not 'marcus'")
58 assert(store.get('foo').arr instanceof Array, "Array property 'arr' of stored object 'foo' is not an instance of Array")
59 assert(store.get('foo').arr.length == 3, "The length of Array property 'arr' stored on object 'foo' is not 3")
60
61 assert(store.enabled = !store.disabled, "Store.enabled is not the reverse of .disabled");
62
63 store.remove('circularReference')
64 var circularOne = {}
65 var circularTwo = { one:circularOne }
66 circularOne.two = circularTwo
67 var threw = false
68 try { store.set('circularReference', circularOne) }
69 catch(e) { threw = true }
70 assert(threw, "storing object with circular reference did not throw")
71 assert(!store.get('circularReference'), "attempting to store object with circular reference which should have faile affected store state")
72
73 // If plain local storage was used before store.js, we should attempt to JSON.parse them into javascript values.
74 // Store values using vanilla localStorage, then read them out using store.js
75 if (typeof localStorage != 'undefined') {
76 var promoteValues = {
77 'int' : 42,
78 'bool' : true,
79 'float' : 3.141592653,
80 'string' : "Don't Panic",
81 'odd_string' : "{ZYX'} abc:;::)))"
82 }
83 for (key in promoteValues) {
84 localStorage[key] = promoteValues[key]
85 }
86 for (key in promoteValues) {
87 assert(store.get(key) == promoteValues[key], key+" was not correctly promoted to valid JSON")
88 store.remove(key)
89 }
90 }
91
92 // The following stored values get tested in doSecondPass after a page reload
93 store.set('firstPassFoo', 'bar')
94 store.set('firstPassObj', { woot: true })
95
96 var all = store.getAll()
97 assert(all.firstPassFoo == 'bar', 'getAll gets firstPassFoo')
98 assert(countProperties(all) == 4, 'getAll gets all 4 values')
99}
100
101function runSecondPass() {
102 assert(store.get('firstPassFoo') == 'bar', "first pass key 'firstPassFoo' not equal to stored value 'bar'")
103
104 var all = store.getAll()
105 assert(all.firstPassFoo == 'bar', "getAll still gets firstPassFoo on second pass")
106 assert(countProperties(all) == 4, "getAll gets all 4 values")
107
108 store.clear()
109 assert(store.get('firstPassFoo') == null, "first pass key 'firstPassFoo' not null after store cleared")
110
111 var all = store.getAll()
112 assert(countProperties(all) == 0, "getAll returns 0 properties after store.clear() has been called")
113}
114
115function countProperties(obj) {
116 var count = 0
117 for (var key in obj) {
118 if (obj.hasOwnProperty(key)) { count++ }
119 }
120 return count
121}