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 @@
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