Commit e05f4588 authored by Robert Knight's avatar Robert Knight

Improve tests for different localStorage-not-accessible situations

Run all of the existing tests to check behavior when localStorage is not
accessible, for the case where accessing `window.localStorage` throws.

The previous tests for this situation only tested the behavior of the
service constructor.
parent 7ef3c902
...@@ -3,6 +3,28 @@ ...@@ -3,6 +3,28 @@
var angular = require('angular'); var angular = require('angular');
var service = require('../local-storage'); 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', () => { describe('sidebar.localStorage', () => {
var fakeWindow; var fakeWindow;
...@@ -11,34 +33,17 @@ describe('sidebar.localStorage', () => { ...@@ -11,34 +33,17 @@ describe('sidebar.localStorage', () => {
.service('localStorage', service) .service('localStorage', service)
); );
context('when accessing localStorage throws an Error', () => { [
it('returns the fallback implementation', () => { windowWithLocalStorageMethodsThatThrow(),
var badWindow = {}; windowWithLocalStoragePropertyThatThrows(),
var fakeWindow = {}; ].forEach(($window) => {
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', () => { context('when browser localStorage is *not* accessible', () => {
var localStorage = null; var localStorage = null;
var key = null; var key = null;
beforeEach(() => { beforeEach(() => {
angular.mock.module('h', { angular.mock.module('h', {
$window: { $window,
localStorage: {},
},
}); });
}); });
...@@ -71,6 +76,7 @@ describe('sidebar.localStorage', () => { ...@@ -71,6 +76,7 @@ describe('sidebar.localStorage', () => {
assert.deepEqual(actual, data); assert.deepEqual(actual, data);
}); });
}); });
});
context('when browser localStorage is accessible', () => { context('when browser localStorage is accessible', () => {
var localStorage; 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