Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #31906 from arcturus/bug-1202916
Browse files Browse the repository at this point in the history
Bug 1202916 - [Contacts] Contacts app not functioning on dogfood-late…
  • Loading branch information
arcturus committed Sep 18, 2015
2 parents 32c86b3 + 9482fd5 commit 69717d7
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 2 deletions.
17 changes: 15 additions & 2 deletions apps/communications/contacts/js/utilities/cookie.js
Expand Up @@ -44,7 +44,17 @@
return null;
}

return JSON.parse(decodeURIComponent(cookieVal));
// Setup default value for cookie with version that will trigger update
var result = {
version: -1
};
try {
result = JSON.parse(decodeURIComponent(cookieVal));
} catch (error) {
console.warn('Could not parse current cookie, rebuilding it');
}

return result;
}

// Load and return the cookie config if present. Returns null if the
Expand Down Expand Up @@ -105,6 +115,9 @@
}

utils.cookie.getDefault = function(prop) {
return COOKIE_DEFAULTS(prop);
return COOKIE_DEFAULTS[prop];
};

utils.cookie.COOKIE_DEFAULTS = COOKIE_DEFAULTS;
utils.cookie.COOKIE_VERSION = COOKIE_VERSION;
})();
76 changes: 76 additions & 0 deletions apps/communications/contacts/test/unit/utilities/cookie_test.js
@@ -0,0 +1,76 @@
'use strict';

requireApp('communications/contacts/js/utilities/cookie.js');

suite('Contacts Cookies', function() {
var COOKIE_NAME = 'preferences';
var COOKIE_PROPS = null;
var subject = null;

suiteSetup(function() {
COOKIE_PROPS = Object.keys(window.utils.cookie.COOKIE_DEFAULTS);
subject = window.utils.cookie;
});

function deleteCookie() {
document.cookie = COOKIE_NAME + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}

function saveCookie(value) {
document.cookie = COOKIE_NAME + '= ' + value;
}

function checkDefaultCookie(cookie) {
assert.equal(cookie.version, subject.COOKIE_VERSION);
COOKIE_PROPS.forEach(function(prop) {
assert.equal(cookie[prop], subject.getDefault(prop));
});
}

setup(function() {
deleteCookie();
});

suite('Bad cookie', function() {
test('No cookie returns null;', function() {
var cookie = subject.load();

assert.isNull(cookie);
});

test('Invalid cookie should return default cookie value', function() {
saveCookie('invalid value');

var cookie = subject.load();
assert.isNotNull(cookie);
checkDefaultCookie(cookie);
});
});

suite('Update cookie', function() {
test('Updating empty cookie with invalid property', function() {
subject.update({test: 1});
var cookie = subject.load();
assert.isNotNull(cookie);
assert.equal(cookie.version, subject.COOKIE_VERSION);
assert.equal(cookie.test, null);
});

test('Updating empty cookie', function() {
subject.update({order: true});
var cookie = subject.load();
assert.isNotNull(cookie);
assert.equal(cookie.version, subject.COOKIE_VERSION);
assert.isTrue(cookie.order);
});

test('Update invalid cookie', function() {
saveCookie('invalid value');
subject.update({order: true});
var cookie = subject.load();
assert.isNotNull(cookie);
assert.equal(cookie.version, subject.COOKIE_VERSION);
assert.isTrue(cookie.order);
});
});
});

0 comments on commit 69717d7

Please sign in to comment.