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
|
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';
equals($.cookie('c'), 'v', 'should return value');
});
test('not existing', 1, function () {
equals($.cookie('whatever'), null, 'should return null');
});
test('decode', 1, function () {
document.cookie = encodeURIComponent(' c') + '=' + encodeURIComponent(' v');
equals($.cookie(' c'), ' v', 'should decode key and value');
});
test('raw: true', 1, function () {
document.cookie = 'c=%20v';
equals($.cookie('c', { raw: true }), '%20v', 'should not decode');
});
module('write', before);
test('String primitive', 1, function () {
$.cookie('c', 'v');
equals(document.cookie, 'c=v', 'should write value');
});
test('String object', 1, function () {
$.cookie('c', new String('v'));
equals(document.cookie, 'c=v', 'should write value');
});
test('return', 1, function () {
equals($.cookie('c', 'v'), 'c=v', 'should return written cookie string');
});
test('raw: true', 1, function () {
equals($.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);
equals(document.cookie, '', 'should delete with null as value');
document.cookie = 'c=v';
$.cookie('c', undefined);
equals(document.cookie, '', 'should delete with undefined as value');
});
|