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 @@ ...@@ -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,64 +33,48 @@ describe('sidebar.localStorage', () => { ...@@ -11,64 +33,48 @@ 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) => {
context('when browser localStorage is *not* accessible', () => {
Object.defineProperty(badWindow, 'localStorage', { var localStorage = null;
get: () => { var key = null;
throw Error('denied');
}, beforeEach(() => {
angular.mock.module('h', {
$window,
});
}); });
var prototypes = [badWindow, fakeWindow] beforeEach(angular.mock.inject((_localStorage_) => {
.map(service) localStorage = _localStorage_;
.map(Object.getPrototypeOf) key = 'test.memory.key';
; }));
assert.strictEqual(prototypes[0], prototypes[1]);
});
});
context('when browser localStorage is *not* accessible', () => { it('sets/gets Item', () => {
var localStorage = null; var value = 'What shall we do with a drunken sailor?';
var key = null; localStorage.setItem(key, value);
var actual = localStorage.getItem(key);
beforeEach(() => { assert.equal(value, actual);
angular.mock.module('h', {
$window: {
localStorage: {},
},
}); });
});
beforeEach(angular.mock.inject((_localStorage_) => { it('removes item', () => {
localStorage = _localStorage_; localStorage.setItem(key, '');
key = 'test.memory.key'; localStorage.removeItem(key);
})); var result = localStorage.getItem(key);
assert.isNull(result);
it('sets/gets Item', () => { });
var value = 'What shall we do with a drunken sailor?';
localStorage.setItem(key, value);
var actual = localStorage.getItem(key);
assert.equal(value, actual);
});
it('removes item', () => {
localStorage.setItem(key, '');
localStorage.removeItem(key);
var result = localStorage.getItem(key);
assert.isNull(result);
});
it('sets/gets Object', () => { it('sets/gets Object', () => {
var data = {'foo': 'bar'}; var data = {'foo': 'bar'};
localStorage.setObject(key, data); localStorage.setObject(key, data);
var stringified = localStorage.getItem(key); var stringified = localStorage.getItem(key);
assert.equal(stringified, JSON.stringify(data)); assert.equal(stringified, JSON.stringify(data));
var actual = localStorage.getObject(key); var actual = localStorage.getObject(key);
assert.deepEqual(actual, data); assert.deepEqual(actual, data);
});
}); });
}); });
......
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