1 | <!DOCTYPE html>
|
2 | <head>
|
3 | <title>store.js test</title>
|
4 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
5 | <style type="text/css">
|
6 | body { margin: 50px; font-family: helvetica; color: #333; }
|
7 | div { color: green; }
|
8 | #errorOutput div { color: red; }
|
9 | </style>
|
10 | <script src="json.js"></script>
|
11 | <script src="store.js"></script>
|
12 | </head>
|
13 | <body>
|
14 | tests for <a href="http://github.com/marcuswestin/store.js">store.js</a>
|
15 | <div id="errorOutput"></div>
|
16 |
|
17 | <script>
|
18 | (function() {
|
19 | var doc = document,
|
20 | errorOutput = doc.getElementById('errorOutput'),
|
21 | testFailed = false,
|
22 | isSecondPass = (doc.location.hash == '#secondPass')
|
23 |
|
24 | function outputError(msg) { errorOutput.appendChild(doc.createElement('div')).innerHTML = msg }
|
25 | function assert(truthy, msg) {
|
26 | if (!truthy) {
|
27 | outputError((isSecondPass ? 'second' : 'first') + ' pass bad assert: ' + msg);
|
28 | if (store.disabled) { outputError('<br>Note that store.disabled == true') }
|
29 | testFailed = true
|
30 | }
|
31 | }
|
32 |
|
33 | function doFirstPass() {
|
34 | store.clear()
|
35 |
|
36 | store.set('foo', 'bar')
|
37 | assert(store.get('foo') == 'bar', "stored key 'foo' not equal to stored value 'bar'")
|
38 |
|
39 | store.remove('foo')
|
40 | assert(store.get('foo') == null, "removed key 'foo' not null")
|
41 |
|
42 | assert(store.set('foo','value') == 'value', "store#set returns the stored value")
|
43 |
|
44 | store.set('foo', 'bar1')
|
45 | store.set('foo', 'bar2')
|
46 | assert(store.get('foo') == 'bar2', "key 'foo' is not equal to second value set 'bar2'")
|
47 |
|
48 | store.set('foo', 'bar')
|
49 | store.set('bar', 'foo')
|
50 | store.remove('foo')
|
51 | assert(store.get('bar') == 'foo', "removing key 'foo' also removed key 'bar'")
|
52 |
|
53 | store.set('foo', 'bar')
|
54 | store.set('bar', 'foo')
|
55 | store.clear()
|
56 | assert(store.get('foo') == null && store.get('bar') == null, "keys foo and bar not cleared after store cleared")
|
57 |
|
58 | store.transact('foosact', function(val) {
|
59 | assert(typeof val == 'object', "new key is not an object at beginning of transaction")
|
60 | val.foo = 'foo'
|
61 | })
|
62 | store.transact('foosact', function(val) {
|
63 | assert(val.foo == 'foo', "first transaction did not register")
|
64 | val.bar = 'bar'
|
65 | })
|
66 | assert(store.get('foosact').bar == 'bar', "second transaction did not register")
|
67 |
|
68 | store.set('foo', { name: 'marcus', arr: [1,2,3] })
|
69 | assert(typeof store.get('foo') == 'object', "type of stored object 'foo' is not 'object'")
|
70 | assert(store.get('foo') instanceof Object, "stored object 'foo' is not an instance of Object")
|
71 | assert(store.get('foo').name == 'marcus', "property 'name' of stored object 'foo' is not 'marcus'")
|
72 | assert(store.get('foo').arr instanceof Array, "Array property 'arr' of stored object 'foo' is not an instance of Array")
|
73 | assert(store.get('foo').arr.length == 3, "The length of Array property 'arr' stored on object 'foo' is not 3")
|
74 |
|
75 | assert(store.enabled = !store.disabled, "Store.enabled is not the reverse of .disabled");
|
76 |
|
77 | store.remove('circularReference')
|
78 | var circularOne = {}
|
79 | var circularTwo = { one:circularOne }
|
80 | circularOne.two = circularTwo
|
81 | var threw = false
|
82 | try { store.set('circularReference', circularOne) }
|
83 | catch(e) { threw = true }
|
84 | assert(threw, "storing object with circular reference did not throw")
|
85 | assert(!store.get('circularReference'), "attempting to store object with circular reference which should have faile affected store state")
|
86 |
|
87 |
|
88 |
|
89 | var promoteValues = {
|
90 | 'int' : 42,
|
91 | 'bool' : true,
|
92 | 'float' : Math.PI,
|
93 | 'string' : "Don't Panic",
|
94 | 'odd_string' : "{ZYX' abc:;::)))"
|
95 | }
|
96 | for (key in promoteValues) {
|
97 | if (window.localStorage) {
|
98 | window.localStorage[key] = promoteValues[key]
|
99 | }
|
100 | }
|
101 | for(key in promoteValues) {
|
102 | assert(store.get(key) == promoteValues[key], key+" was not correctly promoted to valid JSON")
|
103 | store.remove(key)
|
104 | }
|
105 |
|
106 |
|
107 | store.set('firstPassFoo', 'bar')
|
108 | store.set('firstPassObj', { woot: true })
|
109 |
|
110 | var all = store.getAll()
|
111 | assert(all.firstPassFoo == 'bar', 'getAll gets firstPassFoo')
|
112 | assert(countProperties(all) == 4, 'getAll gets all 4 values')
|
113 | }
|
114 |
|
115 | function doSecondPass() {
|
116 | assert(store.get('firstPassFoo') == 'bar', "first pass key 'firstPassFoo' not equal to stored value 'bar'")
|
117 |
|
118 | var all = store.getAll()
|
119 | assert(all.firstPassFoo == 'bar', "getAll still gets firstPassFoo on second pass")
|
120 | assert(countProperties(all) == 4, "getAll gets all 4 values")
|
121 |
|
122 | store.clear()
|
123 | assert(store.get('firstPassFoo') == null, "first pass key 'firstPassFoo' not null after store cleared")
|
124 |
|
125 | var all = store.getAll()
|
126 | assert(countProperties(all) == 0, "getAll returns 0 properties after store.clear() has been called")
|
127 | }
|
128 |
|
129 | function countProperties(obj) {
|
130 | var count = 0
|
131 | for (var key in obj) {
|
132 | if (obj.hasOwnProperty(key)) { count++ }
|
133 | }
|
134 | return count
|
135 | }
|
136 |
|
137 | try {
|
138 | if (isSecondPass) { doSecondPass() }
|
139 | else { doFirstPass() }
|
140 | } catch(e) {
|
141 | assert(false, 'Tests should not throw: "' + JSON.stringify(e) + '"')
|
142 | }
|
143 |
|
144 | if (!testFailed) {
|
145 | if (!isSecondPass) {
|
146 | doc.location.hash = '#secondPass'
|
147 | doc.location.reload()
|
148 | } else {
|
149 | doc.location.hash = '#'
|
150 | doc.body.appendChild(doc.createElement('div')).innerHTML = 'Tests passed'
|
151 | }
|
152 | }
|
153 | })()
|
154 | </script>
|
155 | </body>
|
156 | </html>
|