Skip to content

Commit

Permalink
Cant have multiple asserts in a test
Browse files Browse the repository at this point in the history
  • Loading branch information
pamelafox committed Nov 4, 2018
1 parent 53707f0 commit ad93e83
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lscache.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 46 additions & 10 deletions tests/tests-cjs.js
Expand Up @@ -204,21 +204,22 @@
* @param {string} key
* @param {Object|string} value
* @param {number} time
* @return true if the value was inserted successfully
*/
set: function(key, value, time) {
if (!supportsStorage()) return;
if (!supportsStorage()) return false;

// If we don't get a string value, try to stringify
// In future, localStorage may properly support storing non-strings
// and this can be removed.

if (!supportsJSON()) return;
if (!supportsJSON()) return false;
try {
value = JSON.stringify(value);
} catch (e) {
// Sometimes we can't stringify due to circular refs
// in complex objects, so we won't bother storing then.
return;
return false;
}

try {
Expand Down Expand Up @@ -258,12 +259,12 @@
} catch (e) {
// value may be larger than total quota
warn("Could not add item with key '" + key + "', perhaps it's too big?", e);
return;
return false;
}
} else {
// If it was some other error, just give up.
warn("Could not add item with key '" + key + "'", e);
return;
return false;
}
}

Expand All @@ -274,6 +275,7 @@
// In case they previously set a time, remove that info from localStorage.
removeItem(expirationKey(key));
}
return true;
},

/**
Expand Down Expand Up @@ -432,8 +434,8 @@ var startTests = function (lscache) {
test('Testing set() and get() with string', function() {
var key = 'thekey';
var value = 'thevalue';
lscache.set(key, value, 1);
if (lscache.supported()) {
var isSet = lscache.set(key, value, 1);
if (isSet) {
equal(lscache.get(key), value, 'We expect value to be ' + value);
} else {
equal(lscache.get(key), null, 'We expect null value');
Expand Down Expand Up @@ -482,6 +484,16 @@ var startTests = function (lscache) {
equal(localStorage.getItem('outside-cache'), 'not part of lscache', 'We expect localStorage value to still persist');
});

test('Testing set() fails with circular references', function() {
var key, value;

key = 'objectkey';
value = {'name': 'Pamela', 'age': 26};
value.itself = value;
equal(lscache.set(key, value, 3), false, 'We expect the value cannot be stored');
equal(lscache.get(key), null, 'We expect value was not stored');
});

test('Testing setBucket()', function() {
var key = 'thekey';
var value1 = 'awesome';
Expand Down Expand Up @@ -558,7 +570,7 @@ var startTests = function (lscache) {

for (i = 0; i <= numKeys; i++) {
currentKey = key + i;
lscache.set(currentKey, longString, i+1);
equal(lscache.set(currentKey, longString, i+1), true, 'We expect new value to be added successfully');
}
// Test that last-to-expire is still there
equal(lscache.get(currentKey), longString, 'We expect newest value to still be there');
Expand All @@ -568,14 +580,14 @@ var startTests = function (lscache) {
// Test trying to add something thats bigger than previous items,
// check that it is successfully added (requires removal of multiple keys)
var veryLongString = longString + longString;
lscache.set(key + 'long', veryLongString, i+1);
equal(lscache.set(key + 'long', veryLongString, i+1), true, 'We expect new value to be added successfully');
equal(lscache.get(key + 'long'), veryLongString, 'We expect long string to get stored');

// Try the same with no expiry times
localStorage.clear();
for (i = 0; i <= numKeys; i++) {
currentKey = key + i;
lscache.set(currentKey, longString);
equal(lscache.set(currentKey, longString), true, 'We expect each value to be added successfully');
}
// Test that latest added is still there
equal(lscache.get(currentKey), longString, 'We expect value to be set');
Expand All @@ -599,6 +611,29 @@ var startTests = function (lscache) {
}, expiryMilliseconds*numExpiryUnits);
});

test('Testing single item exceeds quota', function() {
var key = 'thekey';
var stringLength = 10000;
var longString = (new Array(stringLength+1)).join('s');

// Figure out this browser's localStorage limit -
// Chrome is around 2.6 mil, for example
var num = 0;
while(num < 10000) {
try {
localStorage.setItem(key + num, longString);
num++;
} catch (e) {
break;
}
}
localStorage.clear();
// Now make string long enough to go over limit.
var veryLongString = (new Array(num+3)).join(longString);
equal(lscache.set(key + 'long', veryLongString), false, 'We expect new value to be too long');
equal(lscache.get(key + 'long'), null, 'We expect nothing was stored');
});

// We do this test last since it must wait 1 minute
asyncTest('Testing set() and get() with string and expiration', 1, function() {

Expand Down Expand Up @@ -683,4 +718,5 @@ if (typeof module !== "undefined" && module.exports) {
// Assuming that lscache has been properly included
startTests(lscache);
}

},{"../lscache":1,"qunit":2}]},{},[3]);
2 changes: 1 addition & 1 deletion tests/tests.js
Expand Up @@ -231,7 +231,7 @@ var startTests = function (lscache) {
var key = 'thekey';
var value = 'thevalue';
var minutes = 1;
equal(lscache.set(key, value, minutes), true, 'We expect the value to be inserted successfully');
lscache.set(key, value, minutes);
setTimeout(function() {
equal(lscache.get(key), null, 'We expect value to be null');
start();
Expand Down

0 comments on commit ad93e83

Please sign in to comment.