[483] | 1 | define(["doh/main", "../hash", "../topic"], function(doh, hash, topic){ |
---|
| 2 | |
---|
| 3 | // utilities for the tests: |
---|
| 4 | function setHash(h){ |
---|
| 5 | h = h || ""; |
---|
| 6 | location.replace('#'+h); |
---|
| 7 | } |
---|
| 8 | |
---|
| 9 | function getHash(){ |
---|
| 10 | var h = location.href, i = h.indexOf("#"); |
---|
| 11 | return (i >= 0) ? h.substring(i + 1) : ""; |
---|
| 12 | } |
---|
| 13 | |
---|
| 14 | doh.register("tests.hash", [ |
---|
| 15 | // hash as an empty string. |
---|
| 16 | { |
---|
| 17 | name: "Getting an empty hash", |
---|
| 18 | setUp: function(){ |
---|
| 19 | setHash(); |
---|
| 20 | }, |
---|
| 21 | runTest: function(t){ |
---|
| 22 | t.is('', hash()); |
---|
| 23 | } |
---|
| 24 | }, |
---|
| 25 | { |
---|
| 26 | name: "Setting an empty hash", |
---|
| 27 | setUp: function(){ |
---|
| 28 | hash(''); |
---|
| 29 | }, |
---|
| 30 | runTest: function(t){ |
---|
| 31 | t.is('', getHash()); |
---|
| 32 | } |
---|
| 33 | }, |
---|
| 34 | // hash as "test" |
---|
| 35 | { |
---|
| 36 | name: "Getting the hash of 'test'", |
---|
| 37 | setUp: function(){ |
---|
| 38 | setHash('test'); |
---|
| 39 | }, |
---|
| 40 | runTest: function(t){ |
---|
| 41 | t.is('test', hash()); |
---|
| 42 | }, |
---|
| 43 | tearDown: function(){ |
---|
| 44 | setHash(); |
---|
| 45 | } |
---|
| 46 | }, |
---|
| 47 | { |
---|
| 48 | name: "Setting the hash to 'test'", |
---|
| 49 | setUp: function(){ |
---|
| 50 | hash('test'); |
---|
| 51 | }, |
---|
| 52 | runTest: function(t){ |
---|
| 53 | t.is('test', getHash()); |
---|
| 54 | }, |
---|
| 55 | tearDown: function(){ |
---|
| 56 | setHash(); |
---|
| 57 | } |
---|
| 58 | }, |
---|
| 59 | // hash with spaces |
---|
| 60 | { |
---|
| 61 | name: "Getting the hash of 'test%20with%20spaces'", |
---|
| 62 | setUp: function(){ |
---|
| 63 | setHash('test%20with%20spaces'); |
---|
| 64 | }, |
---|
| 65 | runTest: function(t){ |
---|
| 66 | t.is('test%20with%20spaces', hash()); |
---|
| 67 | }, |
---|
| 68 | tearDown: function(){ |
---|
| 69 | setHash(); |
---|
| 70 | } |
---|
| 71 | }, |
---|
| 72 | { |
---|
| 73 | name: "Setting the hash of 'test%20with%20spaces'", |
---|
| 74 | setUp: function(){ |
---|
| 75 | setHash('test%20with%20spaces'); |
---|
| 76 | }, |
---|
| 77 | runTest: function(t){ |
---|
| 78 | t.is('test%20with%20spaces', getHash()); |
---|
| 79 | }, |
---|
| 80 | tearDown: function(){ |
---|
| 81 | setHash(); |
---|
| 82 | } |
---|
| 83 | }, |
---|
| 84 | // hash with encoded hash |
---|
| 85 | { |
---|
| 86 | name: "Getting the hash of 'test%23with%23encoded%23hashes'", |
---|
| 87 | setUp: function(){ |
---|
| 88 | setHash('test%23with%23encoded%23hashes'); |
---|
| 89 | }, |
---|
| 90 | runTest: function(t){ |
---|
| 91 | t.is('test%23with%23encoded%23hashes', hash()); |
---|
| 92 | }, |
---|
| 93 | tearDown: function(){ |
---|
| 94 | setHash(); |
---|
| 95 | } |
---|
| 96 | }, |
---|
| 97 | { |
---|
| 98 | name: "Setting the hash of 'test%23with%23encoded%23hashes'", |
---|
| 99 | setUp: function(){ |
---|
| 100 | setHash('test%23with%23encoded%23hashes'); |
---|
| 101 | }, |
---|
| 102 | runTest: function(t){ |
---|
| 103 | t.is('test%23with%23encoded%23hashes', getHash()); |
---|
| 104 | }, |
---|
| 105 | tearDown: function(){ |
---|
| 106 | setHash(); |
---|
| 107 | } |
---|
| 108 | }, |
---|
| 109 | // hash with plus character: test+with+pluses |
---|
| 110 | { |
---|
| 111 | name: "Getting the hash of 'test+with+pluses'", |
---|
| 112 | setUp: function(){ |
---|
| 113 | setHash('test+with+pluses'); |
---|
| 114 | }, |
---|
| 115 | runTest: function(t){ |
---|
| 116 | t.is('test+with+pluses', hash()); |
---|
| 117 | }, |
---|
| 118 | tearDown: function(){ |
---|
| 119 | setHash(); |
---|
| 120 | } |
---|
| 121 | }, |
---|
| 122 | { |
---|
| 123 | name: "Setting the hash to 'test+with+pluses'", |
---|
| 124 | setUp: function(){ |
---|
| 125 | hash('test+with+pluses'); |
---|
| 126 | }, |
---|
| 127 | runTest: function(t){ |
---|
| 128 | t.is('test+with+pluses', getHash()); |
---|
| 129 | }, |
---|
| 130 | tearDown: function(){ |
---|
| 131 | setHash(); |
---|
| 132 | } |
---|
| 133 | }, |
---|
| 134 | // hash with leading space |
---|
| 135 | { |
---|
| 136 | name: "Getting the hash of '%20leadingSpace'", |
---|
| 137 | setUp: function(){ |
---|
| 138 | setHash('%20leadingSpace'); |
---|
| 139 | }, |
---|
| 140 | runTest: function(t){ |
---|
| 141 | t.is('%20leadingSpace', hash()); |
---|
| 142 | }, |
---|
| 143 | tearDown: function(){ |
---|
| 144 | setHash(); |
---|
| 145 | } |
---|
| 146 | }, |
---|
| 147 | { |
---|
| 148 | name: "Setting the hash to '%20leadingSpace'", |
---|
| 149 | setUp: function(){ |
---|
| 150 | hash('%20leadingSpace'); |
---|
| 151 | }, |
---|
| 152 | runTest: function(t){ |
---|
| 153 | t.is('%20leadingSpace', getHash()); |
---|
| 154 | }, |
---|
| 155 | tearDown: function(){ |
---|
| 156 | setHash(); |
---|
| 157 | } |
---|
| 158 | }, |
---|
| 159 | |
---|
| 160 | // hash with trailing space: |
---|
| 161 | { |
---|
| 162 | name: "Getting the hash of 'trailingSpace%20'", |
---|
| 163 | setUp: function(){ |
---|
| 164 | setHash('trailingSpace%20'); |
---|
| 165 | }, |
---|
| 166 | runTest: function(t){ |
---|
| 167 | t.is('trailingSpace%20', hash()); |
---|
| 168 | }, |
---|
| 169 | tearDown: function(){ |
---|
| 170 | setHash(); |
---|
| 171 | } |
---|
| 172 | }, |
---|
| 173 | { |
---|
| 174 | name: "Setting the hash to 'trailingSpace%20'", |
---|
| 175 | setUp: function(){ |
---|
| 176 | hash('trailingSpace%20'); |
---|
| 177 | }, |
---|
| 178 | runTest: function(t){ |
---|
| 179 | t.is('trailingSpace%20', getHash()); |
---|
| 180 | }, |
---|
| 181 | tearDown: function(){ |
---|
| 182 | setHash(); |
---|
| 183 | } |
---|
| 184 | }, |
---|
| 185 | // hash with underscores. |
---|
| 186 | { |
---|
| 187 | name: "Getting the hash of 'under_score'", |
---|
| 188 | setUp: function(){ |
---|
| 189 | setHash('under_score'); |
---|
| 190 | }, |
---|
| 191 | runTest: function(t){ |
---|
| 192 | t.is('under_score', hash()); |
---|
| 193 | }, |
---|
| 194 | tearDown: function(){ |
---|
| 195 | setHash(); |
---|
| 196 | } |
---|
| 197 | }, |
---|
| 198 | { |
---|
| 199 | name: "Setting the hash to 'under_score'", |
---|
| 200 | setUp: function(){ |
---|
| 201 | hash('under_score'); |
---|
| 202 | }, |
---|
| 203 | runTest: function(t){ |
---|
| 204 | t.is('under_score', getHash()); |
---|
| 205 | }, |
---|
| 206 | tearDown: function(){ |
---|
| 207 | setHash(); |
---|
| 208 | } |
---|
| 209 | }, |
---|
| 210 | { |
---|
| 211 | name: "Getting the hash of 'extra&instring'", |
---|
| 212 | setUp: function(){ |
---|
| 213 | setHash("extra&instring"); |
---|
| 214 | }, |
---|
| 215 | runTest: function(t){ |
---|
| 216 | t.is("extra&instring", hash()); |
---|
| 217 | }, |
---|
| 218 | tearDown: function(){ |
---|
| 219 | setHash(); |
---|
| 220 | } |
---|
| 221 | }, |
---|
| 222 | { |
---|
| 223 | name: "Setting the hash to 'extra&instring'", |
---|
| 224 | setUp: function(){ |
---|
| 225 | hash('extra&instring'); |
---|
| 226 | }, |
---|
| 227 | runTest: function(t){ |
---|
| 228 | t.is('extra&instring', getHash()); |
---|
| 229 | }, |
---|
| 230 | tearDown: function(){ |
---|
| 231 | setHash(); |
---|
| 232 | } |
---|
| 233 | }, |
---|
| 234 | { |
---|
| 235 | name: "Getting the hash of 'extra?instring'", |
---|
| 236 | setUp: function(){ |
---|
| 237 | setHash('extra?instring'); |
---|
| 238 | }, |
---|
| 239 | runTest: function(t){ |
---|
| 240 | t.is('extra?instring', hash()); |
---|
| 241 | }, |
---|
| 242 | tearDown: function(){ |
---|
| 243 | setHash(); |
---|
| 244 | } |
---|
| 245 | }, |
---|
| 246 | { |
---|
| 247 | name: "Setting the hash of 'extra?instring'", |
---|
| 248 | setUp: function(){ |
---|
| 249 | hash('extra?instring'); |
---|
| 250 | }, |
---|
| 251 | runTest: function(t){ |
---|
| 252 | t.is('extra?instring', getHash()); |
---|
| 253 | }, |
---|
| 254 | tearDown: function(){ |
---|
| 255 | setHash(); |
---|
| 256 | } |
---|
| 257 | }, |
---|
| 258 | { |
---|
| 259 | name: "Getting the hash resembling a query parameter ('?testa=3&testb=test')", |
---|
| 260 | setUp: function(){ |
---|
| 261 | setHash('?testa=3&testb=test'); |
---|
| 262 | }, |
---|
| 263 | runTest: function(t){ |
---|
| 264 | t.is('?testa=3&testb=test', hash()); |
---|
| 265 | }, |
---|
| 266 | tearDown: function(){ |
---|
| 267 | setHash(); |
---|
| 268 | } |
---|
| 269 | }, |
---|
| 270 | { |
---|
| 271 | name: "Setting the hash resembling a query parameter ('?testa=3&testb=test')", |
---|
| 272 | setUp: function(){ |
---|
| 273 | hash('?testa=3&testb=test'); |
---|
| 274 | }, |
---|
| 275 | runTest: function(t){ |
---|
| 276 | t.is('?testa=3&testb=test', getHash()); |
---|
| 277 | }, |
---|
| 278 | tearDown: function(){ |
---|
| 279 | setHash(); |
---|
| 280 | } |
---|
| 281 | }, |
---|
| 282 | { |
---|
| 283 | name: "Setting the hash to '#leadingHash' should result in the hash being 'leadingHash'", |
---|
| 284 | setUp: function(){ |
---|
| 285 | hash('#leadingHash'); |
---|
| 286 | }, |
---|
| 287 | runTest: function(t){ |
---|
| 288 | t.is('leadingHash', getHash()); |
---|
| 289 | }, |
---|
| 290 | tearDown: function(){ |
---|
| 291 | setHash(); |
---|
| 292 | } |
---|
| 293 | }, |
---|
| 294 | { |
---|
| 295 | _s: null, // used for the subscriber. |
---|
| 296 | |
---|
| 297 | name: "Hash change publishes to '/dojo/hashchange'", |
---|
| 298 | setUp: function(t){ |
---|
| 299 | setHash(); |
---|
| 300 | }, |
---|
| 301 | runTest: function(t){ |
---|
| 302 | var d = new doh.Deferred(); |
---|
| 303 | this._s = topic.subscribe('/dojo/hashchange', d.getTestCallback(function(value){ |
---|
| 304 | doh.assertEqual('test', value); |
---|
| 305 | })); |
---|
| 306 | |
---|
| 307 | hash('test'); |
---|
| 308 | return d; |
---|
| 309 | }, |
---|
| 310 | tearDown: function(){ |
---|
| 311 | this._s.remove(); |
---|
| 312 | setHash(); |
---|
| 313 | } |
---|
| 314 | } |
---|
| 315 | ]); |
---|
| 316 | }); |
---|