forked from carhartl/jquery-cookie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
121 lines (97 loc) · 3.86 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var before = {
setup: function () {
cookies = document.cookie.split('; ')
for (var i = 0, c; (c = (cookies)[i]) && (c = c.split('=')[0]); i++) {
document.cookie = c + '=; expires=' + new Date(0).toUTCString();
}
}
};
module('read', before);
test('simple value', 1, function () {
document.cookie = 'c=v';
equal($.cookie('c'), 'v', 'should return value');
});
test('empty value', 1, function () {
$.cookie('c', '');
equal($.cookie('c'), '', 'should return value');
});
test('simple value with an expires value', 1, function () {
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 5);
document.cookie = 'c=testcookie; expires=' + tomorrow.toUTCString();
equal($.cookie('c'), 'testcookie', 'should return value');
});
test('not existing', 1, function () {
equal($.cookie('whatever'), null, 'should return null');
});
test('decode', 1, function () {
document.cookie = encodeURIComponent(' c') + '=' + encodeURIComponent(' v');
equal($.cookie(' c'), ' v', 'should decode key and value');
});
test('raw: true', 1, function () {
document.cookie = 'c=%20v';
equal($.cookie('c', { raw: true }), '%20v', 'should not decode');
});
module('write', before);
test('String primitive', 1, function () {
$.cookie('c', 'v');
equal(document.cookie, 'c=v', 'should write value');
});
test('String object', 1, function () {
$.cookie('c', new String('v'));
equal(document.cookie, 'c=v', 'should write value');
});
test('value "[object Object]"', 1, function() {
$.cookie('c', '[object Object]');
equal($.cookie('c'), '[object Object]', 'should write value');
});
test('number', 1, function() {
$.cookie('c', 1234);
equal($.cookie('c'), '1234', 'should write value');
});
test('with expires as a number (7 days from now)', 1, function() {
var seven_days_from_now = new Date();
seven_days_from_now.setDate(seven_days_from_now.getDate() + 7);
equal($.cookie('c', 'v', {expires:7}), 'c=v; expires='+seven_days_from_now.toUTCString(), 'should return the cookie string with expires');
});
test('with expires as a negative number (yesterday)', 2, function() {
var yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
equal($.cookie('c', 'v', {expires:-1}), 'c=v; expires='+yesterday.toUTCString(), 'should return the cookie string with expires');
equal(document.cookie, '', 'should not save expired cookie');
});
test('with expires as an object ({hours:1, minutes:2, seconds:3, days:4})',1, function() {
var expiry = {
hours:1,
minutes:2,
days:4
};
var expected_expiry = new Date();
expected_expiry = new Date(expected_expiry.setHours(expected_expiry.getHours() + expiry.hours));
expected_expiry = new Date(expected_expiry.setMinutes(expected_expiry.getMinutes() + expiry.minutes));
expected_expiry.setDate(expected_expiry.getDate() + expiry.days);
equal($.cookie('c', 'v', {expires:expiry}), 'c=v; expires='+expected_expiry.toUTCString(), 'should return the cookie string with expires');
});
test('with expires as an object - ignores garbage ({myhours:1})',1, function() {
var expiry = {
myhours: 10
};
var expected_expiry = new Date();
equal($.cookie('c', 'v', {expires:expiry}), 'c=v; expires='+expected_expiry.toUTCString(), 'should return the cookie string with expires');
});
test('return value', 1, function () {
equal($.cookie('c', 'v'), 'c=v', 'should return written cookie string');
});
test('raw: true', 1, function () {
equal($.cookie('c', ' v', { raw: true }).split('=')[1],
' v', 'should not encode');
});
module('delete', before);
test('delete', 2, function () {
document.cookie = 'c=v';
$.cookie('c', null);
equal(document.cookie, '', 'should delete with null as value');
document.cookie = 'c=v';
$.cookie('c', undefined);
equal(document.cookie, '', 'should delete with undefined as value');
});