Commit 4fe1dfbf authored by Robert Knight's avatar Robert Knight Committed by GitHub

Merge pull request #468 from hypothesis/localstorage-tests

Improve tests for different localStorage-not-accessible situations
parents 7ef3c902 e05f4588
......@@ -3,6 +3,28 @@
var angular = require('angular');
var service = require('../local-storage');
function windowWithLocalStoragePropertyThatThrows() {
var win = {};
Object.defineProperty(win, 'localStorage', {
get() {
throw Error('denied');
},
});
return win;
}
function windowWithLocalStorageMethodsThatThrow() {
var throwErr = sinon.stub.throws(new Error('Denied'));
return {
localStorage: {
getItem: throwErr,
removeItem: throwErr,
setItem: throwErr,
},
};
}
describe('sidebar.localStorage', () => {
var fakeWindow;
......@@ -11,34 +33,17 @@ describe('sidebar.localStorage', () => {
.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]);
});
});
[
windowWithLocalStorageMethodsThatThrow(),
windowWithLocalStoragePropertyThatThrows(),
].forEach(($window) => {
context('when browser localStorage is *not* accessible', () => {
var localStorage = null;
var key = null;
beforeEach(() => {
angular.mock.module('h', {
$window: {
localStorage: {},
},
$window,
});
});
......@@ -71,6 +76,7 @@ describe('sidebar.localStorage', () => {
assert.deepEqual(actual, data);
});
});
});
context('when browser localStorage is accessible', () => {
var localStorage;
......
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