From ad93e83a04e70f8ab6f25258cb8b70db6902710f Mon Sep 17 00:00:00 2001 From: Pamela Fox Date: Sun, 4 Nov 2018 13:08:17 -0800 Subject: [PATCH] Cant have multiple asserts in a test --- lscache.min.js | 2 +- tests/tests-cjs.js | 56 +++++++++++++++++++++++++++++++++++++--------- tests/tests.js | 2 +- 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/lscache.min.js b/lscache.min.js index 15b83f1..247e37c 100644 --- a/lscache.min.js +++ b/lscache.min.js @@ -1 +1 @@ -!function(a,b){"function"==typeof define&&define.amd?define([],b):"undefined"!=typeof module&&module.exports?module.exports=b():a.lscache=b()}(this,function(){function a(){var a="__lscachetest__",c=a;if(void 0!==o)return o;try{if(!localStorage)return!1}catch(a){return!1}try{h(a,c),i(a),o=!0}catch(a){o=!(!b(a)||!localStorage.length)}return o}function b(a){return a&&("QUOTA_EXCEEDED_ERR"===a.name||"NS_ERROR_DOM_QUOTA_REACHED"===a.name||"QuotaExceededError"===a.name)}function c(){return void 0===p&&(p=null!=window.JSON),p}function d(a){return a.replace(/[[\]{}()*+?.\\^$|]/g,"\\$&")}function e(a){return a+r}function f(){return Math.floor((new Date).getTime()/t)}function g(a){return localStorage.getItem(q+v+a)}function h(a,b){localStorage.removeItem(q+v+a),localStorage.setItem(q+v+a,b)}function i(a){localStorage.removeItem(q+v+a)}function j(a){for(var b=new RegExp("^"+q+d(v)+"(.*)"),c=localStorage.length-1;c>=0;--c){var f=localStorage.key(c);f=f&&f.match(b),f=f&&f[1],f&&f.indexOf(r)<0&&a(f,e(f))}}function k(a){var b=e(a);i(a),i(b)}function l(a){var b=e(a),c=g(b);if(c){var d=parseInt(c,s);if(f()>=d)return i(a),i(b),!0}}function m(a,b){w&&"console"in window&&"function"==typeof window.console.warn&&(window.console.warn("lscache - "+a),b&&window.console.warn("lscache - The error was: "+b.message))}function n(a){return Math.floor(864e13/a)}var o,p,q="lscache-",r="-cacheexpiration",s=10,t=6e4,u=n(t),v="",w=!1,x={set:function(d,l,n){if(a()&&c()){try{l=JSON.stringify(l)}catch(a){return}try{h(d,l)}catch(a){if(!b(a))return void m("Could not add item with key '"+d+"'",a);var o,p=[];j(function(a,b){var c=g(b);c=c?parseInt(c,s):u,p.push({key:a,size:(g(a)||"").length,expiration:c})}),p.sort(function(a,b){return b.expiration-a.expiration});for(var q=(l||"").length;p.length&&q>0;)o=p.pop(),m("Cache is full, removing item with key '"+d+"'"),k(o.key),q-=o.size;try{h(d,l)}catch(a){return void m("Could not add item with key '"+d+"', perhaps it's too big?",a)}}n?h(e(d),(f()+n).toString(s)):i(e(d))}},get:function(b){if(!a())return null;if(l(b))return null;var d=g(b);if(!d||!c())return d;try{return JSON.parse(d)}catch(a){return d}},remove:function(b){a()&&k(b)},supported:function(){return a()},flush:function(){a()&&j(function(a){k(a)})},flushExpired:function(){a()&&j(function(a){l(a)})},setBucket:function(a){v=a},resetBucket:function(){v=""},getExpiryMilliseconds:function(){return t},setExpiryMilliseconds:function(a){t=a,u=n(t)},enableWarnings:function(a){w=a}};return x}); \ No newline at end of file +!function(a,b){"function"==typeof define&&define.amd?define([],b):"undefined"!=typeof module&&module.exports?module.exports=b():a.lscache=b()}(this,function(){function a(){var a="__lscachetest__",c=a;if(void 0!==o)return o;try{if(!localStorage)return!1}catch(a){return!1}try{h(a,c),i(a),o=!0}catch(a){o=!(!b(a)||!localStorage.length)}return o}function b(a){return a&&("QUOTA_EXCEEDED_ERR"===a.name||"NS_ERROR_DOM_QUOTA_REACHED"===a.name||"QuotaExceededError"===a.name)}function c(){return void 0===p&&(p=null!=window.JSON),p}function d(a){return a.replace(/[[\]{}()*+?.\\^$|]/g,"\\$&")}function e(a){return a+r}function f(){return Math.floor((new Date).getTime()/t)}function g(a){return localStorage.getItem(q+v+a)}function h(a,b){localStorage.removeItem(q+v+a),localStorage.setItem(q+v+a,b)}function i(a){localStorage.removeItem(q+v+a)}function j(a){for(var b=new RegExp("^"+q+d(v)+"(.*)"),c=localStorage.length-1;c>=0;--c){var f=localStorage.key(c);f=f&&f.match(b),f=f&&f[1],f&&f.indexOf(r)<0&&a(f,e(f))}}function k(a){var b=e(a);i(a),i(b)}function l(a){var b=e(a),c=g(b);if(c){var d=parseInt(c,s);if(f()>=d)return i(a),i(b),!0}}function m(a,b){w&&"console"in window&&"function"==typeof window.console.warn&&(window.console.warn("lscache - "+a),b&&window.console.warn("lscache - The error was: "+b.message))}function n(a){return Math.floor(864e13/a)}var o,p,q="lscache-",r="-cacheexpiration",s=10,t=6e4,u=n(t),v="",w=!1,x={set:function(d,l,n){if(!a())return!1;if(!c())return!1;try{l=JSON.stringify(l)}catch(a){return!1}try{h(d,l)}catch(a){if(!b(a))return m("Could not add item with key '"+d+"'",a),!1;var o,p=[];j(function(a,b){var c=g(b);c=c?parseInt(c,s):u,p.push({key:a,size:(g(a)||"").length,expiration:c})}),p.sort(function(a,b){return b.expiration-a.expiration});for(var q=(l||"").length;p.length&&q>0;)o=p.pop(),m("Cache is full, removing item with key '"+d+"'"),k(o.key),q-=o.size;try{h(d,l)}catch(a){return m("Could not add item with key '"+d+"', perhaps it's too big?",a),!1}}return n?h(e(d),(f()+n).toString(s)):i(e(d)),!0},get:function(b){if(!a())return null;if(l(b))return null;var d=g(b);if(!d||!c())return d;try{return JSON.parse(d)}catch(a){return d}},remove:function(b){a()&&k(b)},supported:function(){return a()},flush:function(){a()&&j(function(a){k(a)})},flushExpired:function(){a()&&j(function(a){l(a)})},setBucket:function(a){v=a},resetBucket:function(){v=""},getExpiryMilliseconds:function(){return t},setExpiryMilliseconds:function(a){t=a,u=n(t)},enableWarnings:function(a){w=a}};return x}); \ No newline at end of file diff --git a/tests/tests-cjs.js b/tests/tests-cjs.js index 25820b6..06b7aba 100644 --- a/tests/tests-cjs.js +++ b/tests/tests-cjs.js @@ -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 { @@ -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; } } @@ -274,6 +275,7 @@ // In case they previously set a time, remove that info from localStorage. removeItem(expirationKey(key)); } + return true; }, /** @@ -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'); @@ -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'; @@ -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'); @@ -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'); @@ -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() { @@ -683,4 +718,5 @@ if (typeof module !== "undefined" && module.exports) { // Assuming that lscache has been properly included startTests(lscache); } + },{"../lscache":1,"qunit":2}]},{},[3]); diff --git a/tests/tests.js b/tests/tests.js index 212f618..ba8dfcd 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -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();