|
1 | 1 | var before = { |
2 | 2 | setup: function () { |
3 | | - document.cookie = 'c=; expires=' + new Date(0).toUTCString(); |
4 | | - document.cookie = encodeURIComponent(' foo ') + '=; expires=' + new Date(0).toUTCString(); |
5 | | - document.cookie = 'raw=; expires=' + new Date(0).toUTCString(); |
| 3 | + cookies = document.cookie.split('; ') |
| 4 | + for (var i = 0, c; (c = (cookies)[i]) && (c = c.split('=')[0]); i++) { |
| 5 | + document.cookie = c + '=; expires=' + new Date(0).toUTCString(); |
| 6 | + } |
6 | 7 | } |
7 | 8 | }; |
8 | 9 |
|
9 | | -module('read', before); |
10 | 10 |
|
11 | | -test('Read', function () { |
12 | | - expect(5); |
| 11 | +module('read', before); |
13 | 12 |
|
14 | | - equals($.cookie('c'), null); |
| 13 | +test('simple value', 1, function () { |
15 | 14 | document.cookie = 'c=v'; |
16 | | - equals($.cookie('c'), 'v', 'Read cookie value'); |
| 15 | + equals($.cookie('c'), 'v', 'should return value'); |
| 16 | +}); |
17 | 17 |
|
18 | | - document.cookie = encodeURIComponent(' foo ') + '=' + encodeURIComponent(' bar '); |
19 | | - equals($.cookie(' foo '), ' bar ', 'Decode key/value'); |
| 18 | +test('not existing', 1, function () { |
| 19 | + equals($.cookie('whatever'), null, 'should return null'); |
| 20 | +}); |
20 | 21 |
|
21 | | - document.cookie = 'raw=%20whatever'; |
22 | | - equals($.cookie('raw', { raw: true }), '%20whatever', 'Do not decode with raw set true'); |
| 22 | +test('decode', 1, function () { |
| 23 | + document.cookie = encodeURIComponent(' c') + '=' + encodeURIComponent(' v'); |
| 24 | + equals($.cookie(' c'), ' v', 'should decode key and value'); |
| 25 | + |
| 26 | + // TODO test with ';' use encodeURI? |
| 27 | +}); |
23 | 28 |
|
24 | | - equals($.cookie('whatever'), null, 'Give null if cookie does not exist'); |
| 29 | +test('raw: true', 1, function () { |
| 30 | + document.cookie = 'c=%20v'; |
| 31 | + equals($.cookie('c', { raw: true }), '%20v', 'should not decode'); |
25 | 32 | }); |
26 | 33 |
|
27 | | -module('Write', before); |
28 | 34 |
|
29 | | -test('Write', function () { |
30 | | - expect(4); |
| 35 | +module('write', before); |
31 | 36 |
|
32 | | - equals($.cookie('c'), null); |
| 37 | +test('simple value', 2, function () { |
33 | 38 | var cookie = $.cookie('c', 'v'); |
34 | | - equals(document.cookie, 'c=v', 'Write cookie value'); |
35 | | - equals(cookie, 'c=v', 'Return cookie string'); |
36 | | - equals($.cookie('raw', ' whatever', { raw: true }).split('=')[1], ' whatever', 'Do not encode with raw set true'); |
| 39 | + equals(cookie, 'c=v', 'should return written cookie string'); |
| 40 | + equals(document.cookie, 'c=v', 'should write String primitive'); |
| 41 | +}); |
| 42 | + |
| 43 | +test('raw: true', 1, function () { |
| 44 | + equals($.cookie('c', ' v', { raw: true }).split('=')[1], |
| 45 | + ' v', 'should not encode'); |
37 | 46 | }); |
38 | 47 |
|
39 | | -module('Delete', before); |
40 | 48 |
|
41 | | -test('Delete', function () { |
42 | | - expect(1); |
| 49 | +module('delete', before); |
43 | 50 |
|
| 51 | +test('delete', 1, function () { |
44 | 52 | document.cookie = 'c=v'; |
45 | 53 | $.cookie('c', null); |
46 | | - equals(document.cookie, '', 'Delete cookie'); |
| 54 | + equals(document.cookie, '', 'should delete with null as value'); |
47 | 55 | }); |
0 commit comments