Commit 7ef3c902 authored by Robert Knight's avatar Robert Knight Committed by GitHub

Merge pull request #464 from tilgovi/fix-localstorage-access-error

Use fallback when local storage throws
parents 2605fce9 60a37459
......@@ -28,14 +28,15 @@ class InMemoryStorage {
*/
// @ngInject
function localStorage($window) {
var storage = $window.localStorage;
let storage;
let testKey = 'hypothesis.testKey';
try {
// Test whether we can read/write localStorage.
var key = 'hypothesis.testKey';
$window.localStorage.setItem(key, key);
$window.localStorage.getItem(key);
$window.localStorage.removeItem(key);
storage = $window.localStorage;
$window.localStorage.setItem(testKey, testKey);
$window.localStorage.getItem(testKey);
$window.localStorage.removeItem(testKey);
} catch (e) {
storage = new InMemoryStorage();
}
......
'use strict';
var angular = require('angular');
var service = require('../local-storage');
describe('sidebar.localStorage', () => {
var fakeWindow;
before(() =>
angular.module('h', [])
.service('localStorage', require('../local-storage'))
.service('localStorage', service)
);
context('when accessing localStorage throws an Error', () => {
it('returns the fallback implementation', () => {
var badWindow = {};
var fakeWindow = {};
Object.defineProperty(badWindow, 'localStorage', {
get: () => {
throw Error('denied');
},
});
var prototypes = [badWindow, fakeWindow]
.map(service)
.map(Object.getPrototypeOf)
;
assert.strictEqual(prototypes[0], prototypes[1]);
});
});
context('when browser localStorage is *not* accessible', () => {
var localStorage = null;
var key = null;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment