Skip to content

Commit 0ae9f7e

Browse files
committed
Fix return value for $.removeCookie() in case cookie deletion failed for whatever reason.
Closes js-cookie#224.
1 parent 3caf209 commit 0ae9f7e

2 files changed

Lines changed: 38 additions & 27 deletions

File tree

‎jquery.cookie.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,13 @@
106106
config.defaults = {};
107107

108108
$.removeCookie = function (key, options) {
109-
if ($.cookie(key) !== undefined) {
110-
// Must not alter options, thus extending a fresh object...
111-
$.cookie(key, '', $.extend({}, options, { expires: -1 }));
112-
return true;
109+
if ($.cookie(key) === undefined) {
110+
return false;
113111
}
114-
return false;
112+
113+
// Must not alter options, thus extending a fresh object...
114+
$.cookie(key, '', $.extend({}, options, { expires: -1 }));
115+
return !$.cookie(key);
115116
};
116117

117118
}));

‎test/tests.js‎

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ test('invalid URL encoding', function () {
110110
expect(1);
111111
document.cookie = 'bad=foo%';
112112
strictEqual($.cookie('bad'), undefined, "won't throw exception, returns undefined");
113-
document.cookie = 'bad=foo'; // Allow to be deleted...
113+
// Delete manually here because it requires raw === true...
114+
$.cookie.raw = true;
115+
$.removeCookie('bad');
114116
});
115117

116118
asyncTest('malformed cookie value in IE (#88, #117)', function () {
@@ -152,6 +154,9 @@ test('Call to read all with a badly encoded cookie', function () {
152154
document.cookie = 'bad=foo%';
153155
document.cookie = 'good=foo';
154156
deepEqual($.cookie(), { good: 'foo' }, 'returns object containing all decodable cookies');
157+
// Delete manually here because it requires raw === true...
158+
$.cookie.raw = true;
159+
$.removeCookie('bad');
155160
});
156161

157162

@@ -213,7 +218,8 @@ test('raw = true', function () {
213218
expect(1);
214219
$.cookie.raw = true;
215220
strictEqual($.cookie('c[1]', 'v[1]'), 'c[1]=v[1]', 'should not encode');
216-
$.each($.cookie(), $.removeCookie);
221+
// Delete manually here because it requires raw === true...
222+
$.removeCookie('c[1]');
217223
});
218224

219225
test('json = true', function () {
@@ -238,42 +244,46 @@ test('deletion', function () {
238244
strictEqual(document.cookie, '', 'should delete the cookie');
239245
});
240246

241-
test('return', function () {
242-
expect(2);
243-
strictEqual($.removeCookie('c'), false, "return false if the cookie wasn't found");
244-
247+
test('when sucessfully deleted', function () {
248+
expect(1);
245249
$.cookie('c', 'v');
246-
strictEqual($.removeCookie('c'), true, 'return true if the cookie was found');
250+
strictEqual($.removeCookie('c'), true, 'returns true');
247251
});
248252

249-
test('with options', function () {
250-
expect(3);
251-
var originalCookie = $.cookie;
252-
var callCount = 0;
253+
test('when deletion failed', function () {
254+
expect(1);
255+
$.cookie('c', 'v');
253256

257+
var originalCookie = $.cookie;
254258
$.cookie = function () {
255-
callCount++;
256-
if (callCount === 1) {
257-
// see https://github.com/carhartl/jquery-cookie/issues/99
258-
strictEqual(arguments.length, 1, 'look up cookie instead of accidently writing a new');
259-
return 'cookie'; // act as if a cookie was found...
260-
}
261-
if (callCount === 2) {
262-
strictEqual(arguments[2].foo, 'bar', 'pass along options when deleting cookie');
259+
// Stub deletion...
260+
if (arguments.length === 1) {
261+
return originalCookie.apply(null, arguments);
263262
}
264263
};
265264

266-
$.removeCookie('c', { foo: 'bar' });
267-
strictEqual(callCount, 2);
265+
strictEqual($.removeCookie('c'), false, 'returns false');
268266

269267
$.cookie = originalCookie;
270268
});
271269

272-
test('passing options reference', function () {
270+
test('when cookie does not exist', function () {
271+
expect(1);
272+
strictEqual($.removeCookie('c'), false, 'returns false');
273+
});
274+
275+
test('with options', function () {
273276
expect(1);
274277
var options = { path: '/' };
275278
$.cookie('c', 'v', options);
279+
$.removeCookie('c', options);
280+
strictEqual(document.cookie, '', 'should delete the cookie');
281+
});
276282

283+
test('passing options reference', function () {
284+
expect(1);
285+
var options = { path: '/' };
286+
$.cookie('c', 'v', options);
277287
$.removeCookie('c', options);
278288
deepEqual(options, { path: '/' }, "won't alter options object");
279289
});

0 commit comments

Comments
 (0)